| 1 |
package cmd |
| 2 |
|
| 3 |
import ( |
| 4 |
"os" |
| 5 |
"path/filepath" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/stretchr/testify/require" |
| 9 |
"github.com/urfave/cli/v2" |
| 10 |
"heckel.io/ntfy/v2/server" |
| 11 |
"heckel.io/ntfy/v2/test" |
| 12 |
"heckel.io/ntfy/v2/user" |
| 13 |
) |
| 14 |
|
| 15 |
func TestCLI_User_Add(t *testing.T) { |
| 16 |
s, conf, port := newTestServerWithAuth(t) |
| 17 |
defer test.StopServer(t, s, port) |
| 18 |
|
| 19 |
app, stdin, stdout, _ := newTestApp() |
| 20 |
stdin.WriteString("mypass\nmypass") |
| 21 |
require.Nil(t, runUserCommand(app, conf, "add", "phil")) |
| 22 |
require.Contains(t, stdout.String(), "user phil added with role user") |
| 23 |
} |
| 24 |
|
| 25 |
func TestCLI_User_Add_Exists(t *testing.T) { |
| 26 |
s, conf, port := newTestServerWithAuth(t) |
| 27 |
defer test.StopServer(t, s, port) |
| 28 |
|
| 29 |
app, stdin, stdout, _ := newTestApp() |
| 30 |
stdin.WriteString("mypass\nmypass") |
| 31 |
require.Nil(t, runUserCommand(app, conf, "add", "phil")) |
| 32 |
require.Contains(t, stdout.String(), "user phil added with role user") |
| 33 |
|
| 34 |
app, stdin, _, _ = newTestApp() |
| 35 |
stdin.WriteString("mypass\nmypass") |
| 36 |
err := runUserCommand(app, conf, "add", "phil") |
| 37 |
require.Error(t, err) |
| 38 |
require.Contains(t, err.Error(), "user phil already exists") |
| 39 |
} |
| 40 |
|
| 41 |
func TestCLI_User_Add_Admin(t *testing.T) { |
| 42 |
s, conf, port := newTestServerWithAuth(t) |
| 43 |
defer test.StopServer(t, s, port) |
| 44 |
|
| 45 |
app, stdin, stdout, _ := newTestApp() |
| 46 |
stdin.WriteString("mypass\nmypass") |
| 47 |
require.Nil(t, runUserCommand(app, conf, "add", "--role=admin", "phil")) |
| 48 |
require.Contains(t, stdout.String(), "user phil added with role admin") |
| 49 |
} |
| 50 |
|
| 51 |
func TestCLI_User_Add_Password_Mismatch(t *testing.T) { |
| 52 |
s, conf, port := newTestServerWithAuth(t) |
| 53 |
defer test.StopServer(t, s, port) |
| 54 |
|
| 55 |
app, stdin, _, _ := newTestApp() |
| 56 |
stdin.WriteString("mypass\nNOTMATCH") |
| 57 |
err := runUserCommand(app, conf, "add", "phil") |
| 58 |
require.Error(t, err) |
| 59 |
require.Contains(t, err.Error(), "passwords do not match: try it again, but this time type slooowwwlly") |
| 60 |
} |
| 61 |
|
| 62 |
func TestCLI_User_ChangePass(t *testing.T) { |
| 63 |
s, conf, port := newTestServerWithAuth(t) |
| 64 |
conf.AuthUsers = []*user.User{ |
| 65 |
{Name: "philuser", Hash: "$2a$10$U4WSIYY6evyGmZaraavM2e2JeVG6EMGUKN1uUwufUeeRd4Jpg6cGC", Role: user.RoleUser}, // philuser:philpass |
| 66 |
} |
| 67 |
defer test.StopServer(t, s, port) |
| 68 |
|
| 69 |
// Add user |
| 70 |
app, stdin, stdout, _ := newTestApp() |
| 71 |
stdin.WriteString("mypass\nmypass") |
| 72 |
require.Nil(t, runUserCommand(app, conf, "add", "phil")) |
| 73 |
require.Contains(t, stdout.String(), "user phil added with role user") |
| 74 |
|
| 75 |
// Change pass |
| 76 |
app, stdin, stdout, _ = newTestApp() |
| 77 |
stdin.WriteString("newpass\nnewpass") |
| 78 |
require.Nil(t, runUserCommand(app, conf, "change-pass", "phil")) |
| 79 |
require.Contains(t, stdout.String(), "changed password for user phil") |
| 80 |
|
| 81 |
// Cannot change provisioned user's pass |
| 82 |
app, stdin, _, _ = newTestApp() |
| 83 |
stdin.WriteString("newpass\nnewpass") |
| 84 |
require.Error(t, runUserCommand(app, conf, "change-pass", "philuser")) |
| 85 |
} |
| 86 |
|
| 87 |
func TestCLI_User_ChangeRole(t *testing.T) { |
| 88 |
s, conf, port := newTestServerWithAuth(t) |
| 89 |
defer test.StopServer(t, s, port) |
| 90 |
|
| 91 |
// Add user |
| 92 |
app, stdin, stdout, _ := newTestApp() |
| 93 |
stdin.WriteString("mypass\nmypass") |
| 94 |
require.Nil(t, runUserCommand(app, conf, "add", "phil")) |
| 95 |
require.Contains(t, stdout.String(), "user phil added with role user") |
| 96 |
|
| 97 |
// Change role |
| 98 |
app, _, stdout, _ = newTestApp() |
| 99 |
require.Nil(t, runUserCommand(app, conf, "change-role", "phil", "admin")) |
| 100 |
require.Contains(t, stdout.String(), "changed role for user phil to admin") |
| 101 |
} |
| 102 |
|
| 103 |
func TestCLI_User_Delete(t *testing.T) { |
| 104 |
s, conf, port := newTestServerWithAuth(t) |
| 105 |
defer test.StopServer(t, s, port) |
| 106 |
|
| 107 |
// Add user |
| 108 |
app, stdin, stdout, _ := newTestApp() |
| 109 |
stdin.WriteString("mypass\nmypass") |
| 110 |
require.Nil(t, runUserCommand(app, conf, "add", "phil")) |
| 111 |
require.Contains(t, stdout.String(), "user phil added with role user") |
| 112 |
|
| 113 |
// Delete user |
| 114 |
app, _, stdout, _ = newTestApp() |
| 115 |
require.Nil(t, runUserCommand(app, conf, "del", "phil")) |
| 116 |
require.Contains(t, stdout.String(), "user phil removed") |
| 117 |
|
| 118 |
// Delete user again (does not exist) |
| 119 |
app, _, _, _ = newTestApp() |
| 120 |
err := runUserCommand(app, conf, "del", "phil") |
| 121 |
require.Error(t, err) |
| 122 |
require.Contains(t, err.Error(), "user phil does not exist") |
| 123 |
} |
| 124 |
|
| 125 |
func TestCLI_User_ResetPass(t *testing.T) { |
| 126 |
s, conf, port := newTestServerWithAuth(t) |
| 127 |
defer test.StopServer(t, s, port) |
| 128 |
|
| 129 |
app, stdin, _, _ := newTestApp() |
| 130 |
stdin.WriteString("mypass\nmypass") |
| 131 |
require.Nil(t, runUserCommand(app, conf, "add", "phil")) |
| 132 |
|
| 133 |
// Prints a working-looking reset link when base-url is set |
| 134 |
app, _, stdout, _ := newTestApp() |
| 135 |
require.Nil(t, runUserCommand(app, conf, "--base-url=https://ntfy.example.com", "reset-pass", "phil")) |
| 136 |
require.Contains(t, stdout.String(), "https://ntfy.example.com/account/password/reset/") |
| 137 |
} |
| 138 |
|
| 139 |
func TestCLI_User_ResetPass_NoBaseURL(t *testing.T) { |
| 140 |
s, conf, port := newTestServerWithAuth(t) |
| 141 |
defer test.StopServer(t, s, port) |
| 142 |
|
| 143 |
app, stdin, _, _ := newTestApp() |
| 144 |
stdin.WriteString("mypass\nmypass") |
| 145 |
require.Nil(t, runUserCommand(app, conf, "add", "phil")) |
| 146 |
|
| 147 |
app, _, _, _ = newTestApp() |
| 148 |
err := runUserCommand(app, conf, "reset-pass", "phil") |
| 149 |
require.Error(t, err) |
| 150 |
require.Contains(t, err.Error(), "base-url") |
| 151 |
} |
| 152 |
|
| 153 |
func TestCLI_User_ResetPass_SendEmailNoPrimary(t *testing.T) { |
| 154 |
s, conf, port := newTestServerWithAuth(t) |
| 155 |
defer test.StopServer(t, s, port) |
| 156 |
|
| 157 |
app, stdin, _, _ := newTestApp() |
| 158 |
stdin.WriteString("mypass\nmypass") |
| 159 |
require.Nil(t, runUserCommand(app, conf, "add", "phil")) |
| 160 |
|
| 161 |
// --send-email requires a primary email; phil has none |
| 162 |
app, _, _, _ = newTestApp() |
| 163 |
err := runUserCommand(app, conf, "--base-url=https://ntfy.example.com", "reset-pass", "--send-email", "phil") |
| 164 |
require.Error(t, err) |
| 165 |
require.Contains(t, err.Error(), "no primary email") |
| 166 |
} |
| 167 |
|
| 168 |
func TestCLI_User_ResetPass_ProvisionedRejected(t *testing.T) { |
| 169 |
s, conf, port := newTestServerWithAuth(t) |
| 170 |
defer test.StopServer(t, s, port) |
| 171 |
|
| 172 |
// Seed a provisioned user into the auth database via config provisioning |
| 173 |
m, err := user.NewSQLiteManager(conf.AuthFile, "", &user.Config{ |
| 174 |
ProvisionEnabled: true, |
| 175 |
Users: []*user.User{ |
| 176 |
{Name: "provuser", Hash: "$2a$10$YLiO8U21sX1uhZamTLJXHuxgVC0Z/GKISibrKCLohPgtG7yIxSk4C", Role: user.RoleUser}, |
| 177 |
}, |
| 178 |
}) |
| 179 |
require.Nil(t, err) |
| 180 |
require.Nil(t, m.Close()) |
| 181 |
|
| 182 |
app, _, _, _ := newTestApp() |
| 183 |
err = runUserCommand(app, conf, "--base-url=https://ntfy.example.com", "reset-pass", "provuser") |
| 184 |
require.Error(t, err) |
| 185 |
require.Contains(t, err.Error(), "provisioned") |
| 186 |
} |
| 187 |
|
| 188 |
func newTestServerWithAuth(t *testing.T) (s *server.Server, conf *server.Config, port int) { |
| 189 |
configFile := filepath.Join(t.TempDir(), "server-dummy.yml") |
| 190 |
require.Nil(t, os.WriteFile(configFile, []byte(""), 0600)) // Dummy config file to avoid lookup of real server.yml |
| 191 |
conf = server.NewConfig() |
| 192 |
conf.File = configFile |
| 193 |
conf.AuthFile = filepath.Join(t.TempDir(), "user.db") |
| 194 |
conf.AuthDefault = user.PermissionDenyAll |
| 195 |
conf.AuthAccessCacheEnabled = false |
| 196 |
s, port = test.StartServerWithConfig(t, conf) |
| 197 |
return |
| 198 |
} |
| 199 |
|
| 200 |
func runUserCommand(app *cli.App, conf *server.Config, args ...string) error { |
| 201 |
userArgs := []string{ |
| 202 |
"ntfy", |
| 203 |
"--log-level=ERROR", |
| 204 |
"user", |
| 205 |
"--config=" + conf.File, // Dummy config file to avoid lookups of real file |
| 206 |
"--auth-file=" + conf.AuthFile, |
| 207 |
"--auth-default-access=" + conf.AuthDefault.String(), |
| 208 |
} |
| 209 |
return app.Run(append(userArgs, args...)) |
| 210 |
} |