Debian Codegraph @

pkg/services/push/mqtt/mqtt_config_test.go

raw
1 package mqtt
2
3 import (
4 "net/url"
5
6 ginkgo "github.com/onsi/ginkgo/v2"
7 gomega "github.com/onsi/gomega"
8 )
9
10 var _ = ginkgo.Describe("Config", func() {
11 ginkgo.Context("GetURL", func() {
12 ginkgo.It("should build URL with all fields populated", func() {
13 config := &Config{
14 Host: "broker.example.com",
15 Port: 1883,
16 Topic: "notifications/alerts",
17 Username: "testuser",
18 Password: "testpass",
19 ClientID: "shoutrrr-client",
20 QoS: QoSAtLeastOnce,
21 Retained: true,
22 CleanSession: false,
23 DisableTLS: true,
24 DisableTLSVerification: true,
25 }
26
27 result := config.GetURL()
28
29 gomega.Expect(result.Scheme).To(gomega.Equal("mqtt"))
30 gomega.Expect(result.Hostname()).To(gomega.Equal("broker.example.com"))
31 gomega.Expect(result.Port()).To(gomega.Equal("1883"))
32 gomega.Expect(result.Path).To(gomega.Equal("notifications/alerts"))
33 gomega.Expect(result.User.Username()).To(gomega.Equal("testuser"))
34 pass, hasPass := result.User.Password()
35 gomega.Expect(hasPass).To(gomega.BeTrue())
36 gomega.Expect(pass).To(gomega.Equal("testpass"))
37 gomega.Expect(result.Query().Get("clientid")).To(gomega.Equal("shoutrrr-client"))
38 gomega.Expect(result.Query().Get("qos")).To(gomega.Equal("AtLeastOnce"))
39 gomega.Expect(result.Query().Get("retained")).To(gomega.Equal("Yes"))
40 gomega.Expect(result.Query().Get("cleansession")).To(gomega.Equal("No"))
41 gomega.Expect(result.Query().Get("disabletls")).To(gomega.Equal("Yes"))
42 gomega.Expect(result.Query().Get("disabletlsverification")).To(gomega.Equal("Yes"))
43 })
44
45 ginkgo.It("should build URL without credentials", func() {
46 config := &Config{
47 Host: "broker.example.com",
48 Port: 1883,
49 Topic: "notifications/alerts",
50 ClientID: "shoutrrr",
51 QoS: QoSAtMostOnce,
52 }
53
54 result := config.GetURL()
55
56 gomega.Expect(result.User).To(gomega.BeNil())
57 gomega.Expect(result.Hostname()).To(gomega.Equal("broker.example.com"))
58 gomega.Expect(result.Path).To(gomega.Equal("notifications/alerts"))
59 })
60
61 ginkgo.It("should build URL with username only", func() {
62 config := &Config{
63 Host: "broker.example.com",
64 Port: 1883,
65 Topic: "notifications/alerts",
66 Username: "testuser",
67 ClientID: "shoutrrr",
68 QoS: QoSAtMostOnce,
69 }
70
71 result := config.GetURL()
72
73 gomega.Expect(result.User.Username()).To(gomega.Equal("testuser"))
74 _, hasPass := result.User.Password()
75 gomega.Expect(hasPass).To(gomega.BeFalse())
76 })
77
78 ginkgo.It("should build URL with default ports", func() {
79 config := &Config{
80 Host: "broker.example.com",
81 Port: 1883,
82 Topic: "test/topic",
83 ClientID: "shoutrrr",
84 QoS: QoSAtMostOnce,
85 }
86
87 result := config.GetURL()
88
89 gomega.Expect(result.Port()).To(gomega.Equal("1883"))
90 })
91
92 ginkgo.It("should handle special characters in topic", func() {
93 config := &Config{
94 Host: "broker.example.com",
95 Port: 1883,
96 Topic: "notifications/alerts/critical",
97 ClientID: "shoutrrr",
98 QoS: QoSAtMostOnce,
99 }
100
101 result := config.GetURL()
102
103 gomega.Expect(result.Path).To(gomega.Equal("notifications/alerts/critical"))
104 })
105
106 ginkgo.It("should build URL with QoS exactly once", func() {
107 config := &Config{
108 Host: "broker.example.com",
109 Port: 1883,
110 Topic: "test/topic",
111 ClientID: "shoutrrr",
112 QoS: QoSExactlyOnce,
113 }
114
115 result := config.GetURL()
116
117 gomega.Expect(result.Query().Get("qos")).To(gomega.Equal("ExactlyOnce"))
118 })
119
120 ginkgo.It("should return nil when password is provided without username", func() {
121 config := &Config{
122 Host: "broker.example.com",
123 Port: 1883,
124 Topic: "notifications/alerts",
125 Password: "secretpassword",
126 ClientID: "shoutrrr",
127 QoS: QoSAtMostOnce,
128 }
129
130 result := config.GetURL()
131
132 gomega.Expect(result).To(gomega.BeNil())
133 })
134
135 ginkgo.It("should return valid URL when both username and password are provided", func() {
136 config := &Config{
137 Host: "broker.example.com",
138 Port: 1883,
139 Topic: "notifications/alerts",
140 Username: "testuser",
141 Password: "secretpassword",
142 ClientID: "shoutrrr",
143 QoS: QoSAtMostOnce,
144 }
145
146 result := config.GetURL()
147
148 gomega.Expect(result).NotTo(gomega.BeNil())
149 gomega.Expect(result.User.Username()).To(gomega.Equal("testuser"))
150 pass, hasPass := result.User.Password()
151 gomega.Expect(hasPass).To(gomega.BeTrue())
152 gomega.Expect(pass).To(gomega.Equal("secretpassword"))
153 })
154 })
155
156 ginkgo.Context("ValidateCredentials", func() {
157 ginkgo.It("should return error when password is provided without username", func() {
158 config := &Config{
159 Password: "secretpassword",
160 }
161
162 err := config.ValidateCredentials()
163
164 gomega.Expect(err).To(gomega.Equal(ErrPasswordWithoutUsername))
165 })
166
167 ginkgo.It("should return nil when both username and password are provided", func() {
168 config := &Config{
169 Username: "testuser",
170 Password: "secretpassword",
171 }
172
173 err := config.ValidateCredentials()
174
175 gomega.Expect(err).ToNot(gomega.HaveOccurred())
176 })
177
178 ginkgo.It("should return nil when only username is provided", func() {
179 config := &Config{
180 Username: "testuser",
181 }
182
183 err := config.ValidateCredentials()
184
185 gomega.Expect(err).ToNot(gomega.HaveOccurred())
186 })
187
188 ginkgo.It("should return nil when no credentials are provided", func() {
189 config := &Config{}
190
191 err := config.ValidateCredentials()
192
193 gomega.Expect(err).ToNot(gomega.HaveOccurred())
194 })
195 })
196
197 ginkgo.Context("SetURL", func() {
198 ginkgo.It("should parse complete URL with all components", func() {
199 config := &Config{}
200 testURL, err := url.Parse(
201 "mqtt://testuser:[email protected]:1883/notifications/alerts?clientid=myclient&qos=1&retained=yes&cleansession=no&disabletls=yes&disabletlsverification=yes",
202 )
203 gomega.Expect(err).NotTo(gomega.HaveOccurred())
204
205 err = config.SetURL(testURL)
206 gomega.Expect(err).NotTo(gomega.HaveOccurred())
207
208 gomega.Expect(config.Host).To(gomega.Equal("broker.example.com"))
209 gomega.Expect(config.Port).To(gomega.Equal(1883))
210 gomega.Expect(config.Topic).To(gomega.Equal("notifications/alerts"))
211 gomega.Expect(config.Username).To(gomega.Equal("testuser"))
212 gomega.Expect(config.Password).To(gomega.Equal("testpass"))
213 gomega.Expect(config.ClientID).To(gomega.Equal("myclient"))
214 gomega.Expect(config.QoS).To(gomega.Equal(QoSAtLeastOnce))
215 gomega.Expect(config.Retained).To(gomega.BeTrue())
216 gomega.Expect(config.CleanSession).To(gomega.BeFalse())
217 gomega.Expect(config.DisableTLS).To(gomega.BeTrue())
218 gomega.Expect(config.DisableTLSVerification).To(gomega.BeTrue())
219 })
220
221 ginkgo.It("should parse URL without credentials", func() {
222 config := &Config{}
223 testURL, err := url.Parse("mqtt://broker.example.com:1883/notifications/alerts")
224 gomega.Expect(err).NotTo(gomega.HaveOccurred())
225
226 err = config.SetURL(testURL)
227 gomega.Expect(err).NotTo(gomega.HaveOccurred())
228
229 gomega.Expect(config.Username).To(gomega.BeEmpty())
230 gomega.Expect(config.Password).To(gomega.BeEmpty())
231 gomega.Expect(config.Host).To(gomega.Equal("broker.example.com"))
232 gomega.Expect(config.Topic).To(gomega.Equal("notifications/alerts"))
233 })
234
235 ginkgo.It("should parse URL with query parameters for QoS and flags", func() {
236 config := &Config{}
237 testURL, err := url.Parse(
238 "mqtt://broker.example.com:1883/test/topic?qos=2&retained=yes&cleansession=no",
239 )
240 gomega.Expect(err).NotTo(gomega.HaveOccurred())
241
242 err = config.SetURL(testURL)
243 gomega.Expect(err).NotTo(gomega.HaveOccurred())
244
245 gomega.Expect(config.QoS).To(gomega.Equal(QoSExactlyOnce))
246 gomega.Expect(config.Retained).To(gomega.BeTrue())
247 gomega.Expect(config.CleanSession).To(gomega.BeFalse())
248 })
249
250 ginkgo.It("should return error for empty topic", func() {
251 config := &Config{}
252 testURL, err := url.Parse("mqtt://broker.example.com:1883/")
253 gomega.Expect(err).NotTo(gomega.HaveOccurred())
254
255 err = config.SetURL(testURL)
256 gomega.Expect(err).To(gomega.Equal(ErrTopicRequired))
257 })
258
259 ginkgo.It("should handle default port for mqtt scheme", func() {
260 config := &Config{}
261 testURL, err := url.Parse("mqtt://broker.example.com:1883/test/topic")
262 gomega.Expect(err).NotTo(gomega.HaveOccurred())
263
264 err = config.SetURL(testURL)
265 gomega.Expect(err).NotTo(gomega.HaveOccurred())
266
267 gomega.Expect(config.Port).To(gomega.Equal(1883))
268 })
269
270 ginkgo.It("should handle default port for mqtts scheme", func() {
271 config := &Config{}
272 testURL, err := url.Parse("mqtts://broker.example.com/test/topic")
273 gomega.Expect(err).NotTo(gomega.HaveOccurred())
274
275 err = config.SetURL(testURL)
276 gomega.Expect(err).NotTo(gomega.HaveOccurred())
277
278 gomega.Expect(config.Port).To(gomega.Equal(8883))
279 })
280
281 ginkgo.It("should parse client_id from query", func() {
282 config := &Config{}
283 testURL, err := url.Parse(
284 "mqtt://broker.example.com:1883/test/topic?clientid=custom-client-id",
285 )
286 gomega.Expect(err).NotTo(gomega.HaveOccurred())
287
288 err = config.SetURL(testURL)
289 gomega.Expect(err).NotTo(gomega.HaveOccurred())
290
291 gomega.Expect(config.ClientID).To(gomega.Equal("custom-client-id"))
292 })
293
294 ginkgo.It("should parse retained flag from query", func() {
295 config := &Config{}
296 testURL, err := url.Parse("mqtt://broker.example.com:1883/test/topic?retained=yes")
297 gomega.Expect(err).NotTo(gomega.HaveOccurred())
298
299 err = config.SetURL(testURL)
300 gomega.Expect(err).NotTo(gomega.HaveOccurred())
301
302 gomega.Expect(config.Retained).To(gomega.BeTrue())
303 })
304
305 ginkgo.It("should parse clean_session flag from query", func() {
306 config := &Config{}
307 testURL, err := url.Parse("mqtt://broker.example.com:1883/test/topic?cleansession=no")
308 gomega.Expect(err).NotTo(gomega.HaveOccurred())
309
310 err = config.SetURL(testURL)
311 gomega.Expect(err).NotTo(gomega.HaveOccurred())
312
313 gomega.Expect(config.CleanSession).To(gomega.BeFalse())
314 })
315
316 ginkgo.It("should parse disable_tls flag from query", func() {
317 config := &Config{}
318 testURL, err := url.Parse("mqtt://broker.example.com:1883/test/topic?disabletls=yes")
319 gomega.Expect(err).NotTo(gomega.HaveOccurred())
320
321 err = config.SetURL(testURL)
322 gomega.Expect(err).NotTo(gomega.HaveOccurred())
323
324 gomega.Expect(config.DisableTLS).To(gomega.BeTrue())
325 })
326
327 ginkgo.It("should parse disable_tls_verification flag from query", func() {
328 config := &Config{}
329 testURL, err := url.Parse(
330 "mqtt://broker.example.com:1883/test/topic?disabletlsverification=yes",
331 )
332 gomega.Expect(err).NotTo(gomega.HaveOccurred())
333
334 err = config.SetURL(testURL)
335 gomega.Expect(err).NotTo(gomega.HaveOccurred())
336
337 gomega.Expect(config.DisableTLSVerification).To(gomega.BeTrue())
338 })
339
340 ginkgo.It("should return error for invalid QoS value", func() {
341 config := &Config{}
342 testURL, err := url.Parse("mqtt://broker.example.com:1883/test/topic?qos=5")
343 gomega.Expect(err).NotTo(gomega.HaveOccurred())
344
345 err = config.SetURL(testURL)
346 gomega.Expect(err).To(gomega.HaveOccurred())
347 gomega.Expect(err.Error()).To(gomega.ContainSubstring("not a valid enum value"))
348 })
349
350 ginkgo.It("should parse URL with username only", func() {
351 config := &Config{}
352 testURL, err := url.Parse("mqtt://[email protected]:1883/test/topic")
353 gomega.Expect(err).NotTo(gomega.HaveOccurred())
354
355 err = config.SetURL(testURL)
356 gomega.Expect(err).NotTo(gomega.HaveOccurred())
357
358 gomega.Expect(config.Username).To(gomega.Equal("testuser"))
359 gomega.Expect(config.Password).To(gomega.BeEmpty())
360 })
361
362 ginkgo.It("should handle nested topic paths", func() {
363 config := &Config{}
364 testURL, err := url.Parse("mqtt://broker.example.com:1883/level1/level2/level3")
365 gomega.Expect(err).NotTo(gomega.HaveOccurred())
366
367 err = config.SetURL(testURL)
368 gomega.Expect(err).NotTo(gomega.HaveOccurred())
369
370 gomega.Expect(config.Topic).To(gomega.Equal("level1/level2/level3"))
371 })
372
373 ginkgo.It("should skip topic validation for dummy URL", func() {
374 config := &Config{}
375 testURL, err := url.Parse("mqtt://[email protected]")
376 gomega.Expect(err).NotTo(gomega.HaveOccurred())
377
378 err = config.SetURL(testURL)
379 gomega.Expect(err).NotTo(gomega.HaveOccurred())
380 })
381
382 ginkgo.It("should parse QoS using numeric string", func() {
383 config := &Config{}
384 testURL, err := url.Parse("mqtt://broker.example.com:1883/test/topic?qos=0")
385 gomega.Expect(err).NotTo(gomega.HaveOccurred())
386
387 err = config.SetURL(testURL)
388 gomega.Expect(err).NotTo(gomega.HaveOccurred())
389
390 gomega.Expect(config.QoS).To(gomega.Equal(QoSAtMostOnce))
391 })
392
393 ginkgo.It("should parse QoS using string name", func() {
394 config := &Config{}
395 testURL, err := url.Parse("mqtt://broker.example.com:1883/test/topic?qos=AtLeastOnce")
396 gomega.Expect(err).NotTo(gomega.HaveOccurred())
397
398 err = config.SetURL(testURL)
399 gomega.Expect(err).NotTo(gomega.HaveOccurred())
400
401 gomega.Expect(config.QoS).To(gomega.Equal(QoSAtLeastOnce))
402 })
403 })
404
405 ginkgo.Context("QueryFields", func() {
406 ginkgo.It("should return all configurable field names", func() {
407 config := &Config{}
408 fields := config.QueryFields()
409
410 gomega.Expect(fields).To(gomega.ContainElements(
411 "clientid",
412 "qos",
413 "retained",
414 "cleansession",
415 "disabletls",
416 "disabletlsverification",
417 ))
418 })
419
420 ginkgo.It("should return non-empty slice", func() {
421 config := &Config{}
422 fields := config.QueryFields()
423
424 gomega.Expect(fields).NotTo(gomega.BeEmpty())
425 })
426 })
427
428 ginkgo.Context("Enums", func() {
429 ginkgo.It("should return QoS enum formatter", func() {
430 config := &Config{}
431 enums := config.Enums()
432
433 gomega.Expect(enums).To(gomega.HaveKey("QoS"))
434 })
435
436 ginkgo.It("should return map with exactly one entry", func() {
437 config := &Config{}
438 enums := config.Enums()
439
440 gomega.Expect(enums).To(gomega.HaveLen(1))
441 })
442 })
443 })
444
445 var _ = ginkgo.Describe("QoS", func() {
446 ginkgo.Context("String", func() {
447 ginkgo.It("should return 'AtMostOnce' for QoSAtMostOnce (0)", func() {
448 qos := QoSAtMostOnce
449 gomega.Expect(qos.String()).To(gomega.Equal("AtMostOnce"))
450 })
451
452 ginkgo.It("should return 'AtLeastOnce' for QoSAtLeastOnce (1)", func() {
453 qos := QoSAtLeastOnce
454 gomega.Expect(qos.String()).To(gomega.Equal("AtLeastOnce"))
455 })
456
457 ginkgo.It("should return 'ExactlyOnce' for QoSExactlyOnce (2)", func() {
458 qos := QoSExactlyOnce
459 gomega.Expect(qos.String()).To(gomega.Equal("ExactlyOnce"))
460 })
461
462 ginkgo.It("should return formatted string for invalid values", func() {
463 qos := QoS(100)
464 result := qos.String()
465 gomega.Expect(result).NotTo(gomega.BeEmpty())
466 })
467 })
468
469 ginkgo.Context("IsValid", func() {
470 ginkgo.It("should return true for QoSAtMostOnce (0)", func() {
471 qos := QoSAtMostOnce
472 gomega.Expect(qos.IsValid()).To(gomega.BeTrue())
473 })
474
475 ginkgo.It("should return true for QoSAtLeastOnce (1)", func() {
476 qos := QoSAtLeastOnce
477 gomega.Expect(qos.IsValid()).To(gomega.BeTrue())
478 })
479
480 ginkgo.It("should return true for QoSExactlyOnce (2)", func() {
481 qos := QoSExactlyOnce
482 gomega.Expect(qos.IsValid()).To(gomega.BeTrue())
483 })
484
485 ginkgo.It("should return false for invalid value 3", func() {
486 qos := QoS(3)
487 gomega.Expect(qos.IsValid()).To(gomega.BeFalse())
488 })
489
490 ginkgo.It("should return false for invalid value 255", func() {
491 qos := QoS(255)
492 gomega.Expect(qos.IsValid()).To(gomega.BeFalse())
493 })
494
495 ginkgo.It("should return false for negative value", func() {
496 qos := QoS(-1)
497 gomega.Expect(qos.IsValid()).To(gomega.BeFalse())
498 })
499 })
500 })
501
502 var _ = ginkgo.Describe("QoSValues", func() {
503 ginkgo.Context("Enum formatter", func() {
504 ginkgo.It("should have correct AtMostOnce value", func() {
505 gomega.Expect(QoSValues.AtMostOnce).To(gomega.Equal(QoSAtMostOnce))
506 })
507
508 ginkgo.It("should have correct AtLeastOnce value", func() {
509 gomega.Expect(QoSValues.AtLeastOnce).To(gomega.Equal(QoSAtLeastOnce))
510 })
511
512 ginkgo.It("should have correct ExactlyOnce value", func() {
513 gomega.Expect(QoSValues.ExactlyOnce).To(gomega.Equal(QoSExactlyOnce))
514 })
515
516 ginkgo.It("should have non-nil Enum formatter", func() {
517 gomega.Expect(QoSValues.Enum).NotTo(gomega.BeNil())
518 })
519 })
520 })