File: token_test.go

package info (click to toggle)
ntfy 2.11.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 19,364 kB
  • sloc: javascript: 16,782; makefile: 282; sh: 105; php: 21; python: 19
file content (50 lines) | stat: -rw-r--r-- 1,616 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package cmd

import (
	"fmt"
	"github.com/stretchr/testify/require"
	"github.com/urfave/cli/v2"
	"heckel.io/ntfy/v2/server"
	"heckel.io/ntfy/v2/test"
	"regexp"
	"testing"
)

func TestCLI_Token_AddListRemove(t *testing.T) {
	s, conf, port := newTestServerWithAuth(t)
	defer test.StopServer(t, s, port)

	app, stdin, _, stderr := newTestApp()
	stdin.WriteString("mypass\nmypass")
	require.Nil(t, runUserCommand(app, conf, "add", "phil"))
	require.Contains(t, stderr.String(), "user phil added with role user")

	app, _, _, stderr = newTestApp()
	require.Nil(t, runTokenCommand(app, conf, "add", "phil"))
	require.Regexp(t, `token tk_.+ created for user phil, never expires`, stderr.String())

	app, _, _, stderr = newTestApp()
	require.Nil(t, runTokenCommand(app, conf, "list", "phil"))
	require.Regexp(t, `user phil\n- tk_.+, never expires, accessed from 0.0.0.0 at .+`, stderr.String())
	re := regexp.MustCompile(`tk_\w+`)
	token := re.FindString(stderr.String())

	app, _, _, stderr = newTestApp()
	require.Nil(t, runTokenCommand(app, conf, "remove", "phil", token))
	require.Regexp(t, fmt.Sprintf("token %s for user phil removed", token), stderr.String())

	app, _, _, stderr = newTestApp()
	require.Nil(t, runTokenCommand(app, conf, "list"))
	require.Equal(t, "no users with tokens\n", stderr.String())
}

func runTokenCommand(app *cli.App, conf *server.Config, args ...string) error {
	userArgs := []string{
		"ntfy",
		"--log-level=ERROR",
		"token",
		"--config=" + conf.File, // Dummy config file to avoid lookups of real file
		"--auth-file=" + conf.AuthFile,
	}
	return app.Run(append(userArgs, args...))
}