publish_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package cmd
  2. import (
  3. "fmt"
  4. "github.com/stretchr/testify/require"
  5. "heckel.io/ntfy/test"
  6. "heckel.io/ntfy/util"
  7. "testing"
  8. )
  9. func TestCLI_Publish_Subscribe_Poll_Real_Server(t *testing.T) {
  10. testMessage := util.RandomString(10)
  11. app, _, _, _ := newTestApp()
  12. require.Nil(t, app.Run([]string{"ntfy", "publish", "ntfytest", "ntfy unit test " + testMessage}))
  13. app2, _, stdout, _ := newTestApp()
  14. require.Nil(t, app2.Run([]string{"ntfy", "subscribe", "--poll", "ntfytest"}))
  15. require.Contains(t, stdout.String(), testMessage)
  16. }
  17. func TestCLI_Publish_Subscribe_Poll(t *testing.T) {
  18. s, port := test.StartServer(t)
  19. defer test.StopServer(t, s, port)
  20. topic := fmt.Sprintf("http://127.0.0.1:%d/mytopic", port)
  21. app, _, stdout, _ := newTestApp()
  22. require.Nil(t, app.Run([]string{"ntfy", "publish", topic, "some message"}))
  23. m := toMessage(t, stdout.String())
  24. require.Equal(t, "some message", m.Message)
  25. app2, _, stdout, _ := newTestApp()
  26. require.Nil(t, app2.Run([]string{"ntfy", "subscribe", "--poll", topic}))
  27. m = toMessage(t, stdout.String())
  28. require.Equal(t, "some message", m.Message)
  29. }
  30. func TestCLI_Publish_All_The_Things(t *testing.T) {
  31. s, port := test.StartServer(t)
  32. defer test.StopServer(t, s, port)
  33. topic := fmt.Sprintf("http://127.0.0.1:%d/mytopic", port)
  34. app, _, stdout, _ := newTestApp()
  35. require.Nil(t, app.Run([]string{
  36. "ntfy", "publish",
  37. "--title", "this is a title",
  38. "--priority", "high",
  39. "--tags", "tag1,tag2",
  40. // No --delay, --email
  41. "--click", "https://ntfy.sh",
  42. "--attach", "https://f-droid.org/F-Droid.apk",
  43. "--filename", "fdroid.apk",
  44. "--no-cache",
  45. "--no-firebase",
  46. topic,
  47. "some message",
  48. }))
  49. m := toMessage(t, stdout.String())
  50. require.Equal(t, "message", m.Event)
  51. require.Equal(t, "mytopic", m.Topic)
  52. require.Equal(t, "some message", m.Message)
  53. require.Equal(t, "this is a title", m.Title)
  54. require.Equal(t, 4, m.Priority)
  55. require.Equal(t, []string{"tag1", "tag2"}, m.Tags)
  56. require.Equal(t, "https://ntfy.sh", m.Click)
  57. require.Equal(t, "https://f-droid.org/F-Droid.apk", m.Attachment.URL)
  58. require.Equal(t, "fdroid.apk", m.Attachment.Name)
  59. require.Equal(t, int64(0), m.Attachment.Size)
  60. require.Equal(t, "", m.Attachment.Owner)
  61. require.Equal(t, int64(0), m.Attachment.Expires)
  62. require.Equal(t, "", m.Attachment.Type)
  63. }