-
Notifications
You must be signed in to change notification settings - Fork 1
/
textlocal_test.go
105 lines (79 loc) · 2.79 KB
/
textlocal_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
package textlocal
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/jarcoal/httpmock"
)
func TestGetErrorFromResponse_ErrUnknown(t *testing.T) {
assert.Equal(t, ErrUnknown, getErrorFromResponse(map[string]interface{}{}))
}
func TestGetErrorFromResponse_ErrCommandNotValid_FromNoCommandSpecified(t *testing.T) {
assert.Equal(t, ErrCommandNotValid, getErrorFromResponse(map[string]interface{}{
"error": []map[string]interface{}{
map[string]interface{}{
"code": noCommandSpecifiedErrorCode,
"message": "No command specified.",
},
},
}))
}
func TestGetErrorFromResponse_ErrCommandNotValid_FromUnrecognisedCommand(t *testing.T) {
assert.Equal(t, ErrCommandNotValid, getErrorFromResponse(map[string]interface{}{
"error": []map[string]interface{}{
map[string]interface{}{
"code": unrecognisedCommandErrorCode,
"message": "Unrecognised command.",
},
},
}))
}
func TestGetErrorFromResponse_ErrInvalidApikey(t *testing.T) {
assert.Equal(t, ErrInvalidApiKey, getErrorFromResponse(map[string]interface{}{
"error": []map[string]interface{}{
map[string]interface{}{
"code": invalidLoginDetailsErrorCode,
"message": "Invalid login details",
},
},
}))
}
func TestThatAnErrorIsReturnedIfNoBaseUrlIsSet(t *testing.T) {
_, e := New("", "api-key")
assert.Equal(t, ErrEmptyBaseUrl, e)
}
func TestThatAnErrorIsReturnedIfApiKeyIsSet(t *testing.T) {
_, e := New(DefaultBaseUrl, "")
assert.Equal(t, ErrNoApiKeyProvided, e)
}
func TestItTrimsSlashFromTheEndOfBaseUrl(t *testing.T) {
c, _ := New("http://localhost/", "api-key")
assert.Equal(t, "http://localhost", c.GetBaseUrl())
}
func TestCreditsCanBeObtained(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
c, _ := New("https://api.txtlocal.com", "abc")
res := httpmock.NewStringResponder(200, `{"balance": {"sms": 361.5, "mms": 0}, "status": "success"}`)
httpmock.RegisterResponder("GET", "https://api.txtlocal.com/balance?apiKey=abc", res)
cr, err := c.GetCredits()
assert.Nil(t, err)
assert.Equal(t, 361, cr.RemainingSMS)
assert.Equal(t, 0, cr.RemainingMMS)
}
func TestCreditsCanBeFailQuietly(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
c, _ := New("https://api.txtlocal.com", "abc")
res := httpmock.NewStringResponder(200, `{"status": "failure"}`)
httpmock.RegisterResponder("GET", "https://api.txtlocal.com/balance?apiKey=abc", res)
_, err := c.GetCredits()
assert.NotNil(t, err)
}
func TestMessageCanBeSent(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
c, _ := New("https://api.txtlocal.com", "abc")
res := httpmock.NewStringResponder(200, `{"status": "success"}`)
httpmock.RegisterResponder("POST", "https://api.txtlocal.com/send", res)
assert.Nil(t, c.SendSMS([]string{"00000000000"}, "Testing", "TestFrom"))
}