server_test.go 157 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145
  1. package server
  2. import (
  3. "bufio"
  4. "context"
  5. "crypto/rand"
  6. "encoding/base64"
  7. "encoding/json"
  8. "fmt"
  9. "golang.org/x/crypto/bcrypt"
  10. "heckel.io/ntfy/v2/user"
  11. "io"
  12. "net/http"
  13. "net/http/httptest"
  14. "net/netip"
  15. "os"
  16. "path/filepath"
  17. "runtime/debug"
  18. "strings"
  19. "sync"
  20. "sync/atomic"
  21. "testing"
  22. "time"
  23. "github.com/SherClockHolmes/webpush-go"
  24. "github.com/stretchr/testify/require"
  25. "heckel.io/ntfy/v2/log"
  26. "heckel.io/ntfy/v2/util"
  27. )
  28. func TestMain(m *testing.M) {
  29. log.SetLevel(log.ErrorLevel)
  30. os.Exit(m.Run())
  31. }
  32. func TestServer_PublishAndPoll(t *testing.T) {
  33. s := newTestServer(t, newTestConfig(t))
  34. response1 := request(t, s, "PUT", "/mytopic", "my first message", nil)
  35. msg1 := toMessage(t, response1.Body.String())
  36. require.NotEmpty(t, msg1.ID)
  37. require.Equal(t, "my first message", msg1.Message)
  38. response2 := request(t, s, "PUT", "/mytopic", "my second\n\nmessage", nil)
  39. msg2 := toMessage(t, response2.Body.String())
  40. require.NotEqual(t, msg1.ID, msg2.ID)
  41. require.NotEmpty(t, msg2.ID)
  42. require.Equal(t, "my second\n\nmessage", msg2.Message)
  43. response := request(t, s, "GET", "/mytopic/json?poll=1", "", nil)
  44. messages := toMessages(t, response.Body.String())
  45. require.Equal(t, 2, len(messages))
  46. require.Equal(t, "my first message", messages[0].Message)
  47. require.Equal(t, "my second\n\nmessage", messages[1].Message)
  48. response = request(t, s, "GET", "/mytopic/sse?poll=1&since=all", "", nil)
  49. lines := strings.Split(strings.TrimSpace(response.Body.String()), "\n")
  50. require.Equal(t, 3, len(lines))
  51. require.Equal(t, "my first message", toMessage(t, strings.TrimPrefix(lines[0], "data: ")).Message)
  52. require.Equal(t, "", lines[1])
  53. require.Equal(t, "my second\n\nmessage", toMessage(t, strings.TrimPrefix(lines[2], "data: ")).Message)
  54. response = request(t, s, "GET", "/mytopic/raw?poll=1", "", nil)
  55. lines = strings.Split(strings.TrimSpace(response.Body.String()), "\n")
  56. require.Equal(t, 2, len(lines))
  57. require.Equal(t, "my first message", lines[0])
  58. require.Equal(t, "my second message", lines[1]) // \n -> " "
  59. }
  60. func TestServer_PublishWithFirebase(t *testing.T) {
  61. sender := newTestFirebaseSender(10)
  62. s := newTestServer(t, newTestConfig(t))
  63. s.firebaseClient = newFirebaseClient(sender, &testAuther{Allow: true})
  64. response := request(t, s, "PUT", "/mytopic", "my first message", nil)
  65. msg1 := toMessage(t, response.Body.String())
  66. require.NotEmpty(t, msg1.ID)
  67. require.Equal(t, "my first message", msg1.Message)
  68. time.Sleep(100 * time.Millisecond) // Firebase publishing happens
  69. require.Equal(t, 1, len(sender.Messages()))
  70. require.Equal(t, "my first message", sender.Messages()[0].Data["message"])
  71. require.Equal(t, "my first message", sender.Messages()[0].APNS.Payload.Aps.Alert.Body)
  72. require.Equal(t, "my first message", sender.Messages()[0].APNS.Payload.CustomData["message"])
  73. }
  74. func TestServer_PublishWithoutFirebase(t *testing.T) {
  75. sender := newTestFirebaseSender(10)
  76. s := newTestServer(t, newTestConfig(t))
  77. s.firebaseClient = newFirebaseClient(sender, &testAuther{Allow: true})
  78. response := request(t, s, "PUT", "/mytopic", "my first message", map[string]string{
  79. "firebase": "no",
  80. })
  81. msg1 := toMessage(t, response.Body.String())
  82. require.NotEmpty(t, msg1.ID)
  83. require.Equal(t, "my first message", msg1.Message)
  84. time.Sleep(100 * time.Millisecond) // Firebase publishing happens
  85. require.Equal(t, 0, len(sender.Messages()))
  86. }
  87. func TestServer_PublishWithFirebase_WithoutUsers_AndWithoutPanic(t *testing.T) {
  88. // This tests issue #641, which used to panic before the fix
  89. firebaseKeyFile := filepath.Join(t.TempDir(), "firebase.json")
  90. contents := `{
  91. "type": "service_account",
  92. "project_id": "ntfy-test",
  93. "private_key_id": "fsfhskjdfhskdhfskdjfhsdf",
  94. "private_key": "lalala",
  95. "client_email": "firebase-adminsdk-muv04@ntfy-test.iam.gserviceaccount.com",
  96. "client_id": "123123213",
  97. "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  98. "token_uri": "https://oauth2.googleapis.com/token",
  99. "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  100. "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-muv04%40ntfy-test.iam.gserviceaccount.com"
  101. }
  102. `
  103. require.Nil(t, os.WriteFile(firebaseKeyFile, []byte(contents), 0600))
  104. c := newTestConfig(t)
  105. c.FirebaseKeyFile = firebaseKeyFile
  106. s := newTestServer(t, c)
  107. response := request(t, s, "PUT", "/mytopic", "my first message", nil)
  108. require.Equal(t, "my first message", toMessage(t, response.Body.String()).Message)
  109. }
  110. func TestServer_SubscribeOpenAndKeepalive(t *testing.T) {
  111. t.Parallel()
  112. c := newTestConfig(t)
  113. c.KeepaliveInterval = time.Second
  114. s := newTestServer(t, c)
  115. rr := httptest.NewRecorder()
  116. ctx, cancel := context.WithCancel(context.Background())
  117. req, err := http.NewRequestWithContext(ctx, "GET", "/mytopic/json", nil)
  118. if err != nil {
  119. t.Fatal(err)
  120. }
  121. doneChan := make(chan bool)
  122. go func() {
  123. s.handle(rr, req)
  124. doneChan <- true
  125. }()
  126. time.Sleep(1300 * time.Millisecond)
  127. cancel()
  128. <-doneChan
  129. messages := toMessages(t, rr.Body.String())
  130. require.Equal(t, 2, len(messages))
  131. require.Equal(t, openEvent, messages[0].Event)
  132. require.Equal(t, "mytopic", messages[0].Topic)
  133. require.Equal(t, "", messages[0].Message)
  134. require.Equal(t, "", messages[0].Title)
  135. require.Equal(t, 0, messages[0].Priority)
  136. require.Nil(t, messages[0].Tags)
  137. require.Equal(t, keepaliveEvent, messages[1].Event)
  138. require.Equal(t, "mytopic", messages[1].Topic)
  139. require.Equal(t, "", messages[1].Message)
  140. require.Equal(t, "", messages[1].Title)
  141. require.Equal(t, 0, messages[1].Priority)
  142. require.Nil(t, messages[1].Tags)
  143. }
  144. func TestServer_PublishAndSubscribe(t *testing.T) {
  145. t.Parallel()
  146. s := newTestServer(t, newTestConfig(t))
  147. subscribeRR := httptest.NewRecorder()
  148. subscribeCancel := subscribe(t, s, "/mytopic/json", subscribeRR)
  149. publishFirstRR := request(t, s, "PUT", "/mytopic", "my first message", nil)
  150. require.Equal(t, 200, publishFirstRR.Code)
  151. time.Sleep(500 * time.Millisecond) // Publishing is done asynchronously, this avoids races
  152. publishSecondRR := request(t, s, "PUT", "/mytopic", "my other message", map[string]string{
  153. "Title": " This is a title ",
  154. "X-Tags": "tag1,tag 2, tag3",
  155. "p": "1",
  156. })
  157. require.Equal(t, 200, publishSecondRR.Code)
  158. subscribeCancel()
  159. messages := toMessages(t, subscribeRR.Body.String())
  160. require.Equal(t, 3, len(messages))
  161. require.Equal(t, openEvent, messages[0].Event)
  162. require.Equal(t, messageEvent, messages[1].Event)
  163. require.Equal(t, "mytopic", messages[1].Topic)
  164. require.Equal(t, "my first message", messages[1].Message)
  165. require.Equal(t, "", messages[1].Title)
  166. require.Equal(t, 0, messages[1].Priority)
  167. require.Nil(t, messages[1].Tags)
  168. require.True(t, time.Now().Add(12*time.Hour-5*time.Second).Unix() < messages[1].Expires)
  169. require.True(t, time.Now().Add(12*time.Hour+5*time.Second).Unix() > messages[1].Expires)
  170. require.Equal(t, messageEvent, messages[2].Event)
  171. require.Equal(t, "mytopic", messages[2].Topic)
  172. require.Equal(t, "my other message", messages[2].Message)
  173. require.Equal(t, "This is a title", messages[2].Title)
  174. require.Equal(t, 1, messages[2].Priority)
  175. require.Equal(t, []string{"tag1", "tag 2", "tag3"}, messages[2].Tags)
  176. }
  177. func TestServer_Publish_Disallowed_Topic(t *testing.T) {
  178. c := newTestConfig(t)
  179. c.DisallowedTopics = []string{"about", "time", "this", "got", "added"}
  180. s := newTestServer(t, c)
  181. rr := request(t, s, "PUT", "/mytopic", "my first message", nil)
  182. require.Equal(t, 200, rr.Code)
  183. rr = request(t, s, "PUT", "/about", "another message", nil)
  184. require.Equal(t, 400, rr.Code)
  185. require.Equal(t, 40010, toHTTPError(t, rr.Body.String()).Code)
  186. }
  187. func TestServer_StaticSites(t *testing.T) {
  188. s := newTestServer(t, newTestConfig(t))
  189. rr := request(t, s, "GET", "/", "", nil)
  190. require.Equal(t, 200, rr.Code)
  191. require.Contains(t, rr.Body.String(), "</html>")
  192. rr = request(t, s, "HEAD", "/", "", nil)
  193. require.Equal(t, 200, rr.Code)
  194. rr = request(t, s, "OPTIONS", "/", "", nil)
  195. require.Equal(t, 200, rr.Code)
  196. rr = request(t, s, "GET", "/does-not-exist.txt", "", nil)
  197. require.Equal(t, 404, rr.Code)
  198. rr = request(t, s, "GET", "/mytopic", "", nil)
  199. require.Equal(t, 200, rr.Code)
  200. require.Contains(t, rr.Body.String(), `<meta name="robots" content="noindex, nofollow" />`)
  201. rr = request(t, s, "GET", "/docs", "", nil)
  202. require.Equal(t, 301, rr.Code)
  203. // Docs test removed, it was failing annoyingly.
  204. }
  205. func TestServer_WebEnabled(t *testing.T) {
  206. conf := newTestConfig(t)
  207. conf.WebRoot = "" // Disable web app
  208. s := newTestServer(t, conf)
  209. rr := request(t, s, "GET", "/", "", nil)
  210. require.Equal(t, 404, rr.Code)
  211. rr = request(t, s, "GET", "/config.js", "", nil)
  212. require.Equal(t, 404, rr.Code)
  213. rr = request(t, s, "GET", "/sw.js", "", nil)
  214. require.Equal(t, 404, rr.Code)
  215. rr = request(t, s, "GET", "/app.html", "", nil)
  216. require.Equal(t, 404, rr.Code)
  217. rr = request(t, s, "GET", "/static/css/home.css", "", nil)
  218. require.Equal(t, 404, rr.Code)
  219. conf2 := newTestConfig(t)
  220. conf2.WebRoot = "/"
  221. s2 := newTestServer(t, conf2)
  222. rr = request(t, s2, "GET", "/", "", nil)
  223. require.Equal(t, 200, rr.Code)
  224. rr = request(t, s2, "GET", "/config.js", "", nil)
  225. require.Equal(t, 200, rr.Code)
  226. rr = request(t, s2, "GET", "/sw.js", "", nil)
  227. require.Equal(t, 200, rr.Code)
  228. rr = request(t, s2, "GET", "/app.html", "", nil)
  229. require.Equal(t, 200, rr.Code)
  230. }
  231. func TestServer_WebPushEnabled(t *testing.T) {
  232. conf := newTestConfig(t)
  233. conf.WebRoot = "" // Disable web app
  234. s := newTestServer(t, conf)
  235. rr := request(t, s, "GET", "/manifest.webmanifest", "", nil)
  236. require.Equal(t, 404, rr.Code)
  237. conf2 := newTestConfig(t)
  238. s2 := newTestServer(t, conf2)
  239. rr = request(t, s2, "GET", "/manifest.webmanifest", "", nil)
  240. require.Equal(t, 404, rr.Code)
  241. conf3 := newTestConfigWithWebPush(t)
  242. s3 := newTestServer(t, conf3)
  243. rr = request(t, s3, "GET", "/manifest.webmanifest", "", nil)
  244. require.Equal(t, 200, rr.Code)
  245. require.Equal(t, "application/manifest+json", rr.Header().Get("Content-Type"))
  246. }
  247. func TestServer_PublishLargeMessage(t *testing.T) {
  248. c := newTestConfig(t)
  249. c.AttachmentCacheDir = "" // Disable attachments
  250. s := newTestServer(t, c)
  251. body := strings.Repeat("this is a large message", 5000)
  252. response := request(t, s, "PUT", "/mytopic", body, nil)
  253. require.Equal(t, 400, response.Code)
  254. }
  255. func TestServer_PublishPriority(t *testing.T) {
  256. s := newTestServer(t, newTestConfig(t))
  257. for prio := 1; prio <= 5; prio++ {
  258. response := request(t, s, "GET", fmt.Sprintf("/mytopic/publish?priority=%d", prio), fmt.Sprintf("priority %d", prio), nil)
  259. msg := toMessage(t, response.Body.String())
  260. require.Equal(t, prio, msg.Priority)
  261. }
  262. response := request(t, s, "GET", "/mytopic/publish?priority=min", "test", nil)
  263. require.Equal(t, 1, toMessage(t, response.Body.String()).Priority)
  264. response = request(t, s, "GET", "/mytopic/send?priority=low", "test", nil)
  265. require.Equal(t, 2, toMessage(t, response.Body.String()).Priority)
  266. response = request(t, s, "GET", "/mytopic/send?priority=default", "test", nil)
  267. require.Equal(t, 3, toMessage(t, response.Body.String()).Priority)
  268. response = request(t, s, "GET", "/mytopic/send?priority=high", "test", nil)
  269. require.Equal(t, 4, toMessage(t, response.Body.String()).Priority)
  270. response = request(t, s, "GET", "/mytopic/send?priority=max", "test", nil)
  271. require.Equal(t, 5, toMessage(t, response.Body.String()).Priority)
  272. response = request(t, s, "GET", "/mytopic/trigger?priority=urgent", "test", nil)
  273. require.Equal(t, 5, toMessage(t, response.Body.String()).Priority)
  274. response = request(t, s, "GET", "/mytopic/trigger?priority=INVALID", "test", nil)
  275. require.Equal(t, 40007, toHTTPError(t, response.Body.String()).Code)
  276. }
  277. func TestServer_PublishPriority_SpecialHTTPHeader(t *testing.T) {
  278. s := newTestServer(t, newTestConfig(t))
  279. response := request(t, s, "POST", "/mytopic", "test", map[string]string{
  280. "Priority": "u=4",
  281. "X-Priority": "5",
  282. })
  283. require.Equal(t, 5, toMessage(t, response.Body.String()).Priority)
  284. response = request(t, s, "POST", "/mytopic?priority=4", "test", map[string]string{
  285. "Priority": "u=9",
  286. })
  287. require.Equal(t, 4, toMessage(t, response.Body.String()).Priority)
  288. response = request(t, s, "POST", "/mytopic", "test", map[string]string{
  289. "p": "2",
  290. "priority": "u=9, i",
  291. })
  292. require.Equal(t, 2, toMessage(t, response.Body.String()).Priority)
  293. }
  294. func TestServer_PublishGETOnlyOneTopic(t *testing.T) {
  295. // This tests a bug that allowed publishing topics with a comma in the name (no ticket)
  296. s := newTestServer(t, newTestConfig(t))
  297. response := request(t, s, "GET", "/mytopic,mytopic2/publish?m=hi", "", nil)
  298. require.Equal(t, 404, response.Code)
  299. }
  300. func TestServer_PublishNoCache(t *testing.T) {
  301. s := newTestServer(t, newTestConfig(t))
  302. response := request(t, s, "PUT", "/mytopic", "this message is not cached", map[string]string{
  303. "Cache": "no",
  304. })
  305. msg := toMessage(t, response.Body.String())
  306. require.NotEmpty(t, msg.ID)
  307. require.Equal(t, "this message is not cached", msg.Message)
  308. require.Equal(t, int64(0), msg.Expires)
  309. response = request(t, s, "GET", "/mytopic/json?poll=1", "", nil)
  310. messages := toMessages(t, response.Body.String())
  311. require.Empty(t, messages)
  312. }
  313. func TestServer_PublishAt(t *testing.T) {
  314. t.Parallel()
  315. s := newTestServer(t, newTestConfig(t))
  316. response := request(t, s, "PUT", "/mytopic", "a message", map[string]string{
  317. "In": "1h",
  318. })
  319. require.Equal(t, 200, response.Code)
  320. response = request(t, s, "GET", "/mytopic/json?poll=1", "", nil)
  321. messages := toMessages(t, response.Body.String())
  322. require.Equal(t, 0, len(messages))
  323. // Update message time to the past
  324. fakeTime := time.Now().Add(-10 * time.Second).Unix()
  325. _, err := s.messageCache.db.Exec(`UPDATE messages SET time=?`, fakeTime)
  326. require.Nil(t, err)
  327. // Trigger delayed message sending
  328. require.Nil(t, s.sendDelayedMessages())
  329. response = request(t, s, "GET", "/mytopic/json?poll=1", "", nil)
  330. messages = toMessages(t, response.Body.String())
  331. require.Equal(t, 1, len(messages))
  332. require.Equal(t, "a message", messages[0].Message)
  333. require.Equal(t, netip.Addr{}, messages[0].Sender) // Never return the sender!
  334. messages, err = s.messageCache.Messages("mytopic", sinceAllMessages, true)
  335. require.Nil(t, err)
  336. require.Equal(t, 1, len(messages))
  337. require.Equal(t, "a message", messages[0].Message)
  338. require.Equal(t, "9.9.9.9", messages[0].Sender.String()) // It's stored in the DB though!
  339. }
  340. func TestServer_PublishAt_FromUser(t *testing.T) {
  341. t.Parallel()
  342. s := newTestServer(t, newTestConfigWithAuthFile(t))
  343. require.Nil(t, s.userManager.AddUser("phil", "phil", user.RoleAdmin, false))
  344. response := request(t, s, "PUT", "/mytopic", "a message", map[string]string{
  345. "Authorization": util.BasicAuth("phil", "phil"),
  346. "In": "1h",
  347. })
  348. require.Equal(t, 200, response.Code)
  349. // Message doesn't show up immediately
  350. response = request(t, s, "GET", "/mytopic/json?poll=1", "", nil)
  351. messages := toMessages(t, response.Body.String())
  352. require.Equal(t, 0, len(messages))
  353. // Update message time to the past
  354. fakeTime := time.Now().Add(-10 * time.Second).Unix()
  355. _, err := s.messageCache.db.Exec(`UPDATE messages SET time=?`, fakeTime)
  356. require.Nil(t, err)
  357. // Trigger delayed message sending
  358. require.Nil(t, s.sendDelayedMessages())
  359. response = request(t, s, "GET", "/mytopic/json?poll=1", "", nil)
  360. messages = toMessages(t, response.Body.String())
  361. require.Equal(t, 1, len(messages))
  362. require.Equal(t, fakeTime, messages[0].Time)
  363. require.Equal(t, "a message", messages[0].Message)
  364. messages, err = s.messageCache.Messages("mytopic", sinceAllMessages, true)
  365. require.Nil(t, err)
  366. require.Equal(t, 1, len(messages))
  367. require.Equal(t, "a message", messages[0].Message)
  368. require.True(t, strings.HasPrefix(messages[0].User, "u_"))
  369. }
  370. func TestServer_PublishAt_Expires(t *testing.T) {
  371. s := newTestServer(t, newTestConfig(t))
  372. response := request(t, s, "PUT", "/mytopic", "a message", map[string]string{
  373. "In": "2 days",
  374. })
  375. require.Equal(t, 200, response.Code)
  376. m := toMessage(t, response.Body.String())
  377. require.True(t, m.Expires > time.Now().Add(12*time.Hour+48*time.Hour-time.Minute).Unix())
  378. require.True(t, m.Expires < time.Now().Add(12*time.Hour+48*time.Hour+time.Minute).Unix())
  379. }
  380. func TestServer_PublishAtWithCacheError(t *testing.T) {
  381. s := newTestServer(t, newTestConfig(t))
  382. response := request(t, s, "PUT", "/mytopic", "a message", map[string]string{
  383. "Cache": "no",
  384. "In": "30 min",
  385. })
  386. require.Equal(t, 400, response.Code)
  387. require.Equal(t, errHTTPBadRequestDelayNoCache, toHTTPError(t, response.Body.String()))
  388. }
  389. func TestServer_PublishAtTooShortDelay(t *testing.T) {
  390. s := newTestServer(t, newTestConfig(t))
  391. response := request(t, s, "PUT", "/mytopic", "a message", map[string]string{
  392. "In": "1s",
  393. })
  394. require.Equal(t, 400, response.Code)
  395. }
  396. func TestServer_PublishAtTooLongDelay(t *testing.T) {
  397. s := newTestServer(t, newTestConfig(t))
  398. response := request(t, s, "PUT", "/mytopic", "a message", map[string]string{
  399. "In": "99999999h",
  400. })
  401. require.Equal(t, 400, response.Code)
  402. }
  403. func TestServer_PublishAtInvalidDelay(t *testing.T) {
  404. s := newTestServer(t, newTestConfig(t))
  405. response := request(t, s, "PUT", "/mytopic?delay=INVALID", "a message", nil)
  406. err := toHTTPError(t, response.Body.String())
  407. require.Equal(t, 400, response.Code)
  408. require.Equal(t, 40004, err.Code)
  409. }
  410. func TestServer_PublishAtTooLarge(t *testing.T) {
  411. s := newTestServer(t, newTestConfig(t))
  412. response := request(t, s, "PUT", "/mytopic?x-in=99999h", "a message", nil)
  413. err := toHTTPError(t, response.Body.String())
  414. require.Equal(t, 400, response.Code)
  415. require.Equal(t, 40006, err.Code)
  416. }
  417. func TestServer_PublishAtAndPrune(t *testing.T) {
  418. s := newTestServer(t, newTestConfig(t))
  419. response := request(t, s, "PUT", "/mytopic", "a message", map[string]string{
  420. "In": "1h",
  421. })
  422. require.Equal(t, 200, response.Code)
  423. s.execManager() // Fire pruning
  424. response = request(t, s, "GET", "/mytopic/json?poll=1&scheduled=1", "", nil)
  425. messages := toMessages(t, response.Body.String())
  426. require.Equal(t, 1, len(messages)) // Not affected by pruning
  427. require.Equal(t, "a message", messages[0].Message)
  428. time.Sleep(time.Second) // FIXME CI failing not sure why
  429. }
  430. func TestServer_PublishAndMultiPoll(t *testing.T) {
  431. s := newTestServer(t, newTestConfig(t))
  432. response := request(t, s, "PUT", "/mytopic1", "message 1", nil)
  433. msg := toMessage(t, response.Body.String())
  434. require.NotEmpty(t, msg.ID)
  435. require.Equal(t, "mytopic1", msg.Topic)
  436. require.Equal(t, "message 1", msg.Message)
  437. response = request(t, s, "PUT", "/mytopic2", "message 2", nil)
  438. msg = toMessage(t, response.Body.String())
  439. require.NotEmpty(t, msg.ID)
  440. require.Equal(t, "mytopic2", msg.Topic)
  441. require.Equal(t, "message 2", msg.Message)
  442. response = request(t, s, "GET", "/mytopic1/json?poll=1", "", nil)
  443. messages := toMessages(t, response.Body.String())
  444. require.Equal(t, 1, len(messages))
  445. require.Equal(t, "mytopic1", messages[0].Topic)
  446. require.Equal(t, "message 1", messages[0].Message)
  447. response = request(t, s, "GET", "/mytopic1,mytopic2/json?poll=1", "", nil)
  448. messages = toMessages(t, response.Body.String())
  449. require.Equal(t, 2, len(messages))
  450. require.Equal(t, "mytopic1", messages[0].Topic)
  451. require.Equal(t, "message 1", messages[0].Message)
  452. require.Equal(t, "mytopic2", messages[1].Topic)
  453. require.Equal(t, "message 2", messages[1].Message)
  454. }
  455. func TestServer_PublishWithNopCache(t *testing.T) {
  456. c := newTestConfig(t)
  457. c.CacheDuration = 0
  458. s := newTestServer(t, c)
  459. subscribeRR := httptest.NewRecorder()
  460. subscribeCancel := subscribe(t, s, "/mytopic/json", subscribeRR)
  461. publishRR := request(t, s, "PUT", "/mytopic", "my first message", nil)
  462. require.Equal(t, 200, publishRR.Code)
  463. subscribeCancel()
  464. messages := toMessages(t, subscribeRR.Body.String())
  465. require.Equal(t, 2, len(messages))
  466. require.Equal(t, openEvent, messages[0].Event)
  467. require.Equal(t, messageEvent, messages[1].Event)
  468. require.Equal(t, "my first message", messages[1].Message)
  469. response := request(t, s, "GET", "/mytopic/json?poll=1", "", nil)
  470. messages = toMessages(t, response.Body.String())
  471. require.Empty(t, messages)
  472. }
  473. func TestServer_PublishAndPollSince(t *testing.T) {
  474. t.Parallel()
  475. s := newTestServer(t, newTestConfig(t))
  476. request(t, s, "PUT", "/mytopic", "test 1", nil)
  477. time.Sleep(1100 * time.Millisecond)
  478. since := time.Now().Unix()
  479. request(t, s, "PUT", "/mytopic", "test 2", nil)
  480. response := request(t, s, "GET", fmt.Sprintf("/mytopic/json?poll=1&since=%d", since), "", nil)
  481. messages := toMessages(t, response.Body.String())
  482. require.Equal(t, 1, len(messages))
  483. require.Equal(t, "test 2", messages[0].Message)
  484. response = request(t, s, "GET", "/mytopic/json?poll=1&since=10s", "", nil)
  485. messages = toMessages(t, response.Body.String())
  486. require.Equal(t, 2, len(messages))
  487. require.Equal(t, "test 1", messages[0].Message)
  488. response = request(t, s, "GET", "/mytopic/json?poll=1&since=100ms", "", nil)
  489. messages = toMessages(t, response.Body.String())
  490. require.Equal(t, 1, len(messages))
  491. require.Equal(t, "test 2", messages[0].Message)
  492. response = request(t, s, "GET", "/mytopic/json?poll=1&since=latest", "", nil)
  493. messages = toMessages(t, response.Body.String())
  494. require.Equal(t, 1, len(messages))
  495. require.Equal(t, "test 2", messages[0].Message)
  496. response = request(t, s, "GET", "/mytopic/json?poll=1&since=INVALID", "", nil)
  497. require.Equal(t, 40008, toHTTPError(t, response.Body.String()).Code)
  498. }
  499. func newMessageWithTimestamp(topic, message string, timestamp int64) *message {
  500. m := newDefaultMessage(topic, message)
  501. m.Time = timestamp
  502. return m
  503. }
  504. func TestServer_PollSinceID_MultipleTopics(t *testing.T) {
  505. s := newTestServer(t, newTestConfig(t))
  506. require.Nil(t, s.messageCache.AddMessage(newMessageWithTimestamp("mytopic1", "test 1", 1655740277)))
  507. markerMessage := newMessageWithTimestamp("mytopic2", "test 2", 1655740283)
  508. require.Nil(t, s.messageCache.AddMessage(markerMessage))
  509. require.Nil(t, s.messageCache.AddMessage(newMessageWithTimestamp("mytopic1", "test 3", 1655740289)))
  510. require.Nil(t, s.messageCache.AddMessage(newMessageWithTimestamp("mytopic2", "test 4", 1655740293)))
  511. require.Nil(t, s.messageCache.AddMessage(newMessageWithTimestamp("mytopic1", "test 5", 1655740297)))
  512. require.Nil(t, s.messageCache.AddMessage(newMessageWithTimestamp("mytopic2", "test 6", 1655740303)))
  513. response := request(t, s, "GET", fmt.Sprintf("/mytopic1,mytopic2/json?poll=1&since=%s", markerMessage.ID), "", nil)
  514. messages := toMessages(t, response.Body.String())
  515. require.Equal(t, 4, len(messages))
  516. require.Equal(t, "test 3", messages[0].Message)
  517. require.Equal(t, "mytopic1", messages[0].Topic)
  518. require.Equal(t, "test 4", messages[1].Message)
  519. require.Equal(t, "mytopic2", messages[1].Topic)
  520. require.Equal(t, "test 5", messages[2].Message)
  521. require.Equal(t, "mytopic1", messages[2].Topic)
  522. require.Equal(t, "test 6", messages[3].Message)
  523. require.Equal(t, "mytopic2", messages[3].Topic)
  524. }
  525. func TestServer_PollSinceID_MultipleTopics_IDDoesNotMatch(t *testing.T) {
  526. s := newTestServer(t, newTestConfig(t))
  527. require.Nil(t, s.messageCache.AddMessage(newMessageWithTimestamp("mytopic1", "test 3", 1655740289)))
  528. require.Nil(t, s.messageCache.AddMessage(newMessageWithTimestamp("mytopic2", "test 4", 1655740293)))
  529. require.Nil(t, s.messageCache.AddMessage(newMessageWithTimestamp("mytopic1", "test 5", 1655740297)))
  530. require.Nil(t, s.messageCache.AddMessage(newMessageWithTimestamp("mytopic2", "test 6", 1655740303)))
  531. response := request(t, s, "GET", "/mytopic1,mytopic2/json?poll=1&since=NoMatchForID", "", nil)
  532. messages := toMessages(t, response.Body.String())
  533. require.Equal(t, 4, len(messages))
  534. require.Equal(t, "test 3", messages[0].Message)
  535. require.Equal(t, "test 4", messages[1].Message)
  536. require.Equal(t, "test 5", messages[2].Message)
  537. require.Equal(t, "test 6", messages[3].Message)
  538. }
  539. func TestServer_PublishViaGET(t *testing.T) {
  540. s := newTestServer(t, newTestConfig(t))
  541. response := request(t, s, "GET", "/mytopic/trigger", "", nil)
  542. msg := toMessage(t, response.Body.String())
  543. require.NotEmpty(t, msg.ID)
  544. require.Equal(t, "triggered", msg.Message)
  545. 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)
  546. msg = toMessage(t, response.Body.String())
  547. require.NotEmpty(t, msg.ID)
  548. require.Equal(t, "This is a test", msg.Message)
  549. require.Equal(t, "This is a title", msg.Title)
  550. require.Equal(t, []string{"skull"}, msg.Tags)
  551. require.Equal(t, 5, msg.Priority)
  552. require.Greater(t, msg.Time, time.Now().Add(23*time.Hour).Unix())
  553. }
  554. func TestServer_PublishMessageInHeaderWithNewlines(t *testing.T) {
  555. s := newTestServer(t, newTestConfig(t))
  556. response := request(t, s, "PUT", "/mytopic", "", map[string]string{
  557. "Message": "Line 1\\nLine 2",
  558. })
  559. msg := toMessage(t, response.Body.String())
  560. require.NotEmpty(t, msg.ID)
  561. require.Equal(t, "Line 1\nLine 2", msg.Message) // \\n -> \n !
  562. }
  563. func TestServer_PublishInvalidTopic(t *testing.T) {
  564. s := newTestServer(t, newTestConfig(t))
  565. s.smtpSender = &testMailer{}
  566. response := request(t, s, "PUT", "/docs", "fail", nil)
  567. require.Equal(t, 40010, toHTTPError(t, response.Body.String()).Code)
  568. }
  569. func TestServer_PollWithQueryFilters(t *testing.T) {
  570. s := newTestServer(t, newTestConfig(t))
  571. response := request(t, s, "PUT", "/mytopic?priority=1&tags=tag1,tag2", "my first message", nil)
  572. msg := toMessage(t, response.Body.String())
  573. require.NotEmpty(t, msg.ID)
  574. response = request(t, s, "PUT", "/mytopic?title=a+title", "my second message", map[string]string{
  575. "Tags": "tag2,tag3",
  576. })
  577. msg = toMessage(t, response.Body.String())
  578. require.NotEmpty(t, msg.ID)
  579. queriesThatShouldReturnMessageOne := []string{
  580. "/mytopic/json?poll=1&priority=1",
  581. "/mytopic/json?poll=1&priority=min",
  582. "/mytopic/json?poll=1&priority=min,low",
  583. "/mytopic/json?poll=1&priority=1,2",
  584. "/mytopic/json?poll=1&p=2,min",
  585. "/mytopic/json?poll=1&tags=tag1",
  586. "/mytopic/json?poll=1&tags=tag1,tag2",
  587. "/mytopic/json?poll=1&message=my+first+message",
  588. }
  589. for _, query := range queriesThatShouldReturnMessageOne {
  590. response = request(t, s, "GET", query, "", nil)
  591. messages := toMessages(t, response.Body.String())
  592. require.Equal(t, 1, len(messages), "Query failed: "+query)
  593. require.Equal(t, "my first message", messages[0].Message, "Query failed: "+query)
  594. }
  595. queriesThatShouldReturnMessageTwo := []string{
  596. "/mytopic/json?poll=1&x-priority=3", // !
  597. "/mytopic/json?poll=1&priority=3",
  598. "/mytopic/json?poll=1&priority=default",
  599. "/mytopic/json?poll=1&p=3",
  600. "/mytopic/json?poll=1&x-tags=tag2,tag3",
  601. "/mytopic/json?poll=1&tags=tag2,tag3",
  602. "/mytopic/json?poll=1&tag=tag2,tag3",
  603. "/mytopic/json?poll=1&ta=tag2,tag3",
  604. "/mytopic/json?poll=1&x-title=a+title",
  605. "/mytopic/json?poll=1&title=a+title",
  606. "/mytopic/json?poll=1&t=a+title",
  607. "/mytopic/json?poll=1&x-message=my+second+message",
  608. "/mytopic/json?poll=1&message=my+second+message",
  609. "/mytopic/json?poll=1&m=my+second+message",
  610. "/mytopic/json?x-poll=1&m=my+second+message",
  611. "/mytopic/json?po=1&m=my+second+message",
  612. }
  613. for _, query := range queriesThatShouldReturnMessageTwo {
  614. response = request(t, s, "GET", query, "", nil)
  615. messages := toMessages(t, response.Body.String())
  616. require.Equal(t, 1, len(messages), "Query failed: "+query)
  617. require.Equal(t, "my second message", messages[0].Message, "Query failed: "+query)
  618. }
  619. queriesThatShouldReturnNoMessages := []string{
  620. "/mytopic/json?poll=1&priority=4",
  621. "/mytopic/json?poll=1&tags=tag1,tag2,tag3",
  622. "/mytopic/json?poll=1&title=another+title",
  623. "/mytopic/json?poll=1&message=my+third+message",
  624. "/mytopic/json?poll=1&message=my+third+message",
  625. }
  626. for _, query := range queriesThatShouldReturnNoMessages {
  627. response = request(t, s, "GET", query, "", nil)
  628. messages := toMessages(t, response.Body.String())
  629. require.Equal(t, 0, len(messages), "Query failed: "+query)
  630. }
  631. }
  632. func TestServer_SubscribeWithQueryFilters(t *testing.T) {
  633. t.Parallel()
  634. c := newTestConfig(t)
  635. c.KeepaliveInterval = 800 * time.Millisecond
  636. s := newTestServer(t, c)
  637. subscribeResponse := httptest.NewRecorder()
  638. subscribeCancel := subscribe(t, s, "/mytopic/json?tags=zfs-issue", subscribeResponse)
  639. response := request(t, s, "PUT", "/mytopic", "my first message", nil)
  640. require.Equal(t, 200, response.Code)
  641. response = request(t, s, "PUT", "/mytopic", "ZFS scrub failed", map[string]string{
  642. "Tags": "zfs-issue,zfs-scrub",
  643. })
  644. require.Equal(t, 200, response.Code)
  645. time.Sleep(850 * time.Millisecond)
  646. subscribeCancel()
  647. messages := toMessages(t, subscribeResponse.Body.String())
  648. require.Equal(t, 3, len(messages))
  649. require.Equal(t, openEvent, messages[0].Event)
  650. require.Equal(t, messageEvent, messages[1].Event)
  651. require.Equal(t, "ZFS scrub failed", messages[1].Message)
  652. require.Equal(t, keepaliveEvent, messages[2].Event)
  653. }
  654. func TestServer_Auth_Success_Admin(t *testing.T) {
  655. c := newTestConfigWithAuthFile(t)
  656. s := newTestServer(t, c)
  657. require.Nil(t, s.userManager.AddUser("phil", "phil", user.RoleAdmin, false))
  658. response := request(t, s, "GET", "/mytopic/auth", "", map[string]string{
  659. "Authorization": util.BasicAuth("phil", "phil"),
  660. })
  661. require.Equal(t, 200, response.Code)
  662. require.Equal(t, `{"success":true}`+"\n", response.Body.String())
  663. }
  664. func TestServer_Auth_Success_User(t *testing.T) {
  665. c := newTestConfigWithAuthFile(t)
  666. c.AuthDefault = user.PermissionDenyAll
  667. s := newTestServer(t, c)
  668. require.Nil(t, s.userManager.AddUser("ben", "ben", user.RoleUser, false))
  669. require.Nil(t, s.userManager.AllowAccess("ben", "mytopic", user.PermissionReadWrite))
  670. response := request(t, s, "GET", "/mytopic/auth", "", map[string]string{
  671. "Authorization": util.BasicAuth("ben", "ben"),
  672. })
  673. require.Equal(t, 200, response.Code)
  674. }
  675. func TestServer_Auth_Success_User_MultipleTopics(t *testing.T) {
  676. c := newTestConfigWithAuthFile(t)
  677. c.AuthDefault = user.PermissionDenyAll
  678. s := newTestServer(t, c)
  679. require.Nil(t, s.userManager.AddUser("ben", "ben", user.RoleUser, false))
  680. require.Nil(t, s.userManager.AllowAccess("ben", "mytopic", user.PermissionReadWrite))
  681. require.Nil(t, s.userManager.AllowAccess("ben", "anothertopic", user.PermissionReadWrite))
  682. response := request(t, s, "GET", "/mytopic,anothertopic/auth", "", map[string]string{
  683. "Authorization": util.BasicAuth("ben", "ben"),
  684. })
  685. require.Equal(t, 200, response.Code)
  686. response = request(t, s, "GET", "/mytopic,anothertopic,NOT-THIS-ONE/auth", "", map[string]string{
  687. "Authorization": util.BasicAuth("ben", "ben"),
  688. })
  689. require.Equal(t, 403, response.Code)
  690. }
  691. func TestServer_Auth_Fail_InvalidPass(t *testing.T) {
  692. c := newTestConfig(t)
  693. c.AuthFile = filepath.Join(t.TempDir(), "user.db")
  694. c.AuthDefault = user.PermissionDenyAll
  695. s := newTestServer(t, c)
  696. require.Nil(t, s.userManager.AddUser("phil", "phil", user.RoleAdmin, false))
  697. response := request(t, s, "GET", "/mytopic/auth", "", map[string]string{
  698. "Authorization": util.BasicAuth("phil", "INVALID"),
  699. })
  700. require.Equal(t, 401, response.Code)
  701. }
  702. func TestServer_Auth_Fail_Unauthorized(t *testing.T) {
  703. c := newTestConfigWithAuthFile(t)
  704. c.AuthDefault = user.PermissionDenyAll
  705. s := newTestServer(t, c)
  706. require.Nil(t, s.userManager.AddUser("ben", "ben", user.RoleUser, false))
  707. require.Nil(t, s.userManager.AllowAccess("ben", "sometopic", user.PermissionReadWrite)) // Not mytopic!
  708. response := request(t, s, "GET", "/mytopic/auth", "", map[string]string{
  709. "Authorization": util.BasicAuth("ben", "ben"),
  710. })
  711. require.Equal(t, 403, response.Code)
  712. }
  713. func TestServer_Auth_Fail_CannotPublish(t *testing.T) {
  714. c := newTestConfigWithAuthFile(t)
  715. c.AuthDefault = user.PermissionReadWrite // Open by default
  716. s := newTestServer(t, c)
  717. require.Nil(t, s.userManager.AddUser("phil", "phil", user.RoleAdmin, false))
  718. require.Nil(t, s.userManager.AllowAccess(user.Everyone, "private", user.PermissionDenyAll))
  719. require.Nil(t, s.userManager.AllowAccess(user.Everyone, "announcements", user.PermissionRead))
  720. response := request(t, s, "PUT", "/mytopic", "test", nil)
  721. require.Equal(t, 200, response.Code)
  722. response = request(t, s, "GET", "/mytopic/json?poll=1", "", nil)
  723. require.Equal(t, 200, response.Code)
  724. response = request(t, s, "PUT", "/announcements", "test", nil)
  725. require.Equal(t, 403, response.Code) // Cannot write as anonymous
  726. response = request(t, s, "PUT", "/announcements", "test", map[string]string{
  727. "Authorization": util.BasicAuth("phil", "phil"),
  728. })
  729. require.Equal(t, 200, response.Code)
  730. response = request(t, s, "GET", "/announcements/json?poll=1", "", nil)
  731. require.Equal(t, 200, response.Code) // Anonymous read allowed
  732. response = request(t, s, "GET", "/private/json?poll=1", "", nil)
  733. require.Equal(t, 403, response.Code) // Anonymous read not allowed
  734. }
  735. func TestServer_Auth_Fail_Rate_Limiting(t *testing.T) {
  736. c := newTestConfigWithAuthFile(t)
  737. c.VisitorAuthFailureLimitBurst = 10
  738. s := newTestServer(t, c)
  739. for i := 0; i < 10; i++ {
  740. response := request(t, s, "PUT", "/announcements", "test", map[string]string{
  741. "Authorization": util.BasicAuth("phil", "phil"),
  742. })
  743. require.Equal(t, 401, response.Code)
  744. }
  745. response := request(t, s, "PUT", "/announcements", "test", map[string]string{
  746. "Authorization": util.BasicAuth("phil", "phil"),
  747. })
  748. require.Equal(t, 429, response.Code)
  749. require.Equal(t, 42909, toHTTPError(t, response.Body.String()).Code)
  750. }
  751. func TestServer_Auth_ViaQuery(t *testing.T) {
  752. c := newTestConfigWithAuthFile(t)
  753. c.AuthDefault = user.PermissionDenyAll
  754. s := newTestServer(t, c)
  755. require.Nil(t, s.userManager.AddUser("ben", "some pass", user.RoleAdmin, false))
  756. u := fmt.Sprintf("/mytopic/json?poll=1&auth=%s", base64.RawURLEncoding.EncodeToString([]byte(util.BasicAuth("ben", "some pass"))))
  757. response := request(t, s, "GET", u, "", nil)
  758. require.Equal(t, 200, response.Code)
  759. u = fmt.Sprintf("/mytopic/json?poll=1&auth=%s", base64.RawURLEncoding.EncodeToString([]byte(util.BasicAuth("ben", "WRONNNGGGG"))))
  760. response = request(t, s, "GET", u, "", nil)
  761. require.Equal(t, 401, response.Code)
  762. }
  763. func TestServer_Auth_NonBasicHeader(t *testing.T) {
  764. s := newTestServer(t, newTestConfigWithAuthFile(t))
  765. response := request(t, s, "PUT", "/mytopic", "test", map[string]string{
  766. "Authorization": "WebPush not-supported",
  767. })
  768. require.Equal(t, 200, response.Code)
  769. response = request(t, s, "PUT", "/mytopic", "test", map[string]string{
  770. "Authorization": "Bearer supported",
  771. })
  772. require.Equal(t, 401, response.Code)
  773. response = request(t, s, "PUT", "/mytopic", "test", map[string]string{
  774. "Authorization": "basic supported",
  775. })
  776. require.Equal(t, 401, response.Code)
  777. }
  778. func TestServer_StatsResetter(t *testing.T) {
  779. t.Parallel()
  780. // This tests the stats resetter for
  781. // - an anonymous user
  782. // - a user without a tier (treated like the same as the anonymous user)
  783. // - a user with a tier
  784. c := newTestConfigWithAuthFile(t)
  785. c.VisitorStatsResetTime = time.Now().Add(2 * time.Second)
  786. s := newTestServer(t, c)
  787. go s.runStatsResetter()
  788. // Create user with tier (tieruser) and user without tier (phil)
  789. require.Nil(t, s.userManager.AddTier(&user.Tier{
  790. Code: "test",
  791. MessageLimit: 5,
  792. MessageExpiryDuration: -5 * time.Second, // Second, what a hack!
  793. }))
  794. require.Nil(t, s.userManager.AddUser("phil", "phil", user.RoleUser, false))
  795. require.Nil(t, s.userManager.AddUser("tieruser", "tieruser", user.RoleUser, false))
  796. require.Nil(t, s.userManager.ChangeTier("tieruser", "test"))
  797. // Send an anonymous message
  798. response := request(t, s, "PUT", "/mytopic", "test", nil)
  799. require.Equal(t, 200, response.Code)
  800. // Send messages from user without tier (phil)
  801. for i := 0; i < 5; i++ {
  802. response := request(t, s, "PUT", "/mytopic", "test", map[string]string{
  803. "Authorization": util.BasicAuth("phil", "phil"),
  804. })
  805. require.Equal(t, 200, response.Code)
  806. }
  807. // Send messages from user with tier
  808. for i := 0; i < 2; i++ {
  809. response := request(t, s, "PUT", "/mytopic", "test", map[string]string{
  810. "Authorization": util.BasicAuth("tieruser", "tieruser"),
  811. })
  812. require.Equal(t, 200, response.Code)
  813. }
  814. // User stats show 6 messages (for user without tier)
  815. response = request(t, s, "GET", "/v1/account", "", map[string]string{
  816. "Authorization": util.BasicAuth("phil", "phil"),
  817. })
  818. require.Equal(t, 200, response.Code)
  819. account, err := util.UnmarshalJSON[apiAccountResponse](io.NopCloser(response.Body))
  820. require.Nil(t, err)
  821. require.Equal(t, int64(6), account.Stats.Messages)
  822. // User stats show 6 messages (for anonymous visitor)
  823. response = request(t, s, "GET", "/v1/account", "", nil)
  824. require.Equal(t, 200, response.Code)
  825. account, err = util.UnmarshalJSON[apiAccountResponse](io.NopCloser(response.Body))
  826. require.Nil(t, err)
  827. require.Equal(t, int64(6), account.Stats.Messages)
  828. // User stats show 2 messages (for user with tier)
  829. response = request(t, s, "GET", "/v1/account", "", map[string]string{
  830. "Authorization": util.BasicAuth("tieruser", "tieruser"),
  831. })
  832. require.Equal(t, 200, response.Code)
  833. account, err = util.UnmarshalJSON[apiAccountResponse](io.NopCloser(response.Body))
  834. require.Nil(t, err)
  835. require.Equal(t, int64(2), account.Stats.Messages)
  836. // Wait for stats resetter to run
  837. waitFor(t, func() bool {
  838. response = request(t, s, "GET", "/v1/account", "", map[string]string{
  839. "Authorization": util.BasicAuth("phil", "phil"),
  840. })
  841. require.Equal(t, 200, response.Code)
  842. account, err = util.UnmarshalJSON[apiAccountResponse](io.NopCloser(response.Body))
  843. require.Nil(t, err)
  844. return account.Stats.Messages == 0
  845. })
  846. // User stats show 0 messages now!
  847. response = request(t, s, "GET", "/v1/account", "", map[string]string{
  848. "Authorization": util.BasicAuth("phil", "phil"),
  849. })
  850. require.Equal(t, 200, response.Code)
  851. account, err = util.UnmarshalJSON[apiAccountResponse](io.NopCloser(response.Body))
  852. require.Nil(t, err)
  853. require.Equal(t, int64(0), account.Stats.Messages)
  854. // Since this is a user without a tier, the anonymous user should have the same stats
  855. response = request(t, s, "GET", "/v1/account", "", nil)
  856. require.Equal(t, 200, response.Code)
  857. account, err = util.UnmarshalJSON[apiAccountResponse](io.NopCloser(response.Body))
  858. require.Nil(t, err)
  859. require.Equal(t, int64(0), account.Stats.Messages)
  860. // User stats show 0 messages (for user with tier)
  861. response = request(t, s, "GET", "/v1/account", "", map[string]string{
  862. "Authorization": util.BasicAuth("tieruser", "tieruser"),
  863. })
  864. require.Equal(t, 200, response.Code)
  865. account, err = util.UnmarshalJSON[apiAccountResponse](io.NopCloser(response.Body))
  866. require.Nil(t, err)
  867. require.Equal(t, int64(0), account.Stats.Messages)
  868. }
  869. func TestServer_StatsResetter_MessageLimiter_EmailsLimiter(t *testing.T) {
  870. // This tests that the messageLimiter (the only fixed limiter) and the emailsLimiter (token bucket)
  871. // is reset by the stats resetter
  872. c := newTestConfigWithAuthFile(t)
  873. s := newTestServer(t, c)
  874. s.smtpSender = &testMailer{}
  875. // Publish some messages, and check stats
  876. for i := 0; i < 3; i++ {
  877. response := request(t, s, "PUT", "/mytopic", "test", nil)
  878. require.Equal(t, 200, response.Code)
  879. }
  880. response := request(t, s, "PUT", "/mytopic", "test", map[string]string{
  881. "Email": "test@email.com",
  882. })
  883. require.Equal(t, 200, response.Code)
  884. rr := request(t, s, "GET", "/v1/account", "", nil)
  885. require.Equal(t, 200, rr.Code)
  886. account, err := util.UnmarshalJSON[apiAccountResponse](io.NopCloser(rr.Body))
  887. require.Nil(t, err)
  888. require.Equal(t, int64(4), account.Stats.Messages)
  889. require.Equal(t, int64(1), account.Stats.Emails)
  890. v := s.visitor(netip.MustParseAddr("9.9.9.9"), nil)
  891. require.Equal(t, int64(4), v.Stats().Messages)
  892. require.Equal(t, int64(4), v.messagesLimiter.Value())
  893. require.Equal(t, int64(1), v.Stats().Emails)
  894. require.Equal(t, int64(1), v.emailsLimiter.Value())
  895. // Reset stats and check again
  896. s.resetStats()
  897. rr = request(t, s, "GET", "/v1/account", "", nil)
  898. require.Equal(t, 200, rr.Code)
  899. account, err = util.UnmarshalJSON[apiAccountResponse](io.NopCloser(rr.Body))
  900. require.Nil(t, err)
  901. require.Equal(t, int64(0), account.Stats.Messages)
  902. require.Equal(t, int64(0), account.Stats.Emails)
  903. v = s.visitor(netip.MustParseAddr("9.9.9.9"), nil)
  904. require.Equal(t, int64(0), v.Stats().Messages)
  905. require.Equal(t, int64(0), v.messagesLimiter.Value())
  906. require.Equal(t, int64(0), v.Stats().Emails)
  907. require.Equal(t, int64(0), v.emailsLimiter.Value())
  908. }
  909. func TestServer_DailyMessageQuotaFromDatabase(t *testing.T) {
  910. t.Parallel()
  911. // This tests that the daily message quota is prefilled originally from the database,
  912. // if the visitor is unknown
  913. c := newTestConfigWithAuthFile(t)
  914. c.AuthStatsQueueWriterInterval = 100 * time.Millisecond
  915. s := newTestServer(t, c)
  916. // Create user, and update it with some message and email stats
  917. require.Nil(t, s.userManager.AddTier(&user.Tier{
  918. Code: "test",
  919. }))
  920. require.Nil(t, s.userManager.AddUser("phil", "phil", user.RoleUser, false))
  921. require.Nil(t, s.userManager.ChangeTier("phil", "test"))
  922. u, err := s.userManager.User("phil")
  923. require.Nil(t, err)
  924. s.userManager.EnqueueUserStats(u.ID, &user.Stats{
  925. Messages: 123456,
  926. Emails: 999,
  927. })
  928. time.Sleep(400 * time.Millisecond)
  929. // Get account and verify stats are read from the DB, and that the visitor also has these stats
  930. rr := request(t, s, "GET", "/v1/account", "", map[string]string{
  931. "Authorization": util.BasicAuth("phil", "phil"),
  932. })
  933. require.Equal(t, 200, rr.Code)
  934. account, err := util.UnmarshalJSON[apiAccountResponse](io.NopCloser(rr.Body))
  935. require.Nil(t, err)
  936. require.Equal(t, int64(123456), account.Stats.Messages)
  937. require.Equal(t, int64(999), account.Stats.Emails)
  938. v := s.visitor(netip.MustParseAddr("9.9.9.9"), u)
  939. require.Equal(t, int64(123456), v.Stats().Messages)
  940. require.Equal(t, int64(123456), v.messagesLimiter.Value())
  941. require.Equal(t, int64(999), v.Stats().Emails)
  942. require.Equal(t, int64(999), v.emailsLimiter.Value())
  943. }
  944. type testMailer struct {
  945. count int
  946. mu sync.Mutex
  947. }
  948. func (t *testMailer) Send(v *visitor, m *message, to string) error {
  949. t.mu.Lock()
  950. defer t.mu.Unlock()
  951. t.count++
  952. return nil
  953. }
  954. func (t *testMailer) Counts() (total int64, success int64, failure int64) {
  955. return 0, 0, 0
  956. }
  957. func (t *testMailer) Count() int {
  958. t.mu.Lock()
  959. defer t.mu.Unlock()
  960. return t.count
  961. }
  962. func TestServer_PublishTooManyRequests_Defaults(t *testing.T) {
  963. s := newTestServer(t, newTestConfig(t))
  964. for i := 0; i < 60; i++ {
  965. response := request(t, s, "PUT", "/mytopic", fmt.Sprintf("message %d", i), nil)
  966. require.Equal(t, 200, response.Code)
  967. }
  968. response := request(t, s, "PUT", "/mytopic", "message", nil)
  969. require.Equal(t, 429, response.Code)
  970. }
  971. func TestServer_PublishTooManyRequests_Defaults_IPv6(t *testing.T) {
  972. s := newTestServer(t, newTestConfig(t))
  973. overrideRemoteAddr1 := func(r *http.Request) {
  974. r.RemoteAddr = "[2001:db8:9999:8888:1::1]:1234"
  975. }
  976. overrideRemoteAddr2 := func(r *http.Request) {
  977. r.RemoteAddr = "[2001:db8:9999:8888:2::1]:1234" // Same /64
  978. }
  979. for i := 0; i < 30; i++ {
  980. response := request(t, s, "PUT", "/mytopic", fmt.Sprintf("message %d", i), nil, overrideRemoteAddr1)
  981. require.Equal(t, 200, response.Code)
  982. }
  983. for i := 0; i < 30; i++ {
  984. response := request(t, s, "PUT", "/mytopic", fmt.Sprintf("message %d", i), nil, overrideRemoteAddr2)
  985. require.Equal(t, 200, response.Code)
  986. }
  987. response := request(t, s, "PUT", "/mytopic", "message", nil, overrideRemoteAddr1)
  988. require.Equal(t, 429, response.Code)
  989. }
  990. func TestServer_PublishTooManyRequests_IPv6_Slash48(t *testing.T) {
  991. c := newTestConfig(t)
  992. c.VisitorRequestLimitBurst = 6
  993. c.VisitorPrefixBitsIPv6 = 48 // Use /48 for IPv6 prefixes
  994. s := newTestServer(t, c)
  995. overrideRemoteAddr1 := func(r *http.Request) {
  996. r.RemoteAddr = "[2001:db8:9999::1]:1234"
  997. }
  998. overrideRemoteAddr2 := func(r *http.Request) {
  999. r.RemoteAddr = "[2001:db8:9999::2]:1234" // Same /48
  1000. }
  1001. for i := 0; i < 3; i++ {
  1002. response := request(t, s, "PUT", "/mytopic", fmt.Sprintf("message %d", i), nil, overrideRemoteAddr1)
  1003. require.Equal(t, 200, response.Code)
  1004. }
  1005. for i := 0; i < 3; i++ {
  1006. response := request(t, s, "PUT", "/mytopic", fmt.Sprintf("message %d", i), nil, overrideRemoteAddr2)
  1007. require.Equal(t, 200, response.Code)
  1008. }
  1009. response := request(t, s, "PUT", "/mytopic", "message", nil, overrideRemoteAddr1)
  1010. require.Equal(t, 429, response.Code)
  1011. }
  1012. func TestServer_PublishTooManyRequests_Defaults_ExemptHosts(t *testing.T) {
  1013. c := newTestConfig(t)
  1014. c.VisitorRequestLimitBurst = 3
  1015. c.VisitorRequestExemptIPAddrs = []netip.Prefix{netip.MustParsePrefix("9.9.9.9/32")} // see request()
  1016. s := newTestServer(t, c)
  1017. for i := 0; i < 5; i++ { // > 3
  1018. response := request(t, s, "PUT", "/mytopic", fmt.Sprintf("message %d", i), nil)
  1019. require.Equal(t, 200, response.Code)
  1020. }
  1021. }
  1022. func TestServer_PublishTooManyRequests_Defaults_ExemptHosts_IPv6(t *testing.T) {
  1023. c := newTestConfig(t)
  1024. c.VisitorRequestLimitBurst = 3
  1025. c.VisitorRequestExemptIPAddrs = []netip.Prefix{netip.MustParsePrefix("2001:db8:9999::/48")}
  1026. s := newTestServer(t, c)
  1027. overrideRemoteAddr := func(r *http.Request) {
  1028. r.RemoteAddr = "[2001:db8:9999::1]:1234"
  1029. }
  1030. for i := 0; i < 5; i++ { // > 3
  1031. response := request(t, s, "PUT", "/mytopic", fmt.Sprintf("message %d", i), nil, overrideRemoteAddr)
  1032. require.Equal(t, 200, response.Code)
  1033. }
  1034. }
  1035. func TestServer_PublishTooManyRequests_Defaults_ExemptHosts_MessageDailyLimit(t *testing.T) {
  1036. c := newTestConfig(t)
  1037. c.VisitorRequestLimitBurst = 10
  1038. c.VisitorMessageDailyLimit = 4
  1039. c.VisitorRequestExemptIPAddrs = []netip.Prefix{netip.MustParsePrefix("9.9.9.9/32")} // see request()
  1040. s := newTestServer(t, c)
  1041. for i := 0; i < 8; i++ { // 4
  1042. response := request(t, s, "PUT", "/mytopic", "message", nil)
  1043. require.Equal(t, 200, response.Code)
  1044. }
  1045. }
  1046. func TestServer_PublishTooManyRequests_ShortReplenish(t *testing.T) {
  1047. t.Parallel()
  1048. c := newTestConfig(t)
  1049. c.VisitorRequestLimitBurst = 60
  1050. c.VisitorRequestLimitReplenish = time.Second
  1051. s := newTestServer(t, c)
  1052. for i := 0; i < 60; i++ {
  1053. response := request(t, s, "PUT", "/mytopic", fmt.Sprintf("message %d", i), nil)
  1054. require.Equal(t, 200, response.Code)
  1055. }
  1056. response := request(t, s, "PUT", "/mytopic", "message", nil)
  1057. require.Equal(t, 429, response.Code)
  1058. time.Sleep(1020 * time.Millisecond)
  1059. response = request(t, s, "PUT", "/mytopic", "message", nil)
  1060. require.Equal(t, 200, response.Code)
  1061. }
  1062. func TestServer_PublishTooManyEmails_Defaults(t *testing.T) {
  1063. s := newTestServer(t, newTestConfig(t))
  1064. s.smtpSender = &testMailer{}
  1065. for i := 0; i < 16; i++ {
  1066. response := request(t, s, "PUT", "/mytopic", fmt.Sprintf("message %d", i), map[string]string{
  1067. "E-Mail": "test@example.com",
  1068. })
  1069. require.Equal(t, 200, response.Code)
  1070. }
  1071. response := request(t, s, "PUT", "/mytopic", "one too many", map[string]string{
  1072. "E-Mail": "test@example.com",
  1073. })
  1074. require.Equal(t, 429, response.Code)
  1075. }
  1076. func TestServer_PublishTooManyEmails_Replenish(t *testing.T) {
  1077. t.Parallel()
  1078. c := newTestConfig(t)
  1079. c.VisitorEmailLimitReplenish = 500 * time.Millisecond
  1080. s := newTestServer(t, c)
  1081. s.smtpSender = &testMailer{}
  1082. for i := 0; i < 16; i++ {
  1083. response := request(t, s, "PUT", "/mytopic", fmt.Sprintf("message %d", i), map[string]string{
  1084. "E-Mail": "test@example.com",
  1085. })
  1086. require.Equal(t, 200, response.Code)
  1087. }
  1088. response := request(t, s, "PUT", "/mytopic", "one too many", map[string]string{
  1089. "E-Mail": "test@example.com",
  1090. })
  1091. require.Equal(t, 429, response.Code)
  1092. time.Sleep(510 * time.Millisecond)
  1093. response = request(t, s, "PUT", "/mytopic", "this should be okay again too many", map[string]string{
  1094. "E-Mail": "test@example.com",
  1095. })
  1096. require.Equal(t, 200, response.Code)
  1097. response = request(t, s, "PUT", "/mytopic", "and bad again", map[string]string{
  1098. "E-Mail": "test@example.com",
  1099. })
  1100. require.Equal(t, 429, response.Code)
  1101. }
  1102. func TestServer_PublishDelayedEmail_Fail(t *testing.T) {
  1103. s := newTestServer(t, newTestConfig(t))
  1104. s.smtpSender = &testMailer{}
  1105. response := request(t, s, "PUT", "/mytopic", "fail", map[string]string{
  1106. "E-Mail": "test@example.com",
  1107. "Delay": "20 min",
  1108. })
  1109. require.Equal(t, 40003, toHTTPError(t, response.Body.String()).Code)
  1110. }
  1111. func TestServer_PublishDelayedCall_Fail(t *testing.T) {
  1112. c := newTestConfigWithAuthFile(t)
  1113. c.TwilioAccount = "AC1234567890"
  1114. c.TwilioAuthToken = "AAEAA1234567890"
  1115. c.TwilioPhoneNumber = "+1234567890"
  1116. s := newTestServer(t, c)
  1117. response := request(t, s, "PUT", "/mytopic", "fail", map[string]string{
  1118. "Call": "yes",
  1119. "Delay": "20 min",
  1120. })
  1121. require.Equal(t, 40037, toHTTPError(t, response.Body.String()).Code)
  1122. }
  1123. func TestServer_PublishEmailNoMailer_Fail(t *testing.T) {
  1124. s := newTestServer(t, newTestConfig(t))
  1125. response := request(t, s, "PUT", "/mytopic", "fail", map[string]string{
  1126. "E-Mail": "test@example.com",
  1127. })
  1128. require.Equal(t, 400, response.Code)
  1129. }
  1130. func TestServer_PublishAndExpungeTopicAfter16Hours(t *testing.T) {
  1131. t.Parallel()
  1132. s := newTestServer(t, newTestConfig(t))
  1133. defer s.messageCache.Close()
  1134. subFn := func(v *visitor, msg *message) error {
  1135. return nil
  1136. }
  1137. // Publish and check last access
  1138. response := request(t, s, "POST", "/mytopic", "test", map[string]string{
  1139. "Cache": "no",
  1140. })
  1141. require.Equal(t, 200, response.Code)
  1142. waitFor(t, func() bool {
  1143. s.mu.Lock()
  1144. tp, exists := s.topics["mytopic"]
  1145. s.mu.Unlock()
  1146. if !exists {
  1147. return false
  1148. }
  1149. // .lastAccess set in t.Publish() -> t.Keepalive() in Goroutine
  1150. tp.mu.RLock()
  1151. defer tp.mu.RUnlock()
  1152. return tp.lastAccess.Unix() >= time.Now().Unix()-2 &&
  1153. tp.lastAccess.Unix() <= time.Now().Unix()+2
  1154. })
  1155. // Hack!
  1156. time.Sleep(time.Second)
  1157. // Topic won't get pruned
  1158. s.execManager()
  1159. require.NotNil(t, s.topics["mytopic"])
  1160. // Fudge with last access, but subscribe, and see that it won't get pruned (because of subscriber)
  1161. subID := s.topics["mytopic"].Subscribe(subFn, "", func() {})
  1162. s.topics["mytopic"].mu.Lock()
  1163. s.topics["mytopic"].lastAccess = time.Now().Add(-17 * time.Hour)
  1164. s.topics["mytopic"].mu.Unlock()
  1165. s.execManager()
  1166. require.NotNil(t, s.topics["mytopic"])
  1167. // It'll finally get pruned now that there are no subscribers and last access is 17 hours ago
  1168. s.topics["mytopic"].Unsubscribe(subID)
  1169. s.execManager()
  1170. require.Nil(t, s.topics["mytopic"])
  1171. }
  1172. func TestServer_TopicKeepaliveOnPoll(t *testing.T) {
  1173. t.Parallel()
  1174. s := newTestServer(t, newTestConfig(t))
  1175. // Create topic by polling once
  1176. response := request(t, s, "GET", "/mytopic/json?poll=1", "", nil)
  1177. require.Equal(t, 200, response.Code)
  1178. // Mess with last access time
  1179. s.topics["mytopic"].lastAccess = time.Now().Add(-17 * time.Hour)
  1180. // Poll again and check keepalive time
  1181. response = request(t, s, "GET", "/mytopic/json?poll=1", "", nil)
  1182. require.Equal(t, 200, response.Code)
  1183. require.True(t, s.topics["mytopic"].lastAccess.Unix() >= time.Now().Unix()-2)
  1184. require.True(t, s.topics["mytopic"].lastAccess.Unix() <= time.Now().Unix()+2)
  1185. }
  1186. func TestServer_UnifiedPushDiscovery(t *testing.T) {
  1187. s := newTestServer(t, newTestConfig(t))
  1188. response := request(t, s, "GET", "/mytopic?up=1", "", nil)
  1189. require.Equal(t, 200, response.Code)
  1190. require.Equal(t, `{"unifiedpush":{"version":1}}`+"\n", response.Body.String())
  1191. }
  1192. func TestServer_PublishUnifiedPushBinary_AndPoll(t *testing.T) {
  1193. b := make([]byte, 12) // Max length
  1194. _, err := rand.Read(b)
  1195. require.Nil(t, err)
  1196. s := newTestServer(t, newTestConfig(t))
  1197. // Register a UnifiedPush subscriber
  1198. response := request(t, s, "GET", "/up123456789012/json?poll=1", "", nil)
  1199. require.Equal(t, 200, response.Code)
  1200. // Publish message to topic
  1201. response = request(t, s, "PUT", "/up123456789012?up=1", string(b), nil)
  1202. require.Equal(t, 200, response.Code)
  1203. m := toMessage(t, response.Body.String())
  1204. require.Equal(t, "base64", m.Encoding)
  1205. b2, err := base64.StdEncoding.DecodeString(m.Message)
  1206. require.Nil(t, err)
  1207. require.Equal(t, b, b2)
  1208. // Retrieve and check published message
  1209. response = request(t, s, "GET", "/up123456789012/json?poll=1", string(b), nil)
  1210. require.Equal(t, 200, response.Code)
  1211. m = toMessage(t, response.Body.String())
  1212. require.Equal(t, "base64", m.Encoding)
  1213. b2, err = base64.StdEncoding.DecodeString(m.Message)
  1214. require.Nil(t, err)
  1215. require.Equal(t, b, b2)
  1216. }
  1217. func TestServer_PublishUnifiedPushBinary_Truncated(t *testing.T) {
  1218. b := make([]byte, 5000) // Longer than max length
  1219. _, err := rand.Read(b)
  1220. require.Nil(t, err)
  1221. s := newTestServer(t, newTestConfig(t))
  1222. // Register a UnifiedPush subscriber
  1223. response := request(t, s, "GET", "/mytopic/json?poll=1", "", nil)
  1224. require.Equal(t, 200, response.Code)
  1225. // Publish message to topic
  1226. response = request(t, s, "PUT", "/mytopic?up=1", string(b), nil)
  1227. require.Equal(t, 200, response.Code)
  1228. m := toMessage(t, response.Body.String())
  1229. require.Equal(t, "base64", m.Encoding)
  1230. b2, err := base64.StdEncoding.DecodeString(m.Message)
  1231. require.Nil(t, err)
  1232. require.Equal(t, 4096, len(b2))
  1233. require.Equal(t, b[:4096], b2)
  1234. }
  1235. func TestServer_PublishUnifiedPushText(t *testing.T) {
  1236. s := newTestServer(t, newTestConfig(t))
  1237. // Register a UnifiedPush subscriber
  1238. response := request(t, s, "GET", "/mytopic/json?poll=1", "", nil)
  1239. require.Equal(t, 200, response.Code)
  1240. // Publish UnifiedPush text message
  1241. response = request(t, s, "PUT", "/mytopic?up=1", "this is a unifiedpush text message", nil)
  1242. require.Equal(t, 200, response.Code)
  1243. m := toMessage(t, response.Body.String())
  1244. require.Equal(t, "", m.Encoding)
  1245. require.Equal(t, "this is a unifiedpush text message", m.Message)
  1246. }
  1247. func TestServer_MatrixGateway_Discovery_Success(t *testing.T) {
  1248. s := newTestServer(t, newTestConfig(t))
  1249. response := request(t, s, "GET", "/_matrix/push/v1/notify", "", nil)
  1250. require.Equal(t, 200, response.Code)
  1251. require.Equal(t, `{"unifiedpush":{"gateway":"matrix"}}`+"\n", response.Body.String())
  1252. }
  1253. func TestServer_MatrixGateway_Discovery_Failure_Unconfigured(t *testing.T) {
  1254. c := newTestConfig(t)
  1255. c.BaseURL = ""
  1256. s := newTestServer(t, c)
  1257. response := request(t, s, "GET", "/_matrix/push/v1/notify", "", nil)
  1258. require.Equal(t, 500, response.Code)
  1259. err := toHTTPError(t, response.Body.String())
  1260. require.Equal(t, 50003, err.Code)
  1261. }
  1262. func TestServer_MatrixGateway_Push_Success(t *testing.T) {
  1263. s := newTestServer(t, newTestConfig(t))
  1264. response := request(t, s, "GET", "/mytopic/json?poll=1", "", nil)
  1265. require.Equal(t, 200, response.Code)
  1266. notification := `{"notification":{"devices":[{"pushkey":"http://127.0.0.1:12345/mytopic?up=1"}]}}`
  1267. response = request(t, s, "POST", "/_matrix/push/v1/notify", notification, nil)
  1268. require.Equal(t, 200, response.Code)
  1269. require.Equal(t, `{"rejected":[]}`+"\n", response.Body.String())
  1270. response = request(t, s, "GET", "/mytopic/json?poll=1", "", nil)
  1271. require.Equal(t, 200, response.Code)
  1272. m := toMessage(t, response.Body.String())
  1273. require.Equal(t, notification, m.Message)
  1274. }
  1275. func TestServer_MatrixGateway_Push_Failure_NoSubscriber(t *testing.T) {
  1276. c := newTestConfig(t)
  1277. c.VisitorSubscriberRateLimiting = true
  1278. s := newTestServer(t, c)
  1279. notification := `{"notification":{"devices":[{"pushkey":"http://127.0.0.1:12345/mytopic?up=1"}]}}`
  1280. response := request(t, s, "POST", "/_matrix/push/v1/notify", notification, nil)
  1281. require.Equal(t, 507, response.Code)
  1282. require.Equal(t, 50701, toHTTPError(t, response.Body.String()).Code)
  1283. }
  1284. func TestServer_MatrixGateway_Push_Failure_NoSubscriber_After13Hours(t *testing.T) {
  1285. c := newTestConfig(t)
  1286. c.VisitorSubscriberRateLimiting = true
  1287. s := newTestServer(t, c)
  1288. notification := `{"notification":{"devices":[{"pushkey":"http://127.0.0.1:12345/mytopic?up=1"}]}}`
  1289. // No success if no rate visitor set (this also creates the topic in memory)
  1290. response := request(t, s, "POST", "/_matrix/push/v1/notify", notification, nil)
  1291. require.Equal(t, 507, response.Code)
  1292. require.Equal(t, 50701, toHTTPError(t, response.Body.String()).Code)
  1293. require.Nil(t, s.topics["mytopic"].rateVisitor)
  1294. // Fake: This topic has been around for 13 hours without a rate visitor
  1295. s.topics["mytopic"].lastAccess = time.Now().Add(-13 * time.Hour)
  1296. // Same request should now return HTTP 200 with a rejected pushkey
  1297. response = request(t, s, "POST", "/_matrix/push/v1/notify", notification, nil)
  1298. require.Equal(t, 200, response.Code)
  1299. require.Equal(t, `{"rejected":["http://127.0.0.1:12345/mytopic?up=1"]}`, strings.TrimSpace(response.Body.String()))
  1300. // Slightly unrelated: Test that topic is pruned after 16 hours
  1301. s.topics["mytopic"].lastAccess = time.Now().Add(-17 * time.Hour)
  1302. s.execManager()
  1303. require.Nil(t, s.topics["mytopic"])
  1304. }
  1305. func TestServer_MatrixGateway_Push_Failure_InvalidPushkey(t *testing.T) {
  1306. s := newTestServer(t, newTestConfig(t))
  1307. notification := `{"notification":{"devices":[{"pushkey":"http://wrong-base-url.com/mytopic?up=1"}]}}`
  1308. response := request(t, s, "POST", "/_matrix/push/v1/notify", notification, nil)
  1309. require.Equal(t, 200, response.Code)
  1310. require.Equal(t, `{"rejected":["http://wrong-base-url.com/mytopic?up=1"]}`+"\n", response.Body.String())
  1311. response = request(t, s, "GET", "/mytopic/json?poll=1", "", nil)
  1312. require.Equal(t, 200, response.Code)
  1313. require.Equal(t, "", response.Body.String()) // Empty!
  1314. }
  1315. func TestServer_MatrixGateway_Push_Failure_EverythingIsWrong(t *testing.T) {
  1316. s := newTestServer(t, newTestConfig(t))
  1317. notification := `{"message":"this is not really a Matrix message"}`
  1318. response := request(t, s, "POST", "/_matrix/push/v1/notify", notification, nil)
  1319. require.Equal(t, 400, response.Code)
  1320. require.Equal(t, 40019, toHTTPError(t, response.Body.String()).Code)
  1321. notification = `this isn't even JSON'`
  1322. response = request(t, s, "POST", "/_matrix/push/v1/notify", notification, nil)
  1323. require.Equal(t, 400, response.Code)
  1324. require.Equal(t, 40019, toHTTPError(t, response.Body.String()).Code)
  1325. }
  1326. func TestServer_MatrixGateway_Push_Failure_Unconfigured(t *testing.T) {
  1327. c := newTestConfig(t)
  1328. c.BaseURL = ""
  1329. s := newTestServer(t, c)
  1330. notification := `{"notification":{"devices":[{"pushkey":"http://127.0.0.1:12345/mytopic?up=1"}]}}`
  1331. response := request(t, s, "POST", "/_matrix/push/v1/notify", notification, nil)
  1332. require.Equal(t, 500, response.Code)
  1333. require.Equal(t, 50003, toHTTPError(t, response.Body.String()).Code)
  1334. }
  1335. func TestServer_PublishActions_AndPoll(t *testing.T) {
  1336. s := newTestServer(t, newTestConfig(t))
  1337. response := request(t, s, "PUT", "/mytopic", "my message", map[string]string{
  1338. "Actions": "view, Open portal, https://home.nest.com/; http, Turn down, https://api.nest.com/device/XZ1D2, body=target_temp_f=65",
  1339. })
  1340. require.Equal(t, 200, response.Code)
  1341. response = request(t, s, "GET", "/mytopic/json?poll=1", "", nil)
  1342. require.Equal(t, 200, response.Code)
  1343. m := toMessage(t, response.Body.String())
  1344. require.Equal(t, 2, len(m.Actions))
  1345. require.Equal(t, "view", m.Actions[0].Action)
  1346. require.Equal(t, "Open portal", m.Actions[0].Label)
  1347. require.Equal(t, "https://home.nest.com/", m.Actions[0].URL)
  1348. require.Equal(t, "http", m.Actions[1].Action)
  1349. require.Equal(t, "Turn down", m.Actions[1].Label)
  1350. require.Equal(t, "https://api.nest.com/device/XZ1D2", m.Actions[1].URL)
  1351. require.Equal(t, "target_temp_f=65", m.Actions[1].Body)
  1352. }
  1353. func TestServer_PublishMarkdown(t *testing.T) {
  1354. s := newTestServer(t, newTestConfig(t))
  1355. response := request(t, s, "PUT", "/mytopic", "**make this bold**", map[string]string{
  1356. "Content-Type": "text/markdown",
  1357. })
  1358. require.Equal(t, 200, response.Code)
  1359. m := toMessage(t, response.Body.String())
  1360. require.Equal(t, "**make this bold**", m.Message)
  1361. require.Equal(t, "text/markdown", m.ContentType)
  1362. }
  1363. func TestServer_PublishMarkdown_QueryParam(t *testing.T) {
  1364. s := newTestServer(t, newTestConfig(t))
  1365. response := request(t, s, "PUT", "/mytopic?md=1", "**make this bold**", nil)
  1366. require.Equal(t, 200, response.Code)
  1367. m := toMessage(t, response.Body.String())
  1368. require.Equal(t, "**make this bold**", m.Message)
  1369. require.Equal(t, "text/markdown", m.ContentType)
  1370. }
  1371. func TestServer_PublishMarkdown_NotMarkdown(t *testing.T) {
  1372. s := newTestServer(t, newTestConfig(t))
  1373. response := request(t, s, "PUT", "/mytopic", "**make this bold**", map[string]string{
  1374. "Content-Type": "not-markdown",
  1375. })
  1376. require.Equal(t, 200, response.Code)
  1377. m := toMessage(t, response.Body.String())
  1378. require.Equal(t, "", m.ContentType)
  1379. }
  1380. func TestServer_PublishAsJSON(t *testing.T) {
  1381. s := newTestServer(t, newTestConfig(t))
  1382. body := `{"topic":"mytopic","message":"A message","title":"a title\nwith lines","tags":["tag1","tag 2"],` +
  1383. `"not-a-thing":"ok", "attach":"http://google.com","filename":"google.pdf", "click":"http://ntfy.sh","priority":4,` +
  1384. `"icon":"https://ntfy.sh/static/img/ntfy.png", "delay":"30min"}`
  1385. response := request(t, s, "PUT", "/", body, nil)
  1386. require.Equal(t, 200, response.Code)
  1387. m := toMessage(t, response.Body.String())
  1388. require.Equal(t, "mytopic", m.Topic)
  1389. require.Equal(t, "A message", m.Message)
  1390. require.Equal(t, "a title\nwith lines", m.Title)
  1391. require.Equal(t, []string{"tag1", "tag 2"}, m.Tags)
  1392. require.Equal(t, "http://google.com", m.Attachment.URL)
  1393. require.Equal(t, "google.pdf", m.Attachment.Name)
  1394. require.Equal(t, "http://ntfy.sh", m.Click)
  1395. require.Equal(t, "https://ntfy.sh/static/img/ntfy.png", m.Icon)
  1396. require.Equal(t, "", m.ContentType)
  1397. require.Equal(t, 4, m.Priority)
  1398. require.True(t, m.Time > time.Now().Unix()+29*60)
  1399. require.True(t, m.Time < time.Now().Unix()+31*60)
  1400. }
  1401. func TestServer_PublishAsJSON_Markdown(t *testing.T) {
  1402. s := newTestServer(t, newTestConfig(t))
  1403. body := `{"topic":"mytopic","message":"**This is bold**","markdown":true}`
  1404. response := request(t, s, "PUT", "/", body, nil)
  1405. require.Equal(t, 200, response.Code)
  1406. m := toMessage(t, response.Body.String())
  1407. require.Equal(t, "mytopic", m.Topic)
  1408. require.Equal(t, "**This is bold**", m.Message)
  1409. require.Equal(t, "text/markdown", m.ContentType)
  1410. }
  1411. func TestServer_PublishAsJSON_RateLimit_MessageDailyLimit(t *testing.T) {
  1412. // Publishing as JSON follows a different path. This ensures that rate
  1413. // limiting works for this endpoint as well
  1414. c := newTestConfig(t)
  1415. c.VisitorMessageDailyLimit = 3
  1416. s := newTestServer(t, c)
  1417. for i := 0; i < 3; i++ {
  1418. response := request(t, s, "PUT", "/", `{"topic":"mytopic","message":"A message"}`, nil)
  1419. require.Equal(t, 200, response.Code)
  1420. }
  1421. response := request(t, s, "PUT", "/", `{"topic":"mytopic","message":"A message"}`, nil)
  1422. require.Equal(t, 429, response.Code)
  1423. require.Equal(t, 42908, toHTTPError(t, response.Body.String()).Code)
  1424. }
  1425. func TestServer_PublishAsJSON_WithEmail(t *testing.T) {
  1426. t.Parallel()
  1427. mailer := &testMailer{}
  1428. s := newTestServer(t, newTestConfig(t))
  1429. s.smtpSender = mailer
  1430. body := `{"topic":"mytopic","message":"A message","email":"phil@example.com"}`
  1431. response := request(t, s, "PUT", "/", body, nil)
  1432. require.Equal(t, 200, response.Code)
  1433. time.Sleep(100 * time.Millisecond) // E-Mail publishing happens in a Go routine
  1434. m := toMessage(t, response.Body.String())
  1435. require.Equal(t, "mytopic", m.Topic)
  1436. require.Equal(t, "A message", m.Message)
  1437. require.Equal(t, 1, mailer.Count())
  1438. }
  1439. func TestServer_PublishAsJSON_WithActions(t *testing.T) {
  1440. s := newTestServer(t, newTestConfig(t))
  1441. body := `{
  1442. "topic":"mytopic",
  1443. "message":"A message",
  1444. "actions": [
  1445. {
  1446. "action": "view",
  1447. "label": "Open portal",
  1448. "url": "https://home.nest.com/"
  1449. },
  1450. {
  1451. "action": "http",
  1452. "label": "Turn down",
  1453. "url": "https://api.nest.com/device/XZ1D2",
  1454. "body": "target_temp_f=65"
  1455. }
  1456. ]
  1457. }`
  1458. response := request(t, s, "POST", "/", body, nil)
  1459. require.Equal(t, 200, response.Code)
  1460. m := toMessage(t, response.Body.String())
  1461. require.Equal(t, "mytopic", m.Topic)
  1462. require.Equal(t, "A message", m.Message)
  1463. require.Equal(t, 2, len(m.Actions))
  1464. require.Equal(t, "view", m.Actions[0].Action)
  1465. require.Equal(t, "Open portal", m.Actions[0].Label)
  1466. require.Equal(t, "https://home.nest.com/", m.Actions[0].URL)
  1467. require.Equal(t, "http", m.Actions[1].Action)
  1468. require.Equal(t, "Turn down", m.Actions[1].Label)
  1469. require.Equal(t, "https://api.nest.com/device/XZ1D2", m.Actions[1].URL)
  1470. require.Equal(t, "target_temp_f=65", m.Actions[1].Body)
  1471. }
  1472. func TestServer_PublishAsJSON_NoCache(t *testing.T) {
  1473. s := newTestServer(t, newTestConfig(t))
  1474. body := `{"topic":"mytopic","message": "this message is not cached","cache":"no"}`
  1475. response := request(t, s, "PUT", "/", body, nil)
  1476. msg := toMessage(t, response.Body.String())
  1477. require.NotEmpty(t, msg.ID)
  1478. require.Equal(t, "this message is not cached", msg.Message)
  1479. require.Equal(t, int64(0), msg.Expires)
  1480. response = request(t, s, "GET", "/mytopic/json?poll=1", "", nil)
  1481. messages := toMessages(t, response.Body.String())
  1482. require.Empty(t, messages)
  1483. }
  1484. func TestServer_PublishAsJSON_WithoutFirebase(t *testing.T) {
  1485. sender := newTestFirebaseSender(10)
  1486. s := newTestServer(t, newTestConfig(t))
  1487. s.firebaseClient = newFirebaseClient(sender, &testAuther{Allow: true})
  1488. body := `{"topic":"mytopic","message": "my first message","firebase":"no"}`
  1489. response := request(t, s, "PUT", "/", body, nil)
  1490. msg1 := toMessage(t, response.Body.String())
  1491. require.NotEmpty(t, msg1.ID)
  1492. require.Equal(t, "my first message", msg1.Message)
  1493. time.Sleep(100 * time.Millisecond) // Firebase publishing happens
  1494. require.Equal(t, 0, len(sender.Messages()))
  1495. }
  1496. func TestServer_PublishAsJSON_Invalid(t *testing.T) {
  1497. s := newTestServer(t, newTestConfig(t))
  1498. body := `{"topic":"mytopic",INVALID`
  1499. response := request(t, s, "PUT", "/", body, nil)
  1500. require.Equal(t, 400, response.Code)
  1501. }
  1502. func TestServer_PublishWithTierBasedMessageLimitAndExpiry(t *testing.T) {
  1503. c := newTestConfigWithAuthFile(t)
  1504. s := newTestServer(t, c)
  1505. // Create tier with certain limits
  1506. require.Nil(t, s.userManager.AddTier(&user.Tier{
  1507. Code: "test",
  1508. MessageLimit: 5,
  1509. MessageExpiryDuration: -5 * time.Second, // Second, what a hack!
  1510. }))
  1511. require.Nil(t, s.userManager.AddUser("phil", "phil", user.RoleUser, false))
  1512. require.Nil(t, s.userManager.ChangeTier("phil", "test"))
  1513. // Publish to reach message limit
  1514. for i := 0; i < 5; i++ {
  1515. response := request(t, s, "PUT", "/mytopic", fmt.Sprintf("this is message %d", i+1), map[string]string{
  1516. "Authorization": util.BasicAuth("phil", "phil"),
  1517. })
  1518. require.Equal(t, 200, response.Code)
  1519. msg := toMessage(t, response.Body.String())
  1520. require.True(t, msg.Expires < time.Now().Unix()+5)
  1521. }
  1522. response := request(t, s, "PUT", "/mytopic", "this is too much", map[string]string{
  1523. "Authorization": util.BasicAuth("phil", "phil"),
  1524. })
  1525. require.Equal(t, 429, response.Code)
  1526. // Run pruning and see if they are gone
  1527. s.execManager()
  1528. response = request(t, s, "GET", "/mytopic/json?poll=1", "", map[string]string{
  1529. "Authorization": util.BasicAuth("phil", "phil"),
  1530. })
  1531. require.Equal(t, 200, response.Code)
  1532. require.Empty(t, response.Body)
  1533. }
  1534. func TestServer_PublishAttachment(t *testing.T) {
  1535. content := "text file!" + util.RandomString(4990) // > 4096
  1536. s := newTestServer(t, newTestConfig(t))
  1537. response := request(t, s, "PUT", "/mytopic", content, nil)
  1538. msg := toMessage(t, response.Body.String())
  1539. require.Equal(t, "attachment.txt", msg.Attachment.Name)
  1540. require.Equal(t, "text/plain; charset=utf-8", msg.Attachment.Type)
  1541. require.Equal(t, int64(5000), msg.Attachment.Size)
  1542. require.GreaterOrEqual(t, msg.Attachment.Expires, time.Now().Add(179*time.Minute).Unix()) // Almost 3 hours
  1543. require.Contains(t, msg.Attachment.URL, "http://127.0.0.1:12345/file/")
  1544. require.Equal(t, netip.Addr{}, msg.Sender) // Should never be returned
  1545. require.FileExists(t, filepath.Join(s.config.AttachmentCacheDir, msg.ID))
  1546. // GET
  1547. path := strings.TrimPrefix(msg.Attachment.URL, "http://127.0.0.1:12345")
  1548. response = request(t, s, "GET", path, "", nil)
  1549. require.Equal(t, 200, response.Code)
  1550. require.Equal(t, "5000", response.Header().Get("Content-Length"))
  1551. require.Equal(t, content, response.Body.String())
  1552. // HEAD
  1553. response = request(t, s, "HEAD", path, "", nil)
  1554. require.Equal(t, 200, response.Code)
  1555. require.Equal(t, "5000", response.Header().Get("Content-Length"))
  1556. require.Equal(t, "", response.Body.String())
  1557. // Slightly unrelated cross-test: make sure we add an owner for internal attachments
  1558. size, err := s.messageCache.AttachmentBytesUsedBySender("9.9.9.9") // See request()
  1559. require.Nil(t, err)
  1560. require.Equal(t, int64(5000), size)
  1561. }
  1562. func TestServer_PublishAttachmentShortWithFilename(t *testing.T) {
  1563. c := newTestConfig(t)
  1564. c.BehindProxy = true
  1565. s := newTestServer(t, c)
  1566. content := "this is an ATTACHMENT"
  1567. response := request(t, s, "PUT", "/mytopic?f=myfile.txt", content, map[string]string{
  1568. "X-Forwarded-For": "1.2.3.4",
  1569. })
  1570. msg := toMessage(t, response.Body.String())
  1571. require.Equal(t, "myfile.txt", msg.Attachment.Name)
  1572. require.Equal(t, "text/plain; charset=utf-8", msg.Attachment.Type)
  1573. require.Equal(t, int64(21), msg.Attachment.Size)
  1574. require.GreaterOrEqual(t, msg.Attachment.Expires, time.Now().Add(3*time.Hour).Unix())
  1575. require.Contains(t, msg.Attachment.URL, "http://127.0.0.1:12345/file/")
  1576. require.Equal(t, netip.Addr{}, msg.Sender) // Should never be returned
  1577. require.FileExists(t, filepath.Join(s.config.AttachmentCacheDir, msg.ID))
  1578. path := strings.TrimPrefix(msg.Attachment.URL, "http://127.0.0.1:12345")
  1579. response = request(t, s, "GET", path, "", nil)
  1580. require.Equal(t, 200, response.Code)
  1581. require.Equal(t, "21", response.Header().Get("Content-Length"))
  1582. require.Equal(t, content, response.Body.String())
  1583. // Slightly unrelated cross-test: make sure we add an owner for internal attachments
  1584. size, err := s.messageCache.AttachmentBytesUsedBySender("1.2.3.4")
  1585. require.Nil(t, err)
  1586. require.Equal(t, int64(21), size)
  1587. }
  1588. func TestServer_PublishAttachmentExternalWithoutFilename(t *testing.T) {
  1589. s := newTestServer(t, newTestConfig(t))
  1590. response := request(t, s, "PUT", "/mytopic", "", map[string]string{
  1591. "Attach": "https://upload.wikimedia.org/wikipedia/commons/f/fd/Pink_flower.jpg",
  1592. })
  1593. msg := toMessage(t, response.Body.String())
  1594. require.Equal(t, "You received a file: Pink_flower.jpg", msg.Message)
  1595. require.Equal(t, "Pink_flower.jpg", msg.Attachment.Name)
  1596. require.Equal(t, "https://upload.wikimedia.org/wikipedia/commons/f/fd/Pink_flower.jpg", msg.Attachment.URL)
  1597. require.Equal(t, "", msg.Attachment.Type)
  1598. require.Equal(t, int64(0), msg.Attachment.Size)
  1599. require.Equal(t, int64(0), msg.Attachment.Expires)
  1600. require.Equal(t, netip.Addr{}, msg.Sender)
  1601. // Slightly unrelated cross-test: make sure we don't add an owner for external attachments
  1602. size, err := s.messageCache.AttachmentBytesUsedBySender("127.0.0.1")
  1603. require.Nil(t, err)
  1604. require.Equal(t, int64(0), size)
  1605. }
  1606. func TestServer_PublishAttachmentExternalWithFilename(t *testing.T) {
  1607. s := newTestServer(t, newTestConfig(t))
  1608. response := request(t, s, "PUT", "/mytopic", "This is a custom message", map[string]string{
  1609. "X-Attach": "https://upload.wikimedia.org/wikipedia/commons/f/fd/Pink_flower.jpg",
  1610. "File": "some file.jpg",
  1611. })
  1612. msg := toMessage(t, response.Body.String())
  1613. require.Equal(t, "This is a custom message", msg.Message)
  1614. require.Equal(t, "some file.jpg", msg.Attachment.Name)
  1615. require.Equal(t, "https://upload.wikimedia.org/wikipedia/commons/f/fd/Pink_flower.jpg", msg.Attachment.URL)
  1616. require.Equal(t, "", msg.Attachment.Type)
  1617. require.Equal(t, int64(0), msg.Attachment.Size)
  1618. require.Equal(t, int64(0), msg.Attachment.Expires)
  1619. require.Equal(t, netip.Addr{}, msg.Sender)
  1620. }
  1621. func TestServer_PublishAttachmentBadURL(t *testing.T) {
  1622. s := newTestServer(t, newTestConfig(t))
  1623. response := request(t, s, "PUT", "/mytopic?a=not+a+URL", "", nil)
  1624. err := toHTTPError(t, response.Body.String())
  1625. require.Equal(t, 400, response.Code)
  1626. require.Equal(t, 400, err.HTTPCode)
  1627. require.Equal(t, 40013, err.Code)
  1628. }
  1629. func TestServer_PublishAttachmentTooLargeContentLength(t *testing.T) {
  1630. content := util.RandomString(5000) // > 4096
  1631. s := newTestServer(t, newTestConfig(t))
  1632. response := request(t, s, "PUT", "/mytopic", content, map[string]string{
  1633. "Content-Length": "20000000",
  1634. })
  1635. err := toHTTPError(t, response.Body.String())
  1636. require.Equal(t, 413, response.Code)
  1637. require.Equal(t, 413, err.HTTPCode)
  1638. require.Equal(t, 41301, err.Code)
  1639. }
  1640. func TestServer_PublishAttachmentTooLargeBodyAttachmentFileSizeLimit(t *testing.T) {
  1641. content := util.RandomString(5001) // > 5000, see below
  1642. c := newTestConfig(t)
  1643. c.AttachmentFileSizeLimit = 5000
  1644. s := newTestServer(t, c)
  1645. response := request(t, s, "PUT", "/mytopic", content, nil)
  1646. err := toHTTPError(t, response.Body.String())
  1647. require.Equal(t, 413, response.Code)
  1648. require.Equal(t, 413, err.HTTPCode)
  1649. require.Equal(t, 41301, err.Code)
  1650. }
  1651. func TestServer_PublishAttachmentExpiryBeforeDelivery(t *testing.T) {
  1652. c := newTestConfig(t)
  1653. c.AttachmentExpiryDuration = 10 * time.Minute
  1654. s := newTestServer(t, c)
  1655. response := request(t, s, "PUT", "/mytopic", util.RandomString(5000), map[string]string{
  1656. "Delay": "11 min", // > AttachmentExpiryDuration
  1657. })
  1658. err := toHTTPError(t, response.Body.String())
  1659. require.Equal(t, 400, response.Code)
  1660. require.Equal(t, 400, err.HTTPCode)
  1661. require.Equal(t, 40015, err.Code)
  1662. }
  1663. func TestServer_PublishAttachmentTooLargeBodyVisitorAttachmentTotalSizeLimit(t *testing.T) {
  1664. c := newTestConfig(t)
  1665. c.VisitorAttachmentTotalSizeLimit = 10000
  1666. s := newTestServer(t, c)
  1667. response := request(t, s, "PUT", "/mytopic", "text file!"+util.RandomString(4990), nil)
  1668. msg := toMessage(t, response.Body.String())
  1669. require.Equal(t, 200, response.Code)
  1670. require.Equal(t, "You received a file: attachment.txt", msg.Message)
  1671. require.Equal(t, int64(5000), msg.Attachment.Size)
  1672. content := util.RandomString(5001) // 5000+5001 > , see below
  1673. response = request(t, s, "PUT", "/mytopic", content, nil)
  1674. err := toHTTPError(t, response.Body.String())
  1675. require.Equal(t, 413, response.Code)
  1676. require.Equal(t, 413, err.HTTPCode)
  1677. require.Equal(t, 41301, err.Code)
  1678. }
  1679. func TestServer_PublishAttachmentAndExpire(t *testing.T) {
  1680. t.Parallel()
  1681. content := util.RandomString(5000) // > 4096
  1682. c := newTestConfig(t)
  1683. c.AttachmentExpiryDuration = time.Millisecond // Hack
  1684. s := newTestServer(t, c)
  1685. // Publish and make sure we can retrieve it
  1686. response := request(t, s, "PUT", "/mytopic", content, nil)
  1687. msg := toMessage(t, response.Body.String())
  1688. require.Contains(t, msg.Attachment.URL, "http://127.0.0.1:12345/file/")
  1689. file := filepath.Join(s.config.AttachmentCacheDir, msg.ID)
  1690. require.FileExists(t, file)
  1691. path := strings.TrimPrefix(msg.Attachment.URL, "http://127.0.0.1:12345")
  1692. response = request(t, s, "GET", path, "", nil)
  1693. require.Equal(t, 200, response.Code)
  1694. require.Equal(t, content, response.Body.String())
  1695. // Prune and makes sure it's gone
  1696. waitFor(t, func() bool {
  1697. s.execManager() // May run many times
  1698. return !util.FileExists(file)
  1699. })
  1700. response = request(t, s, "GET", path, "", nil)
  1701. require.Equal(t, 404, response.Code)
  1702. }
  1703. func TestServer_PublishAttachmentWithTierBasedExpiry(t *testing.T) {
  1704. t.Parallel()
  1705. content := util.RandomString(5000) // > 4096
  1706. c := newTestConfigWithAuthFile(t)
  1707. c.AttachmentExpiryDuration = time.Millisecond // Hack
  1708. s := newTestServer(t, c)
  1709. // Create tier with certain limits
  1710. sevenDays := time.Duration(604800) * time.Second
  1711. require.Nil(t, s.userManager.AddTier(&user.Tier{
  1712. Code: "test",
  1713. MessageLimit: 10,
  1714. MessageExpiryDuration: sevenDays,
  1715. AttachmentFileSizeLimit: 50_000,
  1716. AttachmentTotalSizeLimit: 200_000,
  1717. AttachmentExpiryDuration: sevenDays, // 7 days
  1718. AttachmentBandwidthLimit: 100000,
  1719. }))
  1720. require.Nil(t, s.userManager.AddUser("phil", "phil", user.RoleUser, false))
  1721. require.Nil(t, s.userManager.ChangeTier("phil", "test"))
  1722. // Publish and make sure we can retrieve it
  1723. response := request(t, s, "PUT", "/mytopic", content, map[string]string{
  1724. "Authorization": util.BasicAuth("phil", "phil"),
  1725. })
  1726. require.Equal(t, 200, response.Code)
  1727. msg := toMessage(t, response.Body.String())
  1728. require.Contains(t, msg.Attachment.URL, "http://127.0.0.1:12345/file/")
  1729. require.True(t, msg.Attachment.Expires > time.Now().Add(sevenDays-30*time.Second).Unix())
  1730. require.True(t, msg.Expires > time.Now().Add(sevenDays-30*time.Second).Unix())
  1731. file := filepath.Join(s.config.AttachmentCacheDir, msg.ID)
  1732. require.FileExists(t, file)
  1733. path := strings.TrimPrefix(msg.Attachment.URL, "http://127.0.0.1:12345")
  1734. response = request(t, s, "GET", path, "", nil)
  1735. require.Equal(t, 200, response.Code)
  1736. require.Equal(t, content, response.Body.String())
  1737. // Prune and makes sure it's still there
  1738. time.Sleep(time.Second) // Sigh ...
  1739. s.execManager()
  1740. require.FileExists(t, file)
  1741. response = request(t, s, "GET", path, "", nil)
  1742. require.Equal(t, 200, response.Code)
  1743. }
  1744. func TestServer_PublishAttachmentWithTierBasedBandwidthLimit(t *testing.T) {
  1745. content := util.RandomString(5000) // > 4096
  1746. c := newTestConfigWithAuthFile(t)
  1747. c.VisitorAttachmentDailyBandwidthLimit = 1000 // Much lower than tier bandwidth!
  1748. s := newTestServer(t, c)
  1749. // Create tier with certain limits
  1750. require.Nil(t, s.userManager.AddTier(&user.Tier{
  1751. Code: "test",
  1752. MessageLimit: 10,
  1753. MessageExpiryDuration: time.Hour,
  1754. AttachmentFileSizeLimit: 50_000,
  1755. AttachmentTotalSizeLimit: 200_000,
  1756. AttachmentExpiryDuration: time.Hour,
  1757. AttachmentBandwidthLimit: 14000, // < 3x5000 bytes -> enough for one upload, one download
  1758. }))
  1759. require.Nil(t, s.userManager.AddUser("phil", "phil", user.RoleUser, false))
  1760. require.Nil(t, s.userManager.ChangeTier("phil", "test"))
  1761. // Publish and make sure we can retrieve it
  1762. rr := request(t, s, "PUT", "/mytopic", content, map[string]string{
  1763. "Authorization": util.BasicAuth("phil", "phil"),
  1764. })
  1765. require.Equal(t, 200, rr.Code)
  1766. msg := toMessage(t, rr.Body.String())
  1767. // Retrieve it (first time succeeds)
  1768. rr = request(t, s, "GET", "/file/"+msg.ID, content, nil) // File downloads do not send auth headers!!
  1769. require.Equal(t, 200, rr.Code)
  1770. require.Equal(t, content, rr.Body.String())
  1771. // Retrieve it AGAIN (fails, due to bandwidth limit)
  1772. rr = request(t, s, "GET", "/file/"+msg.ID, content, nil)
  1773. require.Equal(t, 429, rr.Code)
  1774. }
  1775. func TestServer_PublishAttachmentWithTierBasedLimits(t *testing.T) {
  1776. smallFile := util.RandomString(20_000)
  1777. largeFile := util.RandomString(50_000)
  1778. c := newTestConfigWithAuthFile(t)
  1779. c.AttachmentFileSizeLimit = 20_000
  1780. c.VisitorAttachmentTotalSizeLimit = 40_000
  1781. s := newTestServer(t, c)
  1782. // Create tier with certain limits
  1783. require.Nil(t, s.userManager.AddTier(&user.Tier{
  1784. Code: "test",
  1785. MessageLimit: 100,
  1786. AttachmentFileSizeLimit: 50_000,
  1787. AttachmentTotalSizeLimit: 200_000,
  1788. AttachmentExpiryDuration: 30 * time.Second,
  1789. AttachmentBandwidthLimit: 1000000,
  1790. }))
  1791. require.Nil(t, s.userManager.AddUser("phil", "phil", user.RoleUser, false))
  1792. require.Nil(t, s.userManager.ChangeTier("phil", "test"))
  1793. // Publish small file as anonymous
  1794. response := request(t, s, "PUT", "/mytopic", smallFile, nil)
  1795. msg := toMessage(t, response.Body.String())
  1796. require.Contains(t, msg.Attachment.URL, "http://127.0.0.1:12345/file/")
  1797. require.FileExists(t, filepath.Join(s.config.AttachmentCacheDir, msg.ID))
  1798. // Publish large file as anonymous
  1799. response = request(t, s, "PUT", "/mytopic", largeFile, nil)
  1800. require.Equal(t, 413, response.Code)
  1801. require.Equal(t, 41301, toHTTPError(t, response.Body.String()).Code)
  1802. // Publish too large file as phil
  1803. response = request(t, s, "PUT", "/mytopic", largeFile+" a few more bytes", map[string]string{
  1804. "Authorization": util.BasicAuth("phil", "phil"),
  1805. })
  1806. require.Equal(t, 413, response.Code)
  1807. require.Equal(t, 41301, toHTTPError(t, response.Body.String()).Code)
  1808. // Publish large file as phil (4x)
  1809. for i := 0; i < 4; i++ {
  1810. response = request(t, s, "PUT", "/mytopic", largeFile, map[string]string{
  1811. "Authorization": util.BasicAuth("phil", "phil"),
  1812. })
  1813. require.Equal(t, 200, response.Code)
  1814. msg = toMessage(t, response.Body.String())
  1815. require.Contains(t, msg.Attachment.URL, "http://127.0.0.1:12345/file/")
  1816. require.FileExists(t, filepath.Join(s.config.AttachmentCacheDir, msg.ID))
  1817. }
  1818. response = request(t, s, "PUT", "/mytopic", largeFile, map[string]string{
  1819. "Authorization": util.BasicAuth("phil", "phil"),
  1820. })
  1821. require.Equal(t, 413, response.Code)
  1822. require.Equal(t, 41301, toHTTPError(t, response.Body.String()).Code)
  1823. }
  1824. func TestServer_PublishAttachmentBandwidthLimit(t *testing.T) {
  1825. content := util.RandomString(5000) // > 4096
  1826. c := newTestConfig(t)
  1827. c.VisitorAttachmentDailyBandwidthLimit = 5*5000 + 123 // A little more than 1 upload and 3 downloads
  1828. s := newTestServer(t, c)
  1829. // Publish attachment
  1830. response := request(t, s, "PUT", "/mytopic", content, nil)
  1831. msg := toMessage(t, response.Body.String())
  1832. require.Contains(t, msg.Attachment.URL, "http://127.0.0.1:12345/file/")
  1833. // Value it 4 times successfully
  1834. path := strings.TrimPrefix(msg.Attachment.URL, "http://127.0.0.1:12345")
  1835. for i := 1; i <= 4; i++ { // 4 successful downloads
  1836. response = request(t, s, "GET", path, "", nil)
  1837. require.Equal(t, 200, response.Code)
  1838. require.Equal(t, content, response.Body.String())
  1839. }
  1840. // And then fail with a 429
  1841. response = request(t, s, "GET", path, "", nil)
  1842. err := toHTTPError(t, response.Body.String())
  1843. require.Equal(t, 429, response.Code)
  1844. require.Equal(t, 42905, err.Code)
  1845. }
  1846. func TestServer_PublishAttachmentBandwidthLimitUploadOnly(t *testing.T) {
  1847. content := util.RandomString(5000) // > 4096
  1848. c := newTestConfig(t)
  1849. c.VisitorAttachmentDailyBandwidthLimit = 5*5000 + 500 // 5 successful uploads
  1850. s := newTestServer(t, c)
  1851. // 5 successful uploads
  1852. for i := 1; i <= 5; i++ {
  1853. response := request(t, s, "PUT", "/mytopic", content, nil)
  1854. msg := toMessage(t, response.Body.String())
  1855. require.Contains(t, msg.Attachment.URL, "http://127.0.0.1:12345/file/")
  1856. }
  1857. // And a failed one
  1858. response := request(t, s, "PUT", "/mytopic", content, nil)
  1859. err := toHTTPError(t, response.Body.String())
  1860. require.Equal(t, 413, response.Code)
  1861. require.Equal(t, 41301, err.Code)
  1862. }
  1863. func TestServer_PublishAttachmentAndImmediatelyGetItWithCacheTimeout(t *testing.T) {
  1864. // This tests the awkward util.Retry in handleFile: Due to the async persisting of messages,
  1865. // the message is not immediately available when attempting to download it.
  1866. c := newTestConfig(t)
  1867. c.CacheBatchTimeout = 500 * time.Millisecond
  1868. c.CacheBatchSize = 10
  1869. s := newTestServer(t, c)
  1870. content := "this is an ATTACHMENT"
  1871. rr := request(t, s, "PUT", "/mytopic?f=myfile.txt", content, nil)
  1872. m := toMessage(t, rr.Body.String())
  1873. require.Equal(t, "myfile.txt", m.Attachment.Name)
  1874. path := strings.TrimPrefix(m.Attachment.URL, "http://127.0.0.1:12345")
  1875. rr = request(t, s, "GET", path, "", nil)
  1876. require.Equal(t, 200, rr.Code) // Not 404!
  1877. require.Equal(t, content, rr.Body.String())
  1878. }
  1879. func TestServer_PublishAttachmentAccountStats(t *testing.T) {
  1880. content := util.RandomString(4999) // > 4096
  1881. c := newTestConfig(t)
  1882. c.AttachmentFileSizeLimit = 5000
  1883. c.VisitorAttachmentTotalSizeLimit = 6000
  1884. s := newTestServer(t, c)
  1885. // Upload one attachment
  1886. response := request(t, s, "PUT", "/mytopic", content, nil)
  1887. msg := toMessage(t, response.Body.String())
  1888. require.Contains(t, msg.Attachment.URL, "http://127.0.0.1:12345/file/")
  1889. // User stats
  1890. response = request(t, s, "GET", "/v1/account", "", nil)
  1891. require.Equal(t, 200, response.Code)
  1892. account, err := util.UnmarshalJSON[apiAccountResponse](io.NopCloser(response.Body))
  1893. require.Nil(t, err)
  1894. require.Equal(t, int64(5000), account.Limits.AttachmentFileSize)
  1895. require.Equal(t, int64(6000), account.Limits.AttachmentTotalSize)
  1896. require.Equal(t, int64(4999), account.Stats.AttachmentTotalSize)
  1897. require.Equal(t, int64(1001), account.Stats.AttachmentTotalSizeRemaining)
  1898. require.Equal(t, int64(1), account.Stats.Messages)
  1899. }
  1900. func TestServer_Visitor_XForwardedFor_None(t *testing.T) {
  1901. c := newTestConfig(t)
  1902. c.BehindProxy = true
  1903. s := newTestServer(t, c)
  1904. r, _ := http.NewRequest("GET", "/bla", nil)
  1905. r.RemoteAddr = "8.9.10.11:1234"
  1906. r.Header.Set("X-Forwarded-For", " ") // Spaces, not empty!
  1907. v, err := s.maybeAuthenticate(r)
  1908. require.Nil(t, err)
  1909. require.Equal(t, "8.9.10.11", v.ip.String())
  1910. }
  1911. func TestServer_Visitor_XForwardedFor_Single(t *testing.T) {
  1912. c := newTestConfig(t)
  1913. c.BehindProxy = true
  1914. s := newTestServer(t, c)
  1915. r, _ := http.NewRequest("GET", "/bla", nil)
  1916. r.RemoteAddr = "8.9.10.11:1234"
  1917. r.Header.Set("X-Forwarded-For", "1.1.1.1")
  1918. v, err := s.maybeAuthenticate(r)
  1919. require.Nil(t, err)
  1920. require.Equal(t, "1.1.1.1", v.ip.String())
  1921. }
  1922. func TestServer_Visitor_XForwardedFor_Multiple(t *testing.T) {
  1923. c := newTestConfig(t)
  1924. c.BehindProxy = true
  1925. s := newTestServer(t, c)
  1926. r, _ := http.NewRequest("GET", "/bla", nil)
  1927. r.RemoteAddr = "8.9.10.11:1234"
  1928. r.Header.Set("X-Forwarded-For", "1.2.3.4 , 2.4.4.2,234.5.2.1 ")
  1929. v, err := s.maybeAuthenticate(r)
  1930. require.Nil(t, err)
  1931. require.Equal(t, "234.5.2.1", v.ip.String())
  1932. }
  1933. func TestServer_Visitor_Custom_ClientIP_Header(t *testing.T) {
  1934. c := newTestConfig(t)
  1935. c.BehindProxy = true
  1936. c.ProxyForwardedHeader = "X-Client-IP"
  1937. s := newTestServer(t, c)
  1938. r, _ := http.NewRequest("GET", "/bla", nil)
  1939. r.RemoteAddr = "8.9.10.11:1234"
  1940. r.Header.Set("X-Client-IP", "1.2.3.4")
  1941. v, err := s.maybeAuthenticate(r)
  1942. require.Nil(t, err)
  1943. require.Equal(t, "1.2.3.4", v.ip.String())
  1944. }
  1945. func TestServer_Visitor_Custom_ClientIP_Header_IPv6(t *testing.T) {
  1946. c := newTestConfig(t)
  1947. c.BehindProxy = true
  1948. c.ProxyForwardedHeader = "X-Client-IP"
  1949. s := newTestServer(t, c)
  1950. r, _ := http.NewRequest("GET", "/bla", nil)
  1951. r.RemoteAddr = "[2001:db8:9999::1]:1234"
  1952. r.Header.Set("X-Client-IP", "2001:db8:7777::1")
  1953. v, err := s.maybeAuthenticate(r)
  1954. require.Nil(t, err)
  1955. require.Equal(t, "2001:db8:7777::1", v.ip.String())
  1956. }
  1957. func TestServer_Visitor_Custom_Forwarded_Header(t *testing.T) {
  1958. c := newTestConfig(t)
  1959. c.BehindProxy = true
  1960. c.ProxyForwardedHeader = "Forwarded"
  1961. c.ProxyTrustedAddresses = []string{"1.2.3.4"}
  1962. s := newTestServer(t, c)
  1963. r, _ := http.NewRequest("GET", "/bla", nil)
  1964. r.RemoteAddr = "8.9.10.11:1234"
  1965. r.Header.Set("Forwarded", " for=5.6.7.8, by=example.com;for=1.2.3.4")
  1966. v, err := s.maybeAuthenticate(r)
  1967. require.Nil(t, err)
  1968. require.Equal(t, "5.6.7.8", v.ip.String())
  1969. }
  1970. func TestServer_Visitor_Custom_Forwarded_Header_IPv6(t *testing.T) {
  1971. c := newTestConfig(t)
  1972. c.BehindProxy = true
  1973. c.ProxyForwardedHeader = "Forwarded"
  1974. c.ProxyTrustedAddresses = []string{"2001:db8:1111::1"}
  1975. s := newTestServer(t, c)
  1976. r, _ := http.NewRequest("GET", "/bla", nil)
  1977. r.RemoteAddr = "[2001:db8:2222::1]:1234"
  1978. r.Header.Set("Forwarded", " for=[2001:db8:1111::1], by=example.com;for=[2001:db8:3333::1]")
  1979. v, err := s.maybeAuthenticate(r)
  1980. require.Nil(t, err)
  1981. require.Equal(t, "2001:db8:3333::1", v.ip.String())
  1982. }
  1983. func TestServer_PublishWhileUpdatingStatsWithLotsOfMessages(t *testing.T) {
  1984. t.Parallel()
  1985. count := 50000
  1986. c := newTestConfig(t)
  1987. c.TotalTopicLimit = 50001
  1988. c.CacheStartupQueries = "pragma journal_mode = WAL; pragma synchronous = normal; pragma temp_store = memory;"
  1989. s := newTestServer(t, c)
  1990. // Add lots of messages
  1991. log.Info("Adding %d messages", count)
  1992. start := time.Now()
  1993. messages := make([]*message, 0)
  1994. for i := 0; i < count; i++ {
  1995. topicID := fmt.Sprintf("topic%d", i)
  1996. _, err := s.topicsFromIDs(topicID) // Add topic to internal s.topics array
  1997. require.Nil(t, err)
  1998. messages = append(messages, newDefaultMessage(topicID, "some message"))
  1999. }
  2000. require.Nil(t, s.messageCache.addMessages(messages))
  2001. log.Info("Done: Adding %d messages; took %s", count, time.Since(start).Round(time.Millisecond))
  2002. // Update stats
  2003. statsChan := make(chan bool)
  2004. go func() {
  2005. log.Info("Updating stats")
  2006. start := time.Now()
  2007. s.execManager()
  2008. log.Info("Done: Updating stats; took %s", time.Since(start).Round(time.Millisecond))
  2009. statsChan <- true
  2010. }()
  2011. time.Sleep(50 * time.Millisecond) // Make sure it starts first
  2012. // Publish message (during stats update)
  2013. log.Info("Publishing message")
  2014. start = time.Now()
  2015. response := request(t, s, "PUT", "/mytopic", "some body", nil)
  2016. m := toMessage(t, response.Body.String())
  2017. require.Equal(t, "some body", m.Message)
  2018. require.True(t, time.Since(start) < 100*time.Millisecond)
  2019. log.Info("Done: Publishing message; took %s", time.Since(start).Round(time.Millisecond))
  2020. // Wait for all goroutines
  2021. select {
  2022. case <-statsChan:
  2023. case <-time.After(10 * time.Second):
  2024. t.Fatal("Timed out waiting for Go routines")
  2025. }
  2026. log.Info("Done: Waiting for all locks")
  2027. }
  2028. func TestServer_AnonymousUser_And_NonTierUser_Are_Same_Visitor(t *testing.T) {
  2029. conf := newTestConfigWithAuthFile(t)
  2030. s := newTestServer(t, conf)
  2031. defer s.closeDatabases()
  2032. // Create user without tier
  2033. require.Nil(t, s.userManager.AddUser("phil", "phil", user.RoleUser, false))
  2034. // Publish a message (anonymous user)
  2035. rr := request(t, s, "POST", "/mytopic", "hi", nil)
  2036. require.Equal(t, 200, rr.Code)
  2037. // Publish a message (non-tier user)
  2038. rr = request(t, s, "POST", "/mytopic", "hi", map[string]string{
  2039. "Authorization": util.BasicAuth("phil", "phil"),
  2040. })
  2041. require.Equal(t, 200, rr.Code)
  2042. // User stats (anonymous user)
  2043. rr = request(t, s, "GET", "/v1/account", "", nil)
  2044. account, _ := util.UnmarshalJSON[apiAccountResponse](io.NopCloser(rr.Body))
  2045. require.Equal(t, int64(2), account.Stats.Messages)
  2046. // User stats (non-tier user)
  2047. rr = request(t, s, "GET", "/v1/account", "", map[string]string{
  2048. "Authorization": util.BasicAuth("phil", "phil"),
  2049. })
  2050. account, _ = util.UnmarshalJSON[apiAccountResponse](io.NopCloser(rr.Body))
  2051. require.Equal(t, int64(2), account.Stats.Messages)
  2052. }
  2053. func TestServer_SubscriberRateLimiting_Success(t *testing.T) {
  2054. c := newTestConfigWithAuthFile(t)
  2055. c.VisitorRequestLimitBurst = 3
  2056. c.VisitorSubscriberRateLimiting = true
  2057. s := newTestServer(t, c)
  2058. // "Register" visitor 1.2.3.4 to topic "upAAAAAAAAAAAA" as a rate limit visitor
  2059. subscriber1Fn := func(r *http.Request) {
  2060. r.RemoteAddr = "1.2.3.4:1234"
  2061. }
  2062. rr := request(t, s, "GET", "/upAAAAAAAAAAAA/json?poll=1", "", nil, subscriber1Fn)
  2063. require.Equal(t, 200, rr.Code)
  2064. require.Equal(t, "", rr.Body.String())
  2065. require.Equal(t, "1.2.3.4", s.topics["upAAAAAAAAAAAA"].rateVisitor.ip.String())
  2066. // "Register" visitor 8.7.7.1 to topic "up012345678912" as a rate limit visitor (implicitly via topic name)
  2067. subscriber2Fn := func(r *http.Request) {
  2068. r.RemoteAddr = "8.7.7.1:1234"
  2069. }
  2070. rr = request(t, s, "GET", "/up012345678912/json?poll=1", "", nil, subscriber2Fn)
  2071. require.Equal(t, 200, rr.Code)
  2072. require.Equal(t, "", rr.Body.String())
  2073. require.Equal(t, "8.7.7.1", s.topics["up012345678912"].rateVisitor.ip.String())
  2074. // Publish 2 messages to "subscriber1topic" as visitor 9.9.9.9. It'd be 3 normally, but the
  2075. // GET request before is also counted towards the request limiter.
  2076. for i := 0; i < 2; i++ {
  2077. rr := request(t, s, "PUT", "/upAAAAAAAAAAAA", "some message", nil)
  2078. require.Equal(t, 200, rr.Code)
  2079. }
  2080. rr = request(t, s, "PUT", "/upAAAAAAAAAAAA", "some message", nil)
  2081. require.Equal(t, 429, rr.Code)
  2082. // Publish another 2 messages to "up012345678912" as visitor 9.9.9.9
  2083. for i := 0; i < 2; i++ {
  2084. rr := request(t, s, "PUT", "/up012345678912", "some message", nil)
  2085. require.Equal(t, 200, rr.Code) // If we fail here, handlePublish is using the wrong visitor!
  2086. }
  2087. rr = request(t, s, "PUT", "/up012345678912", "some message", nil)
  2088. require.Equal(t, 429, rr.Code)
  2089. // Hurray! At this point, visitor 9.9.9.9 has published 4 messages, even though
  2090. // VisitorRequestLimitBurst is 3. That means it's working.
  2091. // Now let's confirm that so far we haven't used up any of visitor 9.9.9.9's request limiter
  2092. // by publishing another 3 requests from it.
  2093. for i := 0; i < 3; i++ {
  2094. rr := request(t, s, "PUT", "/some-other-topic", "some message", nil)
  2095. require.Equal(t, 200, rr.Code)
  2096. }
  2097. rr = request(t, s, "PUT", "/some-other-topic", "some message", nil)
  2098. require.Equal(t, 429, rr.Code)
  2099. }
  2100. func TestServer_SubscriberRateLimiting_NotWrongTopic(t *testing.T) {
  2101. c := newTestConfigWithAuthFile(t)
  2102. c.VisitorSubscriberRateLimiting = true
  2103. s := newTestServer(t, c)
  2104. subscriberFn := func(r *http.Request) {
  2105. r.RemoteAddr = "1.2.3.4:1234"
  2106. }
  2107. rr := request(t, s, "GET", "/alerts,upAAAAAAAAAAAA,upBBBBBBBBBBBB/json?poll=1", "", nil, subscriberFn)
  2108. require.Equal(t, 200, rr.Code)
  2109. require.Equal(t, "", rr.Body.String())
  2110. require.Nil(t, s.topics["alerts"].rateVisitor)
  2111. require.Equal(t, "1.2.3.4", s.topics["upAAAAAAAAAAAA"].rateVisitor.ip.String())
  2112. require.Equal(t, "1.2.3.4", s.topics["upBBBBBBBBBBBB"].rateVisitor.ip.String())
  2113. }
  2114. func TestServer_SubscriberRateLimiting_NotEnabled_Failed(t *testing.T) {
  2115. c := newTestConfigWithAuthFile(t)
  2116. c.VisitorRequestLimitBurst = 3
  2117. c.VisitorSubscriberRateLimiting = false
  2118. s := newTestServer(t, c)
  2119. // Subscriber rate limiting is disabled!
  2120. // Registering visitor 1.2.3.4 to topic has no effect
  2121. rr := request(t, s, "GET", "/upAAAAAAAAAAAA/json?poll=1", "", nil, func(r *http.Request) {
  2122. r.RemoteAddr = "1.2.3.4:1234"
  2123. })
  2124. require.Equal(t, 200, rr.Code)
  2125. require.Equal(t, "", rr.Body.String())
  2126. require.Nil(t, s.topics["upAAAAAAAAAAAA"].rateVisitor)
  2127. // Registering visitor 8.7.7.1 to topic has no effect
  2128. rr = request(t, s, "GET", "/up012345678912/json?poll=1", "", nil, func(r *http.Request) {
  2129. r.RemoteAddr = "8.7.7.1:1234"
  2130. })
  2131. require.Equal(t, 200, rr.Code)
  2132. require.Equal(t, "", rr.Body.String())
  2133. require.Nil(t, s.topics["up012345678912"].rateVisitor)
  2134. // Publish 3 messages to "upAAAAAAAAAAAA" as visitor 9.9.9.9
  2135. for i := 0; i < 3; i++ {
  2136. rr := request(t, s, "PUT", "/subscriber1topic", "some message", nil)
  2137. require.Equal(t, 200, rr.Code)
  2138. }
  2139. rr = request(t, s, "PUT", "/subscriber1topic", "some message", nil)
  2140. require.Equal(t, 429, rr.Code)
  2141. rr = request(t, s, "PUT", "/up012345678912", "some message", nil)
  2142. require.Equal(t, 429, rr.Code)
  2143. }
  2144. func TestServer_SubscriberRateLimiting_UP_Only(t *testing.T) {
  2145. c := newTestConfigWithAuthFile(t)
  2146. c.VisitorRequestLimitBurst = 3
  2147. c.VisitorSubscriberRateLimiting = true
  2148. s := newTestServer(t, c)
  2149. // "Register" 5 different UnifiedPush visitors
  2150. for i := 0; i < 5; i++ {
  2151. subscriberFn := func(r *http.Request) {
  2152. r.RemoteAddr = fmt.Sprintf("1.2.3.%d:1234", i+1)
  2153. }
  2154. rr := request(t, s, "GET", fmt.Sprintf("/up12345678901%d/json?poll=1", i), "", nil, subscriberFn)
  2155. require.Equal(t, 200, rr.Code)
  2156. }
  2157. // Publish 2 messages per topic
  2158. for i := 0; i < 5; i++ {
  2159. for j := 0; j < 2; j++ {
  2160. rr := request(t, s, "PUT", fmt.Sprintf("/up12345678901%d?up=1", i), "some message", nil)
  2161. require.Equal(t, 200, rr.Code)
  2162. }
  2163. }
  2164. }
  2165. func TestServer_Matrix_SubscriberRateLimiting_UP_Only(t *testing.T) {
  2166. c := newTestConfig(t)
  2167. c.VisitorRequestLimitBurst = 3
  2168. c.VisitorSubscriberRateLimiting = true
  2169. s := newTestServer(t, c)
  2170. // "Register" 5 different UnifiedPush visitors
  2171. for i := 0; i < 5; i++ {
  2172. rr := request(t, s, "GET", fmt.Sprintf("/up12345678901%d/json?poll=1", i), "", nil, func(r *http.Request) {
  2173. r.RemoteAddr = fmt.Sprintf("1.2.3.%d:1234", i+1)
  2174. })
  2175. require.Equal(t, 200, rr.Code)
  2176. }
  2177. // Publish 2 messages per topic
  2178. for i := 0; i < 5; i++ {
  2179. notification := fmt.Sprintf(`{"notification":{"devices":[{"pushkey":"http://127.0.0.1:12345/up12345678901%d?up=1"}]}}`, i)
  2180. for j := 0; j < 2; j++ {
  2181. response := request(t, s, "POST", "/_matrix/push/v1/notify", notification, nil)
  2182. require.Equal(t, 200, response.Code)
  2183. require.Equal(t, `{"rejected":[]}`+"\n", response.Body.String())
  2184. }
  2185. response := request(t, s, "POST", "/_matrix/push/v1/notify", notification, nil)
  2186. require.Equal(t, 429, response.Code, notification)
  2187. require.Equal(t, 42901, toHTTPError(t, response.Body.String()).Code)
  2188. }
  2189. }
  2190. func TestServer_SubscriberRateLimiting_VisitorExpiration(t *testing.T) {
  2191. c := newTestConfig(t)
  2192. c.VisitorRequestLimitBurst = 3
  2193. c.VisitorSubscriberRateLimiting = true
  2194. s := newTestServer(t, c)
  2195. // "Register" rate visitor
  2196. subscriberFn := func(r *http.Request) {
  2197. r.RemoteAddr = "1.2.3.4:1234"
  2198. }
  2199. rr := request(t, s, "GET", "/upAAAAAAAAAAAA/json?poll=1", "", nil, subscriberFn)
  2200. require.Equal(t, 200, rr.Code)
  2201. require.Equal(t, "1.2.3.4", s.topics["upAAAAAAAAAAAA"].rateVisitor.ip.String())
  2202. require.Equal(t, s.visitors["ip:1.2.3.4"], s.topics["upAAAAAAAAAAAA"].rateVisitor)
  2203. // Publish message, observe rate visitor tokens being decreased
  2204. response := request(t, s, "POST", "/upAAAAAAAAAAAA", "some message", nil)
  2205. require.Equal(t, 200, response.Code)
  2206. require.Equal(t, int64(0), s.visitors["ip:9.9.9.9"].messagesLimiter.Value())
  2207. require.Equal(t, int64(1), s.topics["upAAAAAAAAAAAA"].rateVisitor.messagesLimiter.Value())
  2208. require.Equal(t, s.visitors["ip:1.2.3.4"], s.topics["upAAAAAAAAAAAA"].rateVisitor)
  2209. // Expire visitor
  2210. s.visitors["ip:1.2.3.4"].seen = time.Now().Add(-1 * 25 * time.Hour)
  2211. s.pruneVisitors()
  2212. // Publish message again, observe that rateVisitor is not used anymore and is reset
  2213. response = request(t, s, "POST", "/upAAAAAAAAAAAA", "some message", nil)
  2214. require.Equal(t, 200, response.Code)
  2215. require.Equal(t, int64(1), s.visitors["ip:9.9.9.9"].messagesLimiter.Value())
  2216. require.Nil(t, s.topics["upAAAAAAAAAAAA"].rateVisitor)
  2217. require.Nil(t, s.visitors["ip:1.2.3.4"])
  2218. }
  2219. func TestServer_SubscriberRateLimiting_ProtectedTopics_WithDefaultReadWrite(t *testing.T) {
  2220. c := newTestConfigWithAuthFile(t)
  2221. c.AuthDefault = user.PermissionReadWrite
  2222. c.VisitorSubscriberRateLimiting = true
  2223. s := newTestServer(t, c)
  2224. // Create some ACLs
  2225. require.Nil(t, s.userManager.AllowAccess(user.Everyone, "announcements", user.PermissionRead))
  2226. // Set rate visitor as ip:1.2.3.4 on topic
  2227. // - "up123456789012": Allowed, because no ACLs and nobody owns the topic
  2228. // - "announcements": NOT allowed, because it has read-only permissions for everyone
  2229. rr := request(t, s, "GET", "/up123456789012,announcements/json?poll=1", "", nil, func(r *http.Request) {
  2230. r.RemoteAddr = "1.2.3.4:1234"
  2231. })
  2232. require.Equal(t, 200, rr.Code)
  2233. require.Equal(t, "1.2.3.4", s.topics["up123456789012"].rateVisitor.ip.String())
  2234. require.Nil(t, s.topics["announcements"].rateVisitor)
  2235. }
  2236. func TestServer_MessageHistoryAndStatsEndpoint(t *testing.T) {
  2237. c := newTestConfig(t)
  2238. c.ManagerInterval = 2 * time.Second
  2239. s := newTestServer(t, c)
  2240. // Publish some messages, and get stats
  2241. for i := 0; i < 5; i++ {
  2242. response := request(t, s, "POST", "/mytopic", "some message", nil)
  2243. require.Equal(t, 200, response.Code)
  2244. }
  2245. require.Equal(t, int64(5), s.messages)
  2246. require.Equal(t, []int64{0}, s.messagesHistory)
  2247. response := request(t, s, "GET", "/v1/stats", "", nil)
  2248. require.Equal(t, 200, response.Code)
  2249. require.Equal(t, `{"messages":5,"messages_rate":0}`+"\n", response.Body.String())
  2250. // Run manager and see message history update
  2251. s.execManager()
  2252. require.Equal(t, []int64{0, 5}, s.messagesHistory)
  2253. response = request(t, s, "GET", "/v1/stats", "", nil)
  2254. require.Equal(t, 200, response.Code)
  2255. require.Equal(t, `{"messages":5,"messages_rate":2.5}`+"\n", response.Body.String()) // 5 messages in 2 seconds = 2.5 messages per second
  2256. // Publish some more messages
  2257. for i := 0; i < 10; i++ {
  2258. response := request(t, s, "POST", "/mytopic", "some message", nil)
  2259. require.Equal(t, 200, response.Code)
  2260. }
  2261. require.Equal(t, int64(15), s.messages)
  2262. require.Equal(t, []int64{0, 5}, s.messagesHistory)
  2263. response = request(t, s, "GET", "/v1/stats", "", nil)
  2264. require.Equal(t, 200, response.Code)
  2265. require.Equal(t, `{"messages":15,"messages_rate":2.5}`+"\n", response.Body.String()) // Rate did not update yet
  2266. // Run manager and see message history update
  2267. s.execManager()
  2268. require.Equal(t, []int64{0, 5, 15}, s.messagesHistory)
  2269. response = request(t, s, "GET", "/v1/stats", "", nil)
  2270. require.Equal(t, 200, response.Code)
  2271. require.Equal(t, `{"messages":15,"messages_rate":3.75}`+"\n", response.Body.String()) // 15 messages in 4 seconds = 3.75 messages per second
  2272. }
  2273. func TestServer_MessageHistoryMaxSize(t *testing.T) {
  2274. t.Parallel()
  2275. s := newTestServer(t, newTestConfig(t))
  2276. for i := 0; i < 20; i++ {
  2277. s.messages = int64(i)
  2278. s.execManager()
  2279. }
  2280. require.Equal(t, []int64{10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, s.messagesHistory)
  2281. }
  2282. func TestServer_MessageCountPersistence(t *testing.T) {
  2283. t.Parallel()
  2284. c := newTestConfig(t)
  2285. s := newTestServer(t, c)
  2286. s.messages = 1234
  2287. s.execManager()
  2288. waitFor(t, func() bool {
  2289. messages, err := s.messageCache.Stats()
  2290. require.Nil(t, err)
  2291. return messages == 1234
  2292. })
  2293. s = newTestServer(t, c)
  2294. require.Equal(t, int64(1234), s.messages)
  2295. }
  2296. func TestServer_PublishWithUTF8MimeHeader(t *testing.T) {
  2297. t.Parallel()
  2298. s := newTestServer(t, newTestConfig(t))
  2299. response := request(t, s, "POST", "/mytopic", "some attachment", map[string]string{
  2300. "X-Filename": "some =?UTF-8?q?=C3=A4?=ttachment.txt",
  2301. "X-Message": "=?UTF-8?B?8J+HqfCfh6o=?=",
  2302. "X-Title": "=?UTF-8?B?bnRmeSDlvojmo5I=?=, no really I mean it! =?UTF-8?Q?This is q=C3=BC=C3=B6ted-print=C3=A4ble.?=",
  2303. "X-Tags": "=?UTF-8?B?8J+HqfCfh6o=?=, =?UTF-8?B?bnRmeSDlvojmo5I=?=",
  2304. "X-Click": "=?uTf-8?b?aHR0cHM6Ly/wn5KpLmxh?=",
  2305. "X-Actions": "http, \"=?utf-8?q?Mettre =C3=A0 jour?=\", \"https://my.tld/webhook/netbird-update\"; =?utf-8?b?aHR0cCwg6L+Z5piv5LiA5Liq5qCH562+LCBodHRwczovL/CfkqkubGE=?=",
  2306. })
  2307. require.Equal(t, 200, response.Code)
  2308. m := toMessage(t, response.Body.String())
  2309. require.Equal(t, "🇩🇪", m.Message)
  2310. require.Equal(t, "ntfy 很棒, no really I mean it! This is qüöted-printäble.", m.Title)
  2311. require.Equal(t, "some ättachment.txt", m.Attachment.Name)
  2312. require.Equal(t, "🇩🇪", m.Tags[0])
  2313. require.Equal(t, "ntfy 很棒", m.Tags[1])
  2314. require.Equal(t, "https://💩.la", m.Click)
  2315. require.Equal(t, "Mettre à jour", m.Actions[0].Label)
  2316. require.Equal(t, "http", m.Actions[1].Action)
  2317. require.Equal(t, "这是一个标签", m.Actions[1].Label)
  2318. require.Equal(t, "https://💩.la", m.Actions[1].URL)
  2319. }
  2320. func TestServer_UpstreamBaseURL_Success(t *testing.T) {
  2321. t.Parallel()
  2322. var pollID atomic.Pointer[string]
  2323. upstreamServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  2324. body, err := io.ReadAll(r.Body)
  2325. require.Nil(t, err)
  2326. require.Equal(t, "/87c9cddf7b0105f5fe849bf084c6e600be0fde99be3223335199b4965bd7b735", r.URL.Path)
  2327. require.Equal(t, "", string(body))
  2328. require.NotEmpty(t, r.Header.Get("X-Poll-ID"))
  2329. pollID.Store(util.String(r.Header.Get("X-Poll-ID")))
  2330. }))
  2331. defer upstreamServer.Close()
  2332. c := newTestConfigWithAuthFile(t)
  2333. c.BaseURL = "http://myserver.internal"
  2334. c.UpstreamBaseURL = upstreamServer.URL
  2335. s := newTestServer(t, c)
  2336. // Send message, and wait for upstream server to receive it
  2337. response := request(t, s, "PUT", "/mytopic", `hi there`, nil)
  2338. require.Equal(t, 200, response.Code)
  2339. m := toMessage(t, response.Body.String())
  2340. require.NotEmpty(t, m.ID)
  2341. require.Equal(t, "hi there", m.Message)
  2342. waitFor(t, func() bool {
  2343. pID := pollID.Load()
  2344. return pID != nil && *pID == m.ID
  2345. })
  2346. }
  2347. func TestServer_UpstreamBaseURL_With_Access_Token_Success(t *testing.T) {
  2348. t.Parallel()
  2349. var pollID atomic.Pointer[string]
  2350. upstreamServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  2351. body, err := io.ReadAll(r.Body)
  2352. require.Nil(t, err)
  2353. require.Equal(t, "/a1c72bcb4daf5af54d13ef86aea8f76c11e8b88320d55f1811d5d7b173bcc1df", r.URL.Path)
  2354. require.Equal(t, "Bearer tk_1234567890", r.Header.Get("Authorization"))
  2355. require.Equal(t, "", string(body))
  2356. require.NotEmpty(t, r.Header.Get("X-Poll-ID"))
  2357. pollID.Store(util.String(r.Header.Get("X-Poll-ID")))
  2358. }))
  2359. defer upstreamServer.Close()
  2360. c := newTestConfigWithAuthFile(t)
  2361. c.BaseURL = "http://myserver.internal"
  2362. c.UpstreamBaseURL = upstreamServer.URL
  2363. c.UpstreamAccessToken = "tk_1234567890"
  2364. s := newTestServer(t, c)
  2365. // Send message, and wait for upstream server to receive it
  2366. response := request(t, s, "PUT", "/mytopic1", `hi there`, nil)
  2367. require.Equal(t, 200, response.Code)
  2368. m := toMessage(t, response.Body.String())
  2369. require.NotEmpty(t, m.ID)
  2370. require.Equal(t, "hi there", m.Message)
  2371. waitFor(t, func() bool {
  2372. pID := pollID.Load()
  2373. return pID != nil && *pID == m.ID
  2374. })
  2375. }
  2376. func TestServer_UpstreamBaseURL_DoNotForwardUnifiedPush(t *testing.T) {
  2377. t.Parallel()
  2378. upstreamServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  2379. t.Fatal("UnifiedPush messages should not be forwarded")
  2380. }))
  2381. defer upstreamServer.Close()
  2382. c := newTestConfigWithAuthFile(t)
  2383. c.BaseURL = "http://myserver.internal"
  2384. c.UpstreamBaseURL = upstreamServer.URL
  2385. s := newTestServer(t, c)
  2386. // Send UP message, this should not forward to upstream server
  2387. response := request(t, s, "PUT", "/mytopic?up=1", `hi there`, nil)
  2388. require.Equal(t, 200, response.Code)
  2389. m := toMessage(t, response.Body.String())
  2390. require.NotEmpty(t, m.ID)
  2391. require.Equal(t, "hi there", m.Message)
  2392. // Forwarding is done asynchronously, so wait a bit.
  2393. // This ensures that the t.Fatal above is actually not triggered.
  2394. time.Sleep(500 * time.Millisecond)
  2395. }
  2396. func TestServer_MessageTemplate(t *testing.T) {
  2397. t.Parallel()
  2398. s := newTestServer(t, newTestConfig(t))
  2399. response := request(t, s, "PUT", "/mytopic", `{"foo":"bar", "nested":{"title":"here"}}`, map[string]string{
  2400. "X-Message": "{{.foo}}",
  2401. "X-Title": "{{.nested.title}}",
  2402. "X-Template": "1",
  2403. })
  2404. require.Equal(t, 200, response.Code)
  2405. m := toMessage(t, response.Body.String())
  2406. require.Equal(t, "bar", m.Message)
  2407. require.Equal(t, "here", m.Title)
  2408. }
  2409. func TestServer_MessageTemplate_RepeatPlaceholder(t *testing.T) {
  2410. t.Parallel()
  2411. s := newTestServer(t, newTestConfig(t))
  2412. response := request(t, s, "PUT", "/mytopic", `{"foo":"bar", "nested":{"title":"here"}}`, map[string]string{
  2413. "Message": "{{.foo}} is {{.foo}}",
  2414. "Title": "{{.nested.title}} is {{.nested.title}}",
  2415. "Template": "1",
  2416. })
  2417. require.Equal(t, 200, response.Code)
  2418. m := toMessage(t, response.Body.String())
  2419. require.Equal(t, "bar is bar", m.Message)
  2420. require.Equal(t, "here is here", m.Title)
  2421. }
  2422. func TestServer_MessageTemplate_JSONBody(t *testing.T) {
  2423. t.Parallel()
  2424. s := newTestServer(t, newTestConfig(t))
  2425. body := `{"topic": "mytopic", "message": "{\"foo\":\"bar\",\"nested\":{\"title\":\"here\"}}"}`
  2426. response := request(t, s, "PUT", "/", body, map[string]string{
  2427. "m": "{{.foo}}",
  2428. "t": "{{.nested.title}}",
  2429. "tpl": "1",
  2430. })
  2431. require.Equal(t, 200, response.Code)
  2432. m := toMessage(t, response.Body.String())
  2433. require.Equal(t, "bar", m.Message)
  2434. require.Equal(t, "here", m.Title)
  2435. }
  2436. func TestServer_MessageTemplate_MalformedJSONBody(t *testing.T) {
  2437. t.Parallel()
  2438. s := newTestServer(t, newTestConfig(t))
  2439. body := `{"topic": "mytopic", "message": "{\"foo\":\"bar\",\"nested\":{\"title\":\"here\"INVALID"}`
  2440. response := request(t, s, "PUT", "/", body, map[string]string{
  2441. "X-Message": "{{.foo}}",
  2442. "X-Title": "{{.nested.title}}",
  2443. "X-Template": "1",
  2444. })
  2445. require.Equal(t, 400, response.Code)
  2446. require.Equal(t, 40042, toHTTPError(t, response.Body.String()).Code)
  2447. }
  2448. func TestServer_MessageTemplate_PlaceholderTypo(t *testing.T) {
  2449. t.Parallel()
  2450. s := newTestServer(t, newTestConfig(t))
  2451. response := request(t, s, "PUT", "/mytopic", `{"foo":"bar", "nested":{"title":"here"}}`, map[string]string{
  2452. "X-Message": "{{.food}}",
  2453. "X-Title": "{{.neste.title}}",
  2454. "X-Template": "1",
  2455. })
  2456. require.Equal(t, 200, response.Code)
  2457. m := toMessage(t, response.Body.String())
  2458. require.Equal(t, "<no value>", m.Message)
  2459. require.Equal(t, "<no value>", m.Title)
  2460. }
  2461. func TestServer_MessageTemplate_MultiplePlaceholders(t *testing.T) {
  2462. t.Parallel()
  2463. s := newTestServer(t, newTestConfig(t))
  2464. response := request(t, s, "PUT", "/mytopic", `{"foo":"bar", "nested":{"title":"here"}}`, map[string]string{
  2465. "X-Message": "{{.foo}} is {{.nested.title}}",
  2466. "X-Template": "1",
  2467. })
  2468. require.Equal(t, 200, response.Code)
  2469. m := toMessage(t, response.Body.String())
  2470. require.Equal(t, "bar is here", m.Message)
  2471. }
  2472. func TestServer_MessageTemplate_Range(t *testing.T) {
  2473. t.Parallel()
  2474. s := newTestServer(t, newTestConfig(t))
  2475. jsonBody := `{"foo": "bar", "errors": [{"level": "severe", "url": "https://severe1.com"},{"level": "warning", "url": "https://warning.com"},{"level": "severe", "url": "https://severe2.com"}]}`
  2476. response := request(t, s, "PUT", "/mytopic", jsonBody, map[string]string{
  2477. "X-Message": `Severe URLs:\n{{range .errors}}{{if eq .level "severe"}}- {{.url}}\n{{end}}{{end}}`,
  2478. "X-Template": "1",
  2479. })
  2480. require.Equal(t, 200, response.Code)
  2481. m := toMessage(t, response.Body.String())
  2482. require.Equal(t, "Severe URLs:\n- https://severe1.com\n- https://severe2.com\n", m.Message)
  2483. }
  2484. func TestServer_MessageTemplate_ExceedMessageSize_TemplatedMessageOK(t *testing.T) {
  2485. t.Parallel()
  2486. c := newTestConfig(t)
  2487. c.MessageSizeLimit = 25 // 25 < len(HTTP body) < 32k, and len(m.Message) < 25
  2488. s := newTestServer(t, c)
  2489. response := request(t, s, "PUT", "/mytopic", `{"foo":"bar", "nested":{"title":"here"}}`, map[string]string{
  2490. "X-Message": "{{.foo}}",
  2491. "X-Title": "{{.nested.title}}",
  2492. "X-Template": "yes",
  2493. })
  2494. require.Equal(t, 200, response.Code)
  2495. m := toMessage(t, response.Body.String())
  2496. require.Equal(t, "bar", m.Message)
  2497. require.Equal(t, "here", m.Title)
  2498. }
  2499. func TestServer_MessageTemplate_ExceedMessageSize_TemplatedMessageTooLong(t *testing.T) {
  2500. t.Parallel()
  2501. c := newTestConfig(t)
  2502. c.MessageSizeLimit = 21 // 21 < len(HTTP body) < 32k, but !len(m.Message) < 21
  2503. s := newTestServer(t, c)
  2504. response := request(t, s, "PUT", "/mytopic", `{"foo":"This is a long message"}`, map[string]string{
  2505. "X-Message": "{{.foo}}",
  2506. "X-Template": "1",
  2507. })
  2508. require.Equal(t, 400, response.Code)
  2509. require.Equal(t, 40041, toHTTPError(t, response.Body.String()).Code)
  2510. }
  2511. func TestServer_MessageTemplate_Grafana(t *testing.T) {
  2512. t.Parallel()
  2513. s := newTestServer(t, newTestConfig(t))
  2514. body := `{"receiver":"ntfy\\.example\\.com/alerts","status":"resolved","alerts":[{"status":"resolved","labels":{"alertname":"Load avg 15m too high","grafana_folder":"Node alerts","instance":"10.108.0.2:9100","job":"node-exporter"},"annotations":{"summary":"15m load average too high"},"startsAt":"2024-03-15T02:28:00Z","endsAt":"2024-03-15T02:42:00Z","generatorURL":"localhost:3000/alerting/grafana/NW9oDw-4z/view","fingerprint":"becbfb94bd81ef48","silenceURL":"localhost:3000/alerting/silence/new?alertmanager=grafana&matcher=alertname%3DLoad+avg+15m+too+high&matcher=grafana_folder%3DNode+alerts&matcher=instance%3D10.108.0.2%3A9100&matcher=job%3Dnode-exporter","dashboardURL":"","panelURL":"","values":{"B":18.98211314475876,"C":0},"valueString":"[ var='B' labels={__name__=node_load15, instance=10.108.0.2:9100, job=node-exporter} value=18.98211314475876 ], [ var='C' labels={__name__=node_load15, instance=10.108.0.2:9100, job=node-exporter} value=0 ]"}],"groupLabels":{"alertname":"Load avg 15m too high","grafana_folder":"Node alerts"},"commonLabels":{"alertname":"Load avg 15m too high","grafana_folder":"Node alerts","instance":"10.108.0.2:9100","job":"node-exporter"},"commonAnnotations":{"summary":"15m load average too high"},"externalURL":"localhost:3000/","version":"1","groupKey":"{}:{alertname=\"Load avg 15m too high\", grafana_folder=\"Node alerts\"}","truncatedAlerts":0,"orgId":1,"title":"[RESOLVED] Load avg 15m too high Node alerts (10.108.0.2:9100 node-exporter)","state":"ok","message":"**Resolved**\n\nValue: B=18.98211314475876, C=0\nLabels:\n - alertname = Load avg 15m too high\n - grafana_folder = Node alerts\n - instance = 10.108.0.2:9100\n - job = node-exporter\nAnnotations:\n - summary = 15m load average too high\nSource: localhost:3000/alerting/grafana/NW9oDw-4z/view\nSilence: localhost:3000/alerting/silence/new?alertmanager=grafana&matcher=alertname%3DLoad+avg+15m+too+high&matcher=grafana_folder%3DNode+alerts&matcher=instance%3D10.108.0.2%3A9100&matcher=job%3Dnode-exporter\n"}`
  2515. response := request(t, s, "PUT", "/mytopic?tpl=yes&title=Grafana+alert:+{{.title}}&message={{.message}}", body, nil)
  2516. require.Equal(t, 200, response.Code)
  2517. m := toMessage(t, response.Body.String())
  2518. require.Equal(t, "Grafana alert: [RESOLVED] Load avg 15m too high Node alerts (10.108.0.2:9100 node-exporter)", m.Title)
  2519. require.Equal(t, `**Resolved**
  2520. Value: B=18.98211314475876, C=0
  2521. Labels:
  2522. - alertname = Load avg 15m too high
  2523. - grafana_folder = Node alerts
  2524. - instance = 10.108.0.2:9100
  2525. - job = node-exporter
  2526. Annotations:
  2527. - summary = 15m load average too high
  2528. Source: localhost:3000/alerting/grafana/NW9oDw-4z/view
  2529. Silence: localhost:3000/alerting/silence/new?alertmanager=grafana&matcher=alertname%3DLoad+avg+15m+too+high&matcher=grafana_folder%3DNode+alerts&matcher=instance%3D10.108.0.2%3A9100&matcher=job%3Dnode-exporter
  2530. `, m.Message)
  2531. }
  2532. func TestServer_MessageTemplate_GitHub(t *testing.T) {
  2533. t.Parallel()
  2534. s := newTestServer(t, newTestConfig(t))
  2535. body := `{"action":"opened","number":1,"pull_request":{"url":"https://api.github.com/repos/binwiederhier/dabble/pulls/1","id":1783420972,"node_id":"PR_kwDOHAbdo85qTNgs","html_url":"https://github.com/binwiederhier/dabble/pull/1","diff_url":"https://github.com/binwiederhier/dabble/pull/1.diff","patch_url":"https://github.com/binwiederhier/dabble/pull/1.patch","issue_url":"https://api.github.com/repos/binwiederhier/dabble/issues/1","number":1,"state":"open","locked":false,"title":"A sample PR from Phil","user":{"login":"binwiederhier","id":664597,"node_id":"MDQ6VXNlcjY2NDU5Nw==","avatar_url":"https://avatars.githubusercontent.com/u/664597?v=4","gravatar_id":"","url":"https://api.github.com/users/binwiederhier","html_url":"https://github.com/binwiederhier","followers_url":"https://api.github.com/users/binwiederhier/followers","following_url":"https://api.github.com/users/binwiederhier/following{/other_user}","gists_url":"https://api.github.com/users/binwiederhier/gists{/gist_id}","starred_url":"https://api.github.com/users/binwiederhier/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/binwiederhier/subscriptions","organizations_url":"https://api.github.com/users/binwiederhier/orgs","repos_url":"https://api.github.com/users/binwiederhier/repos","events_url":"https://api.github.com/users/binwiederhier/events{/privacy}","received_events_url":"https://api.github.com/users/binwiederhier/received_events","type":"User","site_admin":false},"body":null,"created_at":"2024-03-21T02:52:09Z","updated_at":"2024-03-21T02:52:09Z","closed_at":null,"merged_at":null,"merge_commit_sha":null,"assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"draft":false,"commits_url":"https://api.github.com/repos/binwiederhier/dabble/pulls/1/commits","review_comments_url":"https://api.github.com/repos/binwiederhier/dabble/pulls/1/comments","review_comment_url":"https://api.github.com/repos/binwiederhier/dabble/pulls/comments{/number}","comments_url":"https://api.github.com/repos/binwiederhier/dabble/issues/1/comments","statuses_url":"https://api.github.com/repos/binwiederhier/dabble/statuses/5703842cc5715ed1e358d23ebb693db09747ae9b","head":{"label":"binwiederhier:aa","ref":"aa","sha":"5703842cc5715ed1e358d23ebb693db09747ae9b","user":{"login":"binwiederhier","id":664597,"node_id":"MDQ6VXNlcjY2NDU5Nw==","avatar_url":"https://avatars.githubusercontent.com/u/664597?v=4","gravatar_id":"","url":"https://api.github.com/users/binwiederhier","html_url":"https://github.com/binwiederhier","followers_url":"https://api.github.com/users/binwiederhier/followers","following_url":"https://api.github.com/users/binwiederhier/following{/other_user}","gists_url":"https://api.github.com/users/binwiederhier/gists{/gist_id}","starred_url":"https://api.github.com/users/binwiederhier/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/binwiederhier/subscriptions","organizations_url":"https://api.github.com/users/binwiederhier/orgs","repos_url":"https://api.github.com/users/binwiederhier/repos","events_url":"https://api.github.com/users/binwiederhier/events{/privacy}","received_events_url":"https://api.github.com/users/binwiederhier/received_events","type":"User","site_admin":false},"repo":{"id":470212003,"node_id":"R_kgDOHAbdow","name":"dabble","full_name":"binwiederhier/dabble","private":false,"owner":{"login":"binwiederhier","id":664597,"node_id":"MDQ6VXNlcjY2NDU5Nw==","avatar_url":"https://avatars.githubusercontent.com/u/664597?v=4","gravatar_id":"","url":"https://api.github.com/users/binwiederhier","html_url":"https://github.com/binwiederhier","followers_url":"https://api.github.com/users/binwiederhier/followers","following_url":"https://api.github.com/users/binwiederhier/following{/other_user}","gists_url":"https://api.github.com/users/binwiederhier/gists{/gist_id}","starred_url":"https://api.github.com/users/binwiederhier/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/binwiederhier/subscriptions","organizations_url":"https://api.github.com/users/binwiederhier/orgs","repos_url":"https://api.github.com/users/binwiederhier/repos","events_url":"https://api.github.com/users/binwiederhier/events{/privacy}","received_events_url":"https://api.github.com/users/binwiederhier/received_events","type":"User","site_admin":false},"html_url":"https://github.com/binwiederhier/dabble","description":"A repo for dabbling","fork":false,"url":"https://api.github.com/repos/binwiederhier/dabble","forks_url":"https://api.github.com/repos/binwiederhier/dabble/forks","keys_url":"https://api.github.com/repos/binwiederhier/dabble/keys{/key_id}","collaborators_url":"https://api.github.com/repos/binwiederhier/dabble/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/binwiederhier/dabble/teams","hooks_url":"https://api.github.com/repos/binwiederhier/dabble/hooks","issue_events_url":"https://api.github.com/repos/binwiederhier/dabble/issues/events{/number}","events_url":"https://api.github.com/repos/binwiederhier/dabble/events","assignees_url":"https://api.github.com/repos/binwiederhier/dabble/assignees{/user}","branches_url":"https://api.github.com/repos/binwiederhier/dabble/branches{/branch}","tags_url":"https://api.github.com/repos/binwiederhier/dabble/tags","blobs_url":"https://api.github.com/repos/binwiederhier/dabble/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/binwiederhier/dabble/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/binwiederhier/dabble/git/refs{/sha}","trees_url":"https://api.github.com/repos/binwiederhier/dabble/git/trees{/sha}","statuses_url":"https://api.github.com/repos/binwiederhier/dabble/statuses/{sha}","languages_url":"https://api.github.com/repos/binwiederhier/dabble/languages","stargazers_url":"https://api.github.com/repos/binwiederhier/dabble/stargazers","contributors_url":"https://api.github.com/repos/binwiederhier/dabble/contributors","subscribers_url":"https://api.github.com/repos/binwiederhier/dabble/subscribers","subscription_url":"https://api.github.com/repos/binwiederhier/dabble/subscription","commits_url":"https://api.github.com/repos/binwiederhier/dabble/commits{/sha}","git_commits_url":"https://api.github.com/repos/binwiederhier/dabble/git/commits{/sha}","comments_url":"https://api.github.com/repos/binwiederhier/dabble/comments{/number}","issue_comment_url":"https://api.github.com/repos/binwiederhier/dabble/issues/comments{/number}","contents_url":"https://api.github.com/repos/binwiederhier/dabble/contents/{+path}","compare_url":"https://api.github.com/repos/binwiederhier/dabble/compare/{base}...{head}","merges_url":"https://api.github.com/repos/binwiederhier/dabble/merges","archive_url":"https://api.github.com/repos/binwiederhier/dabble/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/binwiederhier/dabble/downloads","issues_url":"https://api.github.com/repos/binwiederhier/dabble/issues{/number}","pulls_url":"https://api.github.com/repos/binwiederhier/dabble/pulls{/number}","milestones_url":"https://api.github.com/repos/binwiederhier/dabble/milestones{/number}","notifications_url":"https://api.github.com/repos/binwiederhier/dabble/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/binwiederhier/dabble/labels{/name}","releases_url":"https://api.github.com/repos/binwiederhier/dabble/releases{/id}","deployments_url":"https://api.github.com/repos/binwiederhier/dabble/deployments","created_at":"2022-03-15T15:06:17Z","updated_at":"2022-03-15T15:06:17Z","pushed_at":"2024-03-21T02:52:10Z","git_url":"git://github.com/binwiederhier/dabble.git","ssh_url":"git@github.com:binwiederhier/dabble.git","clone_url":"https://github.com/binwiederhier/dabble.git","svn_url":"https://github.com/binwiederhier/dabble","homepage":null,"size":1,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"has_discussions":false,"forks_count":0,"mirror_url":null,"archived":false,"disabled":false,"open_issues_count":1,"license":null,"allow_forking":true,"is_template":false,"web_commit_signoff_required":false,"topics":[],"visibility":"public","forks":0,"open_issues":1,"watchers":0,"default_branch":"main","allow_squash_merge":true,"allow_merge_commit":true,"allow_rebase_merge":true,"allow_auto_merge":false,"delete_branch_on_merge":false,"allow_update_branch":false,"use_squash_pr_title_as_default":false,"squash_merge_commit_message":"COMMIT_MESSAGES","squash_merge_commit_title":"COMMIT_OR_PR_TITLE","merge_commit_message":"PR_TITLE","merge_commit_title":"MERGE_MESSAGE"}},"base":{"label":"binwiederhier:main","ref":"main","sha":"72d931a20bb83d123ab45accaf761150c8b01211","user":{"login":"binwiederhier","id":664597,"node_id":"MDQ6VXNlcjY2NDU5Nw==","avatar_url":"https://avatars.githubusercontent.com/u/664597?v=4","gravatar_id":"","url":"https://api.github.com/users/binwiederhier","html_url":"https://github.com/binwiederhier","followers_url":"https://api.github.com/users/binwiederhier/followers","following_url":"https://api.github.com/users/binwiederhier/following{/other_user}","gists_url":"https://api.github.com/users/binwiederhier/gists{/gist_id}","starred_url":"https://api.github.com/users/binwiederhier/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/binwiederhier/subscriptions","organizations_url":"https://api.github.com/users/binwiederhier/orgs","repos_url":"https://api.github.com/users/binwiederhier/repos","events_url":"https://api.github.com/users/binwiederhier/events{/privacy}","received_events_url":"https://api.github.com/users/binwiederhier/received_events","type":"User","site_admin":false},"repo":{"id":470212003,"node_id":"R_kgDOHAbdow","name":"dabble","full_name":"binwiederhier/dabble","private":false,"owner":{"login":"binwiederhier","id":664597,"node_id":"MDQ6VXNlcjY2NDU5Nw==","avatar_url":"https://avatars.githubusercontent.com/u/664597?v=4","gravatar_id":"","url":"https://api.github.com/users/binwiederhier","html_url":"https://github.com/binwiederhier","followers_url":"https://api.github.com/users/binwiederhier/followers","following_url":"https://api.github.com/users/binwiederhier/following{/other_user}","gists_url":"https://api.github.com/users/binwiederhier/gists{/gist_id}","starred_url":"https://api.github.com/users/binwiederhier/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/binwiederhier/subscriptions","organizations_url":"https://api.github.com/users/binwiederhier/orgs","repos_url":"https://api.github.com/users/binwiederhier/repos","events_url":"https://api.github.com/users/binwiederhier/events{/privacy}","received_events_url":"https://api.github.com/users/binwiederhier/received_events","type":"User","site_admin":false},"html_url":"https://github.com/binwiederhier/dabble","description":"A repo for dabbling","fork":false,"url":"https://api.github.com/repos/binwiederhier/dabble","forks_url":"https://api.github.com/repos/binwiederhier/dabble/forks","keys_url":"https://api.github.com/repos/binwiederhier/dabble/keys{/key_id}","collaborators_url":"https://api.github.com/repos/binwiederhier/dabble/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/binwiederhier/dabble/teams","hooks_url":"https://api.github.com/repos/binwiederhier/dabble/hooks","issue_events_url":"https://api.github.com/repos/binwiederhier/dabble/issues/events{/number}","events_url":"https://api.github.com/repos/binwiederhier/dabble/events","assignees_url":"https://api.github.com/repos/binwiederhier/dabble/assignees{/user}","branches_url":"https://api.github.com/repos/binwiederhier/dabble/branches{/branch}","tags_url":"https://api.github.com/repos/binwiederhier/dabble/tags","blobs_url":"https://api.github.com/repos/binwiederhier/dabble/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/binwiederhier/dabble/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/binwiederhier/dabble/git/refs{/sha}","trees_url":"https://api.github.com/repos/binwiederhier/dabble/git/trees{/sha}","statuses_url":"https://api.github.com/repos/binwiederhier/dabble/statuses/{sha}","languages_url":"https://api.github.com/repos/binwiederhier/dabble/languages","stargazers_url":"https://api.github.com/repos/binwiederhier/dabble/stargazers","contributors_url":"https://api.github.com/repos/binwiederhier/dabble/contributors","subscribers_url":"https://api.github.com/repos/binwiederhier/dabble/subscribers","subscription_url":"https://api.github.com/repos/binwiederhier/dabble/subscription","commits_url":"https://api.github.com/repos/binwiederhier/dabble/commits{/sha}","git_commits_url":"https://api.github.com/repos/binwiederhier/dabble/git/commits{/sha}","comments_url":"https://api.github.com/repos/binwiederhier/dabble/comments{/number}","issue_comment_url":"https://api.github.com/repos/binwiederhier/dabble/issues/comments{/number}","contents_url":"https://api.github.com/repos/binwiederhier/dabble/contents/{+path}","compare_url":"https://api.github.com/repos/binwiederhier/dabble/compare/{base}...{head}","merges_url":"https://api.github.com/repos/binwiederhier/dabble/merges","archive_url":"https://api.github.com/repos/binwiederhier/dabble/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/binwiederhier/dabble/downloads","issues_url":"https://api.github.com/repos/binwiederhier/dabble/issues{/number}","pulls_url":"https://api.github.com/repos/binwiederhier/dabble/pulls{/number}","milestones_url":"https://api.github.com/repos/binwiederhier/dabble/milestones{/number}","notifications_url":"https://api.github.com/repos/binwiederhier/dabble/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/binwiederhier/dabble/labels{/name}","releases_url":"https://api.github.com/repos/binwiederhier/dabble/releases{/id}","deployments_url":"https://api.github.com/repos/binwiederhier/dabble/deployments","created_at":"2022-03-15T15:06:17Z","updated_at":"2022-03-15T15:06:17Z","pushed_at":"2024-03-21T02:52:10Z","git_url":"git://github.com/binwiederhier/dabble.git","ssh_url":"git@github.com:binwiederhier/dabble.git","clone_url":"https://github.com/binwiederhier/dabble.git","svn_url":"https://github.com/binwiederhier/dabble","homepage":null,"size":1,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"has_discussions":false,"forks_count":0,"mirror_url":null,"archived":false,"disabled":false,"open_issues_count":1,"license":null,"allow_forking":true,"is_template":false,"web_commit_signoff_required":false,"topics":[],"visibility":"public","forks":0,"open_issues":1,"watchers":0,"default_branch":"main","allow_squash_merge":true,"allow_merge_commit":true,"allow_rebase_merge":true,"allow_auto_merge":false,"delete_branch_on_merge":false,"allow_update_branch":false,"use_squash_pr_title_as_default":false,"squash_merge_commit_message":"COMMIT_MESSAGES","squash_merge_commit_title":"COMMIT_OR_PR_TITLE","merge_commit_message":"PR_TITLE","merge_commit_title":"MERGE_MESSAGE"}},"_links":{"self":{"href":"https://api.github.com/repos/binwiederhier/dabble/pulls/1"},"html":{"href":"https://github.com/binwiederhier/dabble/pull/1"},"issue":{"href":"https://api.github.com/repos/binwiederhier/dabble/issues/1"},"comments":{"href":"https://api.github.com/repos/binwiederhier/dabble/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/binwiederhier/dabble/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/binwiederhier/dabble/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/binwiederhier/dabble/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/binwiederhier/dabble/statuses/5703842cc5715ed1e358d23ebb693db09747ae9b"}},"author_association":"OWNER","auto_merge":null,"active_lock_reason":null,"merged":false,"mergeable":null,"rebaseable":null,"mergeable_state":"unknown","merged_by":null,"comments":0,"review_comments":0,"maintainer_can_modify":false,"commits":1,"additions":1,"deletions":1,"changed_files":1},"repository":{"id":470212003,"node_id":"R_kgDOHAbdow","name":"dabble","full_name":"binwiederhier/dabble","private":false,"owner":{"login":"binwiederhier","id":664597,"node_id":"MDQ6VXNlcjY2NDU5Nw==","avatar_url":"https://avatars.githubusercontent.com/u/664597?v=4","gravatar_id":"","url":"https://api.github.com/users/binwiederhier","html_url":"https://github.com/binwiederhier","followers_url":"https://api.github.com/users/binwiederhier/followers","following_url":"https://api.github.com/users/binwiederhier/following{/other_user}","gists_url":"https://api.github.com/users/binwiederhier/gists{/gist_id}","starred_url":"https://api.github.com/users/binwiederhier/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/binwiederhier/subscriptions","organizations_url":"https://api.github.com/users/binwiederhier/orgs","repos_url":"https://api.github.com/users/binwiederhier/repos","events_url":"https://api.github.com/users/binwiederhier/events{/privacy}","received_events_url":"https://api.github.com/users/binwiederhier/received_events","type":"User","site_admin":false},"html_url":"https://github.com/binwiederhier/dabble","description":"A repo for dabbling","fork":false,"url":"https://api.github.com/repos/binwiederhier/dabble","forks_url":"https://api.github.com/repos/binwiederhier/dabble/forks","keys_url":"https://api.github.com/repos/binwiederhier/dabble/keys{/key_id}","collaborators_url":"https://api.github.com/repos/binwiederhier/dabble/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/binwiederhier/dabble/teams","hooks_url":"https://api.github.com/repos/binwiederhier/dabble/hooks","issue_events_url":"https://api.github.com/repos/binwiederhier/dabble/issues/events{/number}","events_url":"https://api.github.com/repos/binwiederhier/dabble/events","assignees_url":"https://api.github.com/repos/binwiederhier/dabble/assignees{/user}","branches_url":"https://api.github.com/repos/binwiederhier/dabble/branches{/branch}","tags_url":"https://api.github.com/repos/binwiederhier/dabble/tags","blobs_url":"https://api.github.com/repos/binwiederhier/dabble/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/binwiederhier/dabble/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/binwiederhier/dabble/git/refs{/sha}","trees_url":"https://api.github.com/repos/binwiederhier/dabble/git/trees{/sha}","statuses_url":"https://api.github.com/repos/binwiederhier/dabble/statuses/{sha}","languages_url":"https://api.github.com/repos/binwiederhier/dabble/languages","stargazers_url":"https://api.github.com/repos/binwiederhier/dabble/stargazers","contributors_url":"https://api.github.com/repos/binwiederhier/dabble/contributors","subscribers_url":"https://api.github.com/repos/binwiederhier/dabble/subscribers","subscription_url":"https://api.github.com/repos/binwiederhier/dabble/subscription","commits_url":"https://api.github.com/repos/binwiederhier/dabble/commits{/sha}","git_commits_url":"https://api.github.com/repos/binwiederhier/dabble/git/commits{/sha}","comments_url":"https://api.github.com/repos/binwiederhier/dabble/comments{/number}","issue_comment_url":"https://api.github.com/repos/binwiederhier/dabble/issues/comments{/number}","contents_url":"https://api.github.com/repos/binwiederhier/dabble/contents/{+path}","compare_url":"https://api.github.com/repos/binwiederhier/dabble/compare/{base}...{head}","merges_url":"https://api.github.com/repos/binwiederhier/dabble/merges","archive_url":"https://api.github.com/repos/binwiederhier/dabble/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/binwiederhier/dabble/downloads","issues_url":"https://api.github.com/repos/binwiederhier/dabble/issues{/number}","pulls_url":"https://api.github.com/repos/binwiederhier/dabble/pulls{/number}","milestones_url":"https://api.github.com/repos/binwiederhier/dabble/milestones{/number}","notifications_url":"https://api.github.com/repos/binwiederhier/dabble/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/binwiederhier/dabble/labels{/name}","releases_url":"https://api.github.com/repos/binwiederhier/dabble/releases{/id}","deployments_url":"https://api.github.com/repos/binwiederhier/dabble/deployments","created_at":"2022-03-15T15:06:17Z","updated_at":"2022-03-15T15:06:17Z","pushed_at":"2024-03-21T02:52:10Z","git_url":"git://github.com/binwiederhier/dabble.git","ssh_url":"git@github.com:binwiederhier/dabble.git","clone_url":"https://github.com/binwiederhier/dabble.git","svn_url":"https://github.com/binwiederhier/dabble","homepage":null,"size":1,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"has_discussions":false,"forks_count":0,"mirror_url":null,"archived":false,"disabled":false,"open_issues_count":1,"license":null,"allow_forking":true,"is_template":false,"web_commit_signoff_required":false,"topics":[],"visibility":"public","forks":0,"open_issues":1,"watchers":0,"default_branch":"main"},"sender":{"login":"binwiederhier","id":664597,"node_id":"MDQ6VXNlcjY2NDU5Nw==","avatar_url":"https://avatars.githubusercontent.com/u/664597?v=4","gravatar_id":"","url":"https://api.github.com/users/binwiederhier","html_url":"https://github.com/binwiederhier","followers_url":"https://api.github.com/users/binwiederhier/followers","following_url":"https://api.github.com/users/binwiederhier/following{/other_user}","gists_url":"https://api.github.com/users/binwiederhier/gists{/gist_id}","starred_url":"https://api.github.com/users/binwiederhier/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/binwiederhier/subscriptions","organizations_url":"https://api.github.com/users/binwiederhier/orgs","repos_url":"https://api.github.com/users/binwiederhier/repos","events_url":"https://api.github.com/users/binwiederhier/events{/privacy}","received_events_url":"https://api.github.com/users/binwiederhier/received_events","type":"User","site_admin":false}}`
  2536. response := request(t, s, "PUT", `/mytopic?tpl=yes&message=[{{.pull_request.head.repo.full_name}}]+Pull+request+{{if+eq+.action+"opened"}}OPENED{{else}}CLOSED{{end}}:+{{.pull_request.title}}`, body, nil)
  2537. require.Equal(t, 200, response.Code)
  2538. m := toMessage(t, response.Body.String())
  2539. require.Equal(t, "", m.Title)
  2540. require.Equal(t, `[binwiederhier/dabble] Pull request OPENED: A sample PR from Phil`, m.Message)
  2541. }
  2542. func TestServer_MessageTemplate_GitHub2(t *testing.T) {
  2543. t.Parallel()
  2544. s := newTestServer(t, newTestConfig(t))
  2545. body := `{"action":"opened","number":1,"pull_request":{"url":"https://api.github.com/repos/binwiederhier/dabble/pulls/1","id":1783420972,"node_id":"PR_kwDOHAbdo85qTNgs","html_url":"https://github.com/binwiederhier/dabble/pull/1","diff_url":"https://github.com/binwiederhier/dabble/pull/1.diff","patch_url":"https://github.com/binwiederhier/dabble/pull/1.patch","issue_url":"https://api.github.com/repos/binwiederhier/dabble/issues/1","number":1,"state":"open","locked":false,"title":"A sample PR from Phil","user":{"login":"binwiederhier","id":664597,"node_id":"MDQ6VXNlcjY2NDU5Nw==","avatar_url":"https://avatars.githubusercontent.com/u/664597?v=4","gravatar_id":"","url":"https://api.github.com/users/binwiederhier","html_url":"https://github.com/binwiederhier","followers_url":"https://api.github.com/users/binwiederhier/followers","following_url":"https://api.github.com/users/binwiederhier/following{/other_user}","gists_url":"https://api.github.com/users/binwiederhier/gists{/gist_id}","starred_url":"https://api.github.com/users/binwiederhier/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/binwiederhier/subscriptions","organizations_url":"https://api.github.com/users/binwiederhier/orgs","repos_url":"https://api.github.com/users/binwiederhier/repos","events_url":"https://api.github.com/users/binwiederhier/events{/privacy}","received_events_url":"https://api.github.com/users/binwiederhier/received_events","type":"User","site_admin":false},"body":null,"created_at":"2024-03-21T02:52:09Z","updated_at":"2024-03-21T02:52:09Z","closed_at":null,"merged_at":null,"merge_commit_sha":null,"assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"draft":false,"commits_url":"https://api.github.com/repos/binwiederhier/dabble/pulls/1/commits","review_comments_url":"https://api.github.com/repos/binwiederhier/dabble/pulls/1/comments","review_comment_url":"https://api.github.com/repos/binwiederhier/dabble/pulls/comments{/number}","comments_url":"https://api.github.com/repos/binwiederhier/dabble/issues/1/comments","statuses_url":"https://api.github.com/repos/binwiederhier/dabble/statuses/5703842cc5715ed1e358d23ebb693db09747ae9b","head":{"label":"binwiederhier:aa","ref":"aa","sha":"5703842cc5715ed1e358d23ebb693db09747ae9b","user":{"login":"binwiederhier","id":664597,"node_id":"MDQ6VXNlcjY2NDU5Nw==","avatar_url":"https://avatars.githubusercontent.com/u/664597?v=4","gravatar_id":"","url":"https://api.github.com/users/binwiederhier","html_url":"https://github.com/binwiederhier","followers_url":"https://api.github.com/users/binwiederhier/followers","following_url":"https://api.github.com/users/binwiederhier/following{/other_user}","gists_url":"https://api.github.com/users/binwiederhier/gists{/gist_id}","starred_url":"https://api.github.com/users/binwiederhier/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/binwiederhier/subscriptions","organizations_url":"https://api.github.com/users/binwiederhier/orgs","repos_url":"https://api.github.com/users/binwiederhier/repos","events_url":"https://api.github.com/users/binwiederhier/events{/privacy}","received_events_url":"https://api.github.com/users/binwiederhier/received_events","type":"User","site_admin":false},"repo":{"id":470212003,"node_id":"R_kgDOHAbdow","name":"dabble","full_name":"binwiederhier/dabble","private":false,"owner":{"login":"binwiederhier","id":664597,"node_id":"MDQ6VXNlcjY2NDU5Nw==","avatar_url":"https://avatars.githubusercontent.com/u/664597?v=4","gravatar_id":"","url":"https://api.github.com/users/binwiederhier","html_url":"https://github.com/binwiederhier","followers_url":"https://api.github.com/users/binwiederhier/followers","following_url":"https://api.github.com/users/binwiederhier/following{/other_user}","gists_url":"https://api.github.com/users/binwiederhier/gists{/gist_id}","starred_url":"https://api.github.com/users/binwiederhier/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/binwiederhier/subscriptions","organizations_url":"https://api.github.com/users/binwiederhier/orgs","repos_url":"https://api.github.com/users/binwiederhier/repos","events_url":"https://api.github.com/users/binwiederhier/events{/privacy}","received_events_url":"https://api.github.com/users/binwiederhier/received_events","type":"User","site_admin":false},"html_url":"https://github.com/binwiederhier/dabble","description":"A repo for dabbling","fork":false,"url":"https://api.github.com/repos/binwiederhier/dabble","forks_url":"https://api.github.com/repos/binwiederhier/dabble/forks","keys_url":"https://api.github.com/repos/binwiederhier/dabble/keys{/key_id}","collaborators_url":"https://api.github.com/repos/binwiederhier/dabble/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/binwiederhier/dabble/teams","hooks_url":"https://api.github.com/repos/binwiederhier/dabble/hooks","issue_events_url":"https://api.github.com/repos/binwiederhier/dabble/issues/events{/number}","events_url":"https://api.github.com/repos/binwiederhier/dabble/events","assignees_url":"https://api.github.com/repos/binwiederhier/dabble/assignees{/user}","branches_url":"https://api.github.com/repos/binwiederhier/dabble/branches{/branch}","tags_url":"https://api.github.com/repos/binwiederhier/dabble/tags","blobs_url":"https://api.github.com/repos/binwiederhier/dabble/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/binwiederhier/dabble/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/binwiederhier/dabble/git/refs{/sha}","trees_url":"https://api.github.com/repos/binwiederhier/dabble/git/trees{/sha}","statuses_url":"https://api.github.com/repos/binwiederhier/dabble/statuses/{sha}","languages_url":"https://api.github.com/repos/binwiederhier/dabble/languages","stargazers_url":"https://api.github.com/repos/binwiederhier/dabble/stargazers","contributors_url":"https://api.github.com/repos/binwiederhier/dabble/contributors","subscribers_url":"https://api.github.com/repos/binwiederhier/dabble/subscribers","subscription_url":"https://api.github.com/repos/binwiederhier/dabble/subscription","commits_url":"https://api.github.com/repos/binwiederhier/dabble/commits{/sha}","git_commits_url":"https://api.github.com/repos/binwiederhier/dabble/git/commits{/sha}","comments_url":"https://api.github.com/repos/binwiederhier/dabble/comments{/number}","issue_comment_url":"https://api.github.com/repos/binwiederhier/dabble/issues/comments{/number}","contents_url":"https://api.github.com/repos/binwiederhier/dabble/contents/{+path}","compare_url":"https://api.github.com/repos/binwiederhier/dabble/compare/{base}...{head}","merges_url":"https://api.github.com/repos/binwiederhier/dabble/merges","archive_url":"https://api.github.com/repos/binwiederhier/dabble/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/binwiederhier/dabble/downloads","issues_url":"https://api.github.com/repos/binwiederhier/dabble/issues{/number}","pulls_url":"https://api.github.com/repos/binwiederhier/dabble/pulls{/number}","milestones_url":"https://api.github.com/repos/binwiederhier/dabble/milestones{/number}","notifications_url":"https://api.github.com/repos/binwiederhier/dabble/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/binwiederhier/dabble/labels{/name}","releases_url":"https://api.github.com/repos/binwiederhier/dabble/releases{/id}","deployments_url":"https://api.github.com/repos/binwiederhier/dabble/deployments","created_at":"2022-03-15T15:06:17Z","updated_at":"2022-03-15T15:06:17Z","pushed_at":"2024-03-21T02:52:10Z","git_url":"git://github.com/binwiederhier/dabble.git","ssh_url":"git@github.com:binwiederhier/dabble.git","clone_url":"https://github.com/binwiederhier/dabble.git","svn_url":"https://github.com/binwiederhier/dabble","homepage":null,"size":1,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"has_discussions":false,"forks_count":0,"mirror_url":null,"archived":false,"disabled":false,"open_issues_count":1,"license":null,"allow_forking":true,"is_template":false,"web_commit_signoff_required":false,"topics":[],"visibility":"public","forks":0,"open_issues":1,"watchers":0,"default_branch":"main","allow_squash_merge":true,"allow_merge_commit":true,"allow_rebase_merge":true,"allow_auto_merge":false,"delete_branch_on_merge":false,"allow_update_branch":false,"use_squash_pr_title_as_default":false,"squash_merge_commit_message":"COMMIT_MESSAGES","squash_merge_commit_title":"COMMIT_OR_PR_TITLE","merge_commit_message":"PR_TITLE","merge_commit_title":"MERGE_MESSAGE"}},"base":{"label":"binwiederhier:main","ref":"main","sha":"72d931a20bb83d123ab45accaf761150c8b01211","user":{"login":"binwiederhier","id":664597,"node_id":"MDQ6VXNlcjY2NDU5Nw==","avatar_url":"https://avatars.githubusercontent.com/u/664597?v=4","gravatar_id":"","url":"https://api.github.com/users/binwiederhier","html_url":"https://github.com/binwiederhier","followers_url":"https://api.github.com/users/binwiederhier/followers","following_url":"https://api.github.com/users/binwiederhier/following{/other_user}","gists_url":"https://api.github.com/users/binwiederhier/gists{/gist_id}","starred_url":"https://api.github.com/users/binwiederhier/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/binwiederhier/subscriptions","organizations_url":"https://api.github.com/users/binwiederhier/orgs","repos_url":"https://api.github.com/users/binwiederhier/repos","events_url":"https://api.github.com/users/binwiederhier/events{/privacy}","received_events_url":"https://api.github.com/users/binwiederhier/received_events","type":"User","site_admin":false},"repo":{"id":470212003,"node_id":"R_kgDOHAbdow","name":"dabble","full_name":"binwiederhier/dabble","private":false,"owner":{"login":"binwiederhier","id":664597,"node_id":"MDQ6VXNlcjY2NDU5Nw==","avatar_url":"https://avatars.githubusercontent.com/u/664597?v=4","gravatar_id":"","url":"https://api.github.com/users/binwiederhier","html_url":"https://github.com/binwiederhier","followers_url":"https://api.github.com/users/binwiederhier/followers","following_url":"https://api.github.com/users/binwiederhier/following{/other_user}","gists_url":"https://api.github.com/users/binwiederhier/gists{/gist_id}","starred_url":"https://api.github.com/users/binwiederhier/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/binwiederhier/subscriptions","organizations_url":"https://api.github.com/users/binwiederhier/orgs","repos_url":"https://api.github.com/users/binwiederhier/repos","events_url":"https://api.github.com/users/binwiederhier/events{/privacy}","received_events_url":"https://api.github.com/users/binwiederhier/received_events","type":"User","site_admin":false},"html_url":"https://github.com/binwiederhier/dabble","description":"A repo for dabbling","fork":false,"url":"https://api.github.com/repos/binwiederhier/dabble","forks_url":"https://api.github.com/repos/binwiederhier/dabble/forks","keys_url":"https://api.github.com/repos/binwiederhier/dabble/keys{/key_id}","collaborators_url":"https://api.github.com/repos/binwiederhier/dabble/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/binwiederhier/dabble/teams","hooks_url":"https://api.github.com/repos/binwiederhier/dabble/hooks","issue_events_url":"https://api.github.com/repos/binwiederhier/dabble/issues/events{/number}","events_url":"https://api.github.com/repos/binwiederhier/dabble/events","assignees_url":"https://api.github.com/repos/binwiederhier/dabble/assignees{/user}","branches_url":"https://api.github.com/repos/binwiederhier/dabble/branches{/branch}","tags_url":"https://api.github.com/repos/binwiederhier/dabble/tags","blobs_url":"https://api.github.com/repos/binwiederhier/dabble/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/binwiederhier/dabble/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/binwiederhier/dabble/git/refs{/sha}","trees_url":"https://api.github.com/repos/binwiederhier/dabble/git/trees{/sha}","statuses_url":"https://api.github.com/repos/binwiederhier/dabble/statuses/{sha}","languages_url":"https://api.github.com/repos/binwiederhier/dabble/languages","stargazers_url":"https://api.github.com/repos/binwiederhier/dabble/stargazers","contributors_url":"https://api.github.com/repos/binwiederhier/dabble/contributors","subscribers_url":"https://api.github.com/repos/binwiederhier/dabble/subscribers","subscription_url":"https://api.github.com/repos/binwiederhier/dabble/subscription","commits_url":"https://api.github.com/repos/binwiederhier/dabble/commits{/sha}","git_commits_url":"https://api.github.com/repos/binwiederhier/dabble/git/commits{/sha}","comments_url":"https://api.github.com/repos/binwiederhier/dabble/comments{/number}","issue_comment_url":"https://api.github.com/repos/binwiederhier/dabble/issues/comments{/number}","contents_url":"https://api.github.com/repos/binwiederhier/dabble/contents/{+path}","compare_url":"https://api.github.com/repos/binwiederhier/dabble/compare/{base}...{head}","merges_url":"https://api.github.com/repos/binwiederhier/dabble/merges","archive_url":"https://api.github.com/repos/binwiederhier/dabble/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/binwiederhier/dabble/downloads","issues_url":"https://api.github.com/repos/binwiederhier/dabble/issues{/number}","pulls_url":"https://api.github.com/repos/binwiederhier/dabble/pulls{/number}","milestones_url":"https://api.github.com/repos/binwiederhier/dabble/milestones{/number}","notifications_url":"https://api.github.com/repos/binwiederhier/dabble/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/binwiederhier/dabble/labels{/name}","releases_url":"https://api.github.com/repos/binwiederhier/dabble/releases{/id}","deployments_url":"https://api.github.com/repos/binwiederhier/dabble/deployments","created_at":"2022-03-15T15:06:17Z","updated_at":"2022-03-15T15:06:17Z","pushed_at":"2024-03-21T02:52:10Z","git_url":"git://github.com/binwiederhier/dabble.git","ssh_url":"git@github.com:binwiederhier/dabble.git","clone_url":"https://github.com/binwiederhier/dabble.git","svn_url":"https://github.com/binwiederhier/dabble","homepage":null,"size":1,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"has_discussions":false,"forks_count":0,"mirror_url":null,"archived":false,"disabled":false,"open_issues_count":1,"license":null,"allow_forking":true,"is_template":false,"web_commit_signoff_required":false,"topics":[],"visibility":"public","forks":0,"open_issues":1,"watchers":0,"default_branch":"main","allow_squash_merge":true,"allow_merge_commit":true,"allow_rebase_merge":true,"allow_auto_merge":false,"delete_branch_on_merge":false,"allow_update_branch":false,"use_squash_pr_title_as_default":false,"squash_merge_commit_message":"COMMIT_MESSAGES","squash_merge_commit_title":"COMMIT_OR_PR_TITLE","merge_commit_message":"PR_TITLE","merge_commit_title":"MERGE_MESSAGE"}},"_links":{"self":{"href":"https://api.github.com/repos/binwiederhier/dabble/pulls/1"},"html":{"href":"https://github.com/binwiederhier/dabble/pull/1"},"issue":{"href":"https://api.github.com/repos/binwiederhier/dabble/issues/1"},"comments":{"href":"https://api.github.com/repos/binwiederhier/dabble/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/binwiederhier/dabble/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/binwiederhier/dabble/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/binwiederhier/dabble/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/binwiederhier/dabble/statuses/5703842cc5715ed1e358d23ebb693db09747ae9b"}},"author_association":"OWNER","auto_merge":null,"active_lock_reason":null,"merged":false,"mergeable":null,"rebaseable":null,"mergeable_state":"unknown","merged_by":null,"comments":0,"review_comments":0,"maintainer_can_modify":false,"commits":1,"additions":1,"deletions":1,"changed_files":1},"repository":{"id":470212003,"node_id":"R_kgDOHAbdow","name":"dabble","full_name":"binwiederhier/dabble","private":false,"owner":{"login":"binwiederhier","id":664597,"node_id":"MDQ6VXNlcjY2NDU5Nw==","avatar_url":"https://avatars.githubusercontent.com/u/664597?v=4","gravatar_id":"","url":"https://api.github.com/users/binwiederhier","html_url":"https://github.com/binwiederhier","followers_url":"https://api.github.com/users/binwiederhier/followers","following_url":"https://api.github.com/users/binwiederhier/following{/other_user}","gists_url":"https://api.github.com/users/binwiederhier/gists{/gist_id}","starred_url":"https://api.github.com/users/binwiederhier/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/binwiederhier/subscriptions","organizations_url":"https://api.github.com/users/binwiederhier/orgs","repos_url":"https://api.github.com/users/binwiederhier/repos","events_url":"https://api.github.com/users/binwiederhier/events{/privacy}","received_events_url":"https://api.github.com/users/binwiederhier/received_events","type":"User","site_admin":false},"html_url":"https://github.com/binwiederhier/dabble","description":"A repo for dabbling","fork":false,"url":"https://api.github.com/repos/binwiederhier/dabble","forks_url":"https://api.github.com/repos/binwiederhier/dabble/forks","keys_url":"https://api.github.com/repos/binwiederhier/dabble/keys{/key_id}","collaborators_url":"https://api.github.com/repos/binwiederhier/dabble/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/binwiederhier/dabble/teams","hooks_url":"https://api.github.com/repos/binwiederhier/dabble/hooks","issue_events_url":"https://api.github.com/repos/binwiederhier/dabble/issues/events{/number}","events_url":"https://api.github.com/repos/binwiederhier/dabble/events","assignees_url":"https://api.github.com/repos/binwiederhier/dabble/assignees{/user}","branches_url":"https://api.github.com/repos/binwiederhier/dabble/branches{/branch}","tags_url":"https://api.github.com/repos/binwiederhier/dabble/tags","blobs_url":"https://api.github.com/repos/binwiederhier/dabble/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/binwiederhier/dabble/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/binwiederhier/dabble/git/refs{/sha}","trees_url":"https://api.github.com/repos/binwiederhier/dabble/git/trees{/sha}","statuses_url":"https://api.github.com/repos/binwiederhier/dabble/statuses/{sha}","languages_url":"https://api.github.com/repos/binwiederhier/dabble/languages","stargazers_url":"https://api.github.com/repos/binwiederhier/dabble/stargazers","contributors_url":"https://api.github.com/repos/binwiederhier/dabble/contributors","subscribers_url":"https://api.github.com/repos/binwiederhier/dabble/subscribers","subscription_url":"https://api.github.com/repos/binwiederhier/dabble/subscription","commits_url":"https://api.github.com/repos/binwiederhier/dabble/commits{/sha}","git_commits_url":"https://api.github.com/repos/binwiederhier/dabble/git/commits{/sha}","comments_url":"https://api.github.com/repos/binwiederhier/dabble/comments{/number}","issue_comment_url":"https://api.github.com/repos/binwiederhier/dabble/issues/comments{/number}","contents_url":"https://api.github.com/repos/binwiederhier/dabble/contents/{+path}","compare_url":"https://api.github.com/repos/binwiederhier/dabble/compare/{base}...{head}","merges_url":"https://api.github.com/repos/binwiederhier/dabble/merges","archive_url":"https://api.github.com/repos/binwiederhier/dabble/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/binwiederhier/dabble/downloads","issues_url":"https://api.github.com/repos/binwiederhier/dabble/issues{/number}","pulls_url":"https://api.github.com/repos/binwiederhier/dabble/pulls{/number}","milestones_url":"https://api.github.com/repos/binwiederhier/dabble/milestones{/number}","notifications_url":"https://api.github.com/repos/binwiederhier/dabble/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/binwiederhier/dabble/labels{/name}","releases_url":"https://api.github.com/repos/binwiederhier/dabble/releases{/id}","deployments_url":"https://api.github.com/repos/binwiederhier/dabble/deployments","created_at":"2022-03-15T15:06:17Z","updated_at":"2022-03-15T15:06:17Z","pushed_at":"2024-03-21T02:52:10Z","git_url":"git://github.com/binwiederhier/dabble.git","ssh_url":"git@github.com:binwiederhier/dabble.git","clone_url":"https://github.com/binwiederhier/dabble.git","svn_url":"https://github.com/binwiederhier/dabble","homepage":null,"size":1,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"has_discussions":false,"forks_count":0,"mirror_url":null,"archived":false,"disabled":false,"open_issues_count":1,"license":null,"allow_forking":true,"is_template":false,"web_commit_signoff_required":false,"topics":[],"visibility":"public","forks":0,"open_issues":1,"watchers":0,"default_branch":"main"},"sender":{"login":"binwiederhier","id":664597,"node_id":"MDQ6VXNlcjY2NDU5Nw==","avatar_url":"https://avatars.githubusercontent.com/u/664597?v=4","gravatar_id":"","url":"https://api.github.com/users/binwiederhier","html_url":"https://github.com/binwiederhier","followers_url":"https://api.github.com/users/binwiederhier/followers","following_url":"https://api.github.com/users/binwiederhier/following{/other_user}","gists_url":"https://api.github.com/users/binwiederhier/gists{/gist_id}","starred_url":"https://api.github.com/users/binwiederhier/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/binwiederhier/subscriptions","organizations_url":"https://api.github.com/users/binwiederhier/orgs","repos_url":"https://api.github.com/users/binwiederhier/repos","events_url":"https://api.github.com/users/binwiederhier/events{/privacy}","received_events_url":"https://api.github.com/users/binwiederhier/received_events","type":"User","site_admin":false}}`
  2546. response := request(t, s, "PUT", `/mytopic?tpl=yes&title={{if+eq+.action+"opened"}}New+PR:+%23{{.number}}+by+{{.pull_request.user.login}}{{else}}[{{.action}}]+PR:+%23{{.number}}+by+{{.pull_request.user.login}}{{end}}&message={{.pull_request.title}}+in+{{.repository.full_name}}.+View+more+at+{{.pull_request.html_url}}`, body, nil)
  2547. require.Equal(t, 200, response.Code)
  2548. m := toMessage(t, response.Body.String())
  2549. require.Equal(t, `New PR: #1 by binwiederhier`, m.Title)
  2550. require.Equal(t, `A sample PR from Phil in binwiederhier/dabble. View more at https://github.com/binwiederhier/dabble/pull/1`, m.Message)
  2551. }
  2552. func TestServer_MessageTemplate_DisallowedCalls(t *testing.T) {
  2553. t.Parallel()
  2554. s := newTestServer(t, newTestConfig(t))
  2555. disallowedTemplates := []string{
  2556. `{{template ""}}`,
  2557. `{{- template ""}}`,
  2558. `{{-
  2559. template ""}}`,
  2560. `{{ call abc}}`,
  2561. `{{ define "aa"}}`,
  2562. `We cannot {{define "aa"}}`,
  2563. `We cannot {{ call "aa"}}`,
  2564. `We cannot {{- template "aa"}}`,
  2565. }
  2566. for _, disallowedTemplate := range disallowedTemplates {
  2567. messageTemplate := disallowedTemplate
  2568. t.Run(disallowedTemplate, func(t *testing.T) {
  2569. t.Parallel()
  2570. response := request(t, s, "PUT", `/mytopic`, `{}`, map[string]string{
  2571. "Template": "yes",
  2572. "Message": messageTemplate,
  2573. })
  2574. require.Equal(t, 400, response.Code)
  2575. require.Equal(t, 40044, toHTTPError(t, response.Body.String()).Code)
  2576. })
  2577. }
  2578. }
  2579. func newTestConfig(t *testing.T) *Config {
  2580. conf := NewConfig()
  2581. conf.BaseURL = "http://127.0.0.1:12345"
  2582. conf.CacheFile = filepath.Join(t.TempDir(), "cache.db")
  2583. conf.CacheStartupQueries = "pragma journal_mode = WAL; pragma synchronous = normal; pragma temp_store = memory;"
  2584. conf.AttachmentCacheDir = t.TempDir()
  2585. return conf
  2586. }
  2587. func configureAuth(t *testing.T, conf *Config) *Config {
  2588. conf.AuthFile = filepath.Join(t.TempDir(), "user.db")
  2589. conf.AuthStartupQueries = "pragma journal_mode = WAL; pragma synchronous = normal; pragma temp_store = memory;"
  2590. conf.AuthBcryptCost = bcrypt.MinCost // This speeds up tests a lot
  2591. return conf
  2592. }
  2593. func newTestConfigWithAuthFile(t *testing.T) *Config {
  2594. conf := newTestConfig(t)
  2595. conf = configureAuth(t, conf)
  2596. return conf
  2597. }
  2598. func newTestConfigWithWebPush(t *testing.T) *Config {
  2599. conf := newTestConfig(t)
  2600. privateKey, publicKey, err := webpush.GenerateVAPIDKeys()
  2601. require.Nil(t, err)
  2602. conf.WebPushFile = filepath.Join(t.TempDir(), "webpush.db")
  2603. conf.WebPushEmailAddress = "testing@example.com"
  2604. conf.WebPushPrivateKey = privateKey
  2605. conf.WebPushPublicKey = publicKey
  2606. return conf
  2607. }
  2608. func newTestServer(t *testing.T, config *Config) *Server {
  2609. server, err := New(config)
  2610. require.Nil(t, err)
  2611. return server
  2612. }
  2613. func request(t *testing.T, s *Server, method, url, body string, headers map[string]string, fn ...func(r *http.Request)) *httptest.ResponseRecorder {
  2614. rr := httptest.NewRecorder()
  2615. r, err := http.NewRequest(method, url, strings.NewReader(body))
  2616. if err != nil {
  2617. t.Fatal(err)
  2618. }
  2619. r.RemoteAddr = "9.9.9.9:1234" // Used for tests
  2620. for k, v := range headers {
  2621. r.Header.Set(k, v)
  2622. }
  2623. for _, f := range fn {
  2624. f(r)
  2625. }
  2626. s.handle(rr, r)
  2627. return rr
  2628. }
  2629. func subscribe(t *testing.T, s *Server, url string, rr *httptest.ResponseRecorder) context.CancelFunc {
  2630. ctx, cancel := context.WithCancel(context.Background())
  2631. req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
  2632. if err != nil {
  2633. t.Fatal(err)
  2634. }
  2635. done := make(chan bool)
  2636. go func() {
  2637. s.handle(rr, req)
  2638. done <- true
  2639. }()
  2640. cancelAndWaitForDone := func() {
  2641. time.Sleep(200 * time.Millisecond)
  2642. cancel()
  2643. <-done
  2644. }
  2645. time.Sleep(200 * time.Millisecond)
  2646. return cancelAndWaitForDone
  2647. }
  2648. func toMessages(t *testing.T, s string) []*message {
  2649. messages := make([]*message, 0)
  2650. scanner := bufio.NewScanner(strings.NewReader(s))
  2651. for scanner.Scan() {
  2652. messages = append(messages, toMessage(t, scanner.Text()))
  2653. }
  2654. return messages
  2655. }
  2656. func toMessage(t *testing.T, s string) *message {
  2657. var m message
  2658. require.Nil(t, json.NewDecoder(strings.NewReader(s)).Decode(&m))
  2659. return &m
  2660. }
  2661. func toHTTPError(t *testing.T, s string) *errHTTP {
  2662. var e errHTTP
  2663. require.Nil(t, json.NewDecoder(strings.NewReader(s)).Decode(&e))
  2664. return &e
  2665. }
  2666. func readAll(t *testing.T, rc io.ReadCloser) string {
  2667. b, err := io.ReadAll(rc)
  2668. if err != nil {
  2669. t.Fatal(err)
  2670. }
  2671. return string(b)
  2672. }
  2673. func waitFor(t *testing.T, f func() bool) {
  2674. waitForWithMaxWait(t, 5*time.Second, f)
  2675. }
  2676. func waitForWithMaxWait(t *testing.T, maxWait time.Duration, f func() bool) {
  2677. start := time.Now()
  2678. for time.Since(start) < maxWait {
  2679. if f() {
  2680. return
  2681. }
  2682. time.Sleep(50 * time.Millisecond)
  2683. }
  2684. t.Fatalf("Function f did not succeed after %v: %v", maxWait, string(debug.Stack()))
  2685. }