Debian Codegraph @

pkg/services/chat/mattermost/mattermost_test.go

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