publish_test.go 5.4 KB

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