ntfy-server/cmd/token_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

51 lines
1.6 KiB
Go
Raw Normal View History

2023-01-30 12:19:51 -05:00
package cmd
import (
"fmt"
"github.com/stretchr/testify/require"
"github.com/urfave/cli/v2"
2023-11-16 20:54:58 -05:00
"heckel.io/ntfy/v2/server"
"heckel.io/ntfy/v2/test"
2023-01-30 12:19:51 -05:00
"regexp"
"testing"
)
func TestCLI_Token_AddListRemove(t *testing.T) {
s, conf, port := newTestServerWithAuth(t)
defer test.StopServer(t, s, port)
2025-07-31 11:35:21 +02:00
app, stdin, stdout, _ := newTestApp()
2023-01-30 12:19:51 -05:00
stdin.WriteString("mypass\nmypass")
require.Nil(t, runUserCommand(app, conf, "add", "phil"))
2025-07-31 11:35:21 +02:00
require.Contains(t, stdout.String(), "user phil added with role user")
2023-01-30 12:19:51 -05:00
2025-07-31 11:35:21 +02:00
app, _, stdout, _ = newTestApp()
2023-01-30 12:19:51 -05:00
require.Nil(t, runTokenCommand(app, conf, "add", "phil"))
2025-07-31 11:35:21 +02:00
require.Regexp(t, `token tk_.+ created for user phil, never expires`, stdout.String())
2023-01-30 12:19:51 -05:00
2025-07-31 11:35:21 +02:00
app, _, stdout, _ = newTestApp()
2023-01-30 12:19:51 -05:00
require.Nil(t, runTokenCommand(app, conf, "list", "phil"))
2025-07-31 11:35:21 +02:00
require.Regexp(t, `user phil\n- tk_.+, never expires, accessed from 0.0.0.0 at .+`, stdout.String())
2023-01-30 12:19:51 -05:00
re := regexp.MustCompile(`tk_\w+`)
2025-07-31 11:35:21 +02:00
token := re.FindString(stdout.String())
2023-01-30 12:19:51 -05:00
2025-07-31 11:35:21 +02:00
app, _, stdout, _ = newTestApp()
2023-01-30 12:19:51 -05:00
require.Nil(t, runTokenCommand(app, conf, "remove", "phil", token))
2025-07-31 11:35:21 +02:00
require.Regexp(t, fmt.Sprintf("token %s for user phil removed", token), stdout.String())
2023-01-30 12:19:51 -05:00
2025-07-31 11:35:21 +02:00
app, _, stdout, _ = newTestApp()
2023-01-30 12:19:51 -05:00
require.Nil(t, runTokenCommand(app, conf, "list"))
2025-07-31 11:35:21 +02:00
require.Equal(t, "no users with tokens\n", stdout.String())
2023-01-30 12:19:51 -05:00
}
func runTokenCommand(app *cli.App, conf *server.Config, args ...string) error {
userArgs := []string{
"ntfy",
2023-02-08 15:20:44 -05:00
"--log-level=ERROR",
2023-01-30 12:19:51 -05:00
"token",
2023-02-08 15:20:44 -05:00
"--config=" + conf.File, // Dummy config file to avoid lookups of real file
2023-01-30 12:19:51 -05:00
"--auth-file=" + conf.AuthFile,
}
return app.Run(append(userArgs, args...))
}