subscribe_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. package cmd
  2. import (
  3. "fmt"
  4. "github.com/stretchr/testify/require"
  5. "net/http"
  6. "net/http/httptest"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "testing"
  11. )
  12. func TestCLI_Subscribe_Default_UserPass_Subscription_Token(t *testing.T) {
  13. message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}`
  14. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  15. require.Equal(t, "/mytopic/json", r.URL.Path)
  16. require.Equal(t, "Bearer tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", r.Header.Get("Authorization"))
  17. w.WriteHeader(http.StatusOK)
  18. w.Write([]byte(message))
  19. }))
  20. defer server.Close()
  21. filename := filepath.Join(t.TempDir(), "client.yml")
  22. require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(`
  23. default-host: %s
  24. default-user: philipp
  25. default-password: mypass
  26. subscribe:
  27. - topic: mytopic
  28. token: tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2
  29. `, server.URL)), 0600))
  30. app, _, stdout, _ := newTestApp()
  31. require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename}))
  32. require.Equal(t, message, strings.TrimSpace(stdout.String()))
  33. }
  34. func TestCLI_Subscribe_Default_Token_Subscription_UserPass(t *testing.T) {
  35. message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}`
  36. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  37. require.Equal(t, "/mytopic/json", r.URL.Path)
  38. require.Equal(t, "Basic cGhpbGlwcDpteXBhc3M=", r.Header.Get("Authorization"))
  39. w.WriteHeader(http.StatusOK)
  40. w.Write([]byte(message))
  41. }))
  42. defer server.Close()
  43. filename := filepath.Join(t.TempDir(), "client.yml")
  44. require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(`
  45. default-host: %s
  46. default-token: tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2
  47. subscribe:
  48. - topic: mytopic
  49. user: philipp
  50. password: mypass
  51. `, server.URL)), 0600))
  52. app, _, stdout, _ := newTestApp()
  53. require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename}))
  54. require.Equal(t, message, strings.TrimSpace(stdout.String()))
  55. }
  56. func TestCLI_Subscribe_Default_Token_Subscription_Token(t *testing.T) {
  57. message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}`
  58. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  59. require.Equal(t, "/mytopic/json", r.URL.Path)
  60. require.Equal(t, "Bearer tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", r.Header.Get("Authorization"))
  61. w.WriteHeader(http.StatusOK)
  62. w.Write([]byte(message))
  63. }))
  64. defer server.Close()
  65. filename := filepath.Join(t.TempDir(), "client.yml")
  66. require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(`
  67. default-host: %s
  68. default-token: tk_FAKETOKEN01234567890FAKETOKEN
  69. subscribe:
  70. - topic: mytopic
  71. token: tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2
  72. `, server.URL)), 0600))
  73. app, _, stdout, _ := newTestApp()
  74. require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename}))
  75. require.Equal(t, message, strings.TrimSpace(stdout.String()))
  76. }
  77. func TestCLI_Subscribe_Default_UserPass_Subscription_UserPass(t *testing.T) {
  78. message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}`
  79. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  80. require.Equal(t, "/mytopic/json", r.URL.Path)
  81. require.Equal(t, "Basic cGhpbGlwcDpteXBhc3M=", r.Header.Get("Authorization"))
  82. w.WriteHeader(http.StatusOK)
  83. w.Write([]byte(message))
  84. }))
  85. defer server.Close()
  86. filename := filepath.Join(t.TempDir(), "client.yml")
  87. require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(`
  88. default-host: %s
  89. default-user: fake
  90. default-password: password
  91. subscribe:
  92. - topic: mytopic
  93. user: philipp
  94. password: mypass
  95. `, server.URL)), 0600))
  96. app, _, stdout, _ := newTestApp()
  97. require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename}))
  98. require.Equal(t, message, strings.TrimSpace(stdout.String()))
  99. }
  100. func TestCLI_Subscribe_Default_Token_Subscription_Empty(t *testing.T) {
  101. message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}`
  102. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  103. require.Equal(t, "/mytopic/json", r.URL.Path)
  104. require.Equal(t, "Bearer tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", r.Header.Get("Authorization"))
  105. w.WriteHeader(http.StatusOK)
  106. w.Write([]byte(message))
  107. }))
  108. defer server.Close()
  109. filename := filepath.Join(t.TempDir(), "client.yml")
  110. require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(`
  111. default-host: %s
  112. default-token: tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2
  113. subscribe:
  114. - topic: mytopic
  115. `, server.URL)), 0600))
  116. app, _, stdout, _ := newTestApp()
  117. require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename}))
  118. require.Equal(t, message, strings.TrimSpace(stdout.String()))
  119. }
  120. func TestCLI_Subscribe_Default_UserPass_Subscription_Empty(t *testing.T) {
  121. message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}`
  122. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  123. require.Equal(t, "/mytopic/json", r.URL.Path)
  124. require.Equal(t, "Basic cGhpbGlwcDpteXBhc3M=", r.Header.Get("Authorization"))
  125. w.WriteHeader(http.StatusOK)
  126. w.Write([]byte(message))
  127. }))
  128. defer server.Close()
  129. filename := filepath.Join(t.TempDir(), "client.yml")
  130. require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(`
  131. default-host: %s
  132. default-user: philipp
  133. default-password: mypass
  134. subscribe:
  135. - topic: mytopic
  136. `, server.URL)), 0600))
  137. app, _, stdout, _ := newTestApp()
  138. require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename}))
  139. require.Equal(t, message, strings.TrimSpace(stdout.String()))
  140. }
  141. func TestCLI_Subscribe_Default_Empty_Subscription_Token(t *testing.T) {
  142. message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}`
  143. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  144. require.Equal(t, "/mytopic/json", r.URL.Path)
  145. require.Equal(t, "Bearer tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", r.Header.Get("Authorization"))
  146. w.WriteHeader(http.StatusOK)
  147. w.Write([]byte(message))
  148. }))
  149. defer server.Close()
  150. filename := filepath.Join(t.TempDir(), "client.yml")
  151. require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(`
  152. default-host: %s
  153. subscribe:
  154. - topic: mytopic
  155. token: tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2
  156. `, server.URL)), 0600))
  157. app, _, stdout, _ := newTestApp()
  158. require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename}))
  159. require.Equal(t, message, strings.TrimSpace(stdout.String()))
  160. }
  161. func TestCLI_Subscribe_Default_Empty_Subscription_UserPass(t *testing.T) {
  162. message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}`
  163. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  164. require.Equal(t, "/mytopic/json", r.URL.Path)
  165. require.Equal(t, "Basic cGhpbGlwcDpteXBhc3M=", r.Header.Get("Authorization"))
  166. w.WriteHeader(http.StatusOK)
  167. w.Write([]byte(message))
  168. }))
  169. defer server.Close()
  170. filename := filepath.Join(t.TempDir(), "client.yml")
  171. require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(`
  172. default-host: %s
  173. subscribe:
  174. - topic: mytopic
  175. user: philipp
  176. password: mypass
  177. `, server.URL)), 0600))
  178. app, _, stdout, _ := newTestApp()
  179. require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename}))
  180. require.Equal(t, message, strings.TrimSpace(stdout.String()))
  181. }
  182. func TestCLI_Subscribe_Default_Token_CLI_Token(t *testing.T) {
  183. message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}`
  184. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  185. require.Equal(t, "/mytopic/json", r.URL.Path)
  186. require.Equal(t, "Bearer tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", r.Header.Get("Authorization"))
  187. w.WriteHeader(http.StatusOK)
  188. w.Write([]byte(message))
  189. }))
  190. defer server.Close()
  191. filename := filepath.Join(t.TempDir(), "client.yml")
  192. require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(`
  193. default-host: %s
  194. default-token: tk_FAKETOKEN0123456789FAKETOKEN
  195. `, server.URL)), 0600))
  196. app, _, stdout, _ := newTestApp()
  197. require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename, "--token", "tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", "mytopic"}))
  198. require.Equal(t, message, strings.TrimSpace(stdout.String()))
  199. }
  200. func TestCLI_Subscribe_Default_Token_CLI_UserPass(t *testing.T) {
  201. message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}`
  202. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  203. require.Equal(t, "/mytopic/json", r.URL.Path)
  204. require.Equal(t, "Basic cGhpbGlwcDpteXBhc3M=", r.Header.Get("Authorization"))
  205. w.WriteHeader(http.StatusOK)
  206. w.Write([]byte(message))
  207. }))
  208. defer server.Close()
  209. filename := filepath.Join(t.TempDir(), "client.yml")
  210. require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(`
  211. default-host: %s
  212. default-token: tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2
  213. `, server.URL)), 0600))
  214. app, _, stdout, _ := newTestApp()
  215. require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename, "--user", "philipp:mypass", "mytopic"}))
  216. require.Equal(t, message, strings.TrimSpace(stdout.String()))
  217. }
  218. func TestCLI_Subscribe_Default_Token_Subscription_Token_CLI_UserPass(t *testing.T) {
  219. message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}`
  220. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  221. require.Equal(t, "/mytopic/json", r.URL.Path)
  222. require.Equal(t, "Bearer tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", r.Header.Get("Authorization"))
  223. w.WriteHeader(http.StatusOK)
  224. w.Write([]byte(message))
  225. }))
  226. defer server.Close()
  227. filename := filepath.Join(t.TempDir(), "client.yml")
  228. require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(`
  229. default-host: %s
  230. default-token: tk_FAKETOKEN01234567890FAKETOKEN
  231. subscribe:
  232. - topic: mytopic
  233. token: tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2
  234. `, server.URL)), 0600))
  235. app, _, stdout, _ := newTestApp()
  236. require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename, "--user", "philipp:mypass"}))
  237. require.Equal(t, message, strings.TrimSpace(stdout.String()))
  238. }
  239. func TestCLI_Subscribe_Token_And_UserPass(t *testing.T) {
  240. app, _, _, _ := newTestApp()
  241. err := app.Run([]string{"ntfy", "subscribe", "--poll", "--token", "tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", "--user", "philipp:mypass", "mytopic", "triggered"})
  242. require.Error(t, err)
  243. require.Equal(t, "cannot set both --user and --token", err.Error())
  244. }
  245. func TestCLI_Subscribe_Default_Token(t *testing.T) {
  246. message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}`
  247. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  248. require.Equal(t, "/mytopic/json", r.URL.Path)
  249. require.Equal(t, "Bearer tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", r.Header.Get("Authorization"))
  250. w.WriteHeader(http.StatusOK)
  251. w.Write([]byte(message))
  252. }))
  253. defer server.Close()
  254. filename := filepath.Join(t.TempDir(), "client.yml")
  255. require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(`
  256. default-host: %s
  257. default-token: tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2
  258. `, server.URL)), 0600))
  259. app, _, stdout, _ := newTestApp()
  260. require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--config=" + filename, "mytopic"}))
  261. require.Equal(t, message, strings.TrimSpace(stdout.String()))
  262. }
  263. func TestCLI_Subscribe_Default_UserPass(t *testing.T) {
  264. message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}`
  265. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  266. require.Equal(t, "/mytopic/json", r.URL.Path)
  267. require.Equal(t, "Basic cGhpbGlwcDpteXBhc3M=", r.Header.Get("Authorization"))
  268. w.WriteHeader(http.StatusOK)
  269. w.Write([]byte(message))
  270. }))
  271. defer server.Close()
  272. filename := filepath.Join(t.TempDir(), "client.yml")
  273. require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(`
  274. default-host: %s
  275. default-user: philipp
  276. default-password: mypass
  277. `, server.URL)), 0600))
  278. app, _, stdout, _ := newTestApp()
  279. require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--config=" + filename, "mytopic"}))
  280. require.Equal(t, message, strings.TrimSpace(stdout.String()))
  281. }