token_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. "regexp"
  9. "testing"
  10. )
  11. func TestCLI_Token_AddListRemove(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. app, _, _, stderr = newTestApp()
  19. require.Nil(t, runTokenCommand(app, conf, "add", "phil"))
  20. require.Regexp(t, `token tk_.+ created for user phil, never expires`, stderr.String())
  21. app, _, _, stderr = newTestApp()
  22. require.Nil(t, runTokenCommand(app, conf, "list", "phil"))
  23. require.Regexp(t, `user phil\n- tk_.+, never expires, accessed from 0.0.0.0 at .+`, stderr.String())
  24. re := regexp.MustCompile(`tk_\w+`)
  25. token := re.FindString(stderr.String())
  26. app, _, _, stderr = newTestApp()
  27. require.Nil(t, runTokenCommand(app, conf, "remove", "phil", token))
  28. require.Regexp(t, fmt.Sprintf("token %s for user phil removed", token), stderr.String())
  29. app, _, _, stderr = newTestApp()
  30. require.Nil(t, runTokenCommand(app, conf, "list"))
  31. require.Equal(t, "no users with tokens\n", stderr.String())
  32. }
  33. func runTokenCommand(app *cli.App, conf *server.Config, args ...string) error {
  34. userArgs := []string{
  35. "ntfy",
  36. "--log-level=ERROR",
  37. "token",
  38. "--config=" + conf.File, // Dummy config file to avoid lookups of real file
  39. "--auth-file=" + conf.AuthFile,
  40. }
  41. return app.Run(append(userArgs, args...))
  42. }