Debian Codegraph @

cmd/access_test.go

raw
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 "testing"
10 )
11
12 func TestCLI_Access_Show(t *testing.T) {
13 s, conf, port := newTestServerWithAuth(t)
14 defer test.StopServer(t, s, port)
15
16 app, _, stdout, _ := newTestApp()
17 require.Nil(t, runAccessCommand(app, conf))
18 require.Contains(t, stdout.String(), "user * (role: anonymous, tier: none)\n- no topic-specific permissions\n- no access to any (other) topics (server config)")
19 }
20
21 func TestCLI_Access_Grant_And_Publish(t *testing.T) {
22 s, conf, port := newTestServerWithAuth(t)
23 defer test.StopServer(t, s, port)
24
25 app, stdin, _, _ := newTestApp()
26 stdin.WriteString("philpass\nphilpass\nbenpass\nbenpass")
27 require.Nil(t, runUserCommand(app, conf, "add", "--role=admin", "phil"))
28 require.Nil(t, runUserCommand(app, conf, "add", "ben"))
29 require.Nil(t, runAccessCommand(app, conf, "ben", "announcements", "rw"))
30 require.Nil(t, runAccessCommand(app, conf, "ben", "sometopic", "read"))
31 require.Nil(t, runAccessCommand(app, conf, "everyone", "announcements", "read"))
32
33 app, _, stdout, _ := newTestApp()
34 require.Nil(t, runAccessCommand(app, conf))
35 expected := `user phil (role: admin, tier: none)
36 - read-write access to all topics (admin role)
37 user ben (role: user, tier: none)
38 - read-write access to topic announcements
39 - read-only access to topic sometopic
40 user * (role: anonymous, tier: none)
41 - read-only access to topic announcements
42 - no access to any (other) topics (server config)
43 `
44 require.Equal(t, expected, stdout.String())
45
46 // See if access permissions match
47 app, _, _, _ = newTestApp()
48 require.Error(t, app.Run([]string{
49 "ntfy",
50 "publish",
51 fmt.Sprintf("http://127.0.0.1:%d/announcements", port),
52 }))
53 require.Nil(t, app.Run([]string{
54 "ntfy",
55 "publish",
56 "-u", "ben:benpass",
57 fmt.Sprintf("http://127.0.0.1:%d/announcements", port),
58 }))
59 require.Nil(t, app.Run([]string{
60 "ntfy",
61 "publish",
62 "-u", "phil:philpass",
63 fmt.Sprintf("http://127.0.0.1:%d/announcements", port),
64 }))
65 require.Nil(t, app.Run([]string{
66 "ntfy",
67 "subscribe",
68 "--poll",
69 fmt.Sprintf("http://127.0.0.1:%d/announcements", port),
70 }))
71 require.Error(t, app.Run([]string{
72 "ntfy",
73 "subscribe",
74 "--poll",
75 fmt.Sprintf("http://127.0.0.1:%d/something-else", port),
76 }))
77 }
78
79 func runAccessCommand(app *cli.App, conf *server.Config, args ...string) error {
80 userArgs := []string{
81 "ntfy",
82 "--log-level=ERROR",
83 "access",
84 "--config=" + conf.File, // Dummy config file to avoid lookups of real file
85 "--auth-file=" + conf.AuthFile,
86 "--auth-default-access=" + conf.AuthDefault.String(),
87 }
88 return app.Run(append(userArgs, args...))
89 }