-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathtest.go
480 lines (399 loc) · 14.6 KB
/
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package handlers
import (
"bytes"
"context"
"fmt"
"io"
"log"
"log/slog"
"mime/multipart"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
_ "github.com/lib/pq" // postgres driver
"github.com/nyaruka/courier"
"github.com/nyaruka/courier/test"
"github.com/nyaruka/gocommon/httpx"
"github.com/nyaruka/gocommon/i18n"
"github.com/nyaruka/gocommon/jsonx"
"github.com/nyaruka/gocommon/urns"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// RequestPrepFunc is our type for a hook for tests to use before a request is fired in a test
type RequestPrepFunc func(*http.Request)
// ExpectedStatus is an expected status update
type ExpectedStatus struct {
MsgID courier.MsgID
ExternalID string
Status courier.MsgStatus
}
// ExpectedEvent is an expected channel event
type ExpectedEvent struct {
Type courier.ChannelEventType
URN urns.URN
Time time.Time
Extra map[string]string
}
// IncomingTestCase defines the test values for a particular test case
type IncomingTestCase struct {
Label string
NoQueueErrorCheck bool
NoInvalidChannelCheck bool
PrepRequest RequestPrepFunc
URL string
Data string
Headers map[string]string
MultipartForm map[string]string
ExpectedRespStatus int
ExpectedBodyContains string
ExpectedContactName *string
ExpectedMsgText *string
ExpectedURN urns.URN
ExpectedURNAuthTokens map[urns.URN]map[string]string
ExpectedAttachments []string
ExpectedDate time.Time
ExpectedExternalID string
ExpectedMsgID int64
ExpectedStatuses []ExpectedStatus
ExpectedEvents []ExpectedEvent
ExpectedErrors []*courier.ChannelError
NoLogsExpected bool
}
// utility method to make a request to a handler URL
func testHandlerRequest(tb testing.TB, s courier.Server, path string, headers map[string]string, data string, multipartFormFields map[string]string, expectedStatus int, expectedBodyContains string, requestPrepFunc RequestPrepFunc) string {
var req *http.Request
var err error
url := fmt.Sprintf("https://%s%s", s.Config().Domain, path)
if data != "" {
req, err = http.NewRequest(http.MethodPost, url, strings.NewReader(data))
require.Nil(tb, err)
// guess our content type
contentType := "application/x-www-form-urlencoded"
if strings.Contains(data, "{") && strings.Contains(data, "}") {
contentType = "application/json"
} else if strings.Contains(data, "<") && strings.Contains(data, ">") {
contentType = "application/xml"
}
req.Header.Set("Content-Type", contentType)
} else if multipartFormFields != nil {
var body bytes.Buffer
bodyMultipartWriter := multipart.NewWriter(&body)
for k, v := range multipartFormFields {
fieldWriter, err := bodyMultipartWriter.CreateFormField(k)
require.Nil(tb, err)
_, err = fieldWriter.Write([]byte(v))
require.Nil(tb, err)
}
contentType := fmt.Sprintf("multipart/form-data;boundary=%v", bodyMultipartWriter.Boundary())
bodyMultipartWriter.Close()
req, err = http.NewRequest(http.MethodPost, url, bytes.NewReader(body.Bytes()))
require.Nil(tb, err)
req.Header.Set("Content-Type", contentType)
} else {
req, err = http.NewRequest(http.MethodGet, url, nil)
}
for key, val := range headers {
req.Header.Set(key, val)
}
require.Nil(tb, err)
if requestPrepFunc != nil {
requestPrepFunc(req)
}
rr := httptest.NewRecorder()
s.Router().ServeHTTP(rr, req)
body := rr.Body.String()
assert.Equal(tb, expectedStatus, rr.Code, "status code mismatch")
if expectedBodyContains != "" {
assert.Contains(tb, body, expectedBodyContains)
}
return body
}
func newServer(backend courier.Backend) courier.Server {
// for benchmarks, log to null
logger := slog.Default()
log.SetOutput(io.Discard)
config := courier.NewDefaultConfig()
config.FacebookWebhookSecret = "fb_webhook_secret"
config.FacebookApplicationSecret = "fb_app_secret"
config.WhatsappAdminSystemUserToken = "wac_admin_system_user_token"
return courier.NewServerWithLogger(config, backend, logger)
}
// RunIncomingTestCases runs all the passed in tests cases for the passed in channel configurations
func RunIncomingTestCases(t *testing.T, channels []courier.Channel, handler courier.ChannelHandler, testCases []IncomingTestCase) {
mb := test.NewMockBackend()
s := newServer(mb)
for _, ch := range channels {
mb.AddChannel(ch)
}
handler.Initialize(s)
for _, tc := range testCases {
t.Run(tc.Label, func(t *testing.T) {
require := require.New(t)
mb.Reset()
testHandlerRequest(t, s, tc.URL, tc.Headers, tc.Data, tc.MultipartForm, tc.ExpectedRespStatus, tc.ExpectedBodyContains, tc.PrepRequest)
if tc.ExpectedMsgText != nil || tc.ExpectedAttachments != nil {
require.Len(mb.WrittenMsgs(), 1, "expected a msg to be written")
msg := mb.WrittenMsgs()[0].(*test.MockMsg)
if tc.ExpectedMsgText != nil {
assert.Equal(t, *tc.ExpectedMsgText, msg.Text())
}
if len(tc.ExpectedAttachments) > 0 {
assert.Equal(t, tc.ExpectedAttachments, msg.Attachments())
}
if !tc.ExpectedDate.IsZero() {
assert.Equal(t, tc.ExpectedDate.Local(), msg.ReceivedOn().Local())
}
if tc.ExpectedExternalID != "" {
assert.Equal(t, tc.ExpectedExternalID, msg.ExternalID())
}
assert.Equal(t, tc.ExpectedURN, msg.URN())
} else {
assert.Empty(t, mb.WrittenMsgs(), "unexpected msg written")
}
actualStatuses := mb.WrittenMsgStatuses()
assert.Len(t, actualStatuses, len(tc.ExpectedStatuses), "unexpected number of status updates written")
for i, expectedStatus := range tc.ExpectedStatuses {
if (len(actualStatuses) - 1) < i {
break
}
actualStatus := actualStatuses[i]
assert.Equal(t, expectedStatus.MsgID, actualStatus.MsgID(), "msg id mismatch for update %d", i)
assert.Equal(t, expectedStatus.ExternalID, actualStatus.ExternalID(), "external id mismatch for update %d", i)
assert.Equal(t, expectedStatus.Status, actualStatus.Status(), "status value mismatch for update %d", i)
}
actualEvents := mb.WrittenChannelEvents()
assert.Len(t, actualEvents, len(tc.ExpectedEvents), "unexpected number of events written")
for i, expectedEvent := range tc.ExpectedEvents {
if (len(actualEvents) - 1) < i {
break
}
actualEvent := actualEvents[i]
assert.Equal(t, expectedEvent.Type, actualEvent.EventType(), "event type mismatch for event %d", i)
assert.Equal(t, expectedEvent.URN, actualEvent.URN(), "URN mismatch for event %d", i)
assert.Equal(t, expectedEvent.Extra, actualEvent.Extra(), "extra mismatch for event %d", i)
if !expectedEvent.Time.IsZero() {
assert.Equal(t, expectedEvent.Time, actualEvent.OccurredOn())
}
}
if tc.ExpectedContactName != nil {
require.Equal(*tc.ExpectedContactName, mb.LastContactName())
}
assert.Equal(t, tc.ExpectedURNAuthTokens, mb.URNAuthTokens())
// unless we know there won't be a log, check one was written
if !tc.NoLogsExpected {
if assert.Equal(t, 1, len(mb.WrittenChannelLogs()), "expected a channel log") {
clog := mb.WrittenChannelLogs()[0]
assert.Equal(t, tc.ExpectedErrors, clog.Errors(), "unexpected errors logged")
}
}
})
}
// check non-channel specific error conditions against first test case
validCase := testCases[0]
if !validCase.NoQueueErrorCheck {
t.Run("Queue Error", func(t *testing.T) {
mb.SetErrorOnQueue(true)
defer mb.SetErrorOnQueue(false)
testHandlerRequest(t, s, validCase.URL, validCase.Headers, validCase.Data, validCase.MultipartForm, 400, "unable to queue message", validCase.PrepRequest)
})
}
if !validCase.NoInvalidChannelCheck {
t.Run("Receive With Invalid Channel", func(t *testing.T) {
mb.ClearChannels()
testHandlerRequest(t, s, validCase.URL, validCase.Headers, validCase.Data, validCase.MultipartForm, 400, "channel not found", validCase.PrepRequest)
})
}
}
// SendPrepFunc allows test cases to modify the channel, msg or server before a message is sent
type SendPrepFunc func(*httptest.Server, courier.ChannelHandler, courier.Channel, courier.MsgOut)
type ExpectedRequest struct {
Headers map[string]string
Path string
Params url.Values
Form url.Values
Body string
BodyContains string
}
func (e *ExpectedRequest) AssertMatches(t *testing.T, actual *http.Request, requestNum int) {
if e.Headers != nil {
for k, v := range e.Headers {
assert.Equal(t, v, actual.Header.Get(k), "header %s mismatch for request %d", k, requestNum)
}
}
if e.Path != "" {
assert.Equal(t, e.Path, actual.URL.Path, "patch mismatch for request %d", requestNum)
}
if e.Params != nil {
assert.Equal(t, e.Params, actual.URL.Query(), "URL params mismatch for request %d", requestNum)
}
if e.Form != nil {
actual.ParseMultipartForm(32 << 20)
assert.Equal(t, e.Form, actual.PostForm, "form mismatch for request %d", requestNum)
}
if e.Body != "" {
value, _ := io.ReadAll(actual.Body)
assert.Equal(t, e.Body, strings.Trim(string(value), "\n"), "body mismatch for request %d", requestNum)
}
if e.BodyContains != "" {
value, _ := io.ReadAll(actual.Body)
assert.Contains(t, string(value), e.BodyContains, "body contains fail for request %d", requestNum)
}
}
// OutgoingTestCase defines the test values for a particular test case
type OutgoingTestCase struct {
Label string
MsgText string
MsgURN string
MsgURNAuth string
MsgAttachments []string
MsgQuickReplies []string
MsgLocale i18n.Locale
MsgTopic string
MsgTemplating string
MsgHighPriority bool
MsgResponseToExternalID string
MsgFlow *courier.FlowReference
MsgOptIn *courier.OptInReference
MsgUserID courier.UserID
MsgOrigin courier.MsgOrigin
MsgContactLastSeenOn *time.Time
MockResponses map[string][]*httpx.MockResponse
ExpectedRequests []ExpectedRequest
ExpectedExtIDs []string
ExpectedError error
ExpectedLogErrors []*courier.ChannelError
ExpectedContactURNs map[string]bool
ExpectedNewURN string
}
// Msg creates the test message for this test case
func (tc *OutgoingTestCase) Msg(mb *test.MockBackend, ch courier.Channel) courier.MsgOut {
msgOrigin := courier.MsgOriginFlow
if tc.MsgOrigin != "" {
msgOrigin = tc.MsgOrigin
}
m := mb.NewOutgoingMsg(ch, 10, urns.URN(tc.MsgURN), tc.MsgText, tc.MsgHighPriority, tc.MsgQuickReplies, tc.MsgTopic, tc.MsgResponseToExternalID, msgOrigin, tc.MsgContactLastSeenOn).(*test.MockMsg)
m.WithLocale(tc.MsgLocale)
m.WithUserID(tc.MsgUserID)
for _, a := range tc.MsgAttachments {
m.WithAttachment(a)
}
if tc.MsgURNAuth != "" {
m.WithURNAuth(tc.MsgURNAuth)
}
if tc.MsgTemplating != "" {
templating := &courier.Templating{}
jsonx.MustUnmarshal([]byte(tc.MsgTemplating), templating)
m.WithTemplating(templating)
}
if tc.MsgFlow != nil {
m.WithFlow(tc.MsgFlow)
}
if tc.MsgOptIn != nil {
m.WithOptIn(tc.MsgOptIn)
}
return m
}
// RunOutgoingTestCases runs all the passed in test cases against the channel
func RunOutgoingTestCases(t *testing.T, channel courier.Channel, handler courier.ChannelHandler, testCases []OutgoingTestCase, checkRedacted []string, setupBackend func(*test.MockBackend)) {
mb := test.NewMockBackend()
if setupBackend != nil {
setupBackend(mb)
}
s := newServer(mb)
mb.AddChannel(channel)
handler.Initialize(s)
for _, tc := range testCases {
mb.Reset()
t.Run(tc.Label, func(t *testing.T) {
require := require.New(t)
msg := tc.Msg(mb, channel)
var mockHTTP *httpx.MockRequestor
actualRequests := make([]*http.Request, 0, 1)
if len(tc.MockResponses) > 0 {
mockHTTP = httpx.NewMockRequestor(tc.MockResponses).Clone()
httpx.SetRequestor(mockHTTP)
}
clog := courier.NewChannelLogForSend(msg, handler.RedactValues(channel))
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*10)
res := &courier.SendResult{}
serr := handler.Send(ctx, msg, res, clog)
externalIDs := res.ExternalIDs()
resNewURN := res.GetNewURN()
if mockHTTP != nil {
httpx.SetRequestor(httpx.DefaultRequestor)
actualRequests = mockHTTP.Requests()
assert.False(t, mockHTTP.HasUnused(), "unused HTTP mocks")
}
cancel()
if len(tc.ExpectedRequests) > 0 {
assert.Len(t, actualRequests, len(tc.ExpectedRequests), "unexpected number of requests made")
for i, expectedRequest := range tc.ExpectedRequests {
if (len(actualRequests) - 1) < i {
break
}
expectedRequest.AssertMatches(t, actualRequests[i], i)
}
}
assert.Equal(t, tc.ExpectedExtIDs, externalIDs, "external IDs mismatch")
assert.Equal(t, tc.ExpectedError, serr, "send method error mismatch")
assert.Equal(t, tc.ExpectedLogErrors, clog.Errors(), "channel log errors mismatch")
if tc.ExpectedContactURNs != nil {
var contactUUID courier.ContactUUID
for urn, shouldBePresent := range tc.ExpectedContactURNs {
contact, _ := mb.GetContact(ctx, channel, urns.URN(urn), nil, "", clog)
if contactUUID == courier.NilContactUUID && shouldBePresent {
contactUUID = contact.UUID()
}
if shouldBePresent {
require.Equal(contactUUID, contact.UUID())
} else {
require.NotEqual(contactUUID, contact.UUID())
}
}
}
if tc.ExpectedNewURN != "" {
require.Equal(urns.URN(tc.ExpectedNewURN), resNewURN)
}
AssertChannelLogRedaction(t, clog, checkRedacted)
})
}
}
// RunChannelBenchmarks runs all the passed in test cases for the passed in channels
func RunChannelBenchmarks(b *testing.B, channels []courier.Channel, handler courier.ChannelHandler, testCases []IncomingTestCase) {
mb := test.NewMockBackend()
s := newServer(mb)
for _, ch := range channels {
mb.AddChannel(ch)
}
handler.Initialize(s)
for _, testCase := range testCases {
mb.Reset()
b.Run(testCase.Label, func(b *testing.B) {
for i := 0; i < b.N; i++ {
testHandlerRequest(b, s, testCase.URL, testCase.Headers, testCase.Data, testCase.MultipartForm, testCase.ExpectedRespStatus, "", testCase.PrepRequest)
}
})
}
}
// asserts that the given channel log doesn't contain any of the given values
func AssertChannelLogRedaction(t *testing.T, clog *courier.ChannelLog, vals []string) {
assertRedacted := func(s string) {
for _, v := range vals {
assert.NotContains(t, s, v, "expected '%s' to not contain redacted value '%s'", s, v)
}
}
for _, h := range clog.HTTPLogs() {
assertRedacted(h.URL)
assertRedacted(h.Request)
assertRedacted(h.Response)
}
for _, e := range clog.Errors() {
assertRedacted(e.Message())
}
}
// Sp is a utility method to get the pointer to the passed in string
func Sp(s string) *string { return &s }