user_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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, stdout, _ := newTestApp()
  16. stdin.WriteString("mypass\nmypass")
  17. require.Nil(t, runUserCommand(app, conf, "add", "phil"))
  18. require.Contains(t, stdout.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, stdout, _ := newTestApp()
  24. stdin.WriteString("mypass\nmypass")
  25. require.Nil(t, runUserCommand(app, conf, "add", "phil"))
  26. require.Contains(t, stdout.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, stdout, _ := newTestApp()
  37. stdin.WriteString("mypass\nmypass")
  38. require.Nil(t, runUserCommand(app, conf, "add", "--role=admin", "phil"))
  39. require.Contains(t, stdout.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. conf.AuthUsers = []*user.User{
  53. {Name: "philuser", Hash: "$2a$10$U4WSIYY6evyGmZaraavM2e2JeVG6EMGUKN1uUwufUeeRd4Jpg6cGC", Role: user.RoleUser}, // philuser:philpass
  54. }
  55. defer test.StopServer(t, s, port)
  56. // Add user
  57. app, stdin, stdout, _ := newTestApp()
  58. stdin.WriteString("mypass\nmypass")
  59. require.Nil(t, runUserCommand(app, conf, "add", "phil"))
  60. require.Contains(t, stdout.String(), "user phil added with role user")
  61. // Change pass
  62. app, stdin, stdout, _ = newTestApp()
  63. stdin.WriteString("newpass\nnewpass")
  64. require.Nil(t, runUserCommand(app, conf, "change-pass", "phil"))
  65. require.Contains(t, stdout.String(), "changed password for user phil")
  66. // Cannot change provisioned user's pass
  67. app, stdin, _, _ = newTestApp()
  68. stdin.WriteString("newpass\nnewpass")
  69. require.Error(t, runUserCommand(app, conf, "change-pass", "philuser"))
  70. }
  71. func TestCLI_User_ChangeRole(t *testing.T) {
  72. s, conf, port := newTestServerWithAuth(t)
  73. defer test.StopServer(t, s, port)
  74. // Add user
  75. app, stdin, stdout, _ := newTestApp()
  76. stdin.WriteString("mypass\nmypass")
  77. require.Nil(t, runUserCommand(app, conf, "add", "phil"))
  78. require.Contains(t, stdout.String(), "user phil added with role user")
  79. // Change role
  80. app, _, stdout, _ = newTestApp()
  81. require.Nil(t, runUserCommand(app, conf, "change-role", "phil", "admin"))
  82. require.Contains(t, stdout.String(), "changed role for user phil to admin")
  83. }
  84. func TestCLI_User_Delete(t *testing.T) {
  85. s, conf, port := newTestServerWithAuth(t)
  86. defer test.StopServer(t, s, port)
  87. // Add user
  88. app, stdin, stdout, _ := newTestApp()
  89. stdin.WriteString("mypass\nmypass")
  90. require.Nil(t, runUserCommand(app, conf, "add", "phil"))
  91. require.Contains(t, stdout.String(), "user phil added with role user")
  92. // Delete user
  93. app, _, stdout, _ = newTestApp()
  94. require.Nil(t, runUserCommand(app, conf, "del", "phil"))
  95. require.Contains(t, stdout.String(), "user phil removed")
  96. // Delete user again (does not exist)
  97. app, _, _, _ = newTestApp()
  98. err := runUserCommand(app, conf, "del", "phil")
  99. require.Error(t, err)
  100. require.Contains(t, err.Error(), "user phil does not exist")
  101. }
  102. func newTestServerWithAuth(t *testing.T) (s *server.Server, conf *server.Config, port int) {
  103. configFile := filepath.Join(t.TempDir(), "server-dummy.yml")
  104. require.Nil(t, os.WriteFile(configFile, []byte(""), 0600)) // Dummy config file to avoid lookup of real server.yml
  105. conf = server.NewConfig()
  106. conf.File = configFile
  107. conf.AuthFile = filepath.Join(t.TempDir(), "user.db")
  108. conf.AuthDefault = user.PermissionDenyAll
  109. s, port = test.StartServerWithConfig(t, conf)
  110. return
  111. }
  112. func runUserCommand(app *cli.App, conf *server.Config, args ...string) error {
  113. userArgs := []string{
  114. "ntfy",
  115. "--log-level=ERROR",
  116. "user",
  117. "--config=" + conf.File, // Dummy config file to avoid lookups of real file
  118. "--auth-file=" + conf.AuthFile,
  119. "--auth-default-access=" + conf.AuthDefault.String(),
  120. }
  121. return app.Run(append(userArgs, args...))
  122. }