access_test.go 2.7 KB

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