Debian Codegraph @

pkg/services/specialized/notifiarr/notifiarr_test.go

raw
1 package notifiarr_test
2
3 import (
4 "io"
5 "log"
6 "net/http"
7 "net/url"
8 "os"
9 "testing"
10
11 "github.com/jarcoal/httpmock"
12 "github.com/onsi/ginkgo/v2"
13 "github.com/onsi/gomega"
14
15 "github.com/nicholas-fedor/shoutrrr/internal/testutils"
16 "github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/notifiarr"
17 "github.com/nicholas-fedor/shoutrrr/pkg/types"
18 )
19
20 var (
21 service *notifiarr.Service
22 envNotifiarrURL *url.URL
23 logger *log.Logger
24 _ = ginkgo.BeforeSuite(func() {
25 service = &notifiarr.Service{}
26 envNotifiarrURL, _ = url.Parse(os.Getenv("SHOUTRRR_NOTIFIARR_URL"))
27 logger = log.New(ginkgo.GinkgoWriter, "Test", log.LstdFlags)
28 })
29 )
30
31 var _ = ginkgo.Describe("the notifiarr service", func() {
32 ginkgo.When("running integration tests", func() {
33 ginkgo.It("should work without errors", func() {
34 if envNotifiarrURL.String() == "" {
35 ginkgo.Skip("No integration test ENV URL was set")
36
37 return
38 }
39
40 serviceURL, _ := url.Parse(envNotifiarrURL.String())
41 err := service.Initialize(serviceURL, testutils.TestLogger())
42 gomega.Expect(err).NotTo(gomega.HaveOccurred())
43
44 err = service.Send("this is an integration test", nil)
45 gomega.Expect(err).NotTo(gomega.HaveOccurred())
46 })
47 })
48
49 ginkgo.Describe("the service", func() {
50 ginkgo.BeforeEach(func() {
51 service = &notifiarr.Service{}
52 service.SetLogger(logger)
53 })
54
55 ginkgo.It("returns the correct service identifier", func() {
56 gomega.Expect(service.GetID()).To(gomega.Equal("notifiarr"))
57 })
58 })
59
60 ginkgo.When("parsing a custom URL", func() {
61 ginkgo.BeforeEach(func() {
62 service = &notifiarr.Service{}
63 service.SetLogger(logger)
64 })
65
66 ginkgo.It("correctly sets API key from custom URL", func() {
67 customURL := testutils.URLMust("notifiarr://apikey123")
68 serviceURL, err := service.GetServiceURLFromCustom(customURL)
69 gomega.Expect(err).NotTo(gomega.HaveOccurred())
70 err = service.Initialize(serviceURL, logger)
71 gomega.Expect(err).NotTo(gomega.HaveOccurred())
72 gomega.Expect(service.Config.APIKey).To(gomega.Equal("apikey123"))
73 })
74 })
75
76 ginkgo.Describe("the notifiarr config", func() {
77 ginkgo.When("parsing the configuration URL", func() {
78 ginkgo.It("should be identical after de-/serialization", func() {
79 testURL := "notifiarr://apikey123?channel=123456789"
80 expectedURL := "notifiarr://apikey123?channel=123456789"
81
82 serviceURL := testutils.URLMust(testURL)
83 err := service.Initialize(serviceURL, logger)
84 gomega.Expect(err).NotTo(gomega.HaveOccurred())
85 gomega.Expect(service.Config.GetURL().String()).To(gomega.Equal(expectedURL))
86 })
87 })
88
89 ginkgo.When("parsing from webhook URL", func() {
90 ginkgo.It("sets config properties from webhook URL query parameters", func() {
91 webhookURL := testutils.URLMust(
92 "https://notifiarr.com/api/v1/notification/passthrough/apikey123?channel=123456789",
93 )
94
95 config, _, err := notifiarr.ConfigFromWebhookURL(webhookURL)
96 gomega.Expect(err).NotTo(gomega.HaveOccurred())
97
98 gomega.Expect(config.APIKey).To(gomega.Equal("apikey123"))
99 gomega.Expect(config.Channel).To(gomega.Equal("123456789"))
100 })
101
102 ginkgo.It(
103 "sets config properties correctly from webhook URL with template and other parameters",
104 func() {
105 webhookURL := testutils.URLMust(
106 "https://notifiarr.com/api/v1/notification/watchtower?template=json&contenttype=application/json&method=POST&titlekey=customtitle&messagekey=custommessage&extra=param",
107 )
108
109 config, _, err := notifiarr.ConfigFromWebhookURL(webhookURL)
110 gomega.Expect(err).NotTo(gomega.HaveOccurred())
111
112 gomega.Expect(config.APIKey).To(gomega.Equal(""))
113 gomega.Expect(config.Channel).To(gomega.Equal(""))
114 gomega.Expect(config.GetURL().RawQuery).
115 To(gomega.Equal("contenttype=application%2Fjson&extra=param&messagekey=custommessage&method=POST&template=json&titlekey=customtitle"))
116 },
117 )
118
119 ginkgo.It("handles custom headers and extra data from webhook URL", func() {
120 webhookURL := testutils.URLMust(
121 "https://example.com/webhook?@Authorization=Bearer token&$extraKey=extraValue&template=json",
122 )
123
124 config, _, err := notifiarr.ConfigFromWebhookURL(webhookURL)
125 gomega.Expect(err).NotTo(gomega.HaveOccurred())
126
127 gomega.Expect(config.APIKey).To(gomega.Equal(""))
128 gomega.Expect(config.Channel).To(gomega.Equal(""))
129 gomega.Expect(config.GetURL().RawQuery).
130 To(gomega.Equal("%24extraKey=extraValue&%40Authorization=Bearer+token&template=json"))
131 })
132 })
133 })
134
135 ginkgo.Describe("sending the payload", func() {
136 ginkgo.BeforeEach(func() {
137 httpmock.Activate()
138
139 service = &notifiarr.Service{}
140 service.SetLogger(logger)
141 })
142
143 ginkgo.AfterEach(func() {
144 httpmock.DeactivateAndReset()
145 })
146
147 ginkgo.When("sending via API", func() {
148 ginkgo.It("succeeds if the server accepts the payload", func() {
149 serviceURL := testutils.URLMust("notifiarr://apikey123")
150 err := service.Initialize(serviceURL, logger)
151 gomega.Expect(err).NotTo(gomega.HaveOccurred())
152
153 httpmock.RegisterResponder(
154 "POST",
155 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
156 httpmock.NewStringResponder(200, ""),
157 )
158
159 err = service.Send("Test message", nil)
160 gomega.Expect(err).NotTo(gomega.HaveOccurred())
161 })
162
163 ginkgo.It("reports an error if sending fails", func() {
164 serviceURL := testutils.URLMust("notifiarr://apikey123")
165 err := service.Initialize(serviceURL, logger)
166 gomega.Expect(err).NotTo(gomega.HaveOccurred())
167
168 httpmock.RegisterResponder(
169 "POST",
170 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
171 httpmock.NewErrorResponder(http.ErrHandlerTimeout),
172 )
173
174 err = service.Send("Test message", nil)
175 gomega.Expect(err).To(gomega.HaveOccurred())
176 })
177
178 ginkgo.It("includes Discord channel in JSON payload when configured", func() {
179 serviceURL := testutils.URLMust("notifiarr://apikey123?channel=123456789")
180 err := service.Initialize(serviceURL, logger)
181 gomega.Expect(err).NotTo(gomega.HaveOccurred())
182
183 httpmock.RegisterResponder(
184 "POST",
185 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
186 func(req *http.Request) (*http.Response, error) {
187 body, _ := io.ReadAll(req.Body)
188 gomega.Expect(string(body)).
189 To(gomega.ContainSubstring(`"ids":{"channel":123456789}`))
190
191 return httpmock.NewStringResponse(200, ""), nil
192 },
193 )
194
195 params := types.Params{"title": "Test Title"}
196 err = service.Send("Test message", &params)
197 gomega.Expect(err).NotTo(gomega.HaveOccurred())
198 })
199
200 ginkgo.It("includes title in notification payload", func() {
201 serviceURL := testutils.URLMust("notifiarr://apikey123")
202 err := service.Initialize(serviceURL, logger)
203 gomega.Expect(err).NotTo(gomega.HaveOccurred())
204
205 httpmock.RegisterResponder(
206 "POST",
207 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
208 func(req *http.Request) (*http.Response, error) {
209 body, _ := io.ReadAll(req.Body)
210 gomega.Expect(string(body)).
211 To(gomega.ContainSubstring(`"discord":{"text":{"title":"Test Title","description":"Test message"}}`))
212
213 return httpmock.NewStringResponse(200, ""), nil
214 },
215 )
216
217 params := types.Params{"title": "Test Title"}
218 err = service.Send("Test message", &params)
219 gomega.Expect(err).NotTo(gomega.HaveOccurred())
220 })
221
222 ginkgo.It("does not mutate the given params", func() {
223 serviceURL := testutils.URLMust("notifiarr://apikey123")
224 err := service.Initialize(serviceURL, logger)
225 gomega.Expect(err).NotTo(gomega.HaveOccurred())
226
227 httpmock.RegisterResponder(
228 "POST",
229 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
230 httpmock.NewStringResponder(200, ""),
231 )
232
233 params := types.Params{"title": "ORIGINAL TITLE"}
234 err = service.Send("Test message", &params)
235 gomega.Expect(err).NotTo(gomega.HaveOccurred())
236 gomega.Expect(params).To(gomega.Equal(types.Params{"title": "ORIGINAL TITLE"}))
237 })
238
239 ginkgo.It("returns error for empty message", func() {
240 serviceURL := testutils.URLMust("notifiarr://apikey123")
241 err := service.Initialize(serviceURL, logger)
242 gomega.Expect(err).NotTo(gomega.HaveOccurred())
243
244 err = service.Send("", nil)
245 gomega.Expect(err).To(gomega.HaveOccurred())
246 gomega.Expect(err).To(gomega.Equal(notifiarr.ErrEmptyMessage))
247 })
248 })
249 })
250
251 ginkgo.Describe("event ID parameter handling", func() {
252 ginkgo.BeforeEach(func() {
253 httpmock.Activate()
254
255 service = &notifiarr.Service{}
256 service.SetLogger(logger)
257 })
258
259 ginkgo.AfterEach(func() {
260 httpmock.DeactivateAndReset()
261 })
262
263 ginkgo.It("includes event ID in payload when provided", func() {
264 serviceURL := testutils.URLMust("notifiarr://apikey123")
265 err := service.Initialize(serviceURL, logger)
266 gomega.Expect(err).NotTo(gomega.HaveOccurred())
267
268 httpmock.RegisterResponder(
269 "POST",
270 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
271 func(req *http.Request) (*http.Response, error) {
272 body, _ := io.ReadAll(req.Body)
273 gomega.Expect(string(body)).
274 To(gomega.ContainSubstring(`"event":"event-123"`))
275
276 return httpmock.NewStringResponse(200, ""), nil
277 },
278 )
279
280 params := types.Params{"id": "event-123"}
281 err = service.Send("Test message", &params)
282 gomega.Expect(err).NotTo(gomega.HaveOccurred())
283 })
284
285 ginkgo.It("omits event ID when not provided", func() {
286 serviceURL := testutils.URLMust("notifiarr://apikey123")
287 err := service.Initialize(serviceURL, logger)
288 gomega.Expect(err).NotTo(gomega.HaveOccurred())
289
290 httpmock.RegisterResponder(
291 "POST",
292 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
293 func(req *http.Request) (*http.Response, error) {
294 body, _ := io.ReadAll(req.Body)
295 gomega.Expect(string(body)).
296 NotTo(gomega.ContainSubstring(`"id"`))
297
298 return httpmock.NewStringResponse(200, ""), nil
299 },
300 )
301
302 err = service.Send("Test message", nil)
303 gomega.Expect(err).NotTo(gomega.HaveOccurred())
304 })
305
306 ginkgo.It("handles empty event ID gracefully", func() {
307 serviceURL := testutils.URLMust("notifiarr://apikey123")
308 err := service.Initialize(serviceURL, logger)
309 gomega.Expect(err).NotTo(gomega.HaveOccurred())
310
311 httpmock.RegisterResponder(
312 "POST",
313 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
314 func(req *http.Request) (*http.Response, error) {
315 body, _ := io.ReadAll(req.Body)
316 gomega.Expect(string(body)).
317 NotTo(gomega.ContainSubstring(`"id"`))
318
319 return httpmock.NewStringResponse(200, ""), nil
320 },
321 )
322
323 params := types.Params{"id": ""}
324 err = service.Send("Test message", &params)
325 gomega.Expect(err).NotTo(gomega.HaveOccurred())
326 })
327 })
328
329 ginkgo.Describe("Discord mention parsing", func() {
330 ginkgo.BeforeEach(func() {
331 httpmock.Activate()
332
333 service = &notifiarr.Service{}
334 service.SetLogger(logger)
335 })
336
337 ginkgo.AfterEach(func() {
338 httpmock.DeactivateAndReset()
339 })
340
341 ginkgo.Describe("parsing user mentions", func() {
342 ginkgo.It("parses single user mention <@123>", func() {
343 serviceURL := testutils.URLMust("notifiarr://apikey123?channel=123456789")
344 err := service.Initialize(serviceURL, logger)
345 gomega.Expect(err).NotTo(gomega.HaveOccurred())
346
347 httpmock.RegisterResponder(
348 "POST",
349 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
350 func(req *http.Request) (*http.Response, error) {
351 body, _ := io.ReadAll(req.Body)
352 gomega.Expect(string(body)).
353 To(gomega.ContainSubstring(`"ping":{"pingUser":123}`))
354
355 return httpmock.NewStringResponse(200, ""), nil
356 },
357 )
358
359 err = service.Send("Hello <@123>!", nil)
360 gomega.Expect(err).NotTo(gomega.HaveOccurred())
361 })
362
363 ginkgo.It("parses user mention with nickname <@!456>", func() {
364 serviceURL := testutils.URLMust("notifiarr://apikey123?channel=123456789")
365 err := service.Initialize(serviceURL, logger)
366 gomega.Expect(err).NotTo(gomega.HaveOccurred())
367
368 httpmock.RegisterResponder(
369 "POST",
370 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
371 func(req *http.Request) (*http.Response, error) {
372 body, _ := io.ReadAll(req.Body)
373 gomega.Expect(string(body)).
374 To(gomega.ContainSubstring(`"ping":{"pingUser":456}`))
375
376 return httpmock.NewStringResponse(200, ""), nil
377 },
378 )
379
380 err = service.Send("Hey <@!456> there", nil)
381 gomega.Expect(err).NotTo(gomega.HaveOccurred())
382 })
383
384 ginkgo.It("parses multiple user mentions", func() {
385 serviceURL := testutils.URLMust("notifiarr://apikey123?channel=123456789")
386 err := service.Initialize(serviceURL, logger)
387 gomega.Expect(err).NotTo(gomega.HaveOccurred())
388
389 httpmock.RegisterResponder(
390 "POST",
391 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
392 func(req *http.Request) (*http.Response, error) {
393 body, _ := io.ReadAll(req.Body)
394 gomega.Expect(string(body)).
395 To(gomega.ContainSubstring(`"ping":{"pingUser":123}`))
396
397 return httpmock.NewStringResponse(200, ""), nil
398 },
399 )
400
401 err = service.Send("<@123> and <@!456> are here", nil)
402 gomega.Expect(err).NotTo(gomega.HaveOccurred())
403 })
404 })
405
406 ginkgo.Describe("parsing role mentions", func() {
407 ginkgo.It("parses single role mention <@&789>", func() {
408 serviceURL := testutils.URLMust("notifiarr://apikey123?channel=123456789")
409 err := service.Initialize(serviceURL, logger)
410 gomega.Expect(err).NotTo(gomega.HaveOccurred())
411
412 httpmock.RegisterResponder(
413 "POST",
414 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
415 func(req *http.Request) (*http.Response, error) {
416 body, _ := io.ReadAll(req.Body)
417 gomega.Expect(string(body)).
418 To(gomega.ContainSubstring(`"ping":{"pingRole":789}`))
419
420 return httpmock.NewStringResponse(200, ""), nil
421 },
422 )
423
424 err = service.Send("Role: <@&789>", nil)
425 gomega.Expect(err).NotTo(gomega.HaveOccurred())
426 })
427
428 ginkgo.It("parses multiple role mentions", func() {
429 serviceURL := testutils.URLMust("notifiarr://apikey123?channel=123456789")
430 err := service.Initialize(serviceURL, logger)
431 gomega.Expect(err).NotTo(gomega.HaveOccurred())
432
433 httpmock.RegisterResponder(
434 "POST",
435 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
436 func(req *http.Request) (*http.Response, error) {
437 body, _ := io.ReadAll(req.Body)
438 gomega.Expect(string(body)).
439 To(gomega.ContainSubstring(`"ping":{"pingRole":111}`))
440
441 return httpmock.NewStringResponse(200, ""), nil
442 },
443 )
444
445 err = service.Send("<@&111> and <@&222> notified", nil)
446 gomega.Expect(err).NotTo(gomega.HaveOccurred())
447 })
448 })
449
450 ginkgo.Describe("parsing mixed mentions", func() {
451 ginkgo.It("parses user and role mentions together", func() {
452 serviceURL := testutils.URLMust("notifiarr://apikey123?channel=123456789")
453 err := service.Initialize(serviceURL, logger)
454 gomega.Expect(err).NotTo(gomega.HaveOccurred())
455
456 httpmock.RegisterResponder(
457 "POST",
458 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
459 func(req *http.Request) (*http.Response, error) {
460 body, _ := io.ReadAll(req.Body)
461 gomega.Expect(string(body)).
462 To(gomega.ContainSubstring(`"ping":{"pingUser":123,"pingRole":456}`))
463
464 return httpmock.NewStringResponse(200, ""), nil
465 },
466 )
467
468 err = service.Send("<@123> <@&456> <@!789>", nil)
469 gomega.Expect(err).NotTo(gomega.HaveOccurred())
470 })
471 })
472
473 ginkgo.Describe("invalid mentions", func() {
474 ginkgo.It("ignores malformed mentions", func() {
475 serviceURL := testutils.URLMust("notifiarr://apikey123?channel=123456789")
476 err := service.Initialize(serviceURL, logger)
477 gomega.Expect(err).NotTo(gomega.HaveOccurred())
478
479 httpmock.RegisterResponder(
480 "POST",
481 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
482 func(req *http.Request) (*http.Response, error) {
483 body, _ := io.ReadAll(req.Body)
484 gomega.Expect(string(body)).
485 NotTo(gomega.ContainSubstring(`"ping"`))
486
487 return httpmock.NewStringResponse(200, ""), nil
488 },
489 )
490
491 err = service.Send("Invalid <@abc> <@&> <@123def>", nil)
492 gomega.Expect(err).NotTo(gomega.HaveOccurred())
493 })
494
495 ginkgo.It("ignores mentions without closing bracket", func() {
496 serviceURL := testutils.URLMust("notifiarr://apikey123?channel=123456789")
497 err := service.Initialize(serviceURL, logger)
498 gomega.Expect(err).NotTo(gomega.HaveOccurred())
499
500 httpmock.RegisterResponder(
501 "POST",
502 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
503 func(req *http.Request) (*http.Response, error) {
504 body, _ := io.ReadAll(req.Body)
505 gomega.Expect(string(body)).
506 NotTo(gomega.ContainSubstring(`"ping"`))
507
508 return httpmock.NewStringResponse(200, ""), nil
509 },
510 )
511
512 err = service.Send("Broken <@123 mention", nil)
513 gomega.Expect(err).NotTo(gomega.HaveOccurred())
514 })
515
516 ginkgo.It("ignores mentions with invalid characters", func() {
517 serviceURL := testutils.URLMust("notifiarr://apikey123?channel=123456789")
518 err := service.Initialize(serviceURL, logger)
519 gomega.Expect(err).NotTo(gomega.HaveOccurred())
520
521 httpmock.RegisterResponder(
522 "POST",
523 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
524 func(req *http.Request) (*http.Response, error) {
525 body, _ := io.ReadAll(req.Body)
526 gomega.Expect(string(body)).
527 NotTo(gomega.ContainSubstring(`"ping"`))
528
529 return httpmock.NewStringResponse(200, ""), nil
530 },
531 )
532
533 err = service.Send("<@12a3> <@&45b6>", nil)
534 gomega.Expect(err).NotTo(gomega.HaveOccurred())
535 })
536 })
537
538 ginkgo.Describe("edge cases", func() {
539 ginkgo.It("handles message without mentions", func() {
540 serviceURL := testutils.URLMust("notifiarr://apikey123?channel=123456789")
541 err := service.Initialize(serviceURL, logger)
542 gomega.Expect(err).NotTo(gomega.HaveOccurred())
543
544 httpmock.RegisterResponder(
545 "POST",
546 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
547 func(req *http.Request) (*http.Response, error) {
548 body, _ := io.ReadAll(req.Body)
549 gomega.Expect(string(body)).
550 NotTo(gomega.ContainSubstring(`"ping"`))
551
552 return httpmock.NewStringResponse(200, ""), nil
553 },
554 )
555
556 err = service.Send("Just a regular message", nil)
557 gomega.Expect(err).NotTo(gomega.HaveOccurred())
558 })
559
560 ginkgo.It("handles mentions at message boundaries", func() {
561 serviceURL := testutils.URLMust("notifiarr://apikey123?channel=123456789")
562 err := service.Initialize(serviceURL, logger)
563 gomega.Expect(err).NotTo(gomega.HaveOccurred())
564
565 httpmock.RegisterResponder(
566 "POST",
567 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
568 func(req *http.Request) (*http.Response, error) {
569 body, _ := io.ReadAll(req.Body)
570 gomega.Expect(string(body)).
571 To(gomega.ContainSubstring(`"ping":{"pingUser":123}`))
572
573 return httpmock.NewStringResponse(200, ""), nil
574 },
575 )
576
577 err = service.Send("<@123>", nil)
578 gomega.Expect(err).NotTo(gomega.HaveOccurred())
579 })
580 })
581 })
582
583 ginkgo.Describe("image/thumbnail URL parameters", func() {
584 ginkgo.BeforeEach(func() {
585 httpmock.Activate()
586
587 service = &notifiarr.Service{}
588 service.SetLogger(logger)
589 })
590
591 ginkgo.AfterEach(func() {
592 httpmock.DeactivateAndReset()
593 })
594
595 ginkgo.It("includes thumbnail URL in payload when configured", func() {
596 serviceURL := testutils.URLMust(
597 "notifiarr://apikey123?thumbnail=https://example.com/image.png",
598 )
599 err := service.Initialize(serviceURL, logger)
600 gomega.Expect(err).NotTo(gomega.HaveOccurred())
601
602 httpmock.RegisterResponder(
603 "POST",
604 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
605 func(req *http.Request) (*http.Response, error) {
606 body, _ := io.ReadAll(req.Body)
607 gomega.Expect(string(body)).
608 To(gomega.ContainSubstring(`"images":{"thumbnail":"https://example.com/image.png"}`))
609
610 return httpmock.NewStringResponse(200, ""), nil
611 },
612 )
613
614 err = service.Send("Test message", nil)
615 gomega.Expect(err).NotTo(gomega.HaveOccurred())
616 })
617
618 ginkgo.It("omits thumbnail when not configured", func() {
619 serviceURL := testutils.URLMust("notifiarr://apikey123")
620 err := service.Initialize(serviceURL, logger)
621 gomega.Expect(err).NotTo(gomega.HaveOccurred())
622
623 httpmock.RegisterResponder(
624 "POST",
625 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
626 func(req *http.Request) (*http.Response, error) {
627 body, _ := io.ReadAll(req.Body)
628 gomega.Expect(string(body)).
629 NotTo(gomega.ContainSubstring(`"thumbnail"`))
630
631 return httpmock.NewStringResponse(200, ""), nil
632 },
633 )
634
635 err = service.Send("Test message", nil)
636 gomega.Expect(err).NotTo(gomega.HaveOccurred())
637 })
638
639 ginkgo.It("handles empty thumbnail URL gracefully", func() {
640 serviceURL := testutils.URLMust("notifiarr://apikey123?thumbnail=")
641 err := service.Initialize(serviceURL, logger)
642 gomega.Expect(err).NotTo(gomega.HaveOccurred())
643
644 httpmock.RegisterResponder(
645 "POST",
646 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
647 func(req *http.Request) (*http.Response, error) {
648 body, _ := io.ReadAll(req.Body)
649 gomega.Expect(string(body)).
650 NotTo(gomega.ContainSubstring(`"thumbnail"`))
651
652 return httpmock.NewStringResponse(200, ""), nil
653 },
654 )
655
656 err = service.Send("Test message", nil)
657 gomega.Expect(err).NotTo(gomega.HaveOccurred())
658 })
659
660 ginkgo.It("includes image URL in payload when configured", func() {
661 serviceURL := testutils.URLMust(
662 "notifiarr://apikey123?image=https://example.com/image.png",
663 )
664 err := service.Initialize(serviceURL, logger)
665 gomega.Expect(err).NotTo(gomega.HaveOccurred())
666
667 httpmock.RegisterResponder(
668 "POST",
669 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
670 func(req *http.Request) (*http.Response, error) {
671 body, _ := io.ReadAll(req.Body)
672 gomega.Expect(string(body)).
673 To(gomega.ContainSubstring(`"images":{"image":"https://example.com/image.png"}`))
674
675 return httpmock.NewStringResponse(200, ""), nil
676 },
677 )
678
679 err = service.Send("Test message", nil)
680 gomega.Expect(err).NotTo(gomega.HaveOccurred())
681 })
682
683 ginkgo.It("omits image when not configured", func() {
684 serviceURL := testutils.URLMust("notifiarr://apikey123")
685 err := service.Initialize(serviceURL, logger)
686 gomega.Expect(err).NotTo(gomega.HaveOccurred())
687
688 httpmock.RegisterResponder(
689 "POST",
690 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
691 func(req *http.Request) (*http.Response, error) {
692 body, _ := io.ReadAll(req.Body)
693 gomega.Expect(string(body)).
694 NotTo(gomega.ContainSubstring(`"image"`))
695
696 return httpmock.NewStringResponse(200, ""), nil
697 },
698 )
699
700 err = service.Send("Test message", nil)
701 gomega.Expect(err).NotTo(gomega.HaveOccurred())
702 })
703
704 ginkgo.It("handles empty image URL gracefully", func() {
705 serviceURL := testutils.URLMust("notifiarr://apikey123?image=")
706 err := service.Initialize(serviceURL, logger)
707 gomega.Expect(err).NotTo(gomega.HaveOccurred())
708
709 httpmock.RegisterResponder(
710 "POST",
711 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
712 func(req *http.Request) (*http.Response, error) {
713 body, _ := io.ReadAll(req.Body)
714 gomega.Expect(string(body)).
715 NotTo(gomega.ContainSubstring(`"image"`))
716
717 return httpmock.NewStringResponse(200, ""), nil
718 },
719 )
720
721 err = service.Send("Test message", nil)
722 gomega.Expect(err).NotTo(gomega.HaveOccurred())
723 })
724
725 ginkgo.It("includes both thumbnail and image in payload when both configured", func() {
726 serviceURL := testutils.URLMust(
727 "notifiarr://apikey123?thumbnail=https://example.com/thumb.png&image=https://example.com/img.png",
728 )
729 err := service.Initialize(serviceURL, logger)
730 gomega.Expect(err).NotTo(gomega.HaveOccurred())
731
732 httpmock.RegisterResponder(
733 "POST",
734 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
735 func(req *http.Request) (*http.Response, error) {
736 body, _ := io.ReadAll(req.Body)
737 gomega.Expect(string(body)).
738 To(gomega.ContainSubstring(`"images":{"thumbnail":"https://example.com/thumb.png","image":"https://example.com/img.png"}`))
739
740 return httpmock.NewStringResponse(200, ""), nil
741 },
742 )
743
744 err = service.Send("Test message", nil)
745 gomega.Expect(err).NotTo(gomega.HaveOccurred())
746 })
747 })
748
749 ginkgo.Describe("color customization parameters", func() {
750 ginkgo.BeforeEach(func() {
751 httpmock.Activate()
752
753 service = &notifiarr.Service{}
754 service.SetLogger(logger)
755 })
756
757 ginkgo.AfterEach(func() {
758 httpmock.DeactivateAndReset()
759 })
760
761 ginkgo.It("includes color in payload when configured", func() {
762 serviceURL := testutils.URLMust("notifiarr://apikey123?color=16711680")
763 err := service.Initialize(serviceURL, logger)
764 gomega.Expect(err).NotTo(gomega.HaveOccurred())
765
766 httpmock.RegisterResponder(
767 "POST",
768 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
769 func(req *http.Request) (*http.Response, error) {
770 body, _ := io.ReadAll(req.Body)
771 gomega.Expect(string(body)).
772 To(gomega.ContainSubstring(`"color":"16711680"`))
773
774 return httpmock.NewStringResponse(200, ""), nil
775 },
776 )
777
778 err = service.Send("Test message", nil)
779 gomega.Expect(err).NotTo(gomega.HaveOccurred())
780 })
781
782 ginkgo.It("includes hex color in payload", func() {
783 serviceURL := testutils.URLMust("notifiarr://apikey123?color=%23FF0000")
784 err := service.Initialize(serviceURL, logger)
785 gomega.Expect(err).NotTo(gomega.HaveOccurred())
786
787 httpmock.RegisterResponder(
788 "POST",
789 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
790 func(req *http.Request) (*http.Response, error) {
791 body, _ := io.ReadAll(req.Body)
792 gomega.Expect(string(body)).
793 To(gomega.ContainSubstring(`"color":"#FF0000"`))
794
795 return httpmock.NewStringResponse(200, ""), nil
796 },
797 )
798
799 err = service.Send("Test message", nil)
800 gomega.Expect(err).NotTo(gomega.HaveOccurred())
801 })
802
803 ginkgo.It("omits color when not configured", func() {
804 serviceURL := testutils.URLMust("notifiarr://apikey123")
805 err := service.Initialize(serviceURL, logger)
806 gomega.Expect(err).NotTo(gomega.HaveOccurred())
807
808 httpmock.RegisterResponder(
809 "POST",
810 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
811 func(req *http.Request) (*http.Response, error) {
812 body, _ := io.ReadAll(req.Body)
813 gomega.Expect(string(body)).
814 NotTo(gomega.ContainSubstring(`"color"`))
815
816 return httpmock.NewStringResponse(200, ""), nil
817 },
818 )
819
820 err = service.Send("Test message", nil)
821 gomega.Expect(err).NotTo(gomega.HaveOccurred())
822 })
823
824 ginkgo.It("handles empty color value gracefully", func() {
825 serviceURL := testutils.URLMust("notifiarr://apikey123?color=")
826 err := service.Initialize(serviceURL, logger)
827 gomega.Expect(err).NotTo(gomega.HaveOccurred())
828
829 httpmock.RegisterResponder(
830 "POST",
831 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
832 func(req *http.Request) (*http.Response, error) {
833 body, _ := io.ReadAll(req.Body)
834 gomega.Expect(string(body)).
835 NotTo(gomega.ContainSubstring(`"color"`))
836
837 return httpmock.NewStringResponse(200, ""), nil
838 },
839 )
840
841 err = service.Send("Test message", nil)
842 gomega.Expect(err).NotTo(gomega.HaveOccurred())
843 })
844 })
845
846 ginkgo.Describe("URL parsing of new parameters", func() {
847 ginkgo.BeforeEach(func() {
848 service = &notifiarr.Service{}
849 service.SetLogger(logger)
850 })
851
852 ginkgo.It("parses all new parameters from URL", func() {
853 serviceURL := testutils.URLMust(
854 "notifiarr://apikey123?channel=123456789&thumbnail=https://example.com/img.png&color=blue",
855 )
856 err := service.Initialize(serviceURL, logger)
857 gomega.Expect(err).NotTo(gomega.HaveOccurred())
858
859 gomega.Expect(service.Config.Channel).To(gomega.Equal("123456789"))
860 gomega.Expect(service.Config.Thumbnail).To(gomega.Equal("https://example.com/img.png"))
861 gomega.Expect(service.Config.Color).To(gomega.Equal("blue"))
862 })
863
864 ginkgo.It("handles URL-encoded values", func() {
865 serviceURL := testutils.URLMust(
866 "notifiarr://apikey123?thumbnail=https%3A//example.com/image%20with%20spaces.png&color=%23FF0000",
867 )
868 err := service.Initialize(serviceURL, logger)
869 gomega.Expect(err).NotTo(gomega.HaveOccurred())
870
871 gomega.Expect(service.Config.Thumbnail).
872 To(gomega.Equal("https://example.com/image with spaces.png"))
873 gomega.Expect(service.Config.Color).To(gomega.Equal("#FF0000"))
874 })
875
876 ginkgo.It("preserves existing parameters while adding new ones", func() {
877 serviceURL := testutils.URLMust(
878 "notifiarr://apikey123?channel=123456789&thumbnail=https://example.com/img.png&color=blue",
879 )
880 err := service.Initialize(serviceURL, logger)
881 gomega.Expect(err).NotTo(gomega.HaveOccurred())
882
883 // Test that existing channel parameter still works
884 gomega.Expect(service.Config.Channel).To(gomega.Equal("123456789"))
885 // Test new parameters
886 gomega.Expect(service.Config.Thumbnail).To(gomega.Equal("https://example.com/img.png"))
887 gomega.Expect(service.Config.Color).To(gomega.Equal("blue"))
888 })
889 })
890
891 ginkgo.Describe("integration tests with all features combined", func() {
892 ginkgo.BeforeEach(func() {
893 httpmock.Activate()
894
895 service = &notifiarr.Service{}
896 service.SetLogger(logger)
897 })
898
899 ginkgo.AfterEach(func() {
900 httpmock.DeactivateAndReset()
901 })
902
903 ginkgo.It("includes all features in payload when fully configured", func() {
904 serviceURL := testutils.URLMust(
905 "notifiarr://apikey123?channel=123456789&thumbnail=https://example.com/img.png&color=16711680",
906 )
907 err := service.Initialize(serviceURL, logger)
908 gomega.Expect(err).NotTo(gomega.HaveOccurred())
909
910 httpmock.RegisterResponder(
911 "POST",
912 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
913 func(req *http.Request) (*http.Response, error) {
914 body, _ := io.ReadAll(req.Body)
915 bodyStr := string(body)
916
917 // Check event ID
918 gomega.Expect(bodyStr).
919 To(gomega.ContainSubstring(`"event":"test-event-123"`))
920 // Check title and description
921 gomega.Expect(bodyStr).
922 To(gomega.ContainSubstring(`"text":{"title":"Test Title","description":"Hello \u003c@123\u003e and \u003c@\u0026456\u003e!"}`))
923 // Check Discord channel
924 gomega.Expect(bodyStr).
925 To(gomega.ContainSubstring(`"ids":{"channel":123456789}`))
926 // Check thumbnail
927 gomega.Expect(bodyStr).
928 To(gomega.ContainSubstring(`"images":{"thumbnail":"https://example.com/img.png"}`))
929 // Check color
930 gomega.Expect(bodyStr).
931 To(gomega.ContainSubstring(`"color":"16711680"`))
932 // Check mentions
933 gomega.Expect(bodyStr).
934 To(gomega.ContainSubstring(`"ping":{"pingUser":123,"pingRole":456}`))
935
936 return httpmock.NewStringResponse(200, ""), nil
937 },
938 )
939
940 params := types.Params{"id": "test-event-123", "title": "Test Title"}
941 err = service.Send("Hello <@123> and <@&456>!", &params)
942 gomega.Expect(err).NotTo(gomega.HaveOccurred())
943 })
944
945 ginkgo.It("handles partial configuration gracefully", func() {
946 serviceURL := testutils.URLMust("notifiarr://apikey123?channel=123456789")
947 err := service.Initialize(serviceURL, logger)
948 gomega.Expect(err).NotTo(gomega.HaveOccurred())
949
950 httpmock.RegisterResponder(
951 "POST",
952 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
953 func(req *http.Request) (*http.Response, error) {
954 body, _ := io.ReadAll(req.Body)
955 bodyStr := string(body)
956
957 // Should have channel
958 gomega.Expect(bodyStr).
959 To(gomega.ContainSubstring(`"ids":{"channel":123456789}`))
960 // Should not have thumbnail or color
961 gomega.Expect(bodyStr).NotTo(gomega.ContainSubstring(`"images"`))
962 gomega.Expect(bodyStr).NotTo(gomega.ContainSubstring(`"color"`))
963 // Should have mentions from message
964 gomega.Expect(bodyStr).
965 To(gomega.ContainSubstring(`"ping":{"pingUser":123}`))
966
967 return httpmock.NewStringResponse(200, ""), nil
968 },
969 )
970
971 params := types.Params{"id": "event-456"}
972 err = service.Send("Message with <@123>", &params)
973 gomega.Expect(err).NotTo(gomega.HaveOccurred())
974 })
975
976 ginkgo.It("works with minimal configuration", func() {
977 serviceURL := testutils.URLMust("notifiarr://apikey123")
978 err := service.Initialize(serviceURL, logger)
979 gomega.Expect(err).NotTo(gomega.HaveOccurred())
980
981 httpmock.RegisterResponder(
982 "POST",
983 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
984 func(req *http.Request) (*http.Response, error) {
985 body, _ := io.ReadAll(req.Body)
986 bodyStr := string(body)
987
988 // Should have basic fields
989 gomega.Expect(bodyStr).
990 To(gomega.ContainSubstring(`"discord":{"text":{"title":"Test","description":"Simple message"}}`))
991 // Should not have Discord section for channel, images, color, ping
992 gomega.Expect(bodyStr).NotTo(gomega.ContainSubstring(`"ids"`))
993 gomega.Expect(bodyStr).NotTo(gomega.ContainSubstring(`"images"`))
994 gomega.Expect(bodyStr).NotTo(gomega.ContainSubstring(`"color"`))
995 gomega.Expect(bodyStr).NotTo(gomega.ContainSubstring(`"ping"`))
996
997 return httpmock.NewStringResponse(200, ""), nil
998 },
999 )
1000
1001 params := types.Params{"title": "Test"}
1002 err = service.Send("Simple message", &params)
1003 gomega.Expect(err).NotTo(gomega.HaveOccurred())
1004 })
1005 })
1006
1007 ginkgo.Describe("advanced text fields (icon, content, footer)", func() {
1008 ginkgo.BeforeEach(func() {
1009 httpmock.Activate()
1010
1011 service = &notifiarr.Service{}
1012 service.SetLogger(logger)
1013 })
1014
1015 ginkgo.AfterEach(func() {
1016 httpmock.DeactivateAndReset()
1017 })
1018
1019 ginkgo.It("includes icon in payload when provided", func() {
1020 serviceURL := testutils.URLMust("notifiarr://apikey123")
1021 err := service.Initialize(serviceURL, logger)
1022 gomega.Expect(err).NotTo(gomega.HaveOccurred())
1023
1024 httpmock.RegisterResponder(
1025 "POST",
1026 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
1027 func(req *http.Request) (*http.Response, error) {
1028 body, _ := io.ReadAll(req.Body)
1029 gomega.Expect(string(body)).
1030 To(gomega.ContainSubstring(`"text":{"icon":"https://example.com/icon.png","description":"Test message"}`))
1031
1032 return httpmock.NewStringResponse(200, ""), nil
1033 },
1034 )
1035
1036 params := types.Params{"icon": "https://example.com/icon.png"}
1037 err = service.Send("Test message", &params)
1038 gomega.Expect(err).NotTo(gomega.HaveOccurred())
1039 })
1040
1041 ginkgo.It("includes content in payload when provided", func() {
1042 serviceURL := testutils.URLMust("notifiarr://apikey123")
1043 err := service.Initialize(serviceURL, logger)
1044 gomega.Expect(err).NotTo(gomega.HaveOccurred())
1045
1046 httpmock.RegisterResponder(
1047 "POST",
1048 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
1049 func(req *http.Request) (*http.Response, error) {
1050 body, _ := io.ReadAll(req.Body)
1051 gomega.Expect(string(body)).
1052 To(gomega.ContainSubstring(`"text":{"content":"Custom content","description":"Test message"}`))
1053
1054 return httpmock.NewStringResponse(200, ""), nil
1055 },
1056 )
1057
1058 params := types.Params{"content": "Custom content"}
1059 err = service.Send("Test message", &params)
1060 gomega.Expect(err).NotTo(gomega.HaveOccurred())
1061 })
1062
1063 ginkgo.It("includes footer in payload when provided", func() {
1064 serviceURL := testutils.URLMust("notifiarr://apikey123")
1065 err := service.Initialize(serviceURL, logger)
1066 gomega.Expect(err).NotTo(gomega.HaveOccurred())
1067
1068 httpmock.RegisterResponder(
1069 "POST",
1070 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
1071 func(req *http.Request) (*http.Response, error) {
1072 body, _ := io.ReadAll(req.Body)
1073 bodyStr := string(body)
1074 gomega.Expect(bodyStr).To(gomega.ContainSubstring(`"footer":"Footer text"`))
1075 gomega.Expect(bodyStr).
1076 To(gomega.ContainSubstring(`"description":"Test message"`))
1077
1078 return httpmock.NewStringResponse(200, ""), nil
1079 },
1080 )
1081
1082 params := types.Params{"footer": "Footer text"}
1083 err = service.Send("Test message", &params)
1084 gomega.Expect(err).NotTo(gomega.HaveOccurred())
1085 })
1086
1087 ginkgo.It("includes all advanced text fields when provided", func() {
1088 serviceURL := testutils.URLMust("notifiarr://apikey123")
1089 err := service.Initialize(serviceURL, logger)
1090 gomega.Expect(err).NotTo(gomega.HaveOccurred())
1091
1092 httpmock.RegisterResponder(
1093 "POST",
1094 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
1095 func(req *http.Request) (*http.Response, error) {
1096 body, _ := io.ReadAll(req.Body)
1097 bodyStr := string(body)
1098 gomega.Expect(bodyStr).
1099 To(gomega.ContainSubstring(`"icon":"https://example.com/icon.png"`))
1100 gomega.Expect(bodyStr).To(gomega.ContainSubstring(`"content":"Custom content"`))
1101 gomega.Expect(bodyStr).To(gomega.ContainSubstring(`"footer":"Footer text"`))
1102 gomega.Expect(bodyStr).
1103 To(gomega.ContainSubstring(`"description":"Test message"`))
1104
1105 return httpmock.NewStringResponse(200, ""), nil
1106 },
1107 )
1108
1109 params := types.Params{
1110 "icon": "https://example.com/icon.png",
1111 "content": "Custom content",
1112 "footer": "Footer text",
1113 }
1114 err = service.Send("Test message", &params)
1115 gomega.Expect(err).NotTo(gomega.HaveOccurred())
1116 })
1117
1118 ginkgo.It("omits advanced text fields when not provided", func() {
1119 serviceURL := testutils.URLMust("notifiarr://apikey123")
1120 err := service.Initialize(serviceURL, logger)
1121 gomega.Expect(err).NotTo(gomega.HaveOccurred())
1122
1123 httpmock.RegisterResponder(
1124 "POST",
1125 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
1126 func(req *http.Request) (*http.Response, error) {
1127 body, _ := io.ReadAll(req.Body)
1128 bodyStr := string(body)
1129 gomega.Expect(bodyStr).
1130 To(gomega.ContainSubstring(`"text":{"description":"Test message"}`))
1131 gomega.Expect(bodyStr).NotTo(gomega.ContainSubstring(`"icon"`))
1132 gomega.Expect(bodyStr).NotTo(gomega.ContainSubstring(`"content"`))
1133 gomega.Expect(bodyStr).NotTo(gomega.ContainSubstring(`"footer"`))
1134
1135 return httpmock.NewStringResponse(200, ""), nil
1136 },
1137 )
1138
1139 err = service.Send("Test message", nil)
1140 gomega.Expect(err).NotTo(gomega.HaveOccurred())
1141 })
1142 })
1143
1144 ginkgo.Describe("combined usage of multiple new fields", func() {
1145 ginkgo.BeforeEach(func() {
1146 httpmock.Activate()
1147
1148 service = &notifiarr.Service{}
1149 service.SetLogger(logger)
1150 })
1151
1152 ginkgo.AfterEach(func() {
1153 httpmock.DeactivateAndReset()
1154 })
1155
1156 ginkgo.It("includes all new fields in payload when fully configured", func() {
1157 serviceURL := testutils.URLMust(
1158 "notifiarr://apikey123?channel=123456789&thumbnail=https://example.com/thumb.png&image=https://example.com/img.png&color=16711680",
1159 )
1160 err := service.Initialize(serviceURL, logger)
1161 gomega.Expect(err).NotTo(gomega.HaveOccurred())
1162
1163 httpmock.RegisterResponder(
1164 "POST",
1165 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
1166 func(req *http.Request) (*http.Response, error) {
1167 body, _ := io.ReadAll(req.Body)
1168 bodyStr := string(body)
1169
1170 // Check event ID
1171 gomega.Expect(bodyStr).
1172 To(gomega.ContainSubstring(`"event":"combined-test"`))
1173 // Check Discord channel
1174 gomega.Expect(bodyStr).
1175 To(gomega.ContainSubstring(`"ids":{"channel":123456789}`))
1176 // Check images
1177 gomega.Expect(bodyStr).
1178 To(gomega.ContainSubstring(`"images":{"thumbnail":"https://example.com/thumb.png","image":"https://example.com/img.png"}`))
1179 // Check color
1180 gomega.Expect(bodyStr).
1181 To(gomega.ContainSubstring(`"color":"16711680"`))
1182 // Check all text fields
1183 gomega.Expect(bodyStr).To(gomega.ContainSubstring(`"title":"Combined Test"`))
1184 gomega.Expect(bodyStr).
1185 To(gomega.ContainSubstring(`"icon":"https://example.com/icon.png"`))
1186 gomega.Expect(bodyStr).To(gomega.ContainSubstring(`"content":"Custom content"`))
1187 gomega.Expect(bodyStr).To(gomega.ContainSubstring(`"footer":"Footer info"`))
1188 gomega.Expect(bodyStr).
1189 To(gomega.ContainSubstring(`"description":"Hello \u003c@123\u003e!"`))
1190 // Check mentions
1191 gomega.Expect(bodyStr).
1192 To(gomega.ContainSubstring(`"ping":{"pingUser":123}`))
1193
1194 return httpmock.NewStringResponse(200, ""), nil
1195 },
1196 )
1197
1198 params := types.Params{
1199 "id": "combined-test",
1200 "title": "Combined Test",
1201 "icon": "https://example.com/icon.png",
1202 "content": "Custom content",
1203 "footer": "Footer info",
1204 }
1205 err = service.Send("Hello <@123>!", &params)
1206 gomega.Expect(err).NotTo(gomega.HaveOccurred())
1207 })
1208
1209 ginkgo.It("handles mixed URL and parameter configuration", func() {
1210 serviceURL := testutils.URLMust(
1211 "notifiarr://apikey123?channel=123456789&thumbnail=https://example.com/thumb.png",
1212 )
1213 err := service.Initialize(serviceURL, logger)
1214 gomega.Expect(err).NotTo(gomega.HaveOccurred())
1215
1216 httpmock.RegisterResponder(
1217 "POST",
1218 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
1219 func(req *http.Request) (*http.Response, error) {
1220 body, _ := io.ReadAll(req.Body)
1221 bodyStr := string(body)
1222
1223 // URL parameters
1224 gomega.Expect(bodyStr).
1225 To(gomega.ContainSubstring(`"ids":{"channel":123456789}`))
1226 gomega.Expect(bodyStr).
1227 To(gomega.ContainSubstring(`"thumbnail":"https://example.com/thumb.png"`))
1228 // Parameter overrides
1229 gomega.Expect(bodyStr).
1230 To(gomega.ContainSubstring(`"color":"blue"`))
1231 gomega.Expect(bodyStr).
1232 To(gomega.ContainSubstring(`"image":"https://example.com/param-image.png"`))
1233 // Text fields from params
1234 gomega.Expect(bodyStr).To(gomega.ContainSubstring(`"title":"Param Title"`))
1235 gomega.Expect(bodyStr).
1236 To(gomega.ContainSubstring(`"icon":"https://example.com/param-icon.png"`))
1237 gomega.Expect(bodyStr).To(gomega.ContainSubstring(`"content":"Param content"`))
1238 gomega.Expect(bodyStr).To(gomega.ContainSubstring(`"description":"Test"`))
1239
1240 return httpmock.NewStringResponse(200, ""), nil
1241 },
1242 )
1243
1244 params := types.Params{
1245 "color": "blue",
1246 "image": "https://example.com/param-image.png",
1247 "title": "Param Title",
1248 "icon": "https://example.com/param-icon.png",
1249 "content": "Param content",
1250 }
1251 err = service.Send("Test", &params)
1252 gomega.Expect(err).NotTo(gomega.HaveOccurred())
1253 })
1254
1255 ginkgo.It("works with partial new field combinations", func() {
1256 serviceURL := testutils.URLMust("notifiarr://apikey123?color=red")
1257 err := service.Initialize(serviceURL, logger)
1258 gomega.Expect(err).NotTo(gomega.HaveOccurred())
1259
1260 httpmock.RegisterResponder(
1261 "POST",
1262 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
1263 func(req *http.Request) (*http.Response, error) {
1264 body, _ := io.ReadAll(req.Body)
1265 bodyStr := string(body)
1266
1267 // Color from URL
1268 gomega.Expect(bodyStr).
1269 To(gomega.ContainSubstring(`"color":"red"`))
1270 // Some text fields from params
1271 gomega.Expect(bodyStr).
1272 To(gomega.ContainSubstring(`"text":{"title":"Partial","content":"Some content","description":"Message"}`))
1273 // No images or channel
1274 gomega.Expect(bodyStr).NotTo(gomega.ContainSubstring(`"ids"`))
1275 gomega.Expect(bodyStr).NotTo(gomega.ContainSubstring(`"images"`))
1276 // No footer or icon
1277 gomega.Expect(bodyStr).NotTo(gomega.ContainSubstring(`"footer"`))
1278 gomega.Expect(bodyStr).NotTo(gomega.ContainSubstring(`"icon"`))
1279
1280 return httpmock.NewStringResponse(200, ""), nil
1281 },
1282 )
1283
1284 params := types.Params{
1285 "title": "Partial",
1286 "content": "Some content",
1287 }
1288 err = service.Send("Message", &params)
1289 gomega.Expect(err).NotTo(gomega.HaveOccurred())
1290 })
1291 })
1292
1293 ginkgo.Describe("parsing functions", func() {
1294 ginkgo.Describe("ParseChannelID", func() {
1295 ginkgo.It("parses valid numeric channel ID", func() {
1296 service := &notifiarr.Service{}
1297 channelID, err := service.ParseChannelID("123456789")
1298 gomega.Expect(err).NotTo(gomega.HaveOccurred())
1299 gomega.Expect(channelID).To(gomega.Equal(123456789))
1300 })
1301
1302 ginkgo.It("returns error for non-numeric channel ID", func() {
1303 service := &notifiarr.Service{}
1304 _, err := service.ParseChannelID("invalid")
1305 gomega.Expect(err).To(gomega.HaveOccurred())
1306 gomega.Expect(err.Error()).To(gomega.ContainSubstring("invalid channel ID"))
1307 })
1308
1309 ginkgo.It("returns error for empty channel ID", func() {
1310 service := &notifiarr.Service{}
1311 _, err := service.ParseChannelID("")
1312 gomega.Expect(err).To(gomega.HaveOccurred())
1313 gomega.Expect(err.Error()).To(gomega.ContainSubstring("invalid channel ID"))
1314 })
1315
1316 ginkgo.It("parses large numeric channel ID", func() {
1317 service := &notifiarr.Service{}
1318 channelID, err := service.ParseChannelID("9223372036854775807")
1319 gomega.Expect(err).NotTo(gomega.HaveOccurred())
1320 gomega.Expect(channelID).To(gomega.Equal(9223372036854775807))
1321 })
1322 })
1323
1324 ginkgo.Describe("ParseFields", func() {
1325 ginkgo.It("parses valid JSON fields", func() {
1326 service := &notifiarr.Service{}
1327 fields, err := service.ParseFields(
1328 `[{"title":"Test","text":"Content","inline":true}]`,
1329 )
1330 gomega.Expect(err).NotTo(gomega.HaveOccurred())
1331 gomega.Expect(fields).To(gomega.HaveLen(1))
1332 gomega.Expect(fields[0].Title).To(gomega.Equal("Test"))
1333 gomega.Expect(fields[0].Text).To(gomega.Equal("Content"))
1334 gomega.Expect(fields[0].Inline).To(gomega.BeTrue())
1335 })
1336
1337 ginkgo.It("returns error for invalid JSON", func() {
1338 service := &notifiarr.Service{}
1339 _, err := service.ParseFields(`invalid json`)
1340 gomega.Expect(err).To(gomega.HaveOccurred())
1341 })
1342
1343 ginkgo.It("parses empty JSON array", func() {
1344 service := &notifiarr.Service{}
1345 fields, err := service.ParseFields(`[]`)
1346 gomega.Expect(err).NotTo(gomega.HaveOccurred())
1347 gomega.Expect(fields).To(gomega.BeEmpty())
1348 })
1349
1350 ginkgo.It("parses multiple fields", func() {
1351 service := &notifiarr.Service{}
1352 fields, err := service.ParseFields(
1353 `[{"title":"Field1","text":"Text1"},{"title":"Field2","text":"Text2"}]`,
1354 )
1355 gomega.Expect(err).NotTo(gomega.HaveOccurred())
1356 gomega.Expect(fields).To(gomega.HaveLen(2))
1357 })
1358 })
1359
1360 ginkgo.Describe("parseMention", func() {
1361 ginkgo.It("parses user mention <@123>", func() {
1362 mentionType, id := notifiarr.ParseMention("<@123>")
1363 gomega.Expect(mentionType).To(gomega.Equal(notifiarr.MentionTypeUser))
1364 gomega.Expect(id).To(gomega.Equal(123))
1365 })
1366
1367 ginkgo.It("parses user mention with nickname <@!456>", func() {
1368 mentionType, id := notifiarr.ParseMention("<@!456>")
1369 gomega.Expect(mentionType).To(gomega.Equal(notifiarr.MentionTypeUser))
1370 gomega.Expect(id).To(gomega.Equal(456))
1371 })
1372
1373 ginkgo.It("parses role mention <@&789>", func() {
1374 mentionType, id := notifiarr.ParseMention("<@&789>")
1375 gomega.Expect(mentionType).To(gomega.Equal(notifiarr.MentionTypeRole))
1376 gomega.Expect(id).To(gomega.Equal(789))
1377 })
1378
1379 ginkgo.It("returns none for invalid mention format", func() {
1380 mentionType, id := notifiarr.ParseMention("invalid")
1381 gomega.Expect(mentionType).To(gomega.Equal(notifiarr.MentionTypeNone))
1382 gomega.Expect(id).To(gomega.Equal(0))
1383 })
1384
1385 ginkgo.It("returns none for mention without closing bracket", func() {
1386 mentionType, id := notifiarr.ParseMention("<@123")
1387 gomega.Expect(mentionType).To(gomega.Equal(notifiarr.MentionTypeNone))
1388 gomega.Expect(id).To(gomega.Equal(0))
1389 })
1390
1391 ginkgo.It("returns none for non-numeric ID", func() {
1392 mentionType, id := notifiarr.ParseMention("<@abc>")
1393 gomega.Expect(mentionType).To(gomega.Equal(notifiarr.MentionTypeNone))
1394 gomega.Expect(id).To(gomega.Equal(0))
1395 })
1396 })
1397
1398 ginkgo.Describe("ExtractPingIDs", func() {
1399 ginkgo.It("extracts user and role IDs from mentions", func() {
1400 service := &notifiarr.Service{}
1401 mentions := []string{"<@123>", "<@&456>", "<@!789>"}
1402 userIDs, roleIDs := service.ExtractPingIDs(mentions)
1403 gomega.Expect(userIDs).To(gomega.Equal([]int{123, 789}))
1404 gomega.Expect(roleIDs).To(gomega.Equal([]int{456}))
1405 })
1406
1407 ginkgo.It("handles empty mentions list", func() {
1408 service := &notifiarr.Service{}
1409 mentions := []string{}
1410 userIDs, roleIDs := service.ExtractPingIDs(mentions)
1411 gomega.Expect(userIDs).To(gomega.BeEmpty())
1412 gomega.Expect(roleIDs).To(gomega.BeEmpty())
1413 })
1414
1415 ginkgo.It("ignores invalid mentions", func() {
1416 service := &notifiarr.Service{}
1417 mentions := []string{"<@123>", "invalid", "<@&456>"}
1418 userIDs, roleIDs := service.ExtractPingIDs(mentions)
1419 gomega.Expect(userIDs).To(gomega.Equal([]int{123}))
1420 gomega.Expect(roleIDs).To(gomega.Equal([]int{456}))
1421 })
1422 })
1423
1424 ginkgo.Describe("parseUpdateFlag", func() {
1425 ginkgo.It("parses 'true' string to true", func() {
1426 params := types.Params{"update": "true"}
1427 result := notifiarr.ParseUpdateFlag(params)
1428 gomega.Expect(result).NotTo(gomega.BeNil())
1429 gomega.Expect(*result).To(gomega.BeTrue())
1430 })
1431
1432 ginkgo.It("parses 'false' string to false", func() {
1433 params := types.Params{"update": "false"}
1434 result := notifiarr.ParseUpdateFlag(params)
1435 gomega.Expect(result).NotTo(gomega.BeNil())
1436 gomega.Expect(*result).To(gomega.BeFalse())
1437 })
1438
1439 ginkgo.It("returns nil for invalid value", func() {
1440 params := types.Params{"update": "invalid"}
1441 result := notifiarr.ParseUpdateFlag(params)
1442 gomega.Expect(result).To(gomega.BeNil())
1443 })
1444
1445 ginkgo.It("returns nil when update key is missing", func() {
1446 params := types.Params{}
1447 result := notifiarr.ParseUpdateFlag(params)
1448 gomega.Expect(result).To(gomega.BeNil())
1449 })
1450
1451 ginkgo.It("returns nil for empty update value", func() {
1452 params := types.Params{"update": ""}
1453 result := notifiarr.ParseUpdateFlag(params)
1454 gomega.Expect(result).To(gomega.BeNil())
1455 })
1456 })
1457 })
1458
1459 ginkgo.Describe("Send method edge cases", func() {
1460 ginkgo.BeforeEach(func() {
1461 httpmock.Activate()
1462
1463 service = &notifiarr.Service{}
1464 service.SetLogger(logger)
1465 })
1466
1467 ginkgo.AfterEach(func() {
1468 httpmock.DeactivateAndReset()
1469 })
1470
1471 ginkgo.It("handles nil params gracefully", func() {
1472 serviceURL := testutils.URLMust("notifiarr://apikey123")
1473 err := service.Initialize(serviceURL, logger)
1474 gomega.Expect(err).NotTo(gomega.HaveOccurred())
1475
1476 httpmock.RegisterResponder(
1477 "POST",
1478 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
1479 httpmock.NewStringResponder(200, ""),
1480 )
1481
1482 err = service.Send("Test message", nil)
1483 gomega.Expect(err).NotTo(gomega.HaveOccurred())
1484 })
1485
1486 ginkgo.It("handles empty params map", func() {
1487 serviceURL := testutils.URLMust("notifiarr://apikey123")
1488 err := service.Initialize(serviceURL, logger)
1489 gomega.Expect(err).NotTo(gomega.HaveOccurred())
1490
1491 httpmock.RegisterResponder(
1492 "POST",
1493 "https://notifiarr.com/api/v1/notification/passthrough/apikey123",
1494 httpmock.NewStringResponder(200, ""),
1495 )
1496
1497 emptyParams := types.Params{}
1498 err = service.Send("Test message", &emptyParams)
1499 gomega.Expect(err).NotTo(gomega.HaveOccurred())
1500 })
1501 })
1502 })
1503
1504 // TestNotifiarr runs the Notifiarr service test suite using Ginkgo.
1505 func TestNotifiarr(t *testing.T) {
1506 t.Parallel()
1507 gomega.RegisterFailHandler(ginkgo.Fail)
1508 ginkgo.RunSpecs(t, "Shoutrrr Notifiarr Suite")
1509 }