publish_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package cmd
  2. import (
  3. "fmt"
  4. "github.com/stretchr/testify/require"
  5. "heckel.io/ntfy/test"
  6. "heckel.io/ntfy/util"
  7. "os"
  8. "os/exec"
  9. "strconv"
  10. "testing"
  11. "time"
  12. )
  13. func TestCLI_Publish_Subscribe_Poll_Real_Server(t *testing.T) {
  14. testMessage := util.RandomString(10)
  15. app, _, _, _ := newTestApp()
  16. require.Nil(t, app.Run([]string{"ntfy", "publish", "ntfytest", "ntfy unit test " + testMessage}))
  17. time.Sleep(3 * time.Second) // Since #502, ntfy.sh writes messages to the cache asynchronously, after a timeout of ~1.5s
  18. app2, _, stdout, _ := newTestApp()
  19. require.Nil(t, app2.Run([]string{"ntfy", "subscribe", "--poll", "ntfytest"}))
  20. require.Contains(t, stdout.String(), testMessage)
  21. }
  22. func TestCLI_Publish_Subscribe_Poll(t *testing.T) {
  23. s, port := test.StartServer(t)
  24. defer test.StopServer(t, s, port)
  25. topic := fmt.Sprintf("http://127.0.0.1:%d/mytopic", port)
  26. app, _, stdout, _ := newTestApp()
  27. require.Nil(t, app.Run([]string{"ntfy", "publish", topic, "some message"}))
  28. m := toMessage(t, stdout.String())
  29. require.Equal(t, "some message", m.Message)
  30. app2, _, stdout, _ := newTestApp()
  31. require.Nil(t, app2.Run([]string{"ntfy", "subscribe", "--poll", topic}))
  32. m = toMessage(t, stdout.String())
  33. require.Equal(t, "some message", m.Message)
  34. }
  35. func TestCLI_Publish_All_The_Things(t *testing.T) {
  36. s, port := test.StartServer(t)
  37. defer test.StopServer(t, s, port)
  38. topic := fmt.Sprintf("http://127.0.0.1:%d/mytopic", port)
  39. app, _, stdout, _ := newTestApp()
  40. require.Nil(t, app.Run([]string{
  41. "ntfy", "publish",
  42. "--title", "this is a title",
  43. "--priority", "high",
  44. "--tags", "tag1,tag2",
  45. // No --delay, --email
  46. "--click", "https://ntfy.sh",
  47. "--icon", "https://ntfy.sh/static/img/ntfy.png",
  48. "--attach", "https://f-droid.org/F-Droid.apk",
  49. "--filename", "fdroid.apk",
  50. "--no-cache",
  51. "--no-firebase",
  52. topic,
  53. "some message",
  54. }))
  55. m := toMessage(t, stdout.String())
  56. require.Equal(t, "message", m.Event)
  57. require.Equal(t, "mytopic", m.Topic)
  58. require.Equal(t, "some message", m.Message)
  59. require.Equal(t, "this is a title", m.Title)
  60. require.Equal(t, 4, m.Priority)
  61. require.Equal(t, []string{"tag1", "tag2"}, m.Tags)
  62. require.Equal(t, "https://ntfy.sh", m.Click)
  63. require.Equal(t, "https://f-droid.org/F-Droid.apk", m.Attachment.URL)
  64. require.Equal(t, "fdroid.apk", m.Attachment.Name)
  65. require.Equal(t, int64(0), m.Attachment.Size)
  66. require.Equal(t, "", m.Attachment.Owner)
  67. require.Equal(t, int64(0), m.Attachment.Expires)
  68. require.Equal(t, "", m.Attachment.Type)
  69. require.Equal(t, "https://ntfy.sh/static/img/ntfy.png", m.Icon)
  70. }
  71. func TestCLI_Publish_Wait_PID_And_Cmd(t *testing.T) {
  72. s, port := test.StartServer(t)
  73. defer test.StopServer(t, s, port)
  74. topic := fmt.Sprintf("http://127.0.0.1:%d/mytopic", port)
  75. // Test: sleep 0.5
  76. sleep := exec.Command("sleep", "0.5")
  77. require.Nil(t, sleep.Start())
  78. go sleep.Wait() // Must be called to release resources
  79. start := time.Now()
  80. app, _, stdout, _ := newTestApp()
  81. require.Nil(t, app.Run([]string{"ntfy", "publish", "--wait-pid", strconv.Itoa(sleep.Process.Pid), topic}))
  82. m := toMessage(t, stdout.String())
  83. require.True(t, time.Since(start) >= 500*time.Millisecond)
  84. require.Regexp(t, `Process with PID \d+ exited after `, m.Message)
  85. // Test: PID does not exist
  86. app, _, _, _ = newTestApp()
  87. err := app.Run([]string{"ntfy", "publish", "--wait-pid", "1234567", topic})
  88. require.Error(t, err)
  89. require.Equal(t, "process with PID 1234567 not running", err.Error())
  90. // Test: Successful command (exit 0)
  91. start = time.Now()
  92. app, _, stdout, _ = newTestApp()
  93. require.Nil(t, app.Run([]string{"ntfy", "publish", "--wait-cmd", topic, "sleep", "0.5"}))
  94. m = toMessage(t, stdout.String())
  95. require.True(t, time.Since(start) >= 500*time.Millisecond)
  96. require.Contains(t, m.Message, `Command succeeded after `)
  97. require.Contains(t, m.Message, `: sleep 0.5`)
  98. // Test: Failing command (exit 1)
  99. app, _, stdout, _ = newTestApp()
  100. require.Nil(t, app.Run([]string{"ntfy", "publish", "--wait-cmd", topic, "/bin/false", "false doesn't care about its args"}))
  101. m = toMessage(t, stdout.String())
  102. require.Contains(t, m.Message, `Command failed after `)
  103. require.Contains(t, m.Message, `(exit code 1): /bin/false "false doesn't care about its args"`, m.Message)
  104. // Test: Non-existing command (hard fail!)
  105. app, _, _, _ = newTestApp()
  106. err = app.Run([]string{"ntfy", "publish", "--wait-cmd", topic, "does-not-exist-no-really", "really though"})
  107. require.Error(t, err)
  108. require.Equal(t, `command failed: does-not-exist-no-really "really though", error: exec: "does-not-exist-no-really": executable file not found in $PATH`, err.Error())
  109. // Tests with NTFY_TOPIC set ////
  110. require.Nil(t, os.Setenv("NTFY_TOPIC", topic))
  111. // Test: Successful command with NTFY_TOPIC
  112. app, _, stdout, _ = newTestApp()
  113. require.Nil(t, app.Run([]string{"ntfy", "publish", "--env-topic", "--cmd", "echo", "hi there"}))
  114. m = toMessage(t, stdout.String())
  115. require.Equal(t, "mytopic", m.Topic)
  116. // Test: Successful --wait-pid with NTFY_TOPIC
  117. sleep = exec.Command("sleep", "0.2")
  118. require.Nil(t, sleep.Start())
  119. go sleep.Wait() // Must be called to release resources
  120. app, _, stdout, _ = newTestApp()
  121. require.Nil(t, app.Run([]string{"ntfy", "publish", "--env-topic", "--wait-pid", strconv.Itoa(sleep.Process.Pid)}))
  122. m = toMessage(t, stdout.String())
  123. require.Regexp(t, `Process with PID \d+ exited after .+ms`, m.Message)
  124. }