File: teams_test.go

package info (click to toggle)
golang-github-nicholas-fedor-shoutrrr 0.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,432 kB
  • sloc: sh: 74; makefile: 5
file content (265 lines) | stat: -rw-r--r-- 9,429 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
package teams

import (
	"errors"
	"log"
	"net/url"
	"testing"

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

const (
	extraIDValue     = "V2ESyij_gAljSoUQHvZoZYzlpAoAXExyOl26dlf1xHEx05"
	scopedWebhookURL = "https://test.webhook.office.com/webhookb2/11111111-4444-4444-8444-cccccccccccc@22222222-4444-4444-8444-cccccccccccc/IncomingWebhook/33333301222222222233333333333344/44444444-4444-4444-8444-cccccccccccc/" + extraIDValue
	scopedDomainHost = "test.webhook.office.com"
	testURLBase      = "teams://11111111-4444-4444-8444-cccccccccccc@22222222-4444-4444-8444-cccccccccccc/33333301222222222233333333333344/44444444-4444-4444-8444-cccccccccccc/" + extraIDValue
	scopedURLBase    = testURLBase + "?host=" + scopedDomainHost
)

var logger = log.New(ginkgo.GinkgoWriter, "Test", log.LstdFlags)

// TestTeams runs the test suite for the Teams package.
func TestTeams(t *testing.T) {
	gomega.RegisterFailHandler(ginkgo.Fail)
	ginkgo.RunSpecs(t, "Shoutrrr Teams Suite")
}

var _ = ginkgo.Describe("the teams service", func() {
	ginkgo.When("creating the webhook URL", func() {
		ginkgo.It("should match the expected output for custom URLs", func() {
			config := Config{}
			config.setFromWebhookParts([5]string{
				"11111111-4444-4444-8444-cccccccccccc",
				"22222222-4444-4444-8444-cccccccccccc",
				"33333301222222222233333333333344",
				"44444444-4444-4444-8444-cccccccccccc",
				extraIDValue,
			})
			apiURL := BuildWebhookURL(
				scopedDomainHost,
				config.Group,
				config.Tenant,
				config.AltID,
				config.GroupOwner,
				config.ExtraID,
			)
			gomega.Expect(apiURL).To(gomega.Equal(scopedWebhookURL))

			parts, err := ParseAndVerifyWebhookURL(apiURL)
			gomega.Expect(err).ToNot(gomega.HaveOccurred())
			gomega.Expect(parts).To(gomega.Equal(config.WebhookParts()))
		})
	})

	ginkgo.Describe("creating a config", func() {
		ginkgo.When("parsing the configuration URL", func() {
			ginkgo.It("should be identical after de-/serialization", func() {
				testURL := testURLBase + "?color=aabbcc&host=test.webhook.office.com&title=Test+title"
				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()
				gomega.Expect(outputURL.String()).To(gomega.Equal(testURL))
			})
		})
	})

	ginkgo.Describe("converting custom URL to service URL", func() {
		ginkgo.When("an invalid custom URL is provided", func() {
			ginkgo.It("should return an error", func() {
				service := Service{}
				testURL := "teams+https://google.com/search?q=what+is+love"
				customURL, err := url.Parse(testURL)
				gomega.Expect(err).NotTo(gomega.HaveOccurred(), "parsing")

				_, err = service.GetConfigURLFromCustom(customURL)
				gomega.Expect(err).To(gomega.HaveOccurred(), "converting")
			})
		})
		ginkgo.When("a valid custom URL is provided", func() {
			ginkgo.It("should set the host field from the custom URL", func() {
				service := Service{}
				testURL := `teams+` + scopedWebhookURL
				customURL, err := url.Parse(testURL)
				gomega.Expect(err).NotTo(gomega.HaveOccurred(), "parsing")

				serviceURL, err := service.GetConfigURLFromCustom(customURL)
				gomega.Expect(err).NotTo(gomega.HaveOccurred(), "converting")
				gomega.Expect(serviceURL.String()).To(gomega.Equal(scopedURLBase))
			})
			ginkgo.It("should preserve the query params in the generated service URL", func() {
				service := Service{}
				testURL := "teams+" + scopedWebhookURL + "?color=f008c1&title=TheTitle"
				customURL, err := url.Parse(testURL)
				gomega.Expect(err).NotTo(gomega.HaveOccurred(), "parsing")

				serviceURL, err := service.GetConfigURLFromCustom(customURL)
				gomega.Expect(err).NotTo(gomega.HaveOccurred(), "converting")
				expectedURL := testURLBase + "?color=f008c1&host=test.webhook.office.com&title=TheTitle"
				gomega.Expect(serviceURL.String()).To(gomega.Equal(expectedURL))
			})
		})
	})

	ginkgo.Describe("sending the payload", func() {
		var err error
		var service Service
		ginkgo.BeforeEach(func() {
			httpmock.Activate()
		})
		ginkgo.AfterEach(func() {
			httpmock.DeactivateAndReset()
		})
		ginkgo.It("should not report an error if the server accepts the payload", func() {
			serviceURL, _ := url.Parse(scopedURLBase)
			err = service.Initialize(serviceURL, logger)
			gomega.Expect(err).NotTo(gomega.HaveOccurred())

			httpmock.RegisterResponder(
				"POST",
				scopedWebhookURL,
				httpmock.NewStringResponder(200, ""),
			)
			err = service.Send("Message", nil)
			gomega.Expect(err).NotTo(gomega.HaveOccurred())
		})
		ginkgo.It("should not panic if an error occurs when sending the payload", func() {
			serviceURL, _ := url.Parse(testURLBase + "?host=test.webhook.office.com")
			err = service.Initialize(serviceURL, logger)
			gomega.Expect(err).NotTo(gomega.HaveOccurred())

			httpmock.RegisterResponder(
				"POST",
				scopedWebhookURL,
				httpmock.NewErrorResponder(errors.New("dummy error")),
			)
			err = service.Send("Message", nil)
			gomega.Expect(err).To(gomega.HaveOccurred())
		})
	})

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

	// Config tests
	ginkgo.Describe("the teams config", func() {
		ginkgo.Describe("setURL", func() {
			ginkgo.It("should set all fields correctly from URL", func() {
				config := &Config{}
				urlStr := testURLBase + "?title=Test&color=red&host=test.webhook.office.com"
				parsedURL, err := url.Parse(urlStr)
				gomega.Expect(err).NotTo(gomega.HaveOccurred())

				err = config.SetURL(parsedURL)
				gomega.Expect(err).NotTo(gomega.HaveOccurred())

				gomega.Expect(config.Group).To(gomega.Equal("11111111-4444-4444-8444-cccccccccccc"))
				gomega.Expect(config.Tenant).
					To(gomega.Equal("22222222-4444-4444-8444-cccccccccccc"))
				gomega.Expect(config.AltID).To(gomega.Equal("33333301222222222233333333333344"))
				gomega.Expect(config.GroupOwner).
					To(gomega.Equal("44444444-4444-4444-8444-cccccccccccc"))
				gomega.Expect(config.ExtraID).To(gomega.Equal(extraIDValue))
				gomega.Expect(config.Title).To(gomega.Equal("Test"))
				gomega.Expect(config.Color).To(gomega.Equal("red"))
				gomega.Expect(config.Host).To(gomega.Equal("test.webhook.office.com"))
			})

			ginkgo.It("should reject URLs missing the extraID", func() {
				config := &Config{}
				urlStr := "teams://11111111-4444-4444-8444-cccccccccccc@22222222-4444-4444-8444-cccccccccccc/33333301222222222233333333333344/44444444-4444-4444-8444-cccccccccccc?host=test.webhook.office.com"
				parsedURL, err := url.Parse(urlStr)
				gomega.Expect(err).NotTo(gomega.HaveOccurred())

				err = config.SetURL(parsedURL)
				gomega.Expect(err).To(gomega.HaveOccurred())
			})

			ginkgo.It("should require the host parameter", func() {
				config := &Config{}
				urlStr := testURLBase
				parsedURL, err := url.Parse(urlStr)
				gomega.Expect(err).NotTo(gomega.HaveOccurred())

				err = config.SetURL(parsedURL)
				gomega.Expect(err).To(gomega.HaveOccurred())
			})
		})

		ginkgo.Describe("getURL", func() {
			ginkgo.It("should generate correct URL with all parameters", func() {
				config := &Config{
					Group:      "11111111-4444-4444-8444-cccccccccccc",
					Tenant:     "22222222-4444-4444-8444-cccccccccccc",
					AltID:      "33333301222222222233333333333344",
					GroupOwner: "44444444-4444-4444-8444-cccccccccccc",
					ExtraID:    extraIDValue,
					Title:      "Test",
					Color:      "red",
					Host:       "test.webhook.office.com",
				}

				urlObj := config.GetURL()
				urlStr := urlObj.String()
				expectedURL := testURLBase + "?color=red&host=test.webhook.office.com&title=Test"
				gomega.Expect(urlStr).To(gomega.Equal(expectedURL))
			})
		})

		ginkgo.Describe("verifyWebhookParts", func() {
			ginkgo.It("should validate correct webhook parts", func() {
				parts := [5]string{
					"11111111-4444-4444-8444-cccccccccccc",
					"22222222-4444-4444-8444-cccccccccccc",
					"33333301222222222233333333333344",
					"44444444-4444-4444-8444-cccccccccccc",
					extraIDValue,
				}
				err := verifyWebhookParts(parts)
				gomega.Expect(err).NotTo(gomega.HaveOccurred())
			})

			ginkgo.It("should reject invalid group ID", func() {
				parts := [5]string{
					"invalid-id",
					"22222222-4444-4444-8444-cccccccccccc",
					"33333333012222222222333333333344",
					"44444444-4444-4444-8444-cccccccccccc",
					extraIDValue,
				}
				err := verifyWebhookParts(parts)
				gomega.Expect(err).To(gomega.HaveOccurred())
			})
		})

		ginkgo.Describe("parseAndVerifyWebhookURL", func() {
			ginkgo.It("should correctly parse valid webhook URL", func() {
				webhookURL := scopedWebhookURL
				parts, err := ParseAndVerifyWebhookURL(webhookURL)
				gomega.Expect(err).NotTo(gomega.HaveOccurred())
				gomega.Expect(parts).To(gomega.Equal([5]string{
					"11111111-4444-4444-8444-cccccccccccc",
					"22222222-4444-4444-8444-cccccccccccc",
					"33333301222222222233333333333344",
					"44444444-4444-4444-8444-cccccccccccc",
					extraIDValue,
				}))
			})

			ginkgo.It("should reject invalid webhook URL", func() {
				webhookURL := "https://teams.microsoft.com/invalid/webhook/url"
				_, err := ParseAndVerifyWebhookURL(webhookURL)
				gomega.Expect(err).To(gomega.HaveOccurred())
			})
		})
	})
})