-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathhandler_test.go
210 lines (200 loc) · 7.62 KB
/
handler_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
package africastalking
import (
"net/url"
"testing"
"time"
"github.com/nyaruka/courier"
. "github.com/nyaruka/courier/handlers"
"github.com/nyaruka/courier/test"
"github.com/nyaruka/gocommon/httpx"
"github.com/nyaruka/gocommon/urns"
)
const (
receiveURL = "/c/at/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/receive/"
statusURL = "/c/at/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status/"
)
var incomingCases = []IncomingTestCase{
{
Label: "Receive Valid",
URL: receiveURL,
Data: "linkId=03090445075804249226&text=Msg&to=21512&id=ec9adc86-51d5-4bc8-8eb0-d8ab0bb53dc3&date=2017-05-03T06%3A04%3A45Z&from=%2B254791541111",
ExpectedRespStatus: 200,
ExpectedBodyContains: "Message Accepted",
ExpectedMsgText: Sp("Msg"),
ExpectedURN: "tel:+254791541111",
ExpectedExternalID: "ec9adc86-51d5-4bc8-8eb0-d8ab0bb53dc3",
ExpectedDate: time.Date(2017, 5, 3, 06, 04, 45, 0, time.UTC),
},
{
Label: "Receive Valid",
URL: receiveURL,
Data: "linkId=03090445075804249226&text=Msg&to=21512&id=ec9adc86-51d5-4bc8-8eb0-d8ab0bb53dc3&date=2017-05-03+06%3A04%3A45&from=%2B254791541111",
ExpectedRespStatus: 200,
ExpectedBodyContains: "Message Accepted",
ExpectedMsgText: Sp("Msg"),
ExpectedURN: "tel:+254791541111",
ExpectedExternalID: "ec9adc86-51d5-4bc8-8eb0-d8ab0bb53dc3",
ExpectedDate: time.Date(2017, 5, 3, 06, 04, 45, 0, time.UTC),
},
{
Label: "Receive Empty",
URL: receiveURL,
Data: "empty",
ExpectedRespStatus: 400,
ExpectedBodyContains: "field 'id' required",
},
{
Label: "Receive Missing Text",
URL: receiveURL,
Data: "linkId=03090445075804249226&to=21512&id=ec9adc86-51d5-4bc8-8eb0-d8ab0bb53dc3&date=2017-05-03T06%3A04%3A45Z&from=%2B254791541111",
ExpectedRespStatus: 400,
ExpectedBodyContains: "field 'text' required",
},
{
Label: "Invalid URN",
URL: receiveURL,
Data: "linkId=03090445075804249226&text=Msg&to=21512&id=ec9adc86-51d5-4bc8-8eb0-d8ab0bb53dc3&date=2017-05-03T06%3A04%3A45Z&from=MTN",
ExpectedRespStatus: 400,
ExpectedBodyContains: "not a possible number",
},
{
Label: "Invalid Date",
URL: receiveURL,
Data: "linkId=03090445075804249226&text=Msg&to=21512&id=ec9adc86-51d5-4bc8-8eb0-d8ab0bb53dc3&date=2017-05-03T06%3A04&from=%2B254791541111",
ExpectedRespStatus: 400,
ExpectedBodyContains: "invalid date format",
},
{
Label: "Status Invalid",
URL: statusURL,
Data: "id=ATXid_dda018a640edfcc5d2ce455de3e4a6e7&status=Borked",
ExpectedRespStatus: 400,
ExpectedBodyContains: "unknown status",
},
{
Label: "Status Missing",
URL: statusURL,
Data: "id=ATXid_dda018a640edfcc5d2ce455de3e4a6e7",
ExpectedRespStatus: 400,
ExpectedBodyContains: "field 'status' required",
},
{
Label: "Status Success",
URL: statusURL,
Data: "id=ATXid_dda018a640edfcc5d2ce455de3e4a6e7&status=Success",
ExpectedRespStatus: 200,
ExpectedBodyContains: `"status":"D"`,
ExpectedStatuses: []ExpectedStatus{{ExternalID: "ATXid_dda018a640edfcc5d2ce455de3e4a6e7", Status: courier.MsgStatusDelivered}},
},
{
Label: "Status Expired",
URL: statusURL,
Data: "id=ATXid_dda018a640edfcc5d2ce455de3e4a6e7&status=Expired",
ExpectedRespStatus: 200,
ExpectedBodyContains: `"status":"F"`,
ExpectedStatuses: []ExpectedStatus{{ExternalID: "ATXid_dda018a640edfcc5d2ce455de3e4a6e7", Status: courier.MsgStatusFailed}},
},
}
func TestIncoming(t *testing.T) {
chs := []courier.Channel{
test.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56ab", "AT", "2020", "US", []string{urns.Phone.Prefix}, nil),
}
RunIncomingTestCases(t, chs, newHandler(), incomingCases)
}
var outgoingCases = []OutgoingTestCase{
{
Label: "Plain Send",
MsgText: "Simple Message ☺",
MsgURN: "tel:+250788383383",
MockResponses: map[string][]*httpx.MockResponse{
"https://api.africastalking.com/version1/messaging": {
httpx.NewMockResponse(200, nil, []byte(`{ "SMSMessageData": {"Recipients": [{"status": "Success", "messageId": "1002"}] } }`)),
},
},
ExpectedRequests: []ExpectedRequest{
{
Headers: map[string]string{"apikey": "KEY"},
Form: url.Values{"message": {"Simple Message ☺"}, "username": {"Username"}, "to": {"+250788383383"}, "from": {"2020"}},
},
},
ExpectedExtIDs: []string{"1002"},
},
{
Label: "Send Attachment",
MsgText: "My pic!",
MsgURN: "tel:+250788383383",
MsgAttachments: []string{"image/jpeg:https://foo.bar/image.jpg"},
MockResponses: map[string][]*httpx.MockResponse{
"https://api.africastalking.com/version1/messaging": {
httpx.NewMockResponse(200, nil, []byte(`{ "SMSMessageData": {"Recipients": [{"status": "Success", "messageId": "1002"}] } }`)),
},
},
ExpectedRequests: []ExpectedRequest{
{Form: url.Values{"message": {"My pic!\nhttps://foo.bar/image.jpg"}, "username": {"Username"}, "to": {"+250788383383"}, "from": {"2020"}}},
},
ExpectedExtIDs: []string{"1002"},
},
{
Label: "Explicit failed status",
MsgText: "Hi",
MsgURN: "tel:+250788383383",
MockResponses: map[string][]*httpx.MockResponse{
"https://api.africastalking.com/version1/messaging": {
httpx.NewMockResponse(200, nil, []byte(`{ "SMSMessageData": {"Recipients": [{"status": "Failed" }] } }`)),
},
},
ExpectedRequests: []ExpectedRequest{
{Form: url.Values{"message": {`Hi`}, "username": {"Username"}, "to": {"+250788383383"}, "from": {"2020"}}},
},
ExpectedError: courier.ErrResponseContent,
},
{
Label: "Missing status value",
MsgText: "Error Message",
MsgURN: "tel:+250788383383",
MockResponses: map[string][]*httpx.MockResponse{
"https://api.africastalking.com/version1/messaging": {
httpx.NewMockResponse(401, nil, []byte(`{ "error": "failed" }`)),
},
},
ExpectedRequests: []ExpectedRequest{
{Form: url.Values{"message": {`Error Message`}, "username": {"Username"}, "to": {"+250788383383"}, "from": {"2020"}}},
},
ExpectedError: courier.ErrResponseStatus,
},
}
var sharedOutgoingCases = []OutgoingTestCase{
{
Label: "Shared Send",
MsgText: "Simple Message ☺",
MsgURN: "tel:+250788383383",
MockResponses: map[string][]*httpx.MockResponse{
"https://api.africastalking.com/version1/messaging": {
httpx.NewMockResponse(200, nil, []byte(`{ "SMSMessageData": {"Recipients": [{"status": "Success", "messageId": "1002"}] } }`)),
},
},
ExpectedRequests: []ExpectedRequest{
{
Headers: map[string]string{"apikey": "KEY"},
Form: url.Values{"message": {"Simple Message ☺"}, "username": {"Username"}, "to": {"+250788383383"}}},
},
ExpectedExtIDs: []string{"1002"},
},
}
func TestOutgoing(t *testing.T) {
defaultChannel := test.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56ab", "AT", "2020", "US",
[]string{urns.Phone.Prefix},
map[string]any{
courier.ConfigUsername: "Username",
courier.ConfigAPIKey: "KEY",
})
sharedChannel := test.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56ab", "AT", "2020", "US",
[]string{urns.Phone.Prefix},
map[string]any{
courier.ConfigUsername: "Username",
courier.ConfigAPIKey: "KEY",
configIsShared: true,
})
RunOutgoingTestCases(t, defaultChannel, newHandler(), outgoingCases, []string{"KEY"}, nil)
RunOutgoingTestCases(t, sharedChannel, newHandler(), sharedOutgoingCases, []string{"KEY"}, nil)
}