-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsms_test.go
254 lines (209 loc) · 6.35 KB
/
sms_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
package sms
import (
"bytes"
"github.com/stretchr/testify/require"
"gopkg.in/jarcoal/httpmock.v1"
"io/ioutil"
"net/http"
"net/url"
"testing"
)
func TestDeliveryStatus_IsDelivered(t *testing.T) {
s := DeliveryStatus(StatusDelivered)
require.True(t, s.IsDelivered())
require.False(t, s.IsInProgress())
require.False(t, s.IsUndelivered())
}
func TestDeliveryStatus_IsInProgress(t *testing.T) {
statuses := []DeliveryStatus{
StatusQueued,
StatusSent,
StatusModerating,
}
for _, s := range statuses {
t.Run("check status "+string(s), func(t *testing.T) {
require.True(t, s.IsInProgress())
require.False(t, s.IsDelivered())
require.False(t, s.IsUndelivered())
})
}
}
func TestDeliveryStatus_IsUndelivered(t *testing.T) {
statuses := []DeliveryStatus{
StatusUnavailable,
StatusSpam,
StatusInvPhone,
"AbsolutelyUnknownStatus",
}
for _, s := range statuses {
t.Run("check status "+string(s), func(t *testing.T) {
require.True(t, s.IsUndelivered())
require.False(t, s.IsInProgress())
require.False(t, s.IsDelivered())
})
}
}
func TestParseStatusResponse_Success(t *testing.T) {
// Arrange.
resp := bytes.NewBufferString("1\n3")
s := Sender{}
// Act.
ds, err := s.parseStatusResponse(resp)
// Assert.
require.Nil(t, err)
require.Equal(t, DeliveryStatus(StatusDelivered), ds)
}
func TestParseStatusResponse_BadCode(t *testing.T) {
// Arrange.
resp := bytes.NewBufferString("Unexpected response")
s := Sender{}
// Act.
_, err := s.parseStatusResponse(resp)
// Assert.
require.NotNil(t, err)
}
func TestParseSendSMSResponse_Success(t *testing.T) {
// Arrange.
req := bytes.NewBufferString(`
1
123
1.75
2016-10-16 15:00:00
Array
(
[to] => 79998887766
[text] => Земля, прощай!
[from] => Komandor
[sendTime] => 2016-10-16 15:00:00
)`)
s := Sender{}
// Act.
r, err := s.parseSendSMSResponse(req)
// Assert.
require.Nil(t, err)
require.Equal(t, "123", r.SMSID)
require.Equal(t, "1.75", r.SMSCost)
// TODO: time.Time.
require.Equal(t, "2016-10-16 15:00:00", r.SentAt)
require.Equal(t, "Array\n(\n [to] => 79998887766\n [text] => Земля, прощай!\n [from] => Komandor\n [sendTime] => 2016-10-16 15:00:00\n)\n", r.DebugInfo)
}
func TestParseSendSMSResponse_Error(t *testing.T) {
// Arrange.
req := bytes.NewBufferString(`
-1
Invalid login/password`)
s := Sender{}
// Act.
_, err := s.parseSendSMSResponse(req)
// Assert.
require.NotNil(t, err)
}
func TestParseSendSMSResponse_MalformedRequest(t *testing.T) {
// Arrange.
req := bytes.NewBufferString(`1
123
1.75
`)
s := Sender{}
// Act.
_, err := s.parseSendSMSResponse(req)
// Assert.
require.NotNil(t, err)
}
type fakeClient struct {
RespToWrite string
req *http.Request
}
func (c *fakeClient) Do(req *http.Request) (*http.Response, error) {
c.req = req
return &http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString(c.RespToWrite)),
}, nil
}
func TestRequest_Success(t *testing.T) {
// Arrange.
httpmock.Activate()
defer httpmock.DeactivateAndReset()
c := &fakeClient{}
httpmock.RegisterResponder(http.MethodGet, "https://sms-rassilka.com/api/simple/send", c.Do)
s := Sender{
Login: "+79998887766",
PasswordMD5: "fd494182a7ee16ae07f641c7c03663d8",
SandboxMode: true,
}
// Act.
s.request("/send", url.Values{"user-arg": []string{"user-val"}})
// Assert.
require.Equal(t, "https", c.req.URL.Scheme)
require.Equal(t, "sms-rassilka.com", c.req.URL.Host)
require.Equal(t, "/api/simple/send", c.req.URL.Path)
require.Equal(t, "+79998887766", c.req.URL.Query().Get("login"))
require.Equal(t, "fd494182a7ee16ae07f641c7c03663d8", c.req.URL.Query().Get("password"))
require.Equal(t, "dev", c.req.URL.Query().Get("mode"))
require.Equal(t, "user-val", c.req.URL.Query().Get("user-arg"))
}
func TestSender_sendSMS(t *testing.T) {
// Arrange.
httpmock.Activate()
defer httpmock.DeactivateAndReset()
c := &fakeClient{}
httpmock.RegisterResponder(http.MethodGet, "https://sms-rassilka.com/api/simple/send", c.Do)
s := Sender{}
// Act.
_, _ = s.sendSMS("+79008007060", "Hello world", "inform", "2016-10-16 15:00:00")
// Assert.
require.Equal(t, "/api/simple/send", c.req.URL.Path)
require.Equal(t, "+79008007060", c.req.URL.Query().Get("to"))
require.Equal(t, "Hello world", c.req.URL.Query().Get("text"))
require.Equal(t, "inform", c.req.URL.Query().Get("from"))
require.Equal(t, "2016-10-16 15:00:00", c.req.URL.Query().Get("sendTime"))
}
func TestSender_QueryStatus(t *testing.T) {
// Arrange.
httpmock.Activate()
defer httpmock.DeactivateAndReset()
c := &fakeClient{}
httpmock.RegisterResponder(http.MethodGet, "https://sms-rassilka.com/api/simple/status", c.Do)
s := Sender{}
// Act.
_, _ = s.QueryStatus("848918")
// Assert.
require.Equal(t, "/api/simple/status", c.req.URL.Path)
require.Equal(t, "848918", c.req.URL.Query().Get("smsId"))
}
func TestSender_SendSMS(t *testing.T) {
// Arrange.
httpmock.Activate()
defer httpmock.DeactivateAndReset()
c := &fakeClient{}
httpmock.RegisterResponder(http.MethodGet, "https://sms-rassilka.com/api/simple/send", c.Do)
s := Sender{}
// Act.
s.SendSMS("+79008007060", "Hello world")
// Assert.
require.Equal(t, "+79008007060", c.req.URL.Query().Get("to"))
require.Equal(t, "Hello world", c.req.URL.Query().Get("text"))
require.Equal(t, "inform", c.req.URL.Query().Get("from"))
require.Equal(t, "", c.req.URL.Query().Get("sendTime"))
// Act.
s.SendSMSAt("+79008007060", "Hello world", "2016-10-16 15:00:00")
// Assert.
require.Equal(t, "+79008007060", c.req.URL.Query().Get("to"))
require.Equal(t, "Hello world", c.req.URL.Query().Get("text"))
require.Equal(t, "inform", c.req.URL.Query().Get("from"))
require.Equal(t, "2016-10-16 15:00:00", c.req.URL.Query().Get("sendTime"))
// Act.
s.SendSMSFrom("+79008007060", "Hello world", "supercompany")
// Assert.
require.Equal(t, "+79008007060", c.req.URL.Query().Get("to"))
require.Equal(t, "Hello world", c.req.URL.Query().Get("text"))
require.Equal(t, "supercompany", c.req.URL.Query().Get("from"))
require.Equal(t, "", c.req.URL.Query().Get("sendTime"))
// Act.
s.SendSMSFromAt("+79008007060", "Hello world", "supercompany", "2016-10-16 15:00:00")
// Assert.
require.Equal(t, "+79008007060", c.req.URL.Query().Get("to"))
require.Equal(t, "Hello world", c.req.URL.Query().Get("text"))
require.Equal(t, "supercompany", c.req.URL.Query().Get("from"))
require.Equal(t, "2016-10-16 15:00:00", c.req.URL.Query().Get("sendTime"))
}