user_test.go 4.1 KB

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