server_test.go 166 KB

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