user_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package cmd
  2. import (
  3. "github.com/stretchr/testify/require"
  4. "github.com/urfave/cli/v2"
  5. "heckel.io/ntfy/v2/server"
  6. "heckel.io/ntfy/v2/test"
  7. "heckel.io/ntfy/v2/user"
  8. "os"
  9. "path/filepath"
  10. "testing"
  11. )
  12. func TestCLI_User_Add(t *testing.T) {
  13. s, conf, port := newTestServerWithAuth(t)
  14. defer test.StopServer(t, s, port)
  15. app, stdin, _, stderr := newTestApp()
  16. stdin.WriteString("mypass\nmypass")
  17. require.Nil(t, runUserCommand(app, conf, "add", "phil"))
  18. require.Contains(t, stderr.String(), "user phil added with role user")
  19. }
  20. func TestCLI_User_Add_Exists(t *testing.T) {
  21. s, conf, port := newTestServerWithAuth(t)
  22. defer test.StopServer(t, s, port)
  23. app, stdin, _, stderr := newTestApp()
  24. stdin.WriteString("mypass\nmypass")
  25. require.Nil(t, runUserCommand(app, conf, "add", "phil"))
  26. require.Contains(t, stderr.String(), "user phil added with role user")
  27. app, stdin, _, _ = newTestApp()
  28. stdin.WriteString("mypass\nmypass")
  29. err := runUserCommand(app, conf, "add", "phil")
  30. require.Error(t, err)
  31. require.Contains(t, err.Error(), "user phil already exists")
  32. }
  33. func TestCLI_User_Add_Admin(t *testing.T) {
  34. s, conf, port := newTestServerWithAuth(t)
  35. defer test.StopServer(t, s, port)
  36. app, stdin, _, stderr := newTestApp()
  37. stdin.WriteString("mypass\nmypass")
  38. require.Nil(t, runUserCommand(app, conf, "add", "--role=admin", "phil"))
  39. require.Contains(t, stderr.String(), "user phil added with role admin")
  40. }
  41. func TestCLI_User_Add_Password_Mismatch(t *testing.T) {
  42. s, conf, port := newTestServerWithAuth(t)
  43. defer test.StopServer(t, s, port)
  44. app, stdin, _, _ := newTestApp()
  45. stdin.WriteString("mypass\nNOTMATCH")
  46. err := runUserCommand(app, conf, "add", "phil")
  47. require.Error(t, err)
  48. require.Contains(t, err.Error(), "passwords do not match: try it again, but this time type slooowwwlly")
  49. }
  50. func TestCLI_User_ChangePass(t *testing.T) {
  51. s, conf, port := newTestServerWithAuth(t)
  52. defer test.StopServer(t, s, port)
  53. // Add user
  54. app, stdin, _, stderr := newTestApp()
  55. stdin.WriteString("mypass\nmypass")
  56. require.Nil(t, runUserCommand(app, conf, "add", "phil"))
  57. require.Contains(t, stderr.String(), "user phil added with role user")
  58. // Change pass
  59. app, stdin, _, stderr = newTestApp()
  60. stdin.WriteString("newpass\nnewpass")
  61. require.Nil(t, runUserCommand(app, conf, "change-pass", "phil"))
  62. require.Contains(t, stderr.String(), "changed password for user phil")
  63. }
  64. func TestCLI_User_ChangeRole(t *testing.T) {
  65. s, conf, port := newTestServerWithAuth(t)
  66. defer test.StopServer(t, s, port)
  67. // Add user
  68. app, stdin, _, stderr := newTestApp()
  69. stdin.WriteString("mypass\nmypass")
  70. require.Nil(t, runUserCommand(app, conf, "add", "phil"))
  71. require.Contains(t, stderr.String(), "user phil added with role user")
  72. // Change role
  73. app, _, _, stderr = newTestApp()
  74. require.Nil(t, runUserCommand(app, conf, "change-role", "phil", "admin"))
  75. require.Contains(t, stderr.String(), "changed role for user phil to admin")
  76. }
  77. func TestCLI_User_Delete(t *testing.T) {
  78. s, conf, port := newTestServerWithAuth(t)
  79. defer test.StopServer(t, s, port)
  80. // Add user
  81. app, stdin, _, stderr := newTestApp()
  82. stdin.WriteString("mypass\nmypass")
  83. require.Nil(t, runUserCommand(app, conf, "add", "phil"))
  84. require.Contains(t, stderr.String(), "user phil added with role user")
  85. // Delete user
  86. app, _, _, stderr = newTestApp()
  87. require.Nil(t, runUserCommand(app, conf, "del", "phil"))
  88. require.Contains(t, stderr.String(), "user phil removed")
  89. // Delete user again (does not exist)
  90. app, _, _, _ = newTestApp()
  91. err := runUserCommand(app, conf, "del", "phil")
  92. require.Error(t, err)
  93. require.Contains(t, err.Error(), "user phil does not exist")
  94. }
  95. func newTestServerWithAuth(t *testing.T) (s *server.Server, conf *server.Config, port int) {
  96. configFile := filepath.Join(t.TempDir(), "server-dummy.yml")
  97. require.Nil(t, os.WriteFile(configFile, []byte(""), 0600)) // Dummy config file to avoid lookup of real server.yml
  98. conf = server.NewConfig()
  99. conf.File = configFile
  100. conf.AuthFile = filepath.Join(t.TempDir(), "user.db")
  101. conf.AuthDefault = user.PermissionDenyAll
  102. s, port = test.StartServerWithConfig(t, conf)
  103. return
  104. }
  105. func runUserCommand(app *cli.App, conf *server.Config, args ...string) error {
  106. userArgs := []string{
  107. "ntfy",
  108. "--log-level=ERROR",
  109. "user",
  110. "--config=" + conf.File, // Dummy config file to avoid lookups of real file
  111. "--auth-file=" + conf.AuthFile,
  112. "--auth-default-access=" + conf.AuthDefault.String(),
  113. }
  114. return app.Run(append(userArgs, args...))
  115. }