Debian Codegraph @

pkg/services/push/ntfy/ntfy_test.go

raw symbols SCIP
1 package ntfy
2
3 import (
4 "io"
5 "net/http"
6
7 "github.com/onsi/ginkgo/v2"
8 "github.com/onsi/gomega"
9
10 mock "github.com/stretchr/testify/mock"
11
12 "github.com/nicholas-fedor/shoutrrr/internal/testutils"
13 "github.com/nicholas-fedor/shoutrrr/pkg/types"
14 "github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient"
15 jsonclientmocks "github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient/mocks"
16 )
17
18 var _ = ginkgo.Describe("Service", func() {
19 var (
20 service *Service
21 mockJSON *jsonclientmocks.MockClient
22 logger types.StdLogger
23 )
24
25 ginkgo.BeforeEach(func() {
26 mockJSON = newMockJSONClient()
27 logger = &noOpLogger{}
28 service = &Service{
29 Config: &Config{
30 Scheme: "https",
31 Host: "ntfy.sh",
32 Topic: "mytopic",
33 },
34 }
35 service.SetLogger(logger)
36 service.client = mockJSON
37 })
38
39 ginkgo.Describe("GetID", func() {
40 ginkgo.It("should return the scheme name", func() {
41 gomega.Expect(service.GetID()).To(gomega.Equal(Scheme))
42 })
43 })
44
45 ginkgo.Describe("Initialize", func() {
46 ginkgo.It("should parse a valid URL and initialize config", func() {
47 serviceURL := mustParseURL("ntfy://user:[email protected]/mytopic?priority=5")
48
49 err := service.Initialize(serviceURL, logger)
50 gomega.Expect(err).NotTo(gomega.HaveOccurred())
51 gomega.Expect(service.Config).NotTo(gomega.BeNil())
52 gomega.Expect(service.Config.Host).To(gomega.Equal("ntfy.example.com"))
53 gomega.Expect(service.Config.Topic).To(gomega.Equal("mytopic"))
54 gomega.Expect(service.Config.Username).To(gomega.Equal("user"))
55 gomega.Expect(service.Config.Password).To(gomega.Equal("pass"))
56 gomega.Expect(service.Config.Priority).To(gomega.Equal(PriorityMax))
57 })
58
59 ginkgo.It("should return an error when topic is missing", func() {
60 serviceURL := mustParseURL("ntfy://ntfy.example.com/")
61
62 err := service.Initialize(serviceURL, logger)
63 gomega.Expect(err).To(gomega.Equal(ErrTopicRequired))
64 })
65
66 ginkgo.It("should skip topic validation for dummy URL", func() {
67 serviceURL := mustParseURL("ntfy://[email protected]")
68
69 err := service.Initialize(serviceURL, logger)
70 gomega.Expect(err).NotTo(gomega.HaveOccurred())
71 })
72
73 ginkgo.It("should force HTTP scheme when DisableTLS is set", func() {
74 serviceURL := mustParseURL("ntfy://ntfy.example.com/mytopic?disabletls=yes")
75
76 err := service.Initialize(serviceURL, logger)
77 gomega.Expect(err).NotTo(gomega.HaveOccurred())
78 gomega.Expect(service.Config.Scheme).To(gomega.Equal("http"))
79 })
80
81 ginkgo.It("should create HTTP client with TLS 1.2 minimum when TLS is enabled", func() {
82 serviceURL := mustParseURL("ntfy://ntfy.example.com/mytopic")
83
84 err := service.Initialize(serviceURL, logger)
85 gomega.Expect(err).NotTo(gomega.HaveOccurred())
86 gomega.Expect(service.httpClient).NotTo(gomega.BeNil())
87 })
88
89 ginkgo.It("should create HTTP client with InsecureSkipVerify when DisableTLSVerification is set", func() {
90 serviceURL := mustParseURL("ntfy://ntfy.example.com/mytopic?disabletlsverification=yes")
91
92 err := service.Initialize(serviceURL, logger)
93 gomega.Expect(err).NotTo(gomega.HaveOccurred())
94 gomega.Expect(service.httpClient).NotTo(gomega.BeNil())
95 })
96
97 ginkgo.It("should use existing HTTP client when already set", func() {
98 existingClient := &http.Client{}
99 service.httpClient = existingClient
100 serviceURL := mustParseURL("ntfy://ntfy.example.com/mytopic")
101
102 err := service.Initialize(serviceURL, logger)
103 gomega.Expect(err).NotTo(gomega.HaveOccurred())
104 gomega.Expect(service.httpClient).To(gomega.BeIdenticalTo(existingClient))
105 })
106
107 ginkgo.It("should create jsonclient from HTTP client", func() {
108 serviceURL := mustParseURL("ntfy://ntfy.example.com/mytopic")
109
110 err := service.Initialize(serviceURL, logger)
111 gomega.Expect(err).NotTo(gomega.HaveOccurred())
112 gomega.Expect(service.client).NotTo(gomega.BeNil())
113 })
114 })
115
116 ginkgo.Describe("Send", func() {
117 ginkgo.BeforeEach(func() {
118 mockJSON.On("Headers").Return(http.Header{})
119 })
120
121 ginkgo.It("should send message via sendAPI", func() {
122 serviceURL := mustParseURL("ntfy://ntfy.example.com/mytopic")
123 gomega.Expect(service.Initialize(serviceURL, logger)).NotTo(gomega.HaveOccurred())
124 service.client = mockJSON
125
126 mockJSON.On("Post", mock.Anything, mock.Anything, mock.Anything).
127 Return(nil)
128
129 err := service.Send("hello", nil)
130 gomega.Expect(err).NotTo(gomega.HaveOccurred())
131 })
132
133 ginkgo.It("should update config from params before sending", func() {
134 serviceURL := mustParseURL("ntfy://ntfy.example.com/mytopic")
135 gomega.Expect(service.Initialize(serviceURL, logger)).NotTo(gomega.HaveOccurred())
136 service.client = mockJSON
137
138 mockJSON.On("Post", mock.Anything, mock.Anything, mock.Anything).
139 Return(nil)
140
141 params := &types.Params{"title": "New Title"}
142 err := service.Send("hello", params)
143 gomega.Expect(err).NotTo(gomega.HaveOccurred())
144 gomega.Expect(service.Config.Title).To(gomega.Equal("New Title"))
145 })
146
147 ginkgo.It("should return error when sendAPI fails", func() {
148 serviceURL := mustParseURL("ntfy://ntfy.example.com/mytopic")
149 gomega.Expect(service.Initialize(serviceURL, logger)).NotTo(gomega.HaveOccurred())
150 service.client = mockJSON
151
152 mockJSON.On("Post", mock.Anything, mock.Anything, mock.Anything).
153 Return(io.ErrClosedPipe)
154 mockJSON.EXPECT().ErrorResponse(mock.Anything, mock.Anything).
155 Return(false)
156
157 err := service.Send("hello", nil)
158 gomega.Expect(err).To(gomega.HaveOccurred())
159 })
160 })
161
162 ginkgo.Describe("SetHTTPClient", func() {
163 ginkgo.It("should set the HTTP client and recreate jsonclient", func() {
164 newClient := &http.Client{}
165 service.SetHTTPClient(newClient)
166 gomega.Expect(service.httpClient).To(gomega.BeIdenticalTo(newClient))
167 gomega.Expect(service.client).NotTo(gomega.BeNil())
168 })
169
170 ginkgo.It("should not recreate jsonclient when client is nil", func() {
171 existingJSON := newMockJSONClient()
172 service.client = existingJSON
173 service.SetHTTPClient(nil)
174 gomega.Expect(service.httpClient).To(gomega.BeNil())
175 gomega.Expect(service.client).To(gomega.BeIdenticalTo(existingJSON))
176 })
177 })
178
179 ginkgo.Describe("sendAPI", func() {
180 var headers http.Header
181
182 ginkgo.BeforeEach(func() {
183 service.Config = &Config{
184 Scheme: "https",
185 Host: "ntfy.example.com",
186 Topic: "mytopic",
187 }
188 headers = http.Header{}
189 mockJSON.On("Headers").Return(headers)
190 })
191
192 ginkgo.It("should set Content-Type to text/plain by default", func() {
193 mockJSON.On("Post", mock.Anything, mock.Anything, mock.Anything).
194 Return(nil)
195
196 err := service.sendAPI(service.Config, "hello")
197 gomega.Expect(err).NotTo(gomega.HaveOccurred())
198 gomega.Expect(headers.Get("Content-Type")).To(gomega.Equal("text/plain; charset=utf-8"))
199 })
200
201 ginkgo.It("should set Content-Type to text/markdown when Markdown is enabled", func() {
202 service.Config.Markdown = true
203
204 mockJSON.On("Post", mock.Anything, mock.Anything, mock.Anything).
205 Return(nil)
206
207 err := service.sendAPI(service.Config, "**hello**")
208 gomega.Expect(err).NotTo(gomega.HaveOccurred())
209 gomega.Expect(headers.Get("Content-Type")).To(gomega.Equal("text/markdown"))
210 })
211
212 ginkgo.It("should set Content-Type to text/plain when Markdown is disabled", func() {
213 service.Config.Markdown = false
214
215 mockJSON.On("Post", mock.Anything, mock.Anything, mock.Anything).
216 Return(nil)
217
218 err := service.sendAPI(service.Config, "hello")
219 gomega.Expect(err).NotTo(gomega.HaveOccurred())
220 gomega.Expect(headers.Get("Content-Type")).To(gomega.Equal("text/plain; charset=utf-8"))
221 })
222
223 ginkgo.It("should set User-Agent header with shoutrrr version", func() {
224 mockJSON.On("Post", mock.Anything, mock.Anything, mock.Anything).
225 Return(nil)
226
227 err := service.sendAPI(service.Config, "hello")
228 gomega.Expect(err).NotTo(gomega.HaveOccurred())
229 gomega.Expect(headers.Get("User-Agent")).To(gomega.ContainSubstring("shoutrrr/"))
230 })
231
232 ginkgo.It("should set Title header when configured", func() {
233 service.Config.Title = "Alert"
234
235 mockJSON.On("Post", mock.Anything, mock.Anything, mock.Anything).
236 Return(nil)
237
238 err := service.sendAPI(service.Config, "hello")
239 gomega.Expect(err).NotTo(gomega.HaveOccurred())
240 gomega.Expect(headers.Get("Title")).To(gomega.Equal("Alert"))
241 })
242
243 ginkgo.It("should set Priority header when configured", func() {
244 service.Config.Priority = PriorityHigh
245
246 mockJSON.On("Post", mock.Anything, mock.Anything, mock.Anything).
247 Return(nil)
248
249 err := service.sendAPI(service.Config, "hello")
250 gomega.Expect(err).NotTo(gomega.HaveOccurred())
251 gomega.Expect(headers.Get("Priority")).To(gomega.Equal("High"))
252 })
253
254 ginkgo.It("should set Tags header as comma-separated list", func() {
255 service.Config.Tags = []string{"warning", "skull"}
256
257 mockJSON.On("Post", mock.Anything, mock.Anything, mock.Anything).
258 Return(nil)
259
260 err := service.sendAPI(service.Config, "hello")
261 gomega.Expect(err).NotTo(gomega.HaveOccurred())
262 gomega.Expect(headers.Get("Tags")).To(gomega.Equal("warning,skull"))
263 })
264
265 ginkgo.It("should set Basic Auth header when username and password are provided", func() {
266 service.Config.Username = "user"
267 service.Config.Password = "pass"
268
269 mockJSON.On("Post", mock.Anything, mock.Anything, mock.Anything).
270 Return(nil)
271
272 err := service.sendAPI(service.Config, "hello")
273 gomega.Expect(err).NotTo(gomega.HaveOccurred())
274 gomega.Expect(headers.Get("Authorization")).To(gomega.HavePrefix("Basic "))
275 })
276
277 ginkgo.It("should set Cache header to no when Cache is disabled", func() {
278 service.Config.Cache = false
279
280 mockJSON.On("Post", mock.Anything, mock.Anything, mock.Anything).
281 Return(nil)
282
283 err := service.sendAPI(service.Config, "hello")
284 gomega.Expect(err).NotTo(gomega.HaveOccurred())
285 gomega.Expect(headers.Get("Cache")).To(gomega.Equal("no"))
286 })
287
288 ginkgo.It("should set Firebase header to no when Firebase is disabled", func() {
289 service.Config.Firebase = false
290
291 mockJSON.On("Post", mock.Anything, mock.Anything, mock.Anything).
292 Return(nil)
293
294 err := service.sendAPI(service.Config, "hello")
295 gomega.Expect(err).NotTo(gomega.HaveOccurred())
296 gomega.Expect(headers.Get("Firebase")).To(gomega.Equal("no"))
297 })
298
299 ginkgo.It("should set Actions header when configured", func() {
300 service.Config.Actions = []string{"view", "open"}
301
302 mockJSON.On("Post", mock.Anything, mock.Anything, mock.Anything).
303 Return(nil)
304
305 err := service.sendAPI(service.Config, "hello")
306 gomega.Expect(err).NotTo(gomega.HaveOccurred())
307 gomega.Expect(headers.Get("Actions")).To(gomega.Equal("view;open"))
308 })
309
310 ginkgo.It("should set Delay header when configured", func() {
311 service.Config.Delay = "2h"
312
313 mockJSON.On("Post", mock.Anything, mock.Anything, mock.Anything).
314 Return(nil)
315
316 err := service.sendAPI(service.Config, "hello")
317 gomega.Expect(err).NotTo(gomega.HaveOccurred())
318 gomega.Expect(headers.Get("Delay")).To(gomega.Equal("2h"))
319 })
320
321 ginkgo.It("should set Click header when configured", func() {
322 service.Config.Click = "https://example.com"
323
324 mockJSON.On("Post", mock.Anything, mock.Anything, mock.Anything).
325 Return(nil)
326
327 err := service.sendAPI(service.Config, "hello")
328 gomega.Expect(err).NotTo(gomega.HaveOccurred())
329 gomega.Expect(headers.Get("Click")).To(gomega.Equal("https://example.com"))
330 })
331
332 ginkgo.It("should set Attach header when configured", func() {
333 service.Config.Attach = "https://example.com/image.png"
334
335 mockJSON.On("Post", mock.Anything, mock.Anything, mock.Anything).
336 Return(nil)
337
338 err := service.sendAPI(service.Config, "hello")
339 gomega.Expect(err).NotTo(gomega.HaveOccurred())
340 gomega.Expect(headers.Get("Attach")).To(gomega.Equal("https://example.com/image.png"))
341 })
342
343 ginkgo.It("should set X-Icon header when configured", func() {
344 service.Config.Icon = "https://example.com/icon.png"
345
346 mockJSON.On("Post", mock.Anything, mock.Anything, mock.Anything).
347 Return(nil)
348
349 err := service.sendAPI(service.Config, "hello")
350 gomega.Expect(err).NotTo(gomega.HaveOccurred())
351 gomega.Expect(headers.Get("X-Icon")).To(gomega.Equal("https://example.com/icon.png"))
352 })
353
354 ginkgo.It("should set Filename header when configured", func() {
355 service.Config.Filename = "document.pdf"
356
357 mockJSON.On("Post", mock.Anything, mock.Anything, mock.Anything).
358 Return(nil)
359
360 err := service.sendAPI(service.Config, "hello")
361 gomega.Expect(err).NotTo(gomega.HaveOccurred())
362 gomega.Expect(headers.Get("Filename")).To(gomega.Equal("document.pdf"))
363 })
364
365 ginkgo.It("should set Email header when configured", func() {
366 service.Config.Email = "[email protected]"
367
368 mockJSON.On("Post", mock.Anything, mock.Anything, mock.Anything).
369 Return(nil)
370
371 err := service.sendAPI(service.Config, "hello")
372 gomega.Expect(err).NotTo(gomega.HaveOccurred())
373 gomega.Expect(headers.Get("Email")).To(gomega.Equal("[email protected]"))
374 })
375
376 ginkgo.It("should call Post with the API URL and message body", func() {
377 mockJSON.On("Post", service.Config.GetAPIURL(), "hello", mock.Anything).
378 Return(nil)
379
380 err := service.sendAPI(service.Config, "hello")
381 gomega.Expect(err).NotTo(gomega.HaveOccurred())
382 })
383
384 ginkgo.It("should set Basic Auth header with username only", func() {
385 service.Config.Username = "user"
386 service.Config.Password = ""
387
388 mockJSON.On("Post", mock.Anything, mock.Anything, mock.Anything).
389 Return(nil)
390
391 err := service.sendAPI(service.Config, "hello")
392 gomega.Expect(err).NotTo(gomega.HaveOccurred())
393 gomega.Expect(headers.Get("Authorization")).To(gomega.HavePrefix("Basic "))
394 })
395
396 ginkgo.It("should set Basic Auth header with password only", func() {
397 service.Config.Username = ""
398 service.Config.Password = "pass"
399
400 mockJSON.On("Post", mock.Anything, mock.Anything, mock.Anything).
401 Return(nil)
402
403 err := service.sendAPI(service.Config, "hello")
404 gomega.Expect(err).NotTo(gomega.HaveOccurred())
405 gomega.Expect(headers.Get("Authorization")).To(gomega.HavePrefix("Basic "))
406 })
407
408 ginkgo.It("should send empty message without error", func() {
409 mockJSON.On("Post", mock.Anything, mock.Anything, mock.Anything).
410 Return(nil)
411
412 err := service.sendAPI(service.Config, "")
413 gomega.Expect(err).NotTo(gomega.HaveOccurred())
414 })
415
416 ginkgo.It("should set single tag without comma separator", func() {
417 service.Config.Tags = []string{"warning"}
418
419 mockJSON.On("Post", mock.Anything, mock.Anything, mock.Anything).
420 Return(nil)
421
422 err := service.sendAPI(service.Config, "hello")
423 gomega.Expect(err).NotTo(gomega.HaveOccurred())
424 gomega.Expect(headers.Get("Tags")).To(gomega.Equal("warning"))
425 })
426
427 ginkgo.It("should return error when Post fails", func() {
428 mockJSON.On("Post", mock.Anything, mock.Anything, mock.Anything).
429 Return(io.ErrClosedPipe)
430 mockJSON.On("ErrorResponse", mock.Anything, mock.Anything).
431 Return(false)
432
433 err := service.sendAPI(service.Config, "hello")
434 gomega.Expect(err).To(gomega.HaveOccurred())
435 })
436
437 ginkgo.It("should return apiResponseError when API returns structured error", func() {
438 responseBody := `{"code":400,"error":"invalid request","link":"https://docs.ntfy.sh"}`
439
440 mockJSON.On("Post", mock.Anything, mock.Anything, mock.Anything).
441 Return(jsonclient.Error{
442 StatusCode: 400,
443 Body: responseBody,
444 })
445 mockJSON.EXPECT().ErrorResponse(mock.Anything, mock.Anything).
446 RunAndReturn(func(_ error, response any) bool {
447 if resp, ok := response.(*apiResponseError); ok {
448 resp.Code = 400
449 resp.Message = "invalid request"
450 resp.Link = "https://docs.ntfy.sh"
451 }
452
453 return true
454 })
455
456 err := service.sendAPI(service.Config, "hello")
457 gomega.Expect(err).To(gomega.HaveOccurred())
458 gomega.Expect(err.Error()).To(gomega.ContainSubstring("invalid request"))
459 })
460
461 ginkgo.It("should return wrapped error when Post fails and ErrorResponse parsing fails", func() {
462 mockJSON.On("Post", mock.Anything, mock.Anything, mock.Anything).
463 Return(io.ErrClosedPipe)
464 mockJSON.On("ErrorResponse", mock.Anything, mock.Anything).
465 Return(false)
466
467 err := service.sendAPI(service.Config, "hello")
468 gomega.Expect(err).To(gomega.HaveOccurred())
469 gomega.Expect(err.Error()).To(gomega.ContainSubstring("posting to ntfy API"))
470 })
471 })
472 })
473
474 var _ = ginkgo.Describe("addHeaderIfNotEmpty", func() {
475 ginkgo.It("should add header when value is non-empty", func() {
476 headers := http.Header{}
477 addHeaderIfNotEmpty(&headers, "X-Test", "value")
478 gomega.Expect(headers.Get("X-Test")).To(gomega.Equal("value"))
479 })
480
481 ginkgo.It("should not add header when value is empty", func() {
482 headers := http.Header{}
483 addHeaderIfNotEmpty(&headers, "X-Test", "")
484 gomega.Expect(headers.Get("X-Test")).To(gomega.Equal(""))
485 })
486
487 ginkgo.It("should append multiple values for the same header", func() {
488 headers := http.Header{}
489 addHeaderIfNotEmpty(&headers, "X-Test", "value1")
490 addHeaderIfNotEmpty(&headers, "X-Test", "value2")
491 gomega.Expect(headers["X-Test"]).To(gomega.Equal([]string{"value1", "value2"}))
492 })
493 })
494
495 var _ = ginkgo.Describe("TLS configuration", func() {
496 var (
497 svc *Service
498 testLogger types.StdLogger
499 )
500
501 ginkgo.BeforeEach(func() {
502 testLogger = &noOpLogger{}
503 })
504
505 ginkgo.Describe("DisableTLSVerification", func() {
506 ginkgo.It("should set InsecureSkipVerify when DisableTLSVerification is true", func() {
507 svc = &Service{}
508 serviceURL := mustParseURL("ntfy://example.com/test?disabletlsverification=yes")
509 gomega.Expect(svc.Initialize(serviceURL, testLogger)).To(gomega.Succeed())
510 transport := svc.httpClient.(*http.Client).Transport.(*http.Transport)
511 gomega.Expect(transport.TLSClientConfig.InsecureSkipVerify).To(gomega.BeTrue())
512 })
513
514 ginkgo.It("should set DisableTLSVerification config when enabled", func() {
515 svc = &Service{}
516 serviceURL := mustParseURL("ntfy://example.com/test?disabletlsverification=yes")
517 gomega.Expect(svc.Initialize(serviceURL, testLogger)).To(gomega.Succeed())
518 gomega.Expect(svc.Config.DisableTLSVerification).To(gomega.BeTrue())
519 })
520
521 ginkgo.It("should not set InsecureSkipVerify when DisableTLSVerification is false", func() {
522 svc = &Service{}
523 serviceURL := mustParseURL("ntfy://example.com/test")
524 gomega.Expect(svc.Initialize(serviceURL, testLogger)).To(gomega.Succeed())
525 transport := svc.httpClient.(*http.Client).Transport.(*http.Transport)
526 gomega.Expect(transport.TLSClientConfig.InsecureSkipVerify).To(gomega.BeFalse())
527 })
528 })
529
530 ginkgo.Describe("DisableTLS", func() {
531 ginkgo.It("should use HTTP scheme when DisableTLS is true", func() {
532 svc = &Service{}
533 serviceURL := mustParseURL("ntfy://example.com/test?disabletls=yes")
534 gomega.Expect(svc.Initialize(serviceURL, testLogger)).To(gomega.Succeed())
535 gomega.Expect(svc.Config.GetAPIURL()).To(gomega.Equal("http://example.com/test"))
536 })
537
538 ginkgo.It("should use HTTPS scheme when DisableTLS is false", func() {
539 config := &Config{
540 Host: "example.com",
541 Topic: "test",
542 Scheme: "https",
543 DisableTLS: false,
544 }
545 gomega.Expect(config.GetAPIURL()).To(gomega.Equal("https://example.com/test"))
546 })
547 })
548 })
549
550 var _ = ginkgo.Describe("service identification", func() {
551 ginkgo.It("should return the correct service ID", func() {
552 svc := &Service{}
553 gomega.Expect(svc.GetID()).To(gomega.Equal(Scheme))
554 })
555 })
556
557 var _ = ginkgo.Describe("service API compliance", func() {
558 var (
559 svc *Service
560 testLogger types.StdLogger
561 )
562
563 ginkgo.BeforeEach(func() {
564 testLogger = &noOpLogger{}
565 })
566
567 ginkgo.It("should pass config API compliance checks", func() {
568 testutils.TestConfigSetInvalidQueryValue(&Config{}, "ntfy://host/topic?foo=bar")
569 testutils.TestConfigGetInvalidQueryValue(&Config{})
570 testutils.TestConfigSetDefaultValues(&Config{})
571 testutils.TestConfigGetEnumsCount(&Config{}, 1)
572 testutils.TestConfigGetFieldsCount(&Config{}, 18)
573 })
574
575 ginkgo.It("should pass service API compliance checks", func() {
576 svc = &Service{}
577 serviceURL := mustParseURL("ntfy://:devicekey@hostname/testtopic")
578 gomega.Expect(svc.Initialize(serviceURL, testLogger)).To(gomega.Succeed())
579 testutils.TestServiceSetInvalidParamValue(svc, "foo", "bar")
580 })
581 })