publish_test.go 5.1 KB

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