| 1 |
package cmd |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"github.com/stretchr/testify/require" |
| 6 |
"github.com/urfave/cli/v2" |
| 7 |
"heckel.io/ntfy/v2/server" |
| 8 |
"heckel.io/ntfy/v2/test" |
| 9 |
"regexp" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
func TestCLI_Token_AddListRemove(t *testing.T) { |
| 14 |
s, conf, port := newTestServerWithAuth(t) |
| 15 |
defer test.StopServer(t, s, port) |
| 16 |
|
| 17 |
app, stdin, stdout, _ := newTestApp() |
| 18 |
stdin.WriteString("mypass\nmypass") |
| 19 |
require.Nil(t, runUserCommand(app, conf, "add", "phil")) |
| 20 |
require.Contains(t, stdout.String(), "user phil added with role user") |
| 21 |
|
| 22 |
app, _, stdout, _ = newTestApp() |
| 23 |
require.Nil(t, runTokenCommand(app, conf, "add", "phil")) |
| 24 |
require.Regexp(t, `token tk_.+ created for user phil, never expires`, stdout.String()) |
| 25 |
|
| 26 |
app, _, stdout, _ = newTestApp() |
| 27 |
require.Nil(t, runTokenCommand(app, conf, "list", "phil")) |
| 28 |
require.Regexp(t, `user phil\n- tk_.+, never expires, accessed from 0.0.0.0 at .+`, stdout.String()) |
| 29 |
re := regexp.MustCompile(`tk_\w+`) |
| 30 |
token := re.FindString(stdout.String()) |
| 31 |
|
| 32 |
app, _, stdout, _ = newTestApp() |
| 33 |
require.Nil(t, runTokenCommand(app, conf, "remove", "phil", token)) |
| 34 |
require.Regexp(t, fmt.Sprintf("token %s for user phil removed", token), stdout.String()) |
| 35 |
|
| 36 |
app, _, stdout, _ = newTestApp() |
| 37 |
require.Nil(t, runTokenCommand(app, conf, "list")) |
| 38 |
require.Equal(t, "no users with tokens\n", stdout.String()) |
| 39 |
} |
| 40 |
|
| 41 |
func runTokenCommand(app *cli.App, conf *server.Config, args ...string) error { |
| 42 |
userArgs := []string{ |
| 43 |
"ntfy", |
| 44 |
"--log-level=ERROR", |
| 45 |
"token", |
| 46 |
"--config=" + conf.File, // Dummy config file to avoid lookups of real file |
| 47 |
"--auth-file=" + conf.AuthFile, |
| 48 |
} |
| 49 |
return app.Run(append(userArgs, args...)) |
| 50 |
} |