File: mattermost_test.go

package info (click to toggle)
golang-github-nicholas-fedor-shoutrrr 0.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,680 kB
  • sloc: sh: 74; makefile: 58
file content (440 lines) | stat: -rw-r--r-- 15,566 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package mattermost

import (
	"fmt"
	"net/url"
	"os"
	"testing"

	"github.com/jarcoal/httpmock"
	"github.com/onsi/ginkgo/v2"
	"github.com/onsi/gomega"

	"github.com/nicholas-fedor/shoutrrr/internal/testutils"
	"github.com/nicholas-fedor/shoutrrr/pkg/types"
)

var (
	service          *Service
	envMattermostURL *url.URL
	_                = ginkgo.BeforeSuite(func() {
		service = &Service{}
		envMattermostURL, _ = url.Parse(os.Getenv("SHOUTRRR_MATTERMOST_URL"))
	})
)

func TestMattermost(t *testing.T) {
	gomega.RegisterFailHandler(ginkgo.Fail)
	ginkgo.RunSpecs(t, "Shoutrrr Mattermost Suite")
}

var _ = ginkgo.Describe("the mattermost service", func() {
	ginkgo.When("running integration tests", func() {
		ginkgo.It("should work without errors", func() {
			if envMattermostURL.String() == "" {
				return
			}
			serviceURL, _ := url.Parse(envMattermostURL.String())
			gomega.Expect(service.Initialize(serviceURL, testutils.TestLogger())).
				To(gomega.Succeed())
			err := service.Send(
				"this is an integration test",
				nil,
			)
			gomega.Expect(err).NotTo(gomega.HaveOccurred())
		})
	})
	ginkgo.Describe("the mattermost config", func() {
		ginkgo.When("generating a config object", func() {
			mattermostURL, _ := url.Parse(
				"mattermost://mattermost.my-domain.com/thisshouldbeanapitoken",
			)
			config := &Config{}
			err := config.SetURL(mattermostURL)
			ginkgo.It("should not have caused an error", func() {
				gomega.Expect(err).NotTo(gomega.HaveOccurred())
			})
			ginkgo.It("should set host", func() {
				gomega.Expect(config.Host).To(gomega.Equal("mattermost.my-domain.com"))
			})
			ginkgo.It("should set token", func() {
				gomega.Expect(config.Token).To(gomega.Equal("thisshouldbeanapitoken"))
			})
			ginkgo.It("should not set channel or username", func() {
				gomega.Expect(config.Channel).To(gomega.BeEmpty())
				gomega.Expect(config.UserName).To(gomega.BeEmpty())
			})
		})
		ginkgo.When("generating a new config with url, that has no token", func() {
			ginkgo.It("should return an error", func() {
				mattermostURL, _ := url.Parse("mattermost://mattermost.my-domain.com")
				config := &Config{}
				err := config.SetURL(mattermostURL)
				gomega.Expect(err).To(gomega.HaveOccurred())
			})
		})
		ginkgo.When("generating a config object with username only", func() {
			mattermostURL, _ := url.Parse(
				"mattermost://testUserName@mattermost.my-domain.com/thisshouldbeanapitoken",
			)
			config := &Config{}
			err := config.SetURL(mattermostURL)
			ginkgo.It("should not have caused an error", func() {
				gomega.Expect(err).NotTo(gomega.HaveOccurred())
			})
			ginkgo.It("should set username", func() {
				gomega.Expect(config.UserName).To(gomega.Equal("testUserName"))
			})
			ginkgo.It("should not set channel", func() {
				gomega.Expect(config.Channel).To(gomega.BeEmpty())
			})
		})
		ginkgo.When("generating a config object with channel only", func() {
			mattermostURL, _ := url.Parse(
				"mattermost://mattermost.my-domain.com/thisshouldbeanapitoken/testChannel",
			)
			config := &Config{}
			err := config.SetURL(mattermostURL)
			ginkgo.It("should not hav caused an error", func() {
				gomega.Expect(err).NotTo(gomega.HaveOccurred())
			})
			ginkgo.It("should set channel", func() {
				gomega.Expect(config.Channel).To(gomega.Equal("testChannel"))
			})
			ginkgo.It("should not set username", func() {
				gomega.Expect(config.UserName).To(gomega.BeEmpty())
			})
		})
		ginkgo.When("generating a config object with channel an userName", func() {
			mattermostURL, _ := url.Parse(
				"mattermost://testUserName@mattermost.my-domain.com/thisshouldbeanapitoken/testChannel",
			)
			config := &Config{}
			err := config.SetURL(mattermostURL)
			ginkgo.It("should not hav caused an error", func() {
				gomega.Expect(err).NotTo(gomega.HaveOccurred())
			})
			ginkgo.It("should set channel", func() {
				gomega.Expect(config.Channel).To(gomega.Equal("testChannel"))
			})
			ginkgo.It("should set username", func() {
				gomega.Expect(config.UserName).To(gomega.Equal("testUserName"))
			})
		})
		ginkgo.When("using DisableTLS and port", func() {
			mattermostURL, _ := url.Parse(
				"mattermost://watchtower@home.lan:8065/token/channel?disabletls=yes",
			)
			config := &Config{}
			gomega.Expect(config.SetURL(mattermostURL)).To(gomega.Succeed())
			ginkgo.It("should preserve host with port", func() {
				gomega.Expect(config.Host).To(gomega.Equal("home.lan:8065"))
			})
			ginkgo.It("should set DisableTLS", func() {
				gomega.Expect(config.DisableTLS).To(gomega.BeTrue())
			})
			ginkgo.It("should generate http URL", func() {
				gomega.Expect(buildURL(config)).To(gomega.Equal("http://home.lan:8065/hooks/token"))
			})
			ginkgo.It("should serialize back correctly", func() {
				gomega.Expect(config.GetURL().String()).
					To(gomega.Equal("mattermost://watchtower@home.lan:8065/token/channel?disabletls=Yes"))
			})
		})
		ginkgo.Describe("initializing with DisableTLS", func() {
			ginkgo.BeforeEach(func() {
				httpmock.Activate()
			})
			ginkgo.AfterEach(func() {
				httpmock.DeactivateAndReset()
			})
			ginkgo.It("should use plain HTTP transport when DisableTLS is true", func() {
				mattermostURL, _ := url.Parse("mattermost://user@host:8080/token?disabletls=yes")
				service := &Service{}
				err := service.Initialize(mattermostURL, testutils.TestLogger())
				gomega.Expect(err).NotTo(gomega.HaveOccurred())

				httpmock.ActivateNonDefault(service.httpClient)
				httpmock.RegisterResponder(
					"POST",
					"http://host:8080/hooks/token",
					httpmock.NewStringResponder(200, ""),
				)

				err = service.Send("Test message", nil)
				gomega.Expect(err).NotTo(gomega.HaveOccurred())
				gomega.Expect(buildURL(service.Config)).
					To(gomega.Equal("http://host:8080/hooks/token"))
			})
		})

		ginkgo.Describe("sending the payload", func() {
			var err error
			ginkgo.BeforeEach(func() {
				httpmock.Activate()
			})
			ginkgo.AfterEach(func() {
				httpmock.DeactivateAndReset()
			})
			ginkgo.It("should not report an error if the server accepts the payload", func() {
				config := Config{
					Host:  "mattermost.host",
					Token: "token",
				}
				serviceURL := config.GetURL()
				service := Service{}
				err = service.Initialize(serviceURL, nil)
				gomega.Expect(err).NotTo(gomega.HaveOccurred())
				httpmock.ActivateNonDefault(service.httpClient)
				httpmock.RegisterResponder(
					"POST",
					"https://mattermost.host/hooks/token",
					httpmock.NewStringResponder(200, ""),
				)
				err = service.Send("Message", nil)
				gomega.Expect(err).NotTo(gomega.HaveOccurred())
			})
			ginkgo.It("should return an error if the server rejects the payload", func() {
				config := Config{
					Host:  "mattermost.host",
					Token: "token",
				}
				serviceURL := config.GetURL()
				service := Service{}
				err = service.Initialize(serviceURL, nil)
				gomega.Expect(err).NotTo(gomega.HaveOccurred())
				httpmock.ActivateNonDefault(service.httpClient)
				httpmock.RegisterResponder(
					"POST",
					"https://mattermost.host/hooks/token",
					httpmock.NewStringResponder(403, "Forbidden"),
				)
				err = service.Send("Message", nil)
				gomega.Expect(err).To(gomega.HaveOccurred())
				gomega.Expect(err.Error()).
					To(gomega.ContainSubstring("failed to send notification to service"))
				resp := httpmock.NewStringResponse(403, "Forbidden")
				resp.Status = "403 Forbidden"
				httpmock.RegisterResponder(
					"POST",
					"https://mattermost.host/hooks/token",
					httpmock.ResponderFromResponse(resp),
				)
			})
		})
	})

	ginkgo.When("generating a config object", func() {
		ginkgo.It("should not set icon", func() {
			slackURL, _ := url.Parse("mattermost://AAAAAAAAA/BBBBBBBBB")
			config, configError := CreateConfigFromURL(slackURL)

			gomega.Expect(configError).NotTo(gomega.HaveOccurred())
			gomega.Expect(config.Icon).To(gomega.BeEmpty())
		})
		ginkgo.It("should set icon", func() {
			slackURL, _ := url.Parse("mattermost://AAAAAAAAA/BBBBBBBBB?icon=test")
			config, configError := CreateConfigFromURL(slackURL)

			gomega.Expect(configError).NotTo(gomega.HaveOccurred())
			gomega.Expect(config.Icon).To(gomega.BeIdenticalTo("test"))
		})
	})
	ginkgo.Describe("creating the payload", func() {
		ginkgo.Describe("the icon fields", func() {
			payload := JSON{}
			ginkgo.It("should set IconURL when the configured icon looks like an URL", func() {
				payload.SetIcon("https://example.com/logo.png")
				gomega.Expect(payload.IconURL).To(gomega.Equal("https://example.com/logo.png"))
				gomega.Expect(payload.IconEmoji).To(gomega.BeEmpty())
			})
			ginkgo.It(
				"should set IconEmoji when the configured icon does not look like an URL",
				func() {
					payload.SetIcon("tanabata_tree")
					gomega.Expect(payload.IconEmoji).To(gomega.Equal("tanabata_tree"))
					gomega.Expect(payload.IconURL).To(gomega.BeEmpty())
				},
			)
			ginkgo.It("should clear both fields when icon is empty", func() {
				payload.SetIcon("")
				gomega.Expect(payload.IconEmoji).To(gomega.BeEmpty())
				gomega.Expect(payload.IconURL).To(gomega.BeEmpty())
			})
		})
	})
	ginkgo.Describe("Sending messages", func() {
		ginkgo.When("sending a message completely without parameters", func() {
			mattermostURL, _ := url.Parse(
				"mattermost://mattermost.my-domain.com/thisshouldbeanapitoken",
			)
			config := &Config{}
			gomega.Expect(config.SetURL(mattermostURL)).To(gomega.Succeed())
			ginkgo.It("should generate the correct url to call", func() {
				generatedURL := buildURL(config)
				gomega.Expect(generatedURL).
					To(gomega.Equal("https://mattermost.my-domain.com/hooks/thisshouldbeanapitoken"))
			})
			ginkgo.It("should generate the correct JSON body", func() {
				json, err := CreateJSONPayload(config, "this is a message", nil)
				gomega.Expect(err).NotTo(gomega.HaveOccurred())
				gomega.Expect(string(json)).To(gomega.Equal("{\"text\":\"this is a message\"}"))
			})
		})
		ginkgo.When("sending a message with pre set username and channel", func() {
			mattermostURL, _ := url.Parse(
				"mattermost://testUserName@mattermost.my-domain.com/thisshouldbeanapitoken/testChannel",
			)
			config := &Config{}
			gomega.Expect(config.SetURL(mattermostURL)).To(gomega.Succeed())
			ginkgo.It("should generate the correct JSON body", func() {
				json, err := CreateJSONPayload(config, "this is a message", nil)
				gomega.Expect(err).NotTo(gomega.HaveOccurred())
				gomega.Expect(string(json)).
					To(gomega.Equal("{\"text\":\"this is a message\",\"username\":\"testUserName\",\"channel\":\"testChannel\"}"))
			})
		})
		ginkgo.When(
			"sending a message with pre set username and channel but overwriting them with parameters",
			func() {
				mattermostURL, _ := url.Parse(
					"mattermost://testUserName@mattermost.my-domain.com/thisshouldbeanapitoken/testChannel",
				)
				config := &Config{}
				gomega.Expect(config.SetURL(mattermostURL)).To(gomega.Succeed())
				ginkgo.It("should generate the correct JSON body", func() {
					params := (*types.Params)(
						&map[string]string{
							"username": "overwriteUserName",
							"channel":  "overwriteChannel",
						},
					)
					json, err := CreateJSONPayload(config, "this is a message", params)
					gomega.Expect(err).NotTo(gomega.HaveOccurred())
					gomega.Expect(string(json)).
						To(gomega.Equal("{\"text\":\"this is a message\",\"username\":\"overwriteUserName\",\"channel\":\"overwriteChannel\"}"))
				})
			},
		)
	})

	ginkgo.When("parsing the configuration URL", func() {
		ginkgo.It("should be identical after de-/serialization", func() {
			input := "mattermost://bot@mattermost.host/token/channel"

			config := &Config{}
			gomega.Expect(config.SetURL(testutils.URLMust(input))).To(gomega.Succeed())
			gomega.Expect(config.GetURL().String()).To(gomega.Equal(input))
		})
	})

	ginkgo.Describe("creating configurations", func() {
		ginkgo.When("given a url with channel field", func() {
			ginkgo.It("should not throw an error", func() {
				serviceURL := testutils.URLMust(`mattermost://user@mockserver/atoken/achannel`)
				gomega.Expect((&Config{}).SetURL(serviceURL)).To(gomega.Succeed())
			})
		})
		ginkgo.When("given a url with title prop", func() {
			ginkgo.It("should not throw an error", func() {
				serviceURL := testutils.URLMust(
					`mattermost://user@mockserver/atoken?icon=https%3A%2F%2Fexample%2Fsomething.png`,
				)
				gomega.Expect((&Config{}).SetURL(serviceURL)).To(gomega.Succeed())
			})
		})
		ginkgo.When("given a url with all fields and props", func() {
			ginkgo.It("should not throw an error", func() {
				serviceURL := testutils.URLMust(
					`mattermost://user@mockserver/atoken/achannel?icon=https%3A%2F%2Fexample%2Fsomething.png`,
				)
				gomega.Expect((&Config{}).SetURL(serviceURL)).To(gomega.Succeed())
			})
		})
		ginkgo.When("given a url with invalid props", func() {
			ginkgo.It("should return an error", func() {
				serviceURL := testutils.URLMust(`matrix://user@mockserver/atoken?foo=bar`)
				gomega.Expect((&Config{}).SetURL(serviceURL)).To(gomega.HaveOccurred())
			})
		})
		ginkgo.When("parsing the configuration URL", func() {
			ginkgo.It("should be identical after de-/serialization", func() {
				testURL := "mattermost://user@mockserver/atoken/achannel?icon=something"

				url, err := url.Parse(testURL)
				gomega.Expect(err).NotTo(gomega.HaveOccurred(), "parsing")

				config := &Config{}
				err = config.SetURL(url)
				gomega.Expect(err).NotTo(gomega.HaveOccurred(), "verifying")

				outputURL := config.GetURL()
				_, _ = fmt.Fprint(ginkgo.GinkgoWriter, outputURL.String(), " ", testURL, "\n")

				gomega.Expect(outputURL.String()).To(gomega.Equal(testURL))
			})
		})
	})

	ginkgo.Describe("sending the payload", func() {
		var err error
		ginkgo.BeforeEach(func() {
			httpmock.Activate()
		})
		ginkgo.AfterEach(func() {
			httpmock.DeactivateAndReset()
		})
		ginkgo.It("should not report an error if the server accepts the payload", func() {
			config := Config{
				Host:  "mattermost.host",
				Token: "token",
			}
			serviceURL := config.GetURL()
			service := Service{}
			err = service.Initialize(serviceURL, nil)
			gomega.Expect(err).NotTo(gomega.HaveOccurred())
			httpmock.ActivateNonDefault(service.httpClient)

			httpmock.RegisterResponder(
				"POST",
				"https://mattermost.host/hooks/token",
				httpmock.NewStringResponder(200, ``),
			)

			err = service.Send("Message", nil)
			gomega.Expect(err).NotTo(gomega.HaveOccurred())
		})
	})

	ginkgo.Describe("the basic service API", func() {
		ginkgo.Describe("the service config", func() {
			ginkgo.It("should implement basic service config API methods correctly", func() {
				testutils.TestConfigGetInvalidQueryValue(&Config{})

				testutils.TestConfigSetDefaultValues(&Config{})

				testutils.TestConfigGetEnumsCount(&Config{}, 0)
				testutils.TestConfigGetFieldsCount(&Config{}, 5)
			})
		})
		ginkgo.Describe("the service instance", func() {
			ginkgo.BeforeEach(func() {
				httpmock.Activate()
			})
			ginkgo.AfterEach(func() {
				httpmock.DeactivateAndReset()
			})
			ginkgo.It("should implement basic service API methods correctly", func() {
				serviceURL := testutils.URLMust("mattermost://mockhost/mocktoken")
				gomega.Expect(service.Initialize(serviceURL, testutils.TestLogger())).
					To(gomega.Succeed())
				testutils.TestServiceSetInvalidParamValue(service, "foo", "bar")
			})
		})
	})

	ginkgo.It("should return the correct service ID", func() {
		service := &Service{}
		gomega.Expect(service.GetID()).To(gomega.Equal("mattermost"))
	})
})