-
Notifications
You must be signed in to change notification settings - Fork 61
/
discord_test.go
279 lines (243 loc) · 8.87 KB
/
discord_test.go
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
package discord_test
import (
"fmt"
"log"
"time"
"github.com/containrrr/shoutrrr/internal/testutils"
. "github.com/containrrr/shoutrrr/pkg/services/discord"
"github.com/containrrr/shoutrrr/pkg/types"
"github.com/jarcoal/httpmock"
"net/url"
"os"
"strings"
"testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestDiscord(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Shoutrrr Discord Suite")
}
var (
dummyColors = [types.MessageLevelCount]uint{}
service *Service
envDiscordURL *url.URL
logger *log.Logger
)
var _ = Describe("the discord service", func() {
BeforeSuite(func() {
service = &Service{}
envDiscordURL, _ = url.Parse(os.Getenv("SHOUTRRR_DISCORD_URL"))
logger = log.New(GinkgoWriter, "Test", log.LstdFlags)
})
When("running integration tests", func() {
It("should work without errors", func() {
if envDiscordURL.String() == "" {
return
}
serviceURL, _ := url.Parse(envDiscordURL.String())
err := service.Initialize(serviceURL, testutils.TestLogger())
Expect(err).NotTo(HaveOccurred())
err = service.Send(
"this is an integration test",
nil,
)
Expect(err).NotTo(HaveOccurred())
})
})
Describe("the service", func() {
It("should implement Service interface", func() {
var impl types.Service = service
Expect(impl).ToNot(BeNil())
})
})
Describe("creating a config", func() {
When("given an url and a message", func() {
It("should return an error if no arguments where supplied", func() {
serviceURL, _ := url.Parse("discord://")
err := service.Initialize(serviceURL, nil)
Expect(err).To(HaveOccurred())
})
It("should not return an error if exactly two arguments are given", func() {
serviceURL, _ := url.Parse("discord://dummyToken@dummyChannel")
err := service.Initialize(serviceURL, nil)
Expect(err).NotTo(HaveOccurred())
})
It("should not return an error when given the raw path parameter", func() {
serviceURL, _ := url.Parse("discord://dummyToken@dummyChannel/raw")
err := service.Initialize(serviceURL, nil)
Expect(err).NotTo(HaveOccurred())
})
It("should set the JSON flag when given the raw path parameter", func() {
serviceURL, _ := url.Parse("discord://dummyToken@dummyChannel/raw")
config := Config{}
err := config.SetURL(serviceURL)
Expect(err).NotTo(HaveOccurred())
Expect(config.JSON).To(BeTrue())
})
It("should not set the JSON flag when not provided raw path parameter", func() {
serviceURL, _ := url.Parse("discord://dummyToken@dummyChannel")
config := Config{}
err := config.SetURL(serviceURL)
Expect(err).NotTo(HaveOccurred())
Expect(config.JSON).NotTo(BeTrue())
})
It("should return an error if more than two arguments are given", func() {
serviceURL, _ := url.Parse("discord://dummyToken@dummyChannel/illegal-argument")
err := service.Initialize(serviceURL, nil)
Expect(err).To(HaveOccurred())
})
})
When("parsing the configuration URL", func() {
It("should be identical after de-/serialization", func() {
testURL := "discord://token@channel?avatar=TestBot.jpg&color=0x112233&colordebug=0x223344&colorerror=0x334455&colorinfo=0x445566&colorwarn=0x556677&splitlines=No&title=Test+Title&username=TestBot"
url, err := url.Parse(testURL)
Expect(err).NotTo(HaveOccurred(), "parsing")
config := &Config{}
err = config.SetURL(url)
Expect(err).NotTo(HaveOccurred(), "verifying")
outputURL := config.GetURL()
Expect(outputURL.String()).To(Equal(testURL))
})
})
})
Describe("creating a json payload", func() {
When("given a blank message", func() {
When("split lines is enabled", func() {
It("should return an error", func() {
items, omitted := CreateItemsFromPlain("", true)
Expect(items).To(BeEmpty())
_, err := CreatePayloadFromItems(items, "title", dummyColors, omitted)
Expect(err).To(HaveOccurred())
})
})
When("split lines is disabled", func() {
It("should return an error", func() {
items, omitted := CreateItemsFromPlain("", false)
Expect(items).To(BeEmpty())
_, err := CreatePayloadFromItems(items, "title", dummyColors, omitted)
Expect(err).To(HaveOccurred())
})
})
})
When("given a message that exceeds the max length", func() {
It("should return a payload with chunked messages", func() {
payload, err := buildPayloadFromHundreds(42, false, "Title", dummyColors)
Expect(err).ToNot(HaveOccurred())
meta := payload.Embeds[0]
items := payload.Embeds[1:]
Expect(items).To(HaveLen(3))
Expect(items[0].Content).To(HaveLen(1994))
Expect(items[1].Content).To(HaveLen(1999))
Expect(items[2].Content).To(HaveLen(205))
Expect(meta.Footer).To(BeNil())
})
It("omit characters above total max", func() {
payload, err := buildPayloadFromHundreds(62, false, "", dummyColors)
Expect(err).ToNot(HaveOccurred())
meta := payload.Embeds[0]
items := payload.Embeds[1:]
Expect(items).To(HaveLen(4))
Expect(items[0].Content).To(HaveLen(1994))
Expect(items[1].Content).To(HaveLen(1999))
Expect(len(items[2].Content)).To(Equal(1999))
Expect(len(items[3].Content)).To(Equal(5))
Expect(meta.Footer.Text).To(ContainSubstring("200"))
})
When("no title is supplied and content fits", func() {
It("should return a payload without a meta chunk", func() {
payload, err := buildPayloadFromHundreds(42, false, "", dummyColors)
Expect(err).ToNot(HaveOccurred())
Expect(payload.Embeds[0].Footer).To(BeNil())
Expect(payload.Embeds[0].Title).To(BeEmpty())
})
})
When("no title is supplied but content was omitted", func() {
It("should return a payload with a meta chunk", func() {
payload, err := buildPayloadFromHundreds(62, false, "", dummyColors)
Expect(err).ToNot(HaveOccurred())
Expect(payload.Embeds[0].Footer).ToNot(BeNil())
})
})
When("title is supplied, but content fits", func() {
It("should return a payload with a meta chunk", func() {
payload, err := buildPayloadFromHundreds(42, false, "Title", dummyColors)
Expect(err).ToNot(HaveOccurred())
Expect(payload.Embeds[0].Title).ToNot(BeEmpty())
})
})
It("rich test 1", func() {
testTime, _ := time.Parse(time.RFC3339, time.RFC3339)
items := []types.MessageItem{
{
Text: "Message",
Timestamp: testTime,
Level: types.Warning,
},
}
payload, err := CreatePayloadFromItems(items, "Title", dummyColors, 0)
Expect(err).ToNot(HaveOccurred())
meta := payload.Embeds[0]
item := payload.Embeds[1]
Expect(payload.Embeds).To(HaveLen(2))
Expect(item.Footer.Text).To(Equal(types.Warning.String()))
Expect(meta.Title).To(Equal("Title"))
Expect(item.Color).To(Equal(dummyColors[types.Warning]))
})
})
})
Describe("sending the payload", func() {
var dummyConfig = Config{
WebhookID: "1",
Token: "dummyToken",
}
var service Service
BeforeEach(func() {
httpmock.Activate()
service = Service{}
if err := service.Initialize(dummyConfig.GetURL(), logger); err != nil {
panic(fmt.Errorf("service initialization failed: %v", err))
}
})
AfterEach(func() {
httpmock.DeactivateAndReset()
})
It("should not report an error if the server accepts the payload", func() {
setupResponder(&dummyConfig, 204, "")
Expect(service.Send("Message", nil)).To(Succeed())
})
It("should report an error if the server response is not OK", func() {
setupResponder(&dummyConfig, 400, "")
Expect(service.Initialize(dummyConfig.GetURL(), logger)).To(Succeed())
Expect(service.Send("Message", nil)).NotTo(Succeed())
})
It("should report an error if the message is empty", func() {
setupResponder(&dummyConfig, 204, "")
Expect(service.Initialize(dummyConfig.GetURL(), logger)).To(Succeed())
Expect(service.Send("", nil)).NotTo(Succeed())
})
When("using a custom json payload", func() {
It("should report an error if the server response is not OK", func() {
config := dummyConfig
config.JSON = true
setupResponder(&config, 400, "")
Expect(service.Initialize(config.GetURL(), logger)).To(Succeed())
Expect(service.Send("Message", nil)).NotTo(Succeed())
})
})
})
})
func buildPayloadFromHundreds(hundreds int, split bool, title string, colors [types.MessageLevelCount]uint) (WebhookPayload, error) {
hundredChars := "this string is exactly (to the letter) a hundred characters long which will make the send func error"
builder := strings.Builder{}
for i := 0; i < hundreds; i++ {
builder.WriteString(hundredChars)
}
items, omitted := CreateItemsFromPlain(builder.String(), split)
println("Items:", len(items), "Omitted:", omitted)
return CreatePayloadFromItems(items, title, colors, omitted)
}
func setupResponder(config *Config, code int, body string) {
targetURL := CreateAPIURLFromConfig(config)
httpmock.RegisterResponder("POST", targetURL, httpmock.NewStringResponder(code, body))
}