Debian Codegraph @

pkg/services/chat/mattermost/mattermost_test.go

raw
1 package mattermost
2
3 import (
4 "fmt"
5 "net/url"
6 "os"
7 "testing"
8
9 "github.com/jarcoal/httpmock"
10 "github.com/onsi/ginkgo/v2"
11 "github.com/onsi/gomega"
12
13 "github.com/nicholas-fedor/shoutrrr/internal/testutils"
14 "github.com/nicholas-fedor/shoutrrr/pkg/types"
15 )
16
17 var (
18 service *Service
19 envMattermostURL *url.URL
20 _ = ginkgo.BeforeSuite(func() {
21 service = &Service{}
22 envMattermostURL, _ = url.Parse(os.Getenv("SHOUTRRR_MATTERMOST_URL"))
23 })
24 )
25
26 var _ = ginkgo.Describe("the mattermost service", func() {
27 ginkgo.When("running integration tests", func() {
28 ginkgo.It("should work without errors", func() {
29 if envMattermostURL.String() == "" {
30 return
31 }
32
33 serviceURL, _ := url.Parse(envMattermostURL.String())
34 gomega.Expect(service.Initialize(serviceURL, testutils.TestLogger())).
35 To(gomega.Succeed())
36 err := service.Send(
37 "this is an integration test",
38 nil,
39 )
40 gomega.Expect(err).NotTo(gomega.HaveOccurred())
41 })
42 })
43 ginkgo.Describe("the mattermost config", func() {
44 ginkgo.When("generating a config object", func() {
45 mattermostURL, _ := url.Parse(
46 "mattermost://mattermost.my-domain.com/thisshouldbeanapitoken",
47 )
48 config := &Config{}
49 err := config.SetURL(mattermostURL)
50
51 ginkgo.It("should not have caused an error", func() {
52 gomega.Expect(err).NotTo(gomega.HaveOccurred())
53 })
54 ginkgo.It("should set host", func() {
55 gomega.Expect(config.Host).To(gomega.Equal("mattermost.my-domain.com"))
56 })
57 ginkgo.It("should set token", func() {
58 gomega.Expect(config.Token).To(gomega.Equal("thisshouldbeanapitoken"))
59 })
60 ginkgo.It("should not set channel or username", func() {
61 gomega.Expect(config.Channel).To(gomega.BeEmpty())
62 gomega.Expect(config.UserName).To(gomega.BeEmpty())
63 })
64 })
65 ginkgo.When("generating a new config with url, that has no token", func() {
66 ginkgo.It("should return an error", func() {
67 mattermostURL, _ := url.Parse("mattermost://mattermost.my-domain.com")
68 config := &Config{}
69 err := config.SetURL(mattermostURL)
70 gomega.Expect(err).To(gomega.HaveOccurred())
71 })
72 })
73 ginkgo.When("generating a config object with username only", func() {
74 mattermostURL, _ := url.Parse(
75 "mattermost://[email protected]/thisshouldbeanapitoken",
76 )
77 config := &Config{}
78 err := config.SetURL(mattermostURL)
79
80 ginkgo.It("should not have caused an error", func() {
81 gomega.Expect(err).NotTo(gomega.HaveOccurred())
82 })
83 ginkgo.It("should set username", func() {
84 gomega.Expect(config.UserName).To(gomega.Equal("testUserName"))
85 })
86 ginkgo.It("should not set channel", func() {
87 gomega.Expect(config.Channel).To(gomega.BeEmpty())
88 })
89 })
90 ginkgo.When("generating a config object with channel only", func() {
91 mattermostURL, _ := url.Parse(
92 "mattermost://mattermost.my-domain.com/thisshouldbeanapitoken/testChannel",
93 )
94 config := &Config{}
95 err := config.SetURL(mattermostURL)
96
97 ginkgo.It("should not hav caused an error", func() {
98 gomega.Expect(err).NotTo(gomega.HaveOccurred())
99 })
100 ginkgo.It("should set channel", func() {
101 gomega.Expect(config.Channel).To(gomega.Equal("testChannel"))
102 })
103 ginkgo.It("should not set username", func() {
104 gomega.Expect(config.UserName).To(gomega.BeEmpty())
105 })
106 })
107 ginkgo.When("generating a config object with channel an userName", func() {
108 mattermostURL, _ := url.Parse(
109 "mattermost://[email protected]/thisshouldbeanapitoken/testChannel",
110 )
111 config := &Config{}
112 err := config.SetURL(mattermostURL)
113
114 ginkgo.It("should not hav caused an error", func() {
115 gomega.Expect(err).NotTo(gomega.HaveOccurred())
116 })
117 ginkgo.It("should set channel", func() {
118 gomega.Expect(config.Channel).To(gomega.Equal("testChannel"))
119 })
120 ginkgo.It("should set username", func() {
121 gomega.Expect(config.UserName).To(gomega.Equal("testUserName"))
122 })
123 })
124 ginkgo.When("using DisableTLS and port", func() {
125 mattermostURL, _ := url.Parse(
126 "mattermost://[email protected]:8065/token/channel?disabletls=yes",
127 )
128 config := &Config{}
129 gomega.Expect(config.SetURL(mattermostURL)).To(gomega.Succeed())
130 ginkgo.It("should preserve host with port", func() {
131 gomega.Expect(config.Host).To(gomega.Equal("home.lan:8065"))
132 })
133 ginkgo.It("should set DisableTLS", func() {
134 gomega.Expect(config.DisableTLS).To(gomega.BeTrue())
135 })
136 ginkgo.It("should generate http URL", func() {
137 gomega.Expect(buildURL(config).String()).To(gomega.Equal("http://home.lan:8065/hooks/token"))
138 })
139 ginkgo.It("should serialize back correctly", func() {
140 gomega.Expect(config.GetURL().String()).
141 To(gomega.Equal("mattermost://[email protected]:8065/token/channel?disabletls=Yes"))
142 })
143 })
144 ginkgo.Describe("initializing with DisableTLS", func() {
145 ginkgo.BeforeEach(func() {
146 httpmock.Activate()
147 })
148 ginkgo.AfterEach(func() {
149 httpmock.DeactivateAndReset()
150 })
151 ginkgo.It("should use plain HTTP transport when DisableTLS is true", func() {
152 mattermostURL, _ := url.Parse("mattermost://user@host:8080/token?disabletls=yes")
153 service := &Service{}
154 err := service.Initialize(mattermostURL, testutils.TestLogger())
155 gomega.Expect(err).NotTo(gomega.HaveOccurred())
156
157 httpmock.ActivateNonDefault(service.httpClient)
158 httpmock.RegisterResponder(
159 "POST",
160 "http://host:8080/hooks/token",
161 httpmock.NewStringResponder(200, ""),
162 )
163
164 err = service.Send("Test message", nil)
165 gomega.Expect(err).NotTo(gomega.HaveOccurred())
166 gomega.Expect(buildURL(service.Config).String()).
167 To(gomega.Equal("http://host:8080/hooks/token"))
168 })
169 })
170
171 ginkgo.Describe("sending the payload", func() {
172 var err error
173
174 ginkgo.BeforeEach(func() {
175 httpmock.Activate()
176 })
177 ginkgo.AfterEach(func() {
178 httpmock.DeactivateAndReset()
179 })
180 ginkgo.It("should not report an error if the server accepts the payload", func() {
181 config := Config{
182 Host: "mattermost.host",
183 Token: "token",
184 }
185 serviceURL := config.GetURL()
186 service := Service{}
187 err = service.Initialize(serviceURL, nil)
188 gomega.Expect(err).NotTo(gomega.HaveOccurred())
189 httpmock.ActivateNonDefault(service.httpClient)
190 httpmock.RegisterResponder(
191 "POST",
192 "https://mattermost.host/hooks/token",
193 httpmock.NewStringResponder(200, ""),
194 )
195
196 err = service.Send("Message", nil)
197 gomega.Expect(err).NotTo(gomega.HaveOccurred())
198 })
199 ginkgo.It("should return an error if the server rejects the payload", func() {
200 config := Config{
201 Host: "mattermost.host",
202 Token: "token",
203 }
204 serviceURL := config.GetURL()
205 service := Service{}
206 err = service.Initialize(serviceURL, nil)
207 gomega.Expect(err).NotTo(gomega.HaveOccurred())
208 httpmock.ActivateNonDefault(service.httpClient)
209 httpmock.RegisterResponder(
210 "POST",
211 "https://mattermost.host/hooks/token",
212 httpmock.NewStringResponder(403, "Forbidden"),
213 )
214
215 err = service.Send("Message", nil)
216 gomega.Expect(err).To(gomega.HaveOccurred())
217 gomega.Expect(err.Error()).
218 To(gomega.ContainSubstring("failed to send notification to service"))
219
220 resp := httpmock.NewStringResponse(403, "Forbidden")
221 resp.Status = "403 Forbidden"
222 httpmock.RegisterResponder(
223 "POST",
224 "https://mattermost.host/hooks/token",
225 httpmock.ResponderFromResponse(resp),
226 )
227 })
228 })
229 })
230
231 ginkgo.When("generating a config object", func() {
232 ginkgo.It("should not set icon", func() {
233 slackURL, _ := url.Parse("mattermost://AAAAAAAAA/BBBBBBBBB")
234 config, configError := CreateConfigFromURL(slackURL)
235
236 gomega.Expect(configError).NotTo(gomega.HaveOccurred())
237 gomega.Expect(config.Icon).To(gomega.BeEmpty())
238 })
239 ginkgo.It("should set icon", func() {
240 slackURL, _ := url.Parse("mattermost://AAAAAAAAA/BBBBBBBBB?icon=test")
241 config, configError := CreateConfigFromURL(slackURL)
242
243 gomega.Expect(configError).NotTo(gomega.HaveOccurred())
244 gomega.Expect(config.Icon).To(gomega.BeIdenticalTo("test"))
245 })
246 })
247 ginkgo.Describe("creating the payload", func() {
248 ginkgo.Describe("the icon fields", func() {
249 payload := JSON{}
250
251 ginkgo.It("should set IconURL when the configured icon looks like an URL", func() {
252 payload.SetIcon("https://example.com/logo.png")
253 gomega.Expect(payload.IconURL).To(gomega.Equal("https://example.com/logo.png"))
254 gomega.Expect(payload.IconEmoji).To(gomega.BeEmpty())
255 })
256 ginkgo.It(
257 "should set IconEmoji when the configured icon does not look like an URL",
258 func() {
259 payload.SetIcon("tanabata_tree")
260 gomega.Expect(payload.IconEmoji).To(gomega.Equal("tanabata_tree"))
261 gomega.Expect(payload.IconURL).To(gomega.BeEmpty())
262 },
263 )
264 ginkgo.It("should clear both fields when icon is empty", func() {
265 payload.SetIcon("")
266 gomega.Expect(payload.IconEmoji).To(gomega.BeEmpty())
267 gomega.Expect(payload.IconURL).To(gomega.BeEmpty())
268 })
269 })
270 })
271 ginkgo.Describe("Sending messages", func() {
272 ginkgo.When("sending a message completely without parameters", func() {
273 mattermostURL, _ := url.Parse(
274 "mattermost://mattermost.my-domain.com/thisshouldbeanapitoken",
275 )
276 config := &Config{}
277 gomega.Expect(config.SetURL(mattermostURL)).To(gomega.Succeed())
278 ginkgo.It("should generate the correct url to call", func() {
279 generatedURL := buildURL(config)
280 gomega.Expect(generatedURL.String()).
281 To(gomega.Equal("https://mattermost.my-domain.com/hooks/thisshouldbeanapitoken"))
282 })
283 ginkgo.It("should generate the correct JSON body", func() {
284 json, err := CreateJSONPayload(config, "this is a message", nil)
285 gomega.Expect(err).NotTo(gomega.HaveOccurred())
286 gomega.Expect(string(json)).To(gomega.Equal("{\"text\":\"this is a message\"}"))
287 })
288 })
289 ginkgo.When("sending a message with pre set username and channel", func() {
290 mattermostURL, _ := url.Parse(
291 "mattermost://[email protected]/thisshouldbeanapitoken/testChannel",
292 )
293 config := &Config{}
294 gomega.Expect(config.SetURL(mattermostURL)).To(gomega.Succeed())
295 ginkgo.It("should generate the correct JSON body", func() {
296 json, err := CreateJSONPayload(config, "this is a message", nil)
297 gomega.Expect(err).NotTo(gomega.HaveOccurred())
298 gomega.Expect(string(json)).
299 To(gomega.Equal("{\"text\":\"this is a message\",\"username\":\"testUserName\",\"channel\":\"testChannel\"}"))
300 })
301 })
302 ginkgo.When(
303 "sending a message with pre set username and channel but overwriting them with parameters",
304 func() {
305 mattermostURL, _ := url.Parse(
306 "mattermost://[email protected]/thisshouldbeanapitoken/testChannel",
307 )
308 config := &Config{}
309 gomega.Expect(config.SetURL(mattermostURL)).To(gomega.Succeed())
310 ginkgo.It("should generate the correct JSON body", func() {
311 params := (*types.Params)(
312 &map[string]string{
313 "username": "overwriteUserName",
314 "channel": "overwriteChannel",
315 },
316 )
317 json, err := CreateJSONPayload(config, "this is a message", params)
318 gomega.Expect(err).NotTo(gomega.HaveOccurred())
319 gomega.Expect(string(json)).
320 To(gomega.Equal("{\"text\":\"this is a message\",\"username\":\"overwriteUserName\",\"channel\":\"overwriteChannel\"}"))
321 })
322 },
323 )
324 ginkgo.When("sending a message with a title set in config", func() {
325 mattermostURL, _ := url.Parse(
326 "mattermost://mattermost.my-domain.com/thisshouldbeanapitoken",
327 )
328 config := &Config{}
329 gomega.Expect(config.SetURL(mattermostURL)).To(gomega.Succeed())
330 config.Title = "watchtower TEST"
331
332 ginkgo.It("should prepend the title to the message text", func() {
333 json, err := CreateJSONPayload(config, "this is a message", nil)
334 gomega.Expect(err).NotTo(gomega.HaveOccurred())
335 gomega.Expect(string(json)).
336 To(gomega.Equal("{\"text\":\"watchtower TEST\\nthis is a message\"}"))
337 })
338 })
339 ginkgo.When("sending a message with a title set via params", func() {
340 mattermostURL, _ := url.Parse(
341 "mattermost://mattermost.my-domain.com/thisshouldbeanapitoken",
342 )
343 config := &Config{}
344 gomega.Expect(config.SetURL(mattermostURL)).To(gomega.Succeed())
345 ginkgo.It("should prepend the title to the message text", func() {
346 params := (*types.Params)(
347 &map[string]string{
348 "title": "watchtower PROD",
349 },
350 )
351 json, err := CreateJSONPayload(config, "this is a message", params)
352 gomega.Expect(err).NotTo(gomega.HaveOccurred())
353 gomega.Expect(string(json)).
354 To(gomega.Equal("{\"text\":\"watchtower PROD\\nthis is a message\"}"))
355 })
356 })
357 ginkgo.When("sending a message without a title", func() {
358 mattermostURL, _ := url.Parse(
359 "mattermost://mattermost.my-domain.com/thisshouldbeanapitoken",
360 )
361 config := &Config{}
362 gomega.Expect(config.SetURL(mattermostURL)).To(gomega.Succeed())
363 ginkgo.It("should not prepend anything to the message text", func() {
364 json, err := CreateJSONPayload(config, "this is a message", nil)
365 gomega.Expect(err).NotTo(gomega.HaveOccurred())
366 gomega.Expect(string(json)).To(gomega.Equal("{\"text\":\"this is a message\"}"))
367 })
368 })
369 ginkgo.When("sending a message with title set in both config and params", func() {
370 mattermostURL, _ := url.Parse(
371 "mattermost://mattermost.my-domain.com/thisshouldbeanapitoken",
372 )
373 config := &Config{}
374 gomega.Expect(config.SetURL(mattermostURL)).To(gomega.Succeed())
375 config.Title = "Config Title"
376
377 ginkgo.It("should use the title from params", func() {
378 params := (*types.Params)(
379 &map[string]string{
380 "title": "Params Title",
381 },
382 )
383 json, err := CreateJSONPayload(config, "this is a message", params)
384 gomega.Expect(err).NotTo(gomega.HaveOccurred())
385 gomega.Expect(string(json)).
386 To(gomega.Equal("{\"text\":\"Params Title\\nthis is a message\"}"))
387 })
388 })
389 })
390
391 ginkgo.When("parsing the configuration URL", func() {
392 ginkgo.It("should be identical after de-/serialization", func() {
393 input := "mattermost://[email protected]/token/channel"
394
395 config := &Config{}
396 gomega.Expect(config.SetURL(testutils.URLMust(input))).To(gomega.Succeed())
397 gomega.Expect(config.GetURL().String()).To(gomega.Equal(input))
398 })
399 })
400
401 ginkgo.Describe("creating configurations", func() {
402 ginkgo.When("given a url with channel field", func() {
403 ginkgo.It("should not throw an error", func() {
404 serviceURL := testutils.URLMust(`mattermost://user@mockserver/atoken/achannel`)
405 gomega.Expect((&Config{}).SetURL(serviceURL)).To(gomega.Succeed())
406 })
407 })
408 ginkgo.When("given a url with title prop", func() {
409 ginkgo.It("should not throw an error", func() {
410 serviceURL := testutils.URLMust(
411 `mattermost://user@mockserver/atoken?icon=https%3A%2F%2Fexample%2Fsomething.png`,
412 )
413 gomega.Expect((&Config{}).SetURL(serviceURL)).To(gomega.Succeed())
414 })
415 })
416 ginkgo.When("given a url with all fields and props", func() {
417 ginkgo.It("should not throw an error", func() {
418 serviceURL := testutils.URLMust(
419 `mattermost://user@mockserver/atoken/achannel?icon=https%3A%2F%2Fexample%2Fsomething.png`,
420 )
421 gomega.Expect((&Config{}).SetURL(serviceURL)).To(gomega.Succeed())
422 })
423 })
424 ginkgo.When("given a url with invalid props", func() {
425 ginkgo.It("should return an error", func() {
426 serviceURL := testutils.URLMust(`matrix://user@mockserver/atoken?foo=bar`)
427 gomega.Expect((&Config{}).SetURL(serviceURL)).To(gomega.HaveOccurred())
428 })
429 })
430 ginkgo.When("parsing the configuration URL", func() {
431 ginkgo.It("should be identical after de-/serialization", func() {
432 testURL := "mattermost://user@mockserver/atoken/achannel?icon=something"
433
434 url, err := url.Parse(testURL)
435 gomega.Expect(err).NotTo(gomega.HaveOccurred(), "parsing")
436
437 config := &Config{}
438 err = config.SetURL(url)
439 gomega.Expect(err).NotTo(gomega.HaveOccurred(), "verifying")
440
441 outputURL := config.GetURL()
442 _, _ = fmt.Fprint(ginkgo.GinkgoWriter, outputURL.String(), " ", testURL, "\n")
443
444 gomega.Expect(outputURL.String()).To(gomega.Equal(testURL))
445 })
446 })
447 })
448
449 ginkgo.Describe("sending the payload", func() {
450 var err error
451
452 ginkgo.BeforeEach(func() {
453 httpmock.Activate()
454 })
455 ginkgo.AfterEach(func() {
456 httpmock.DeactivateAndReset()
457 })
458 ginkgo.It("should not report an error if the server accepts the payload", func() {
459 config := Config{
460 Host: "mattermost.host",
461 Token: "token",
462 }
463 serviceURL := config.GetURL()
464 service := Service{}
465 err = service.Initialize(serviceURL, nil)
466 gomega.Expect(err).NotTo(gomega.HaveOccurred())
467 httpmock.ActivateNonDefault(service.httpClient)
468
469 httpmock.RegisterResponder(
470 "POST",
471 "https://mattermost.host/hooks/token",
472 httpmock.NewStringResponder(200, ``),
473 )
474
475 err = service.Send("Message", nil)
476 gomega.Expect(err).NotTo(gomega.HaveOccurred())
477 })
478 })
479
480 ginkgo.Describe("the basic service API", func() {
481 ginkgo.Describe("the service config", func() {
482 ginkgo.It("should implement basic service config API methods correctly", func() {
483 testutils.TestConfigGetInvalidQueryValue(&Config{})
484
485 testutils.TestConfigSetDefaultValues(&Config{})
486
487 testutils.TestConfigGetEnumsCount(&Config{}, 0)
488 testutils.TestConfigGetFieldsCount(&Config{}, 5)
489 })
490 })
491 ginkgo.Describe("the service instance", func() {
492 ginkgo.BeforeEach(func() {
493 httpmock.Activate()
494 })
495 ginkgo.AfterEach(func() {
496 httpmock.DeactivateAndReset()
497 })
498 ginkgo.It("should implement basic service API methods correctly", func() {
499 serviceURL := testutils.URLMust("mattermost://mockhost/mocktoken")
500 gomega.Expect(service.Initialize(serviceURL, testutils.TestLogger())).
501 To(gomega.Succeed())
502 testutils.TestServiceSetInvalidParamValue(service, "foo", "bar")
503 })
504 })
505 })
506
507 ginkgo.It("should return the correct service ID", func() {
508 service := &Service{}
509 gomega.Expect(service.GetID()).To(gomega.Equal("mattermost"))
510 })
511 })
512
513 func TestMattermost(t *testing.T) {
514 t.Parallel()
515 gomega.RegisterFailHandler(ginkgo.Fail)
516 ginkgo.RunSpecs(t, "Shoutrrr Mattermost Suite")
517 }