server_test.go 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  1. package server
  2. import (
  3. "bufio"
  4. "context"
  5. "encoding/base64"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/stretchr/testify/require"
  9. "heckel.io/ntfy/auth"
  10. "heckel.io/ntfy/util"
  11. "math/rand"
  12. "net/http"
  13. "net/http/httptest"
  14. "os"
  15. "path/filepath"
  16. "strings"
  17. "sync"
  18. "testing"
  19. "time"
  20. )
  21. func TestServer_PublishAndPoll(t *testing.T) {
  22. s := newTestServer(t, newTestConfig(t))
  23. response1 := request(t, s, "PUT", "/mytopic", "my first message", nil)
  24. msg1 := toMessage(t, response1.Body.String())
  25. require.NotEmpty(t, msg1.ID)
  26. require.Equal(t, "my first message", msg1.Message)
  27. response2 := request(t, s, "PUT", "/mytopic", "my second\n\nmessage", nil)
  28. msg2 := toMessage(t, response2.Body.String())
  29. require.NotEqual(t, msg1.ID, msg2.ID)
  30. require.NotEmpty(t, msg2.ID)
  31. require.Equal(t, "my second\n\nmessage", msg2.Message)
  32. response := request(t, s, "GET", "/mytopic/json?poll=1", "", nil)
  33. messages := toMessages(t, response.Body.String())
  34. require.Equal(t, 2, len(messages))
  35. require.Equal(t, "my first message", messages[0].Message)
  36. require.Equal(t, "my second\n\nmessage", messages[1].Message)
  37. response = request(t, s, "GET", "/mytopic/sse?poll=1&since=all", "", nil)
  38. lines := strings.Split(strings.TrimSpace(response.Body.String()), "\n")
  39. require.Equal(t, 3, len(lines))
  40. require.Equal(t, "my first message", toMessage(t, strings.TrimPrefix(lines[0], "data: ")).Message)
  41. require.Equal(t, "", lines[1])
  42. require.Equal(t, "my second\n\nmessage", toMessage(t, strings.TrimPrefix(lines[2], "data: ")).Message)
  43. response = request(t, s, "GET", "/mytopic/raw?poll=1", "", nil)
  44. lines = strings.Split(strings.TrimSpace(response.Body.String()), "\n")
  45. require.Equal(t, 2, len(lines))
  46. require.Equal(t, "my first message", lines[0])
  47. require.Equal(t, "my second message", lines[1]) // \n -> " "
  48. }
  49. func TestServer_SubscribeOpenAndKeepalive(t *testing.T) {
  50. c := newTestConfig(t)
  51. c.KeepaliveInterval = time.Second
  52. s := newTestServer(t, c)
  53. rr := httptest.NewRecorder()
  54. ctx, cancel := context.WithCancel(context.Background())
  55. req, err := http.NewRequestWithContext(ctx, "GET", "/mytopic/json", nil)
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. doneChan := make(chan bool)
  60. go func() {
  61. s.handle(rr, req)
  62. doneChan <- true
  63. }()
  64. time.Sleep(1300 * time.Millisecond)
  65. cancel()
  66. <-doneChan
  67. messages := toMessages(t, rr.Body.String())
  68. require.Equal(t, 2, len(messages))
  69. require.Equal(t, openEvent, messages[0].Event)
  70. require.Equal(t, "mytopic", messages[0].Topic)
  71. require.Equal(t, "", messages[0].Message)
  72. require.Equal(t, "", messages[0].Title)
  73. require.Equal(t, 0, messages[0].Priority)
  74. require.Nil(t, messages[0].Tags)
  75. require.Equal(t, keepaliveEvent, messages[1].Event)
  76. require.Equal(t, "mytopic", messages[1].Topic)
  77. require.Equal(t, "", messages[1].Message)
  78. require.Equal(t, "", messages[1].Title)
  79. require.Equal(t, 0, messages[1].Priority)
  80. require.Nil(t, messages[1].Tags)
  81. }
  82. func TestServer_PublishAndSubscribe(t *testing.T) {
  83. s := newTestServer(t, newTestConfig(t))
  84. subscribeRR := httptest.NewRecorder()
  85. subscribeCancel := subscribe(t, s, "/mytopic/json", subscribeRR)
  86. publishFirstRR := request(t, s, "PUT", "/mytopic", "my first message", nil)
  87. require.Equal(t, 200, publishFirstRR.Code)
  88. publishSecondRR := request(t, s, "PUT", "/mytopic", "my other message", map[string]string{
  89. "Title": " This is a title ",
  90. "X-Tags": "tag1,tag 2, tag3",
  91. "p": "1",
  92. })
  93. require.Equal(t, 200, publishSecondRR.Code)
  94. subscribeCancel()
  95. messages := toMessages(t, subscribeRR.Body.String())
  96. require.Equal(t, 3, len(messages))
  97. require.Equal(t, openEvent, messages[0].Event)
  98. require.Equal(t, messageEvent, messages[1].Event)
  99. require.Equal(t, "mytopic", messages[1].Topic)
  100. require.Equal(t, "my first message", messages[1].Message)
  101. require.Equal(t, "", messages[1].Title)
  102. require.Equal(t, 0, messages[1].Priority)
  103. require.Nil(t, messages[1].Tags)
  104. require.Equal(t, messageEvent, messages[2].Event)
  105. require.Equal(t, "mytopic", messages[2].Topic)
  106. require.Equal(t, "my other message", messages[2].Message)
  107. require.Equal(t, "This is a title", messages[2].Title)
  108. require.Equal(t, 1, messages[2].Priority)
  109. require.Equal(t, []string{"tag1", "tag 2", "tag3"}, messages[2].Tags)
  110. }
  111. func TestServer_StaticSites(t *testing.T) {
  112. s := newTestServer(t, newTestConfig(t))
  113. rr := request(t, s, "GET", "/", "", nil)
  114. require.Equal(t, 200, rr.Code)
  115. require.Contains(t, rr.Body.String(), "</html>")
  116. rr = request(t, s, "HEAD", "/", "", nil)
  117. require.Equal(t, 200, rr.Code)
  118. rr = request(t, s, "OPTIONS", "/", "", nil)
  119. require.Equal(t, 200, rr.Code)
  120. rr = request(t, s, "GET", "/does-not-exist.txt", "", nil)
  121. require.Equal(t, 404, rr.Code)
  122. rr = request(t, s, "GET", "/mytopic", "", nil)
  123. require.Equal(t, 200, rr.Code)
  124. require.Contains(t, rr.Body.String(), `<meta name="robots" content="noindex, nofollow"/>`)
  125. rr = request(t, s, "GET", "/static/css/home.css", "", nil)
  126. require.Equal(t, 200, rr.Code)
  127. require.Contains(t, rr.Body.String(), `html, body {`)
  128. rr = request(t, s, "GET", "/docs", "", nil)
  129. require.Equal(t, 301, rr.Code)
  130. // Docs test removed, it was failing annoyingly.
  131. rr = request(t, s, "GET", "/example.html", "", nil)
  132. require.Equal(t, 200, rr.Code)
  133. require.Contains(t, rr.Body.String(), "</html>")
  134. }
  135. func TestServer_PublishLargeMessage(t *testing.T) {
  136. c := newTestConfig(t)
  137. c.AttachmentCacheDir = "" // Disable attachments
  138. s := newTestServer(t, c)
  139. body := strings.Repeat("this is a large message", 5000)
  140. response := request(t, s, "PUT", "/mytopic", body, nil)
  141. require.Equal(t, 400, response.Code)
  142. }
  143. func TestServer_PublishPriority(t *testing.T) {
  144. s := newTestServer(t, newTestConfig(t))
  145. for prio := 1; prio <= 5; prio++ {
  146. response := request(t, s, "GET", fmt.Sprintf("/mytopic/publish?priority=%d", prio), fmt.Sprintf("priority %d", prio), nil)
  147. msg := toMessage(t, response.Body.String())
  148. require.Equal(t, prio, msg.Priority)
  149. }
  150. response := request(t, s, "GET", "/mytopic/publish?priority=min", "test", nil)
  151. require.Equal(t, 1, toMessage(t, response.Body.String()).Priority)
  152. response = request(t, s, "GET", "/mytopic/send?priority=low", "test", nil)
  153. require.Equal(t, 2, toMessage(t, response.Body.String()).Priority)
  154. response = request(t, s, "GET", "/mytopic/send?priority=default", "test", nil)
  155. require.Equal(t, 3, toMessage(t, response.Body.String()).Priority)
  156. response = request(t, s, "GET", "/mytopic/send?priority=high", "test", nil)
  157. require.Equal(t, 4, toMessage(t, response.Body.String()).Priority)
  158. response = request(t, s, "GET", "/mytopic/send?priority=max", "test", nil)
  159. require.Equal(t, 5, toMessage(t, response.Body.String()).Priority)
  160. response = request(t, s, "GET", "/mytopic/trigger?priority=urgent", "test", nil)
  161. require.Equal(t, 5, toMessage(t, response.Body.String()).Priority)
  162. response = request(t, s, "GET", "/mytopic/trigger?priority=INVALID", "test", nil)
  163. require.Equal(t, 40007, toHTTPError(t, response.Body.String()).Code)
  164. }
  165. func TestServer_PublishGETOnlyOneTopic(t *testing.T) {
  166. // This tests a bug that allowed publishing topics with a comma in the name (no ticket)
  167. s := newTestServer(t, newTestConfig(t))
  168. response := request(t, s, "GET", "/mytopic,mytopic2/publish?m=hi", "", nil)
  169. require.Equal(t, 404, response.Code)
  170. }
  171. func TestServer_PublishNoCache(t *testing.T) {
  172. s := newTestServer(t, newTestConfig(t))
  173. response := request(t, s, "PUT", "/mytopic", "this message is not cached", map[string]string{
  174. "Cache": "no",
  175. })
  176. msg := toMessage(t, response.Body.String())
  177. require.NotEmpty(t, msg.ID)
  178. require.Equal(t, "this message is not cached", msg.Message)
  179. response = request(t, s, "GET", "/mytopic/json?poll=1", "", nil)
  180. messages := toMessages(t, response.Body.String())
  181. require.Empty(t, messages)
  182. }
  183. func TestServer_PublishAt(t *testing.T) {
  184. c := newTestConfig(t)
  185. c.MinDelay = time.Second
  186. c.AtSenderInterval = 100 * time.Millisecond
  187. s := newTestServer(t, c)
  188. response := request(t, s, "PUT", "/mytopic", "a message", map[string]string{
  189. "In": "1s",
  190. })
  191. require.Equal(t, 200, response.Code)
  192. response = request(t, s, "GET", "/mytopic/json?poll=1", "", nil)
  193. messages := toMessages(t, response.Body.String())
  194. require.Equal(t, 0, len(messages))
  195. time.Sleep(time.Second)
  196. require.Nil(t, s.sendDelayedMessages())
  197. response = request(t, s, "GET", "/mytopic/json?poll=1", "", nil)
  198. messages = toMessages(t, response.Body.String())
  199. require.Equal(t, 1, len(messages))
  200. require.Equal(t, "a message", messages[0].Message)
  201. }
  202. func TestServer_PublishAtWithCacheError(t *testing.T) {
  203. s := newTestServer(t, newTestConfig(t))
  204. response := request(t, s, "PUT", "/mytopic", "a message", map[string]string{
  205. "Cache": "no",
  206. "In": "30 min",
  207. })
  208. require.Equal(t, 400, response.Code)
  209. require.Equal(t, errHTTPBadRequestDelayNoCache, toHTTPError(t, response.Body.String()))
  210. }
  211. func TestServer_PublishAtTooShortDelay(t *testing.T) {
  212. s := newTestServer(t, newTestConfig(t))
  213. response := request(t, s, "PUT", "/mytopic", "a message", map[string]string{
  214. "In": "1s",
  215. })
  216. require.Equal(t, 400, response.Code)
  217. }
  218. func TestServer_PublishAtTooLongDelay(t *testing.T) {
  219. s := newTestServer(t, newTestConfig(t))
  220. response := request(t, s, "PUT", "/mytopic", "a message", map[string]string{
  221. "In": "99999999h",
  222. })
  223. require.Equal(t, 400, response.Code)
  224. }
  225. func TestServer_PublishAtInvalidDelay(t *testing.T) {
  226. s := newTestServer(t, newTestConfig(t))
  227. response := request(t, s, "PUT", "/mytopic?delay=INVALID", "a message", nil)
  228. err := toHTTPError(t, response.Body.String())
  229. require.Equal(t, 400, response.Code)
  230. require.Equal(t, 40004, err.Code)
  231. }
  232. func TestServer_PublishAtTooLarge(t *testing.T) {
  233. s := newTestServer(t, newTestConfig(t))
  234. response := request(t, s, "PUT", "/mytopic?x-in=99999h", "a message", nil)
  235. err := toHTTPError(t, response.Body.String())
  236. require.Equal(t, 400, response.Code)
  237. require.Equal(t, 40006, err.Code)
  238. }
  239. func TestServer_PublishAtAndPrune(t *testing.T) {
  240. s := newTestServer(t, newTestConfig(t))
  241. response := request(t, s, "PUT", "/mytopic", "a message", map[string]string{
  242. "In": "1h",
  243. })
  244. require.Equal(t, 200, response.Code)
  245. s.updateStatsAndPrune() // Fire pruning
  246. response = request(t, s, "GET", "/mytopic/json?poll=1&scheduled=1", "", nil)
  247. messages := toMessages(t, response.Body.String())
  248. require.Equal(t, 1, len(messages)) // Not affected by pruning
  249. require.Equal(t, "a message", messages[0].Message)
  250. }
  251. func TestServer_PublishAndMultiPoll(t *testing.T) {
  252. s := newTestServer(t, newTestConfig(t))
  253. response := request(t, s, "PUT", "/mytopic1", "message 1", nil)
  254. msg := toMessage(t, response.Body.String())
  255. require.NotEmpty(t, msg.ID)
  256. require.Equal(t, "mytopic1", msg.Topic)
  257. require.Equal(t, "message 1", msg.Message)
  258. response = request(t, s, "PUT", "/mytopic2", "message 2", nil)
  259. msg = toMessage(t, response.Body.String())
  260. require.NotEmpty(t, msg.ID)
  261. require.Equal(t, "mytopic2", msg.Topic)
  262. require.Equal(t, "message 2", msg.Message)
  263. response = request(t, s, "GET", "/mytopic1/json?poll=1", "", nil)
  264. messages := toMessages(t, response.Body.String())
  265. require.Equal(t, 1, len(messages))
  266. require.Equal(t, "mytopic1", messages[0].Topic)
  267. require.Equal(t, "message 1", messages[0].Message)
  268. response = request(t, s, "GET", "/mytopic1,mytopic2/json?poll=1", "", nil)
  269. messages = toMessages(t, response.Body.String())
  270. require.Equal(t, 2, len(messages))
  271. require.Equal(t, "mytopic1", messages[0].Topic)
  272. require.Equal(t, "message 1", messages[0].Message)
  273. require.Equal(t, "mytopic2", messages[1].Topic)
  274. require.Equal(t, "message 2", messages[1].Message)
  275. }
  276. func TestServer_PublishWithNopCache(t *testing.T) {
  277. c := newTestConfig(t)
  278. c.CacheDuration = 0
  279. s := newTestServer(t, c)
  280. subscribeRR := httptest.NewRecorder()
  281. subscribeCancel := subscribe(t, s, "/mytopic/json", subscribeRR)
  282. publishRR := request(t, s, "PUT", "/mytopic", "my first message", nil)
  283. require.Equal(t, 200, publishRR.Code)
  284. subscribeCancel()
  285. messages := toMessages(t, subscribeRR.Body.String())
  286. require.Equal(t, 2, len(messages))
  287. require.Equal(t, openEvent, messages[0].Event)
  288. require.Equal(t, messageEvent, messages[1].Event)
  289. require.Equal(t, "my first message", messages[1].Message)
  290. response := request(t, s, "GET", "/mytopic/json?poll=1", "", nil)
  291. messages = toMessages(t, response.Body.String())
  292. require.Empty(t, messages)
  293. }
  294. func TestServer_PublishAndPollSince(t *testing.T) {
  295. s := newTestServer(t, newTestConfig(t))
  296. request(t, s, "PUT", "/mytopic", "test 1", nil)
  297. time.Sleep(1100 * time.Millisecond)
  298. since := time.Now().Unix()
  299. request(t, s, "PUT", "/mytopic", "test 2", nil)
  300. response := request(t, s, "GET", fmt.Sprintf("/mytopic/json?poll=1&since=%d", since), "", nil)
  301. messages := toMessages(t, response.Body.String())
  302. require.Equal(t, 1, len(messages))
  303. require.Equal(t, "test 2", messages[0].Message)
  304. response = request(t, s, "GET", "/mytopic/json?poll=1&since=10s", "", nil)
  305. messages = toMessages(t, response.Body.String())
  306. require.Equal(t, 2, len(messages))
  307. require.Equal(t, "test 1", messages[0].Message)
  308. response = request(t, s, "GET", "/mytopic/json?poll=1&since=100ms", "", nil)
  309. messages = toMessages(t, response.Body.String())
  310. require.Equal(t, 1, len(messages))
  311. require.Equal(t, "test 2", messages[0].Message)
  312. response = request(t, s, "GET", "/mytopic/json?poll=1&since=INVALID", "", nil)
  313. require.Equal(t, 40008, toHTTPError(t, response.Body.String()).Code)
  314. }
  315. func TestServer_PublishViaGET(t *testing.T) {
  316. s := newTestServer(t, newTestConfig(t))
  317. response := request(t, s, "GET", "/mytopic/trigger", "", nil)
  318. msg := toMessage(t, response.Body.String())
  319. require.NotEmpty(t, msg.ID)
  320. require.Equal(t, "triggered", msg.Message)
  321. response = request(t, s, "GET", "/mytopic/send?message=This+is+a+test&t=This+is+a+title&tags=skull&x-priority=5&delay=24h", "", nil)
  322. msg = toMessage(t, response.Body.String())
  323. require.NotEmpty(t, msg.ID)
  324. require.Equal(t, "This is a test", msg.Message)
  325. require.Equal(t, "This is a title", msg.Title)
  326. require.Equal(t, []string{"skull"}, msg.Tags)
  327. require.Equal(t, 5, msg.Priority)
  328. require.Greater(t, msg.Time, time.Now().Add(23*time.Hour).Unix())
  329. }
  330. func TestServer_PublishMessageInHeaderWithNewlines(t *testing.T) {
  331. s := newTestServer(t, newTestConfig(t))
  332. response := request(t, s, "PUT", "/mytopic", "", map[string]string{
  333. "Message": "Line 1\\nLine 2",
  334. })
  335. msg := toMessage(t, response.Body.String())
  336. require.NotEmpty(t, msg.ID)
  337. require.Equal(t, "Line 1\nLine 2", msg.Message) // \\n -> \n !
  338. }
  339. func TestServer_PublishFirebase(t *testing.T) {
  340. // This is unfortunately not much of a test, since it merely fires the messages towards Firebase,
  341. // but cannot re-read them. There is no way from Go to read the messages back, or even get an error back.
  342. // I tried everything. I already had written the test, and it increases the code coverage, so I'll leave it ... :shrug: ...
  343. c := newTestConfig(t)
  344. c.FirebaseKeyFile = firebaseServiceAccountFile(t) // May skip the test!
  345. s := newTestServer(t, c)
  346. // Normal message
  347. response := request(t, s, "PUT", "/mytopic", "This is a message for firebase", nil)
  348. msg := toMessage(t, response.Body.String())
  349. require.NotEmpty(t, msg.ID)
  350. // Keepalive message
  351. require.Nil(t, s.firebase(newKeepaliveMessage(firebaseControlTopic)))
  352. time.Sleep(500 * time.Millisecond) // Time for sends
  353. }
  354. func TestServer_PublishInvalidTopic(t *testing.T) {
  355. s := newTestServer(t, newTestConfig(t))
  356. s.mailer = &testMailer{}
  357. response := request(t, s, "PUT", "/docs", "fail", nil)
  358. require.Equal(t, 40010, toHTTPError(t, response.Body.String()).Code)
  359. }
  360. func TestServer_PollWithQueryFilters(t *testing.T) {
  361. s := newTestServer(t, newTestConfig(t))
  362. response := request(t, s, "PUT", "/mytopic?priority=1&tags=tag1,tag2", "my first message", nil)
  363. msg := toMessage(t, response.Body.String())
  364. require.NotEmpty(t, msg.ID)
  365. response = request(t, s, "PUT", "/mytopic?title=a+title", "my second message", map[string]string{
  366. "Tags": "tag2,tag3",
  367. })
  368. msg = toMessage(t, response.Body.String())
  369. require.NotEmpty(t, msg.ID)
  370. queriesThatShouldReturnMessageOne := []string{
  371. "/mytopic/json?poll=1&priority=1",
  372. "/mytopic/json?poll=1&priority=min",
  373. "/mytopic/json?poll=1&priority=min,low",
  374. "/mytopic/json?poll=1&priority=1,2",
  375. "/mytopic/json?poll=1&p=2,min",
  376. "/mytopic/json?poll=1&tags=tag1",
  377. "/mytopic/json?poll=1&tags=tag1,tag2",
  378. "/mytopic/json?poll=1&message=my+first+message",
  379. }
  380. for _, query := range queriesThatShouldReturnMessageOne {
  381. response = request(t, s, "GET", query, "", nil)
  382. messages := toMessages(t, response.Body.String())
  383. require.Equal(t, 1, len(messages), "Query failed: "+query)
  384. require.Equal(t, "my first message", messages[0].Message, "Query failed: "+query)
  385. }
  386. queriesThatShouldReturnMessageTwo := []string{
  387. "/mytopic/json?poll=1&x-priority=3", // !
  388. "/mytopic/json?poll=1&priority=3",
  389. "/mytopic/json?poll=1&priority=default",
  390. "/mytopic/json?poll=1&p=3",
  391. "/mytopic/json?poll=1&x-tags=tag2,tag3",
  392. "/mytopic/json?poll=1&tags=tag2,tag3",
  393. "/mytopic/json?poll=1&tag=tag2,tag3",
  394. "/mytopic/json?poll=1&ta=tag2,tag3",
  395. "/mytopic/json?poll=1&x-title=a+title",
  396. "/mytopic/json?poll=1&title=a+title",
  397. "/mytopic/json?poll=1&t=a+title",
  398. "/mytopic/json?poll=1&x-message=my+second+message",
  399. "/mytopic/json?poll=1&message=my+second+message",
  400. "/mytopic/json?poll=1&m=my+second+message",
  401. "/mytopic/json?x-poll=1&m=my+second+message",
  402. "/mytopic/json?po=1&m=my+second+message",
  403. }
  404. for _, query := range queriesThatShouldReturnMessageTwo {
  405. response = request(t, s, "GET", query, "", nil)
  406. messages := toMessages(t, response.Body.String())
  407. require.Equal(t, 1, len(messages), "Query failed: "+query)
  408. require.Equal(t, "my second message", messages[0].Message, "Query failed: "+query)
  409. }
  410. queriesThatShouldReturnNoMessages := []string{
  411. "/mytopic/json?poll=1&priority=4",
  412. "/mytopic/json?poll=1&tags=tag1,tag2,tag3",
  413. "/mytopic/json?poll=1&title=another+title",
  414. "/mytopic/json?poll=1&message=my+third+message",
  415. "/mytopic/json?poll=1&message=my+third+message",
  416. }
  417. for _, query := range queriesThatShouldReturnNoMessages {
  418. response = request(t, s, "GET", query, "", nil)
  419. messages := toMessages(t, response.Body.String())
  420. require.Equal(t, 0, len(messages), "Query failed: "+query)
  421. }
  422. }
  423. func TestServer_SubscribeWithQueryFilters(t *testing.T) {
  424. c := newTestConfig(t)
  425. c.KeepaliveInterval = 800 * time.Millisecond
  426. s := newTestServer(t, c)
  427. subscribeResponse := httptest.NewRecorder()
  428. subscribeCancel := subscribe(t, s, "/mytopic/json?tags=zfs-issue", subscribeResponse)
  429. response := request(t, s, "PUT", "/mytopic", "my first message", nil)
  430. require.Equal(t, 200, response.Code)
  431. response = request(t, s, "PUT", "/mytopic", "ZFS scrub failed", map[string]string{
  432. "Tags": "zfs-issue,zfs-scrub",
  433. })
  434. require.Equal(t, 200, response.Code)
  435. time.Sleep(850 * time.Millisecond)
  436. subscribeCancel()
  437. messages := toMessages(t, subscribeResponse.Body.String())
  438. require.Equal(t, 3, len(messages))
  439. require.Equal(t, openEvent, messages[0].Event)
  440. require.Equal(t, messageEvent, messages[1].Event)
  441. require.Equal(t, "ZFS scrub failed", messages[1].Message)
  442. require.Equal(t, keepaliveEvent, messages[2].Event)
  443. }
  444. func TestServer_Auth_Success_Admin(t *testing.T) {
  445. c := newTestConfig(t)
  446. c.AuthFile = filepath.Join(t.TempDir(), "user.db")
  447. s := newTestServer(t, c)
  448. manager := s.auth.(auth.Manager)
  449. require.Nil(t, manager.AddUser("phil", "phil", auth.RoleAdmin))
  450. response := request(t, s, "GET", "/mytopic/auth", "", map[string]string{
  451. "Authorization": basicAuth("phil:phil"),
  452. })
  453. require.Equal(t, 200, response.Code)
  454. require.Equal(t, `{"success":true}`+"\n", response.Body.String())
  455. }
  456. func TestServer_Auth_Success_User(t *testing.T) {
  457. c := newTestConfig(t)
  458. c.AuthFile = filepath.Join(t.TempDir(), "user.db")
  459. c.AuthDefaultRead = false
  460. c.AuthDefaultWrite = false
  461. s := newTestServer(t, c)
  462. manager := s.auth.(auth.Manager)
  463. require.Nil(t, manager.AddUser("ben", "ben", auth.RoleUser))
  464. require.Nil(t, manager.AllowAccess("ben", "mytopic", true, true))
  465. response := request(t, s, "GET", "/mytopic/auth", "", map[string]string{
  466. "Authorization": basicAuth("ben:ben"),
  467. })
  468. require.Equal(t, 200, response.Code)
  469. }
  470. func TestServer_Auth_Success_User_MultipleTopics(t *testing.T) {
  471. c := newTestConfig(t)
  472. c.AuthFile = filepath.Join(t.TempDir(), "user.db")
  473. c.AuthDefaultRead = false
  474. c.AuthDefaultWrite = false
  475. s := newTestServer(t, c)
  476. manager := s.auth.(auth.Manager)
  477. require.Nil(t, manager.AddUser("ben", "ben", auth.RoleUser))
  478. require.Nil(t, manager.AllowAccess("ben", "mytopic", true, true))
  479. require.Nil(t, manager.AllowAccess("ben", "anothertopic", true, true))
  480. response := request(t, s, "GET", "/mytopic,anothertopic/auth", "", map[string]string{
  481. "Authorization": basicAuth("ben:ben"),
  482. })
  483. require.Equal(t, 200, response.Code)
  484. response = request(t, s, "GET", "/mytopic,anothertopic,NOT-THIS-ONE/auth", "", map[string]string{
  485. "Authorization": basicAuth("ben:ben"),
  486. })
  487. require.Equal(t, 403, response.Code)
  488. }
  489. func TestServer_Auth_Fail_InvalidPass(t *testing.T) {
  490. c := newTestConfig(t)
  491. c.AuthFile = filepath.Join(t.TempDir(), "user.db")
  492. c.AuthDefaultRead = false
  493. c.AuthDefaultWrite = false
  494. s := newTestServer(t, c)
  495. manager := s.auth.(auth.Manager)
  496. require.Nil(t, manager.AddUser("phil", "phil", auth.RoleAdmin))
  497. response := request(t, s, "GET", "/mytopic/auth", "", map[string]string{
  498. "Authorization": basicAuth("phil:INVALID"),
  499. })
  500. require.Equal(t, 401, response.Code)
  501. }
  502. func TestServer_Auth_Fail_Unauthorized(t *testing.T) {
  503. c := newTestConfig(t)
  504. c.AuthFile = filepath.Join(t.TempDir(), "user.db")
  505. c.AuthDefaultRead = false
  506. c.AuthDefaultWrite = false
  507. s := newTestServer(t, c)
  508. manager := s.auth.(auth.Manager)
  509. require.Nil(t, manager.AddUser("ben", "ben", auth.RoleUser))
  510. require.Nil(t, manager.AllowAccess("ben", "sometopic", true, true)) // Not mytopic!
  511. response := request(t, s, "GET", "/mytopic/auth", "", map[string]string{
  512. "Authorization": basicAuth("ben:ben"),
  513. })
  514. require.Equal(t, 403, response.Code)
  515. }
  516. func TestServer_Auth_Fail_CannotPublish(t *testing.T) {
  517. c := newTestConfig(t)
  518. c.AuthFile = filepath.Join(t.TempDir(), "user.db")
  519. c.AuthDefaultRead = true // Open by default
  520. c.AuthDefaultWrite = true // Open by default
  521. s := newTestServer(t, c)
  522. manager := s.auth.(auth.Manager)
  523. require.Nil(t, manager.AddUser("phil", "phil", auth.RoleAdmin))
  524. require.Nil(t, manager.AllowAccess(auth.Everyone, "private", false, false))
  525. require.Nil(t, manager.AllowAccess(auth.Everyone, "announcements", true, false))
  526. response := request(t, s, "PUT", "/mytopic", "test", nil)
  527. require.Equal(t, 200, response.Code)
  528. response = request(t, s, "GET", "/mytopic/json?poll=1", "", nil)
  529. require.Equal(t, 200, response.Code)
  530. response = request(t, s, "PUT", "/announcements", "test", nil)
  531. require.Equal(t, 403, response.Code) // Cannot write as anonymous
  532. response = request(t, s, "PUT", "/announcements", "test", map[string]string{
  533. "Authorization": basicAuth("phil:phil"),
  534. })
  535. require.Equal(t, 200, response.Code)
  536. response = request(t, s, "GET", "/announcements/json?poll=1", "", nil)
  537. require.Equal(t, 200, response.Code) // Anonymous read allowed
  538. response = request(t, s, "GET", "/private/json?poll=1", "", nil)
  539. require.Equal(t, 403, response.Code) // Anonymous read not allowed
  540. }
  541. func TestServer_Auth_ViaQuery(t *testing.T) {
  542. c := newTestConfig(t)
  543. c.AuthFile = filepath.Join(t.TempDir(), "user.db")
  544. c.AuthDefaultRead = false
  545. c.AuthDefaultWrite = false
  546. s := newTestServer(t, c)
  547. manager := s.auth.(auth.Manager)
  548. require.Nil(t, manager.AddUser("ben", "some pass", auth.RoleAdmin))
  549. u := fmt.Sprintf("/mytopic/json?poll=1&auth=%s", base64.RawURLEncoding.EncodeToString([]byte(basicAuth("ben:some pass"))))
  550. response := request(t, s, "GET", u, "", nil)
  551. require.Equal(t, 200, response.Code)
  552. u = fmt.Sprintf("/mytopic/json?poll=1&auth=%s", base64.RawURLEncoding.EncodeToString([]byte(basicAuth("ben:WRONNNGGGG"))))
  553. response = request(t, s, "GET", u, "", nil)
  554. require.Equal(t, 401, response.Code)
  555. }
  556. /*
  557. func TestServer_Curl_Publish_Poll(t *testing.T) {
  558. s, port := test.StartServer(t)
  559. defer test.StopServer(t, s, port)
  560. cmd := exec.Command("sh", "-c", fmt.Sprintf(`curl -sd "This is a test" localhost:%d/mytopic`, port))
  561. require.Nil(t, cmd.Run())
  562. b, err := cmd.CombinedOutput()
  563. require.Nil(t, err)
  564. msg := toMessage(t, string(b))
  565. require.Equal(t, "This is a test", msg.Message)
  566. cmd = exec.Command("sh", "-c", fmt.Sprintf(`curl "localhost:%d/mytopic?poll=1"`, port))
  567. require.Nil(t, cmd.Run())
  568. b, err = cmd.CombinedOutput()
  569. require.Nil(t, err)
  570. msg = toMessage(t, string(b))
  571. require.Equal(t, "This is a test", msg.Message)
  572. }
  573. */
  574. type testMailer struct {
  575. count int
  576. mu sync.Mutex
  577. }
  578. func (t *testMailer) Send(from, to string, m *message) error {
  579. t.mu.Lock()
  580. defer t.mu.Unlock()
  581. t.count++
  582. return nil
  583. }
  584. func (t *testMailer) Count() int {
  585. t.mu.Lock()
  586. defer t.mu.Unlock()
  587. return t.count
  588. }
  589. func TestServer_PublishTooRequests_Defaults(t *testing.T) {
  590. s := newTestServer(t, newTestConfig(t))
  591. for i := 0; i < 60; i++ {
  592. response := request(t, s, "PUT", "/mytopic", fmt.Sprintf("message %d", i), nil)
  593. require.Equal(t, 200, response.Code)
  594. }
  595. response := request(t, s, "PUT", "/mytopic", "message", nil)
  596. require.Equal(t, 429, response.Code)
  597. }
  598. func TestServer_PublishTooRequests_Defaults_ExemptHosts(t *testing.T) {
  599. c := newTestConfig(t)
  600. c.VisitorRequestExemptIPAddrs = []string{"9.9.9.9"} // see request()
  601. s := newTestServer(t, c)
  602. for i := 0; i < 65; i++ { // > 60
  603. response := request(t, s, "PUT", "/mytopic", fmt.Sprintf("message %d", i), nil)
  604. require.Equal(t, 200, response.Code)
  605. }
  606. }
  607. func TestServer_PublishTooRequests_ShortReplenish(t *testing.T) {
  608. c := newTestConfig(t)
  609. c.VisitorRequestLimitBurst = 60
  610. c.VisitorRequestLimitReplenish = 500 * time.Millisecond
  611. s := newTestServer(t, c)
  612. for i := 0; i < 60; i++ {
  613. response := request(t, s, "PUT", "/mytopic", fmt.Sprintf("message %d", i), nil)
  614. require.Equal(t, 200, response.Code)
  615. }
  616. response := request(t, s, "PUT", "/mytopic", "message", nil)
  617. require.Equal(t, 429, response.Code)
  618. time.Sleep(510 * time.Millisecond)
  619. response = request(t, s, "PUT", "/mytopic", "message", nil)
  620. require.Equal(t, 200, response.Code)
  621. }
  622. func TestServer_PublishTooManyEmails_Defaults(t *testing.T) {
  623. s := newTestServer(t, newTestConfig(t))
  624. s.mailer = &testMailer{}
  625. for i := 0; i < 16; i++ {
  626. response := request(t, s, "PUT", "/mytopic", fmt.Sprintf("message %d", i), map[string]string{
  627. "E-Mail": "test@example.com",
  628. })
  629. require.Equal(t, 200, response.Code)
  630. }
  631. response := request(t, s, "PUT", "/mytopic", "one too many", map[string]string{
  632. "E-Mail": "test@example.com",
  633. })
  634. require.Equal(t, 429, response.Code)
  635. }
  636. func TestServer_PublishTooManyEmails_Replenish(t *testing.T) {
  637. c := newTestConfig(t)
  638. c.VisitorEmailLimitReplenish = 500 * time.Millisecond
  639. s := newTestServer(t, c)
  640. s.mailer = &testMailer{}
  641. for i := 0; i < 16; i++ {
  642. response := request(t, s, "PUT", "/mytopic", fmt.Sprintf("message %d", i), map[string]string{
  643. "E-Mail": "test@example.com",
  644. })
  645. require.Equal(t, 200, response.Code)
  646. }
  647. response := request(t, s, "PUT", "/mytopic", "one too many", map[string]string{
  648. "E-Mail": "test@example.com",
  649. })
  650. require.Equal(t, 429, response.Code)
  651. time.Sleep(510 * time.Millisecond)
  652. response = request(t, s, "PUT", "/mytopic", "this should be okay again too many", map[string]string{
  653. "E-Mail": "test@example.com",
  654. })
  655. require.Equal(t, 200, response.Code)
  656. response = request(t, s, "PUT", "/mytopic", "and bad again", map[string]string{
  657. "E-Mail": "test@example.com",
  658. })
  659. require.Equal(t, 429, response.Code)
  660. }
  661. func TestServer_PublishDelayedEmail_Fail(t *testing.T) {
  662. s := newTestServer(t, newTestConfig(t))
  663. s.mailer = &testMailer{}
  664. response := request(t, s, "PUT", "/mytopic", "fail", map[string]string{
  665. "E-Mail": "test@example.com",
  666. "Delay": "20 min",
  667. })
  668. require.Equal(t, 400, response.Code)
  669. }
  670. func TestServer_PublishEmailNoMailer_Fail(t *testing.T) {
  671. s := newTestServer(t, newTestConfig(t))
  672. response := request(t, s, "PUT", "/mytopic", "fail", map[string]string{
  673. "E-Mail": "test@example.com",
  674. })
  675. require.Equal(t, 400, response.Code)
  676. }
  677. func TestServer_UnifiedPushDiscovery(t *testing.T) {
  678. s := newTestServer(t, newTestConfig(t))
  679. response := request(t, s, "GET", "/mytopic?up=1", "", nil)
  680. require.Equal(t, 200, response.Code)
  681. require.Equal(t, `{"unifiedpush":{"version":1}}`+"\n", response.Body.String())
  682. }
  683. func TestServer_PublishUnifiedPushBinary_AndPoll(t *testing.T) {
  684. b := make([]byte, 12) // Max length
  685. _, err := rand.Read(b)
  686. require.Nil(t, err)
  687. s := newTestServer(t, newTestConfig(t))
  688. response := request(t, s, "PUT", "/mytopic?up=1", string(b), nil)
  689. require.Equal(t, 200, response.Code)
  690. m := toMessage(t, response.Body.String())
  691. require.Equal(t, "base64", m.Encoding)
  692. b2, err := base64.StdEncoding.DecodeString(m.Message)
  693. require.Nil(t, err)
  694. require.Equal(t, b, b2)
  695. response = request(t, s, "GET", "/mytopic/json?poll=1", string(b), nil)
  696. require.Equal(t, 200, response.Code)
  697. m = toMessage(t, response.Body.String())
  698. require.Equal(t, "base64", m.Encoding)
  699. b2, err = base64.StdEncoding.DecodeString(m.Message)
  700. require.Nil(t, err)
  701. require.Equal(t, b, b2)
  702. }
  703. func TestServer_PublishUnifiedPushBinary_Truncated(t *testing.T) {
  704. b := make([]byte, 5000) // Longer than max length
  705. _, err := rand.Read(b)
  706. require.Nil(t, err)
  707. s := newTestServer(t, newTestConfig(t))
  708. response := request(t, s, "PUT", "/mytopic?up=1", string(b), nil)
  709. require.Equal(t, 200, response.Code)
  710. m := toMessage(t, response.Body.String())
  711. require.Equal(t, "base64", m.Encoding)
  712. b2, err := base64.StdEncoding.DecodeString(m.Message)
  713. require.Nil(t, err)
  714. require.Equal(t, 4096, len(b2))
  715. require.Equal(t, b[:4096], b2)
  716. }
  717. func TestServer_PublishUnifiedPushText(t *testing.T) {
  718. s := newTestServer(t, newTestConfig(t))
  719. response := request(t, s, "PUT", "/mytopic?up=1", "this is a unifiedpush text message", nil)
  720. require.Equal(t, 200, response.Code)
  721. m := toMessage(t, response.Body.String())
  722. require.Equal(t, "", m.Encoding)
  723. require.Equal(t, "this is a unifiedpush text message", m.Message)
  724. }
  725. func TestServer_PublishActions_AndPoll(t *testing.T) {
  726. s := newTestServer(t, newTestConfig(t))
  727. response := request(t, s, "PUT", "/mytopic", "my message", map[string]string{
  728. "Actions": "view, Open portal, https://home.nest.com/; http, Turn down, https://api.nest.com/device/XZ1D2, body=target_temp_f=65",
  729. })
  730. require.Equal(t, 200, response.Code)
  731. response = request(t, s, "GET", "/mytopic/json?poll=1", "", nil)
  732. require.Equal(t, 200, response.Code)
  733. m := toMessage(t, response.Body.String())
  734. require.Equal(t, 2, len(m.Actions))
  735. require.Equal(t, "view", m.Actions[0].Action)
  736. require.Equal(t, "Open portal", m.Actions[0].Label)
  737. require.Equal(t, "https://home.nest.com/", m.Actions[0].URL)
  738. require.Equal(t, "http", m.Actions[1].Action)
  739. require.Equal(t, "Turn down", m.Actions[1].Label)
  740. require.Equal(t, "https://api.nest.com/device/XZ1D2", m.Actions[1].URL)
  741. require.Equal(t, "target_temp_f=65", m.Actions[1].Body)
  742. }
  743. func TestServer_PublishAsJSON(t *testing.T) {
  744. s := newTestServer(t, newTestConfig(t))
  745. body := `{"topic":"mytopic","message":"A message","title":"a title\nwith lines","tags":["tag1","tag 2"],` +
  746. `"not-a-thing":"ok", "attach":"http://google.com","filename":"google.pdf", "click":"http://ntfy.sh","priority":4,` +
  747. `"delay":"30min"}`
  748. response := request(t, s, "PUT", "/", body, nil)
  749. require.Equal(t, 200, response.Code)
  750. m := toMessage(t, response.Body.String())
  751. require.Equal(t, "mytopic", m.Topic)
  752. require.Equal(t, "A message", m.Message)
  753. require.Equal(t, "a title\nwith lines", m.Title)
  754. require.Equal(t, []string{"tag1", "tag 2"}, m.Tags)
  755. require.Equal(t, "http://google.com", m.Attachment.URL)
  756. require.Equal(t, "google.pdf", m.Attachment.Name)
  757. require.Equal(t, "http://ntfy.sh", m.Click)
  758. require.Equal(t, 4, m.Priority)
  759. require.True(t, m.Time > time.Now().Unix()+29*60)
  760. require.True(t, m.Time < time.Now().Unix()+31*60)
  761. }
  762. func TestServer_PublishAsJSON_WithEmail(t *testing.T) {
  763. mailer := &testMailer{}
  764. s := newTestServer(t, newTestConfig(t))
  765. s.mailer = mailer
  766. body := `{"topic":"mytopic","message":"A message","email":"phil@example.com"}`
  767. response := request(t, s, "PUT", "/", body, nil)
  768. require.Equal(t, 200, response.Code)
  769. m := toMessage(t, response.Body.String())
  770. require.Equal(t, "mytopic", m.Topic)
  771. require.Equal(t, "A message", m.Message)
  772. require.Equal(t, 1, mailer.Count())
  773. }
  774. func TestServer_PublishAsJSON_WithActions(t *testing.T) {
  775. s := newTestServer(t, newTestConfig(t))
  776. body := `{
  777. "topic":"mytopic",
  778. "message":"A message",
  779. "actions": [
  780. {
  781. "action": "view",
  782. "label": "Open portal",
  783. "url": "https://home.nest.com/"
  784. },
  785. {
  786. "action": "http",
  787. "label": "Turn down",
  788. "url": "https://api.nest.com/device/XZ1D2",
  789. "body": "target_temp_f=65"
  790. }
  791. ]
  792. }`
  793. response := request(t, s, "POST", "/", body, nil)
  794. require.Equal(t, 200, response.Code)
  795. m := toMessage(t, response.Body.String())
  796. require.Equal(t, "mytopic", m.Topic)
  797. require.Equal(t, "A message", m.Message)
  798. require.Equal(t, 2, len(m.Actions))
  799. require.Equal(t, "view", m.Actions[0].Action)
  800. require.Equal(t, "Open portal", m.Actions[0].Label)
  801. require.Equal(t, "https://home.nest.com/", m.Actions[0].URL)
  802. require.Equal(t, "http", m.Actions[1].Action)
  803. require.Equal(t, "Turn down", m.Actions[1].Label)
  804. require.Equal(t, "https://api.nest.com/device/XZ1D2", m.Actions[1].URL)
  805. require.Equal(t, "target_temp_f=65", m.Actions[1].Body)
  806. }
  807. func TestServer_PublishAsJSON_Invalid(t *testing.T) {
  808. s := newTestServer(t, newTestConfig(t))
  809. body := `{"topic":"mytopic",INVALID`
  810. response := request(t, s, "PUT", "/", body, nil)
  811. require.Equal(t, 400, response.Code)
  812. }
  813. func TestServer_PublishAttachment(t *testing.T) {
  814. content := util.RandomString(5000) // > 4096
  815. s := newTestServer(t, newTestConfig(t))
  816. response := request(t, s, "PUT", "/mytopic", content, nil)
  817. msg := toMessage(t, response.Body.String())
  818. require.Equal(t, "attachment.txt", msg.Attachment.Name)
  819. require.Equal(t, "text/plain; charset=utf-8", msg.Attachment.Type)
  820. require.Equal(t, int64(5000), msg.Attachment.Size)
  821. require.GreaterOrEqual(t, msg.Attachment.Expires, time.Now().Add(179*time.Minute).Unix()) // Almost 3 hours
  822. require.Contains(t, msg.Attachment.URL, "http://127.0.0.1:12345/file/")
  823. require.Equal(t, "", msg.Attachment.Owner) // Should never be returned
  824. require.FileExists(t, filepath.Join(s.config.AttachmentCacheDir, msg.ID))
  825. path := strings.TrimPrefix(msg.Attachment.URL, "http://127.0.0.1:12345")
  826. response = request(t, s, "GET", path, "", nil)
  827. require.Equal(t, 200, response.Code)
  828. require.Equal(t, "5000", response.Header().Get("Content-Length"))
  829. require.Equal(t, content, response.Body.String())
  830. // Slightly unrelated cross-test: make sure we add an owner for internal attachments
  831. size, err := s.messageCache.AttachmentBytesUsed("9.9.9.9") // See request()
  832. require.Nil(t, err)
  833. require.Equal(t, int64(5000), size)
  834. }
  835. func TestServer_PublishAttachmentShortWithFilename(t *testing.T) {
  836. c := newTestConfig(t)
  837. c.BehindProxy = true
  838. s := newTestServer(t, c)
  839. content := "this is an ATTACHMENT"
  840. response := request(t, s, "PUT", "/mytopic?f=myfile.txt", content, map[string]string{
  841. "X-Forwarded-For": "1.2.3.4",
  842. })
  843. msg := toMessage(t, response.Body.String())
  844. require.Equal(t, "myfile.txt", msg.Attachment.Name)
  845. require.Equal(t, "text/plain; charset=utf-8", msg.Attachment.Type)
  846. require.Equal(t, int64(21), msg.Attachment.Size)
  847. require.GreaterOrEqual(t, msg.Attachment.Expires, time.Now().Add(3*time.Hour).Unix())
  848. require.Contains(t, msg.Attachment.URL, "http://127.0.0.1:12345/file/")
  849. require.Equal(t, "", msg.Attachment.Owner) // Should never be returned
  850. require.FileExists(t, filepath.Join(s.config.AttachmentCacheDir, msg.ID))
  851. path := strings.TrimPrefix(msg.Attachment.URL, "http://127.0.0.1:12345")
  852. response = request(t, s, "GET", path, "", nil)
  853. require.Equal(t, 200, response.Code)
  854. require.Equal(t, "21", response.Header().Get("Content-Length"))
  855. require.Equal(t, content, response.Body.String())
  856. // Slightly unrelated cross-test: make sure we add an owner for internal attachments
  857. size, err := s.messageCache.AttachmentBytesUsed("1.2.3.4")
  858. require.Nil(t, err)
  859. require.Equal(t, int64(21), size)
  860. }
  861. func TestServer_PublishAttachmentExternalWithoutFilename(t *testing.T) {
  862. s := newTestServer(t, newTestConfig(t))
  863. response := request(t, s, "PUT", "/mytopic", "", map[string]string{
  864. "Attach": "https://upload.wikimedia.org/wikipedia/commons/f/fd/Pink_flower.jpg",
  865. })
  866. msg := toMessage(t, response.Body.String())
  867. require.Equal(t, "You received a file: Pink_flower.jpg", msg.Message)
  868. require.Equal(t, "Pink_flower.jpg", msg.Attachment.Name)
  869. require.Equal(t, "https://upload.wikimedia.org/wikipedia/commons/f/fd/Pink_flower.jpg", msg.Attachment.URL)
  870. require.Equal(t, "", msg.Attachment.Type)
  871. require.Equal(t, int64(0), msg.Attachment.Size)
  872. require.Equal(t, int64(0), msg.Attachment.Expires)
  873. require.Equal(t, "", msg.Attachment.Owner)
  874. // Slightly unrelated cross-test: make sure we don't add an owner for external attachments
  875. size, err := s.messageCache.AttachmentBytesUsed("127.0.0.1")
  876. require.Nil(t, err)
  877. require.Equal(t, int64(0), size)
  878. }
  879. func TestServer_PublishAttachmentExternalWithFilename(t *testing.T) {
  880. s := newTestServer(t, newTestConfig(t))
  881. response := request(t, s, "PUT", "/mytopic", "This is a custom message", map[string]string{
  882. "X-Attach": "https://upload.wikimedia.org/wikipedia/commons/f/fd/Pink_flower.jpg",
  883. "File": "some file.jpg",
  884. })
  885. msg := toMessage(t, response.Body.String())
  886. require.Equal(t, "This is a custom message", msg.Message)
  887. require.Equal(t, "some file.jpg", msg.Attachment.Name)
  888. require.Equal(t, "https://upload.wikimedia.org/wikipedia/commons/f/fd/Pink_flower.jpg", msg.Attachment.URL)
  889. require.Equal(t, "", msg.Attachment.Type)
  890. require.Equal(t, int64(0), msg.Attachment.Size)
  891. require.Equal(t, int64(0), msg.Attachment.Expires)
  892. require.Equal(t, "", msg.Attachment.Owner)
  893. }
  894. func TestServer_PublishAttachmentBadURL(t *testing.T) {
  895. s := newTestServer(t, newTestConfig(t))
  896. response := request(t, s, "PUT", "/mytopic?a=not+a+URL", "", nil)
  897. err := toHTTPError(t, response.Body.String())
  898. require.Equal(t, 400, response.Code)
  899. require.Equal(t, 400, err.HTTPCode)
  900. require.Equal(t, 40013, err.Code)
  901. }
  902. func TestServer_PublishAttachmentTooLargeContentLength(t *testing.T) {
  903. content := util.RandomString(5000) // > 4096
  904. s := newTestServer(t, newTestConfig(t))
  905. response := request(t, s, "PUT", "/mytopic", content, map[string]string{
  906. "Content-Length": "20000000",
  907. })
  908. err := toHTTPError(t, response.Body.String())
  909. require.Equal(t, 413, response.Code)
  910. require.Equal(t, 413, err.HTTPCode)
  911. require.Equal(t, 41301, err.Code)
  912. }
  913. func TestServer_PublishAttachmentTooLargeBodyAttachmentFileSizeLimit(t *testing.T) {
  914. content := util.RandomString(5001) // > 5000, see below
  915. c := newTestConfig(t)
  916. c.AttachmentFileSizeLimit = 5000
  917. s := newTestServer(t, c)
  918. response := request(t, s, "PUT", "/mytopic", content, nil)
  919. err := toHTTPError(t, response.Body.String())
  920. require.Equal(t, 413, response.Code)
  921. require.Equal(t, 413, err.HTTPCode)
  922. require.Equal(t, 41301, err.Code)
  923. }
  924. func TestServer_PublishAttachmentExpiryBeforeDelivery(t *testing.T) {
  925. c := newTestConfig(t)
  926. c.AttachmentExpiryDuration = 10 * time.Minute
  927. s := newTestServer(t, c)
  928. response := request(t, s, "PUT", "/mytopic", util.RandomString(5000), map[string]string{
  929. "Delay": "11 min", // > AttachmentExpiryDuration
  930. })
  931. err := toHTTPError(t, response.Body.String())
  932. require.Equal(t, 400, response.Code)
  933. require.Equal(t, 400, err.HTTPCode)
  934. require.Equal(t, 40015, err.Code)
  935. }
  936. func TestServer_PublishAttachmentTooLargeBodyVisitorAttachmentTotalSizeLimit(t *testing.T) {
  937. c := newTestConfig(t)
  938. c.VisitorAttachmentTotalSizeLimit = 10000
  939. s := newTestServer(t, c)
  940. response := request(t, s, "PUT", "/mytopic", util.RandomString(5000), nil)
  941. msg := toMessage(t, response.Body.String())
  942. require.Equal(t, 200, response.Code)
  943. require.Equal(t, "You received a file: attachment.txt", msg.Message)
  944. require.Equal(t, int64(5000), msg.Attachment.Size)
  945. content := util.RandomString(5001) // 5000+5001 > , see below
  946. response = request(t, s, "PUT", "/mytopic", content, nil)
  947. err := toHTTPError(t, response.Body.String())
  948. require.Equal(t, 413, response.Code)
  949. require.Equal(t, 413, err.HTTPCode)
  950. require.Equal(t, 41301, err.Code)
  951. }
  952. func TestServer_PublishAttachmentAndPrune(t *testing.T) {
  953. content := util.RandomString(5000) // > 4096
  954. c := newTestConfig(t)
  955. c.AttachmentExpiryDuration = time.Millisecond // Hack
  956. s := newTestServer(t, c)
  957. // Publish and make sure we can retrieve it
  958. response := request(t, s, "PUT", "/mytopic", content, nil)
  959. msg := toMessage(t, response.Body.String())
  960. require.Contains(t, msg.Attachment.URL, "http://127.0.0.1:12345/file/")
  961. file := filepath.Join(s.config.AttachmentCacheDir, msg.ID)
  962. require.FileExists(t, file)
  963. path := strings.TrimPrefix(msg.Attachment.URL, "http://127.0.0.1:12345")
  964. response = request(t, s, "GET", path, "", nil)
  965. require.Equal(t, 200, response.Code)
  966. require.Equal(t, content, response.Body.String())
  967. // Prune and makes sure it's gone
  968. time.Sleep(time.Second) // Sigh ...
  969. s.updateStatsAndPrune()
  970. require.NoFileExists(t, file)
  971. response = request(t, s, "GET", path, "", nil)
  972. require.Equal(t, 404, response.Code)
  973. }
  974. func TestServer_PublishAttachmentBandwidthLimit(t *testing.T) {
  975. content := util.RandomString(5000) // > 4096
  976. c := newTestConfig(t)
  977. c.VisitorAttachmentDailyBandwidthLimit = 5*5000 + 123 // A little more than 1 upload and 3 downloads
  978. s := newTestServer(t, c)
  979. // Publish attachment
  980. response := request(t, s, "PUT", "/mytopic", content, nil)
  981. msg := toMessage(t, response.Body.String())
  982. require.Contains(t, msg.Attachment.URL, "http://127.0.0.1:12345/file/")
  983. // Get it 4 times successfully
  984. path := strings.TrimPrefix(msg.Attachment.URL, "http://127.0.0.1:12345")
  985. for i := 1; i <= 4; i++ { // 4 successful downloads
  986. response = request(t, s, "GET", path, "", nil)
  987. require.Equal(t, 200, response.Code)
  988. require.Equal(t, content, response.Body.String())
  989. }
  990. // And then fail with a 429
  991. response = request(t, s, "GET", path, "", nil)
  992. err := toHTTPError(t, response.Body.String())
  993. require.Equal(t, 429, response.Code)
  994. require.Equal(t, 42905, err.Code)
  995. }
  996. func TestServer_PublishAttachmentBandwidthLimitUploadOnly(t *testing.T) {
  997. content := util.RandomString(5000) // > 4096
  998. c := newTestConfig(t)
  999. c.VisitorAttachmentDailyBandwidthLimit = 5*5000 + 500 // 5 successful uploads
  1000. s := newTestServer(t, c)
  1001. // 5 successful uploads
  1002. for i := 1; i <= 5; i++ {
  1003. response := request(t, s, "PUT", "/mytopic", content, nil)
  1004. msg := toMessage(t, response.Body.String())
  1005. require.Contains(t, msg.Attachment.URL, "http://127.0.0.1:12345/file/")
  1006. }
  1007. // And a failed one
  1008. response := request(t, s, "PUT", "/mytopic", content, nil)
  1009. err := toHTTPError(t, response.Body.String())
  1010. require.Equal(t, 413, response.Code)
  1011. require.Equal(t, 41301, err.Code)
  1012. }
  1013. func TestServer_PublishAttachmentUserStats(t *testing.T) {
  1014. content := util.RandomString(4999) // > 4096
  1015. c := newTestConfig(t)
  1016. c.AttachmentFileSizeLimit = 5000
  1017. c.VisitorAttachmentTotalSizeLimit = 6000
  1018. s := newTestServer(t, c)
  1019. // Upload one attachment
  1020. response := request(t, s, "PUT", "/mytopic", content, nil)
  1021. msg := toMessage(t, response.Body.String())
  1022. require.Contains(t, msg.Attachment.URL, "http://127.0.0.1:12345/file/")
  1023. // User stats
  1024. response = request(t, s, "GET", "/user/stats", "", nil)
  1025. require.Equal(t, 200, response.Code)
  1026. var stats visitorStats
  1027. require.Nil(t, json.NewDecoder(strings.NewReader(response.Body.String())).Decode(&stats))
  1028. require.Equal(t, int64(5000), stats.AttachmentFileSizeLimit)
  1029. require.Equal(t, int64(6000), stats.VisitorAttachmentBytesTotal)
  1030. require.Equal(t, int64(4999), stats.VisitorAttachmentBytesUsed)
  1031. require.Equal(t, int64(1001), stats.VisitorAttachmentBytesRemaining)
  1032. }
  1033. func newTestConfig(t *testing.T) *Config {
  1034. conf := NewConfig()
  1035. conf.BaseURL = "http://127.0.0.1:12345"
  1036. conf.CacheFile = filepath.Join(t.TempDir(), "cache.db")
  1037. conf.AttachmentCacheDir = t.TempDir()
  1038. return conf
  1039. }
  1040. func newTestServer(t *testing.T, config *Config) *Server {
  1041. server, err := New(config)
  1042. if err != nil {
  1043. t.Fatal(err)
  1044. }
  1045. return server
  1046. }
  1047. func request(t *testing.T, s *Server, method, url, body string, headers map[string]string) *httptest.ResponseRecorder {
  1048. rr := httptest.NewRecorder()
  1049. req, err := http.NewRequest(method, url, strings.NewReader(body))
  1050. if err != nil {
  1051. t.Fatal(err)
  1052. }
  1053. req.RemoteAddr = "9.9.9.9" // Used for tests
  1054. for k, v := range headers {
  1055. req.Header.Set(k, v)
  1056. }
  1057. s.handle(rr, req)
  1058. return rr
  1059. }
  1060. func subscribe(t *testing.T, s *Server, url string, rr *httptest.ResponseRecorder) context.CancelFunc {
  1061. ctx, cancel := context.WithCancel(context.Background())
  1062. req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
  1063. if err != nil {
  1064. t.Fatal(err)
  1065. }
  1066. done := make(chan bool)
  1067. go func() {
  1068. s.handle(rr, req)
  1069. done <- true
  1070. }()
  1071. cancelAndWaitForDone := func() {
  1072. time.Sleep(100 * time.Millisecond)
  1073. cancel()
  1074. <-done
  1075. }
  1076. time.Sleep(100 * time.Millisecond)
  1077. return cancelAndWaitForDone
  1078. }
  1079. func toMessages(t *testing.T, s string) []*message {
  1080. messages := make([]*message, 0)
  1081. scanner := bufio.NewScanner(strings.NewReader(s))
  1082. for scanner.Scan() {
  1083. messages = append(messages, toMessage(t, scanner.Text()))
  1084. }
  1085. return messages
  1086. }
  1087. func toMessage(t *testing.T, s string) *message {
  1088. var m message
  1089. require.Nil(t, json.NewDecoder(strings.NewReader(s)).Decode(&m))
  1090. return &m
  1091. }
  1092. func toHTTPError(t *testing.T, s string) *errHTTP {
  1093. var e errHTTP
  1094. require.Nil(t, json.NewDecoder(strings.NewReader(s)).Decode(&e))
  1095. return &e
  1096. }
  1097. func firebaseServiceAccountFile(t *testing.T) string {
  1098. if os.Getenv("NTFY_TEST_FIREBASE_SERVICE_ACCOUNT_FILE") != "" {
  1099. return os.Getenv("NTFY_TEST_FIREBASE_SERVICE_ACCOUNT_FILE")
  1100. } else if os.Getenv("NTFY_TEST_FIREBASE_SERVICE_ACCOUNT") != "" {
  1101. filename := filepath.Join(t.TempDir(), "firebase.json")
  1102. require.NotNil(t, os.WriteFile(filename, []byte(os.Getenv("NTFY_TEST_FIREBASE_SERVICE_ACCOUNT")), 0600))
  1103. return filename
  1104. }
  1105. t.SkipNow()
  1106. return ""
  1107. }
  1108. func basicAuth(s string) string {
  1109. return fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(s)))
  1110. }