Debian Codegraph @

pkg/services/chat/zulip/zulip_config_test.go

raw
1 package zulip
2
3 import (
4 "net/url"
5
6 "github.com/onsi/gomega"
7
8 ginkgo "github.com/onsi/ginkgo/v2"
9
10 "github.com/nicholas-fedor/shoutrrr/internal/testutils"
11 )
12
13 var _ = ginkgo.Describe("Config", func() {
14 ginkgo.Describe("Clone", func() {
15 ginkgo.It("should create an independent copy with all fields preserved", func() {
16 original := &Config{
17 BotMail: "[email protected]",
18 BotKey: "secret-key",
19 Host: "zulip.example.com",
20 Type: MessageTypeChannel,
21 Stream: "general",
22 Topic: "announcements",
23 Title: "Deployment",
24 To: "[email protected],[email protected]",
25 ReadBySender: true,
26 }
27
28 clone := original.Clone()
29
30 gomega.Expect(clone).NotTo(gomega.BeNil())
31 gomega.Expect(clone.BotMail).To(gomega.Equal(original.BotMail))
32 gomega.Expect(clone.BotKey).To(gomega.Equal(original.BotKey))
33 gomega.Expect(clone.Host).To(gomega.Equal(original.Host))
34 gomega.Expect(clone.Type).To(gomega.Equal(original.Type))
35 gomega.Expect(clone.Stream).To(gomega.Equal(original.Stream))
36 gomega.Expect(clone.Topic).To(gomega.Equal(original.Topic))
37 gomega.Expect(clone.Title).To(gomega.Equal(original.Title))
38 gomega.Expect(clone.To).To(gomega.Equal(original.To))
39 gomega.Expect(clone.ReadBySender).To(gomega.Equal(original.ReadBySender))
40 })
41
42 ginkgo.It("should create a distinct pointer from the original", func() {
43 original := &Config{
44 BotMail: "[email protected]",
45 BotKey: "secret-key",
46 Host: "zulip.example.com",
47 }
48
49 clone := original.Clone()
50
51 gomega.Expect(clone).NotTo(gomega.BeIdenticalTo(original))
52 })
53
54 ginkgo.It("should handle empty config", func() {
55 original := &Config{}
56
57 clone := original.Clone()
58
59 gomega.Expect(clone).NotTo(gomega.BeNil())
60 gomega.Expect(clone.BotMail).To(gomega.BeEmpty())
61 gomega.Expect(clone.BotKey).To(gomega.BeEmpty())
62 gomega.Expect(clone.Host).To(gomega.BeEmpty())
63 gomega.Expect(clone.Type).To(gomega.BeEmpty())
64 gomega.Expect(clone.Stream).To(gomega.BeEmpty())
65 gomega.Expect(clone.Topic).To(gomega.BeEmpty())
66 gomega.Expect(clone.Title).To(gomega.BeEmpty())
67 gomega.Expect(clone.To).To(gomega.BeEmpty())
68 gomega.Expect(clone.ReadBySender).To(gomega.BeFalse())
69 })
70 })
71
72 ginkgo.Describe("GetURL", func() {
73 ginkgo.It("should generate URL with scheme, host, user, and password", func() {
74 cfg := &Config{
75 BotMail: "[email protected]",
76 BotKey: "secret-key",
77 Host: "zulip.example.com",
78 }
79
80 resultURL := cfg.GetURL()
81
82 gomega.Expect(resultURL.Scheme).To(gomega.Equal(Scheme))
83 gomega.Expect(resultURL.Host).To(gomega.Equal("zulip.example.com"))
84 gomega.Expect(resultURL.User.Username()).To(gomega.Equal("[email protected]"))
85 password, isSet := resultURL.User.Password()
86 gomega.Expect(isSet).To(gomega.BeTrue())
87 gomega.Expect(password).To(gomega.Equal("secret-key"))
88 })
89
90 ginkgo.It("should include stream as query parameter when set", func() {
91 cfg := &Config{
92 BotMail: "[email protected]",
93 BotKey: "secret-key",
94 Host: "zulip.example.com",
95 Stream: "general",
96 }
97
98 resultURL := cfg.GetURL()
99
100 gomega.Expect(resultURL.Query().Get("stream")).To(gomega.Equal("general"))
101 })
102
103 ginkgo.It("should include topic as query parameter when set", func() {
104 cfg := &Config{
105 BotMail: "[email protected]",
106 BotKey: "secret-key",
107 Host: "zulip.example.com",
108 Topic: "announcements",
109 }
110
111 resultURL := cfg.GetURL()
112
113 gomega.Expect(resultURL.Query().Get("topic")).To(gomega.Equal("announcements"))
114 })
115
116 ginkgo.It("should include both stream and topic when both are set", func() {
117 cfg := &Config{
118 BotMail: "[email protected]",
119 BotKey: "secret-key",
120 Host: "zulip.example.com",
121 Stream: "general",
122 Topic: "announcements",
123 }
124
125 resultURL := cfg.GetURL()
126
127 gomega.Expect(resultURL.Query().Get("stream")).To(gomega.Equal("general"))
128 gomega.Expect(resultURL.Query().Get("topic")).To(gomega.Equal("announcements"))
129 })
130
131 ginkgo.It("should omit stream and topic query parameters when empty", func() {
132 cfg := &Config{
133 BotMail: "[email protected]",
134 BotKey: "secret-key",
135 Host: "zulip.example.com",
136 }
137
138 resultURL := cfg.GetURL()
139
140 gomega.Expect(resultURL.Query().Get("stream")).To(gomega.BeEmpty())
141 gomega.Expect(resultURL.Query().Get("topic")).To(gomega.BeEmpty())
142 })
143
144 ginkgo.It("should include type, to, and read_by_sender when set for direct message", func() {
145 cfg := &Config{
146 BotMail: "[email protected]",
147 BotKey: "secret-key",
148 Host: "zulip.example.com",
149 Type: MessageTypeDirect,
150 To: "[email protected],[email protected]",
151 ReadBySender: true,
152 }
153
154 resultURL := cfg.GetURL()
155
156 gomega.Expect(resultURL.Query().Get("type")).To(gomega.Equal("direct"))
157 gomega.Expect(resultURL.Query().Get("to")).To(gomega.Equal("[email protected],[email protected]"))
158 gomega.Expect(resultURL.Query().Get("read_by_sender")).To(gomega.Equal("true"))
159 })
160
161 ginkgo.It("should omit read_by_sender when false", func() {
162 cfg := &Config{
163 BotMail: "[email protected]",
164 BotKey: "secret-key",
165 Host: "zulip.example.com",
166 Stream: "general",
167 }
168
169 resultURL := cfg.GetURL()
170
171 gomega.Expect(resultURL.Query().Get("read_by_sender")).To(gomega.BeEmpty())
172 })
173 })
174
175 ginkgo.Describe("SetURL", func() {
176 ginkgo.It("should parse a valid URL with all fields", func() {
177 cfg := &Config{}
178 serviceURL := testutils.URLMust("zulip://[email protected]:[email protected]?stream=general&topic=announcements")
179
180 err := cfg.SetURL(serviceURL)
181
182 gomega.Expect(err).NotTo(gomega.HaveOccurred())
183 gomega.Expect(cfg.BotMail).To(gomega.Equal("[email protected]"))
184 gomega.Expect(cfg.BotKey).To(gomega.Equal("secret-key"))
185 gomega.Expect(cfg.Host).To(gomega.Equal("zulip.example.com"))
186 gomega.Expect(cfg.Stream).To(gomega.Equal("general"))
187 gomega.Expect(cfg.Topic).To(gomega.Equal("announcements"))
188 })
189
190 ginkgo.It("should parse a valid URL with only required fields", func() {
191 cfg := &Config{}
192 serviceURL := testutils.URLMust("zulip://[email protected]:[email protected]")
193
194 err := cfg.SetURL(serviceURL)
195
196 gomega.Expect(err).NotTo(gomega.HaveOccurred())
197 gomega.Expect(cfg.BotMail).To(gomega.Equal("[email protected]"))
198 gomega.Expect(cfg.BotKey).To(gomega.Equal("secret-key"))
199 gomega.Expect(cfg.Host).To(gomega.Equal("zulip.example.com"))
200 gomega.Expect(cfg.Stream).To(gomega.BeEmpty())
201 gomega.Expect(cfg.Topic).To(gomega.BeEmpty())
202 })
203
204 ginkgo.It("should parse type, to, and read_by_sender from URL for direct message", func() {
205 cfg := &Config{}
206 serviceURL := testutils.URLMust("zulip://[email protected]:[email protected]?type=direct&[email protected],[email protected]&read_by_sender=true")
207
208 err := cfg.SetURL(serviceURL)
209
210 gomega.Expect(err).NotTo(gomega.HaveOccurred())
211 gomega.Expect(cfg.Type).To(gomega.Equal(MessageTypeDirect))
212 gomega.Expect(cfg.To).To(gomega.Equal("[email protected],[email protected]"))
213 gomega.Expect(cfg.ReadBySender).To(gomega.BeTrue())
214 })
215
216 ginkgo.It("should default read_by_sender to false when not set to true", func() {
217 cfg := &Config{}
218 serviceURL := testutils.URLMust("zulip://[email protected]:[email protected]?type=channel&stream=general")
219
220 err := cfg.SetURL(serviceURL)
221
222 gomega.Expect(err).NotTo(gomega.HaveOccurred())
223 gomega.Expect(cfg.ReadBySender).To(gomega.BeFalse())
224 })
225
226 ginkgo.It("should reset read_by_sender when parsing a second URL without the parameter", func() {
227 cfg := &Config{}
228 urlWithReadBySender := testutils.URLMust("zulip://[email protected]:[email protected]?read_by_sender=true")
229 err := cfg.SetURL(urlWithReadBySender)
230 gomega.Expect(err).NotTo(gomega.HaveOccurred())
231 gomega.Expect(cfg.ReadBySender).To(gomega.BeTrue())
232
233 urlWithoutReadBySender := testutils.URLMust("zulip://[email protected]:[email protected]")
234 err = cfg.SetURL(urlWithoutReadBySender)
235 gomega.Expect(err).NotTo(gomega.HaveOccurred())
236 gomega.Expect(cfg.ReadBySender).To(gomega.BeFalse())
237 })
238
239 ginkgo.It("should return ErrMissingBotMail when bot mail is empty", func() {
240 cfg := &Config{}
241 serviceURL := testutils.URLMust("zulip://:[email protected]")
242
243 err := cfg.SetURL(serviceURL)
244
245 gomega.Expect(err).To(gomega.MatchError(ErrMissingBotMail))
246 })
247
248 ginkgo.It("should return ErrMissingAPIKey when API key is missing", func() {
249 cfg := &Config{}
250 serviceURL := testutils.URLMust("zulip://[email protected]@zulip.example.com")
251
252 err := cfg.SetURL(serviceURL)
253
254 gomega.Expect(err).To(gomega.MatchError(ErrMissingAPIKey))
255 })
256
257 ginkgo.It("should return ErrMissingHost when host is empty", func() {
258 cfg := &Config{}
259 serviceURL := testutils.URLMust("zulip://[email protected]:secret-key@")
260
261 err := cfg.SetURL(serviceURL)
262
263 gomega.Expect(err).To(gomega.MatchError(ErrMissingHost))
264 })
265
266 ginkgo.It("should accept dummy URL without validation errors", func() {
267 cfg := &Config{}
268 serviceURL := testutils.URLMust("zulip://[email protected]")
269
270 err := cfg.SetURL(serviceURL)
271
272 gomega.Expect(err).NotTo(gomega.HaveOccurred())
273 })
274 })
275
276 ginkgo.Describe("getURL", func() {
277 ginkgo.It("should construct URL with scheme and host from config", func() {
278 cfg := &Config{
279 BotMail: "[email protected]",
280 BotKey: "secret-key",
281 Host: "zulip.example.com",
282 }
283
284 resultURL := cfg.getURL(nil)
285
286 gomega.Expect(resultURL.Scheme).To(gomega.Equal(Scheme))
287 gomega.Expect(resultURL.Host).To(gomega.Equal("zulip.example.com"))
288 })
289
290 ginkgo.It("should set user info from config", func() {
291 cfg := &Config{
292 BotMail: "[email protected]",
293 BotKey: "secret-key",
294 Host: "zulip.example.com",
295 }
296
297 resultURL := cfg.getURL(nil)
298
299 gomega.Expect(resultURL.User.Username()).To(gomega.Equal("[email protected]"))
300 password, isSet := resultURL.User.Password()
301 gomega.Expect(isSet).To(gomega.BeTrue())
302 gomega.Expect(password).To(gomega.Equal("secret-key"))
303 })
304
305 ginkgo.It("should add stream query parameter when stream is set", func() {
306 cfg := &Config{
307 BotMail: "[email protected]",
308 BotKey: "secret-key",
309 Host: "zulip.example.com",
310 Stream: "general",
311 }
312
313 resultURL := cfg.getURL(nil)
314
315 gomega.Expect(resultURL.Query().Get("stream")).To(gomega.Equal("general"))
316 })
317
318 ginkgo.It("should add topic query parameter when topic is set", func() {
319 cfg := &Config{
320 BotMail: "[email protected]",
321 BotKey: "secret-key",
322 Host: "zulip.example.com",
323 Topic: "announcements",
324 }
325
326 resultURL := cfg.getURL(nil)
327
328 gomega.Expect(resultURL.Query().Get("topic")).To(gomega.Equal("announcements"))
329 })
330
331 ginkgo.It("should not add query parameters when stream and topic are empty", func() {
332 cfg := &Config{
333 BotMail: "[email protected]",
334 BotKey: "secret-key",
335 Host: "zulip.example.com",
336 }
337
338 resultURL := cfg.getURL(nil)
339
340 gomega.Expect(resultURL.RawQuery).To(gomega.BeEmpty())
341 })
342 })
343
344 ginkgo.Describe("setURL", func() {
345 ginkgo.It("should populate all fields from a complete URL", func() {
346 cfg := &Config{}
347 serviceURL := testutils.URLMust("zulip://[email protected]:[email protected]?stream=general&topic=announcements")
348
349 err := cfg.setURL(nil, serviceURL)
350
351 gomega.Expect(err).NotTo(gomega.HaveOccurred())
352 gomega.Expect(cfg.BotMail).To(gomega.Equal("[email protected]"))
353 gomega.Expect(cfg.BotKey).To(gomega.Equal("secret-key"))
354 gomega.Expect(cfg.Host).To(gomega.Equal("zulip.example.com"))
355 gomega.Expect(cfg.Stream).To(gomega.Equal("general"))
356 gomega.Expect(cfg.Topic).To(gomega.Equal("announcements"))
357 gomega.Expect(cfg.Type).To(gomega.BeEmpty())
358 gomega.Expect(cfg.To).To(gomega.BeEmpty())
359 gomega.Expect(cfg.ReadBySender).To(gomega.BeFalse())
360 })
361
362 ginkgo.It("should return ErrMissingBotMail when bot mail is empty", func() {
363 cfg := &Config{}
364 serviceURL := testutils.URLMust("zulip://:[email protected]")
365
366 err := cfg.setURL(nil, serviceURL)
367
368 gomega.Expect(err).To(gomega.MatchError(ErrMissingBotMail))
369 })
370
371 ginkgo.It("should return ErrMissingAPIKey when password is not set", func() {
372 cfg := &Config{}
373 serviceURL := testutils.URLMust("zulip://[email protected]@zulip.example.com")
374
375 err := cfg.setURL(nil, serviceURL)
376
377 gomega.Expect(err).To(gomega.MatchError(ErrMissingAPIKey))
378 })
379
380 ginkgo.It("should return ErrMissingHost when host is empty", func() {
381 cfg := &Config{}
382 serviceURL := testutils.URLMust("zulip://[email protected]:secret-key@")
383
384 err := cfg.setURL(nil, serviceURL)
385
386 gomega.Expect(err).To(gomega.MatchError(ErrMissingHost))
387 })
388
389 ginkgo.It("should skip validation for dummy URLs", func() {
390 cfg := &Config{}
391 serviceURL := &url.URL{
392 Scheme: "zulip",
393 Host: "dummy.com",
394 User: url.User("dummy"),
395 }
396
397 err := cfg.setURL(nil, serviceURL)
398
399 gomega.Expect(err).NotTo(gomega.HaveOccurred())
400 })
401 })
402
403 ginkgo.Describe("CreateConfigFromURL", func() {
404 ginkgo.It("should create a valid config from a complete URL", func() {
405 serviceURL := testutils.URLMust("zulip://[email protected]:[email protected]?stream=general&topic=announcements")
406
407 config, err := CreateConfigFromURL(serviceURL)
408
409 gomega.Expect(err).NotTo(gomega.HaveOccurred())
410 gomega.Expect(config).NotTo(gomega.BeNil())
411 gomega.Expect(config.BotMail).To(gomega.Equal("[email protected]"))
412 gomega.Expect(config.BotKey).To(gomega.Equal("secret-key"))
413 gomega.Expect(config.Host).To(gomega.Equal("zulip.example.com"))
414 gomega.Expect(config.Stream).To(gomega.Equal("general"))
415 gomega.Expect(config.Topic).To(gomega.Equal("announcements"))
416 gomega.Expect(config.Type).To(gomega.BeEmpty())
417 gomega.Expect(config.To).To(gomega.BeEmpty())
418 gomega.Expect(config.ReadBySender).To(gomega.BeFalse())
419 })
420
421 ginkgo.It("should create a valid config from URL with direct message fields", func() {
422 serviceURL := testutils.URLMust("zulip://[email protected]:[email protected]?type=direct&[email protected]&read_by_sender=true")
423
424 config, err := CreateConfigFromURL(serviceURL)
425
426 gomega.Expect(err).NotTo(gomega.HaveOccurred())
427 gomega.Expect(config.Type).To(gomega.Equal(MessageTypeDirect))
428 gomega.Expect(config.To).To(gomega.Equal("[email protected]"))
429 gomega.Expect(config.ReadBySender).To(gomega.BeTrue())
430 })
431
432 ginkgo.It("should return an error for invalid URL", func() {
433 serviceURL := testutils.URLMust("zulip://:[email protected]")
434
435 config, err := CreateConfigFromURL(serviceURL)
436
437 gomega.Expect(err).To(gomega.HaveOccurred())
438 gomega.Expect(config).NotTo(gomega.BeNil())
439 })
440
441 ginkgo.It("should create config with only required fields", func() {
442 serviceURL := testutils.URLMust("zulip://[email protected]:[email protected]")
443
444 config, err := CreateConfigFromURL(serviceURL)
445
446 gomega.Expect(err).NotTo(gomega.HaveOccurred())
447 gomega.Expect(config.Stream).To(gomega.BeEmpty())
448 gomega.Expect(config.Topic).To(gomega.BeEmpty())
449 gomega.Expect(config.Type).To(gomega.BeEmpty())
450 gomega.Expect(config.To).To(gomega.BeEmpty())
451 gomega.Expect(config.ReadBySender).To(gomega.BeFalse())
452 })
453 })
454
455 ginkgo.Describe("isDummyURL", func() {
456 ginkgo.It("should return true for the dummy URL", func() {
457 dummyURL := &url.URL{
458 Scheme: "zulip",
459 Host: "dummy.com",
460 User: url.User("dummy"),
461 }
462
463 gomega.Expect(isDummyURL(dummyURL)).To(gomega.BeTrue())
464 })
465
466 ginkgo.It("should return false for a regular URL", func() {
467 regularURL := testutils.URLMust("zulip://[email protected]:[email protected]")
468
469 gomega.Expect(isDummyURL(regularURL)).To(gomega.BeFalse())
470 })
471
472 ginkgo.It("should return false when host matches but user does not", func() {
473 url1 := &url.URL{
474 Scheme: "zulip",
475 Host: "dummy.com",
476 User: url.User("other"),
477 }
478
479 gomega.Expect(isDummyURL(url1)).To(gomega.BeFalse())
480 })
481
482 ginkgo.It("should return false when user matches but host does not", func() {
483 url1 := &url.URL{
484 Scheme: "zulip",
485 Host: "zulip.example.com",
486 User: url.User("dummy"),
487 }
488
489 gomega.Expect(isDummyURL(url1)).To(gomega.BeFalse())
490 })
491 })
492
493 ginkgo.Describe("Port preservation", func() {
494 ginkgo.It("should preserve non-standard port in host", func() {
495 cfg := &Config{}
496 serviceURL := testutils.URLMust("zulip://[email protected]:[email protected]:8443?stream=general")
497
498 err := cfg.SetURL(serviceURL)
499
500 gomega.Expect(err).NotTo(gomega.HaveOccurred())
501 gomega.Expect(cfg.Host).To(gomega.Equal("zulip.example.com:8443"))
502 })
503
504 ginkgo.It("should preserve standard port in host", func() {
505 cfg := &Config{}
506 serviceURL := testutils.URLMust("zulip://[email protected]:[email protected]:443")
507
508 err := cfg.SetURL(serviceURL)
509
510 gomega.Expect(err).NotTo(gomega.HaveOccurred())
511 gomega.Expect(cfg.Host).To(gomega.Equal("zulip.example.com:443"))
512 })
513
514 ginkgo.It("should include port in GetURL round-trip", func() {
515 cfg := &Config{
516 BotMail: "[email protected]",
517 BotKey: "secret-key",
518 Host: "zulip.example.com:8443",
519 Stream: "general",
520 Topic: "alerts",
521 }
522
523 resultURL := cfg.GetURL()
524
525 gomega.Expect(resultURL.Host).To(gomega.Equal("zulip.example.com:8443"))
526 })
527 })
528 })