user_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. "path/filepath"
  8. "testing"
  9. )
  10. func TestCLI_User_Add(t *testing.T) {
  11. s, conf, port := newTestServerWithAuth(t)
  12. defer test.StopServer(t, s, port)
  13. app, stdin, _, stderr := newTestApp()
  14. stdin.WriteString("mypass\nmypass")
  15. require.Nil(t, runUserCommand(app, conf, "add", "phil"))
  16. require.Contains(t, stderr.String(), "user phil added with role user")
  17. }
  18. func TestCLI_User_Add_Exists(t *testing.T) {
  19. s, conf, port := newTestServerWithAuth(t)
  20. defer test.StopServer(t, s, port)
  21. app, stdin, _, stderr := newTestApp()
  22. stdin.WriteString("mypass\nmypass")
  23. require.Nil(t, runUserCommand(app, conf, "add", "phil"))
  24. require.Contains(t, stderr.String(), "user phil added with role user")
  25. app, stdin, _, _ = newTestApp()
  26. stdin.WriteString("mypass\nmypass")
  27. err := runUserCommand(app, conf, "add", "phil")
  28. require.Error(t, err)
  29. require.Contains(t, err.Error(), "user phil already exists")
  30. }
  31. func TestCLI_User_Add_Admin(t *testing.T) {
  32. s, conf, port := newTestServerWithAuth(t)
  33. defer test.StopServer(t, s, port)
  34. app, stdin, _, stderr := newTestApp()
  35. stdin.WriteString("mypass\nmypass")
  36. require.Nil(t, runUserCommand(app, conf, "add", "--role=admin", "phil"))
  37. require.Contains(t, stderr.String(), "user phil added with role admin")
  38. }
  39. func TestCLI_User_Add_Password_Mismatch(t *testing.T) {
  40. s, conf, port := newTestServerWithAuth(t)
  41. defer test.StopServer(t, s, port)
  42. app, stdin, _, _ := newTestApp()
  43. stdin.WriteString("mypass\nNOTMATCH")
  44. err := runUserCommand(app, conf, "add", "phil")
  45. require.Error(t, err)
  46. require.Contains(t, err.Error(), "passwords do not match: try it again, but this time type slooowwwlly")
  47. }
  48. func TestCLI_User_ChangePass(t *testing.T) {
  49. s, conf, port := newTestServerWithAuth(t)
  50. defer test.StopServer(t, s, port)
  51. // Add user
  52. app, stdin, _, stderr := newTestApp()
  53. stdin.WriteString("mypass\nmypass")
  54. require.Nil(t, runUserCommand(app, conf, "add", "phil"))
  55. require.Contains(t, stderr.String(), "user phil added with role user")
  56. // Change pass
  57. app, stdin, _, stderr = newTestApp()
  58. stdin.WriteString("newpass\nnewpass")
  59. require.Nil(t, runUserCommand(app, conf, "change-pass", "phil"))
  60. require.Contains(t, stderr.String(), "changed password for user phil")
  61. }
  62. func TestCLI_User_ChangeRole(t *testing.T) {
  63. s, conf, port := newTestServerWithAuth(t)
  64. defer test.StopServer(t, s, port)
  65. // Add user
  66. app, stdin, _, stderr := newTestApp()
  67. stdin.WriteString("mypass\nmypass")
  68. require.Nil(t, runUserCommand(app, conf, "add", "phil"))
  69. require.Contains(t, stderr.String(), "user phil added with role user")
  70. // Change role
  71. app, _, _, stderr = newTestApp()
  72. require.Nil(t, runUserCommand(app, conf, "change-role", "phil", "admin"))
  73. require.Contains(t, stderr.String(), "changed role for user phil to admin")
  74. }
  75. func TestCLI_User_Delete(t *testing.T) {
  76. s, conf, port := newTestServerWithAuth(t)
  77. defer test.StopServer(t, s, port)
  78. // Add user
  79. app, stdin, _, stderr := newTestApp()
  80. stdin.WriteString("mypass\nmypass")
  81. require.Nil(t, runUserCommand(app, conf, "add", "phil"))
  82. require.Contains(t, stderr.String(), "user phil added with role user")
  83. // Delete user
  84. app, _, _, stderr = newTestApp()
  85. require.Nil(t, runUserCommand(app, conf, "del", "phil"))
  86. require.Contains(t, stderr.String(), "user phil removed")
  87. // Delete user again (does not exist)
  88. app, _, _, _ = newTestApp()
  89. err := runUserCommand(app, conf, "del", "phil")
  90. require.Error(t, err)
  91. require.Contains(t, err.Error(), "user phil does not exist")
  92. }
  93. func newTestServerWithAuth(t *testing.T) (s *server.Server, conf *server.Config, port int) {
  94. conf = server.NewConfig()
  95. conf.AuthFile = filepath.Join(t.TempDir(), "user.db")
  96. conf.AuthDefaultRead = false
  97. conf.AuthDefaultWrite = false
  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=" + confToDefaultAccess(conf),
  107. }
  108. return app.Run(append(userArgs, args...))
  109. }
  110. func confToDefaultAccess(conf *server.Config) string {
  111. var defaultAccess string
  112. if conf.AuthDefaultRead && conf.AuthDefaultWrite {
  113. defaultAccess = "read-write"
  114. } else if conf.AuthDefaultRead && !conf.AuthDefaultWrite {
  115. defaultAccess = "read-only"
  116. } else if !conf.AuthDefaultRead && conf.AuthDefaultWrite {
  117. defaultAccess = "write-only"
  118. } else if !conf.AuthDefaultRead && !conf.AuthDefaultWrite {
  119. defaultAccess = "deny-all"
  120. }
  121. return defaultAccess
  122. }