access_test.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package cmd
  2. import (
  3. "fmt"
  4. "github.com/stretchr/testify/require"
  5. "github.com/urfave/cli/v2"
  6. "heckel.io/ntfy/server"
  7. "heckel.io/ntfy/test"
  8. "testing"
  9. )
  10. func TestCLI_Access_Show(t *testing.T) {
  11. s, conf, port := newTestServerWithAuth(t)
  12. defer test.StopServer(t, s, port)
  13. app, _, _, stderr := newTestApp()
  14. require.Nil(t, runAccessCommand(app, conf))
  15. require.Contains(t, stderr.String(), "user * (anonymous)\n- no topic-specific permissions\n- no access to any (other) topics (server config)")
  16. }
  17. func TestCLI_Access_Grant_And_Publish(t *testing.T) {
  18. s, conf, port := newTestServerWithAuth(t)
  19. defer test.StopServer(t, s, port)
  20. app, stdin, _, _ := newTestApp()
  21. stdin.WriteString("philpass\nphilpass\nbenpass\nbenpass")
  22. require.Nil(t, runUserCommand(app, conf, "add", "--role=admin", "phil"))
  23. require.Nil(t, runUserCommand(app, conf, "add", "ben"))
  24. app, stdin, _, _ = newTestApp()
  25. require.Nil(t, runAccessCommand(app, conf, "ben", "announcements", "rw"))
  26. require.Nil(t, runAccessCommand(app, conf, "ben", "sometopic", "read"))
  27. require.Nil(t, runAccessCommand(app, conf, "everyone", "announcements", "read"))
  28. app, _, _, stderr := newTestApp()
  29. require.Nil(t, runAccessCommand(app, conf))
  30. expected := `user phil (admin)
  31. - read-write access to all topics (admin role)
  32. user ben (user)
  33. - read-write access to topic announcements
  34. - read-only access to topic sometopic
  35. user * (anonymous)
  36. - read-only access to topic announcements
  37. - no access to any (other) topics (server config)
  38. `
  39. require.Equal(t, expected, stderr.String())
  40. // See if access permissions match
  41. app, _, _, _ = newTestApp()
  42. require.Error(t, app.Run([]string{
  43. "ntfy",
  44. "publish",
  45. fmt.Sprintf("http://127.0.0.1:%d/announcements", port),
  46. }))
  47. require.Nil(t, app.Run([]string{
  48. "ntfy",
  49. "publish",
  50. "-u", "ben:benpass",
  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", "phil:philpass",
  57. fmt.Sprintf("http://127.0.0.1:%d/announcements", port),
  58. }))
  59. require.Nil(t, app.Run([]string{
  60. "ntfy",
  61. "subscribe",
  62. "--poll",
  63. fmt.Sprintf("http://127.0.0.1:%d/announcements", port),
  64. }))
  65. require.Error(t, app.Run([]string{
  66. "ntfy",
  67. "subscribe",
  68. "--poll",
  69. fmt.Sprintf("http://127.0.0.1:%d/something-else", port),
  70. }))
  71. }
  72. func runAccessCommand(app *cli.App, conf *server.Config, args ...string) error {
  73. userArgs := []string{
  74. "ntfy",
  75. "access",
  76. "--auth-file=" + conf.AuthFile,
  77. "--auth-default-access=" + conf.AuthDefault.String(),
  78. }
  79. return app.Run(append(userArgs, args...))
  80. }