forked from getarcaneapp/arcane
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneric_sender_test.go
More file actions
416 lines (388 loc) · 11.2 KB
/
generic_sender_test.go
File metadata and controls
416 lines (388 loc) · 11.2 KB
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
package notifications
import (
"testing"
"github.com/getarcaneapp/arcane/backend/internal/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestBuildGenericURL(t *testing.T) {
tests := []struct {
name string
config models.GenericConfig
wantURL string
wantErr string
}{
{
name: "basic HTTPS webhook",
config: models.GenericConfig{
WebhookURL: "https://webhook.example.com/notify",
},
wantURL: "generic://webhook.example.com/notify?disabletls=no&template=json",
},
{
name: "basic HTTP webhook",
config: models.GenericConfig{
WebhookURL: "http://webhook.example.com/notify",
},
wantURL: "generic://webhook.example.com/notify?disabletls=yes&template=json",
},
{
name: "webhook without scheme defaults to HTTPS",
config: models.GenericConfig{
WebhookURL: "webhook.example.com/notify",
},
wantURL: "generic://webhook.example.com/notify?disabletls=no&template=json",
},
{
name: "webhook without scheme with port",
config: models.GenericConfig{
WebhookURL: "webhook.example.com:8080/notify",
},
wantURL: "generic://webhook.example.com:8080/notify?disabletls=no&template=json",
},
{
name: "webhook without scheme with DisableTLS",
config: models.GenericConfig{
WebhookURL: "webhook.example.com/notify",
DisableTLS: true,
},
wantURL: "generic://webhook.example.com/notify?disabletls=yes&template=json",
},
{
name: "webhook with port",
config: models.GenericConfig{
WebhookURL: "https://webhook.example.com:8443/api/notify",
},
wantURL: "generic://webhook.example.com:8443/api/notify?disabletls=no&template=json",
},
{
name: "webhook with custom content type",
config: models.GenericConfig{
WebhookURL: "https://webhook.example.com/notify",
ContentType: "application/x-www-form-urlencoded",
},
wantURL: "generic://webhook.example.com/notify?contenttype=application%2Fx-www-form-urlencoded&disabletls=no&template=json",
},
{
name: "webhook with POST method",
config: models.GenericConfig{
WebhookURL: "https://webhook.example.com/notify",
Method: "POST",
},
wantURL: "generic://webhook.example.com/notify?disabletls=no&method=POST&template=json",
},
{
name: "webhook with custom title and message keys",
config: models.GenericConfig{
WebhookURL: "https://webhook.example.com/notify",
TitleKey: "subject",
MessageKey: "body",
},
wantURL: "generic://webhook.example.com/notify?disabletls=no&messagekey=body&template=json&titlekey=subject",
},
{
name: "webhook with DisableTLS ignored for HTTPS",
config: models.GenericConfig{
WebhookURL: "https://webhook.example.com/notify",
DisableTLS: true,
},
wantURL: "generic://webhook.example.com/notify?disabletls=no&template=json",
},
{
name: "webhook with single custom header",
config: models.GenericConfig{
WebhookURL: "https://webhook.example.com/notify",
CustomHeaders: map[string]string{
"Authorization": "Bearer token123",
},
},
wantURL: "generic://webhook.example.com/notify?%40Authorization=Bearer+token123&disabletls=no&template=json",
},
{
name: "webhook with multiple custom headers",
config: models.GenericConfig{
WebhookURL: "https://webhook.example.com/notify",
CustomHeaders: map[string]string{
"Authorization": "Bearer token123",
"X-Api-Key": "secret-key",
"X-Source": "Arcane",
},
},
// Note: URL encoding may vary in order due to map iteration
wantURL: "generic://webhook.example.com/notify",
},
{
name: "webhook with all options",
config: models.GenericConfig{
WebhookURL: "https://webhook.example.com:8443/api/v1/notify",
ContentType: "application/json",
Method: "PUT",
TitleKey: "heading",
MessageKey: "content",
DisableTLS: true,
CustomHeaders: map[string]string{
"Authorization": "Bearer token123",
},
},
wantURL: "generic://webhook.example.com:8443/api/v1/notify",
},
{
name: "webhook URL with query params preserved",
config: models.GenericConfig{
WebhookURL: "http://www.pushplus.plus/send?token=abc123",
},
wantURL: "generic://www.pushplus.plus/send?disabletls=yes&template=json&token=abc123",
},
{
name: "webhook URL with multiple query params preserved",
config: models.GenericConfig{
WebhookURL: "https://api.example.com/webhook?token=abc&channel=general",
},
wantURL: "generic://api.example.com/webhook?channel=general&disabletls=no&template=json&token=abc",
},
{
name: "PushPlus webhook with content message key",
config: models.GenericConfig{
WebhookURL: "http://www.pushplus.plus/send?token=abc123",
Method: "POST",
MessageKey: "content",
},
// Shoutrrr's generic service preserves `token=abc123` through to the
// outbound HTTP request untouched, while consuming the config keys
// (disabletls, template, method, messagekey). This is what PushPlus
// needs: POST http://www.pushplus.plus/send?token=abc123 with
// {"title":"...","content":"..."} at the root.
wantURL: "generic://www.pushplus.plus/send?disabletls=yes&messagekey=content&method=POST&template=json&token=abc123",
},
{
name: "empty webhook URL",
config: models.GenericConfig{
WebhookURL: "",
},
wantErr: "webhook URL is empty",
},
{
name: "invalid webhook URL",
config: models.GenericConfig{
WebhookURL: "://invalid-url",
},
wantErr: "invalid webhook URL",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotURL, err := BuildGenericURL(tt.config)
if tt.wantErr != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), tt.wantErr)
return
}
require.NoError(t, err)
// For tests with multiple headers or all options, just check prefix
if tt.name == "webhook with multiple custom headers" || tt.name == "webhook with all options" {
assert.Contains(t, gotURL, tt.wantURL)
} else {
assert.Equal(t, tt.wantURL, gotURL)
}
})
}
}
func TestBuildGenericURL_HTTPSchemeHandling(t *testing.T) {
tests := []struct {
name string
webhookURL string
wantHost string
}{
{
name: "HTTPS URL",
webhookURL: "https://webhook.example.com/notify",
wantHost: "webhook.example.com",
},
{
name: "HTTP URL",
webhookURL: "http://webhook.example.com/notify",
wantHost: "webhook.example.com",
},
{
name: "URL with custom port",
webhookURL: "https://webhook.example.com:9443/notify",
wantHost: "webhook.example.com:9443",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config := models.GenericConfig{
WebhookURL: tt.webhookURL,
}
gotURL, err := BuildGenericURL(config)
require.NoError(t, err)
// Verify the scheme is always "generic"
assert.Contains(t, gotURL, "generic://")
// Verify the host is preserved
assert.Contains(t, gotURL, tt.wantHost)
})
}
}
func TestBuildGenericURL_CustomHeadersEncoding(t *testing.T) {
config := models.GenericConfig{
WebhookURL: "https://webhook.example.com/notify",
CustomHeaders: map[string]string{
"Authorization": "Bearer token-with-special-chars!@#",
"X-Custom-Value": "value with spaces",
},
}
gotURL, err := BuildGenericURL(config)
require.NoError(t, err)
// Verify headers are prefixed with @
assert.Contains(t, gotURL, "%40Authorization=")
assert.Contains(t, gotURL, "%40X-Custom-Value=")
// Verify special characters and spaces are encoded
assert.Contains(t, gotURL, "value+with+spaces")
}
func TestBuildGenericURL_DisableTLSFlag(t *testing.T) {
tests := []struct {
name string
disableTLS bool
wantParam string
}{
{
name: "TLS enabled (default)",
disableTLS: false,
wantParam: "disabletls=no",
},
{
name: "TLS disabled",
disableTLS: true,
wantParam: "disabletls=yes",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config := models.GenericConfig{
WebhookURL: "webhook.example.com/notify",
DisableTLS: tt.disableTLS,
}
gotURL, err := BuildGenericURL(config)
require.NoError(t, err)
assert.Contains(t, gotURL, tt.wantParam)
})
}
}
func TestBuildGenericURL_CustomKeys(t *testing.T) {
tests := []struct {
name string
titleKey string
messageKey string
wantTitle string
wantMsg string
}{
{
name: "default keys (empty)",
titleKey: "",
messageKey: "",
wantTitle: "",
wantMsg: "",
},
{
name: "custom title key only",
titleKey: "subject",
messageKey: "",
wantTitle: "titlekey=subject",
wantMsg: "",
},
{
name: "custom message key only",
titleKey: "",
messageKey: "body",
wantTitle: "",
wantMsg: "messagekey=body",
},
{
name: "both custom keys",
titleKey: "heading",
messageKey: "content",
wantTitle: "titlekey=heading",
wantMsg: "messagekey=content",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config := models.GenericConfig{
WebhookURL: "https://webhook.example.com/notify",
TitleKey: tt.titleKey,
MessageKey: tt.messageKey,
}
gotURL, err := BuildGenericURL(config)
require.NoError(t, err)
if tt.wantTitle != "" {
assert.Contains(t, gotURL, tt.wantTitle)
} else if tt.titleKey == "" {
assert.NotContains(t, gotURL, "titlekey=")
}
if tt.wantMsg != "" {
assert.Contains(t, gotURL, tt.wantMsg)
} else if tt.messageKey == "" {
assert.NotContains(t, gotURL, "messagekey=")
}
})
}
}
// TestBuildGenericURL_PreservesUserShoutrrrConfigKeys verifies that an
// explicit Shoutrrr config key embedded by the user in the webhook URL is
// never silently overwritten by the provider defaults, the configured field
// values, or the URL-scheme-derived TLS flag.
func TestBuildGenericURL_PreservesUserShoutrrrConfigKeys(t *testing.T) {
tests := []struct {
name string
config models.GenericConfig
wantInURL []string
notInURL []string
}{
{
name: "user template wins over default json",
config: models.GenericConfig{
WebhookURL: "https://example.com/api?template=custom",
},
wantInURL: []string{"template=custom"},
notInURL: []string{"template=json"},
},
{
name: "user disabletls wins over scheme-derived value",
config: models.GenericConfig{
WebhookURL: "https://example.com/api?disabletls=yes",
},
wantInURL: []string{"disabletls=yes"},
notInURL: []string{"disabletls=no"},
},
{
name: "user messagekey wins over configured value",
config: models.GenericConfig{
WebhookURL: "https://example.com/api?messagekey=user_msg",
MessageKey: "configured_msg",
},
wantInURL: []string{"messagekey=user_msg"},
notInURL: []string{"messagekey=configured_msg"},
},
{
name: "user method wins over configured value",
config: models.GenericConfig{
WebhookURL: "https://example.com/api?method=PUT",
Method: "POST",
},
wantInURL: []string{"method=PUT"},
notInURL: []string{"method=POST"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotURL, err := BuildGenericURL(tt.config)
require.NoError(t, err)
for _, want := range tt.wantInURL {
assert.Contains(t, gotURL, want)
}
for _, notWant := range tt.notInURL {
assert.NotContains(t, gotURL, notWant)
}
})
}
}