-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin_test.go
427 lines (378 loc) · 11 KB
/
plugin_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
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
package main
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/gorilla/websocket"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
// MockStorageHandler is a mock for the StorageHandler interface
type MockStorageHandler struct {
mock.Mock
}
func (m *MockStorageHandler) GetString(key string) (string, error) {
args := m.Called(key)
return args.String(0), args.Error(1)
}
func (m *MockStorageHandler) GetInt(key string) (int, error) {
args := m.Called(key)
return args.Int(0), args.Error(1)
}
func (m *MockStorageHandler) GetBool(key string) (bool, error) {
args := m.Called(key)
return args.Bool(0), args.Error(1)
}
func (m *MockStorageHandler) SetString(key string, value string) error {
args := m.Called(key, value)
return args.Error(0)
}
func (m *MockStorageHandler) SetInt(key string, value int) error {
args := m.Called(key, value)
return args.Error(0)
}
func (m *MockStorageHandler) SetBool(key string, value bool) error {
args := m.Called(key, value)
return args.Error(0)
}
func TestMultiNotifierPlugin_ValidateAndSetConfig(t *testing.T) {
tests := []struct {
name string
config *Config
expectError bool
expectHooks int
}{
{
name: "Valid configuration",
config: &Config{
ClientToken: "test-token",
HostServer: "ws://localhost:8080",
WebHooks: []*WebHook{
{
Url: "http://example.com",
Method: "POST",
Body: "{\"message\": \"{{.message}}\"}",
},
},
},
expectError: false,
expectHooks: 1,
},
{
name: "Empty webhook URL",
config: &Config{
ClientToken: "test-token",
HostServer: "ws://localhost:8080",
WebHooks: []*WebHook{
{
Method: "POST",
Body: "{\"message\": \"{{.message}}\"}",
},
},
},
expectError: true,
expectHooks: 0,
},
{
name: "Invalid webhook URL",
config: &Config{
ClientToken: "test-token",
HostServer: "ws://localhost:8080",
WebHooks: []*WebHook{
{
Url: "example.com",
Method: "POST",
Body: "{\"message\": \"{{.message}}\"}",
},
},
},
expectError: true,
expectHooks: 0,
},
{
name: "Empty webhook body be allowed",
config: &Config{
ClientToken: "test-token",
HostServer: "ws://localhost:8080",
WebHooks: []*WebHook{
{
Url: "http://example.com",
Method: "POST",
},
},
},
expectError: false,
expectHooks: 1,
},
{
name: "Default method and content type",
config: &Config{
ClientToken: "test-token",
HostServer: "ws://localhost:8080",
WebHooks: []*WebHook{
{
Url: "http://example.com",
Body: "{\"message\": \"{{.message}}\"}",
},
},
},
expectError: false,
expectHooks: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
plugin := &MultiNotifierPlugin{}
err := plugin.ValidateAndSetConfig(tt.config)
if tt.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expectHooks, len(plugin.config.WebHooks), "Unexpected number of webhooks")
if len(plugin.config.WebHooks) > 0 {
assert.Equal(t, "POST", plugin.config.WebHooks[0].Method, "Default method should be POST")
assert.Equal(t, "text/plain", plugin.config.WebHooks[0].Header["Content-Type"], "Default Content-Type should be text/plain")
}
}
})
}
}
func TestReceiveMessages(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
// Create a mock WebSocket server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
upgrader := websocket.Upgrader{}
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return
}
defer c.Close()
// Send a test message
msg := MessageExternal{
ID: 1,
ApplicationID: 1,
Message: "Test message",
Title: "Test title",
Priority: 1,
Date: time.Now(),
}
msgJSON, _ := json.Marshal(msg)
c.WriteMessage(websocket.TextMessage, msgJSON)
time.Sleep(100 * time.Millisecond)
c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
}))
defer server.Close()
// Set up httpmock responder
httpmock.RegisterResponder("POST", "http://example.com", httpmock.NewStringResponder(200, "OK"))
wsURL := "ws" + strings.TrimPrefix(server.URL, "http")
// Create a plugin instance
plugin := &MultiNotifierPlugin{
config: &Config{
ClientToken: "test-token",
HostServer: wsURL,
WebHooks: []*WebHook{
{
Url: "http://example.com",
Method: "POST",
Body: "{{.title}} - {{.message}}",
},
},
},
}
// Run the receiveMessages method
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err := plugin.receiveMessages(ctx, wsURL)
assert.Error(t, err, "Expected an error from receiveMessages")
assert.Equal(t, "read message error: websocket: close 1000 (normal)", err.Error(),
"Unexpected error message: %v", err)
// Check if the HTTP request was made
callCount := httpmock.GetTotalCallCount()
assert.Equal(t, 1, callCount, "Expected 1 HTTP call, got %d", callCount)
// Check the details of the HTTP request
calls := httpmock.GetCallCountInfo()
assert.Equal(t, 1, calls["POST http://example.com"], "Expected 1 POST call to http://example.com")
}
func TestMultiNotifierPlugin_SendMessage(t *testing.T) {
plugin := &MultiNotifierPlugin{
config: &Config{
WebHooks: []*WebHook{
{
Url: "http://example.com",
Method: "POST",
Body: "{\"message\": \"{{.message}}\"}",
Apps: []uint{1, 2},
},
},
},
}
// Create a test server to handle the webhook request
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var body map[string]string
json.NewDecoder(r.Body).Decode(&body)
assert.Equal(t, "Test message", body["message"])
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
// Update the webhook URL to use the test server
plugin.config.WebHooks[0].Url = server.URL
msg := &MessageExternal{
ID: 1,
ApplicationID: 1,
Message: "Test message",
Title: "Test title",
Priority: 1,
Date: time.Now(),
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Should pass
errs := plugin.sendMessage(ctx, msg, plugin.config.WebHooks)
assert.Empty(t, errs)
// Test with a non-matching app ID
msg.ApplicationID = 3
errs = plugin.sendMessage(ctx, msg, plugin.config.WebHooks)
assert.Empty(t, errs)
// Should fail when context is canceled
canceledCtx, cancel := context.WithCancel(context.Background())
cancel() // Immediately cancel the context
errs = plugin.sendMessage(canceledCtx, msg, plugin.config.WebHooks)
assert.NotEmpty(t, errs)
}
func TestMultiNotifierPlugin_Enable(t *testing.T) {
plugin := &MultiNotifierPlugin{
config: &Config{
ClientToken: "test-token",
HostServer: "ws://localhost:8080",
},
}
err := plugin.Enable()
assert.NoError(t, err)
assert.NotNil(t, plugin.cancel)
// Clean up
plugin.Disable()
}
func TestMultiNotifierPlugin_Disable(t *testing.T) {
plugin := &MultiNotifierPlugin{}
ctx, cancel := context.WithCancel(context.Background())
plugin.cancel = cancel
err := plugin.Disable()
assert.NoError(t, err)
assert.Error(t, ctx.Err(), "Context should have been cancelled")
err = plugin.Disable()
assert.NoError(t, err, "Calling Disable multiple times should not produce an error")
}
func TestMultiNotifierPlugin_DefaultConfig(t *testing.T) {
plugin := &MultiNotifierPlugin{}
config := plugin.DefaultConfig()
assert.IsType(t, &Config{}, config)
defaultConfig := config.(*Config)
assert.Equal(t, "CrMo3UaAQG1H37G", defaultConfig.ClientToken)
assert.Equal(t, "ws://localhost", defaultConfig.HostServer)
}
func TestProcessTemplateString(t *testing.T) {
testCases := []struct {
name string
template string
msg *MessageExternal
expected string
}{
{
name: "Simple text template",
template: "Title: {{.title}}, Message: {{.message}}",
msg: &MessageExternal{
Title: "Test Title",
Message: "Test Message",
},
expected: "Title: Test Title, Message: Test Message",
},
{
name: "Template with JSON-like content",
template: "{\"title\": \"{{.title}}\", \"message\": \"{{.message}}\"}",
msg: &MessageExternal{
Title: "JSON Title",
Message: "JSON Message",
},
expected: "{\"title\": \"JSON Title\", \"message\": \"JSON Message\"}",
},
{
name: "Template with actual JSON message",
template: "Title: {{.title}}, Content: {{.message}}",
msg: &MessageExternal{
Title: "JSON Content",
Message: "{\"key1\": \"value1\", \"key2\": \"value2\"}",
},
expected: "Title: JSON Content, Content: {\"key1\": \"value1\", \"key2\": \"value2\"}",
},
// {{.message}} without being quoted will be processed as a plain text template
{
name: "JSON template with raw JSON message",
template: "{\"title\": \"{{.title}}\", \"content\": {{.message}}}",
msg: &MessageExternal{
Title: "Raw JSON",
Message: "{\"key1\": \"value1\", \"key2\": \"value2\"}",
},
expected: "{\"title\": \"Raw JSON\", \"content\": {\"key1\": \"value1\", \"key2\": \"value2\"}}",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result, err := processTemplateString(tc.template, tc.msg)
assert.NoError(t, err)
assert.Equal(t, tc.expected, result)
})
}
}
func TestProcessJSONRecursive(t *testing.T) {
testCases := []struct {
name string
template string
msg *MessageExternal
expected string
}{
{
name: "Simple JSON template",
template: "{\"title\": \"{{.title}}\", \"message\": \"{{.message}}\"}",
msg: &MessageExternal{
Title: "Test Title",
Message: "Test Message",
},
expected: "{\"message\":\"Test Message\",\"title\":\"Test Title\"}",
},
{
name: "JSON template with escaped message",
template: "{\"title\": \"{{.title}}\", \"content\": \"{{.message}}\"}",
msg: &MessageExternal{
Title: "JSON Content",
Message: "{\"key1\": \"value1\", \"key2\": \"value2\"}",
},
expected: "{\"content\":\"{\\\"key1\\\": \\\"value1\\\", \\\"key2\\\": \\\"value2\\\"}\",\"title\":\"JSON Content\"}",
},
{
name: "JSON template with escaped message in array",
template: "{\"title\": \"{{.title}}\", \"content\": [\"{{.message}}\"]}",
msg: &MessageExternal{
Title: "JSON Content",
Message: "{\"key1\": \"value1\", \"key2\": \"value2\"}",
},
expected: "{\"content\":[\"{\\\"key1\\\": \\\"value1\\\", \\\"key2\\\": \\\"value2\\\"}\"],\"title\":\"JSON Content\"}",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var jsonBody map[string]interface{}
isJSON := json.Unmarshal([]byte(tc.template), &jsonBody) == nil
assert.True(t, isJSON, "Template should be JSON formated.")
err := processJSONRecursive(jsonBody, tc.msg)
assert.NoError(t, err)
newBody, err := json.Marshal(jsonBody)
assert.Equal(t, tc.expected, string(newBody))
})
}
}