-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathcluster_test.go
232 lines (195 loc) · 6.45 KB
/
cluster_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
//go:build cluster
// +build cluster
package twilio
import (
"os"
"testing"
"github.com/twilio/twilio-go/client"
Api "github.com/twilio/twilio-go/rest/api/v2010"
ChatV2 "github.com/twilio/twilio-go/rest/chat/v2"
EventsV1 "github.com/twilio/twilio-go/rest/events/v1"
"github.com/stretchr/testify/assert"
IamV1 "github.com/twilio/twilio-go/rest/iam/v1"
)
var from string
var to string
var testClient *RestClient
func TestMain(m *testing.M) {
from = os.Getenv("TWILIO_FROM_NUMBER")
to = os.Getenv("TWILIO_TO_NUMBER")
var apiKey = os.Getenv("TWILIO_API_KEY")
var secret = os.Getenv("TWILIO_API_SECRET")
var accountSid = os.Getenv("TWILIO_ACCOUNT_SID")
testClient = NewRestClientWithParams(ClientParams{apiKey, secret, accountSid, nil, nil})
ret := m.Run()
os.Exit(ret)
}
func TestSendingAText(t *testing.T) {
params := &Api.CreateMessageParams{}
params.SetTo(to)
params.SetFrom(from)
params.SetBody("Hello there")
resp, err := testClient.Api.CreateMessage(params)
assert.Nil(t, err)
assert.NotNil(t, resp)
assert.Equal(t, "Hello there", *resp.Body)
assert.Equal(t, from, *resp.From)
assert.Equal(t, to, *resp.To)
}
func TestListingNumbers(t *testing.T) {
resp, err := testClient.Api.ListIncomingPhoneNumber(nil)
assert.Nil(t, err)
assert.NotNil(t, resp)
// from, to numbers plus any other numbers that's configured for the account.
assert.GreaterOrEqual(t, len(resp), 2)
}
func TestListingANumber(t *testing.T) {
params := &Api.ListIncomingPhoneNumberParams{}
params.SetPhoneNumber(from)
resp, err := testClient.Api.ListIncomingPhoneNumber(params)
assert.Nil(t, err)
assert.NotNil(t, resp)
assert.Equal(t, 1, len(resp))
assert.Equal(t, from, *resp[0].PhoneNumber)
}
func TestSpecialCharacters(t *testing.T) {
serviceParams := &ChatV2.CreateServiceParams{}
serviceParams.SetFriendlyName("service|friendly&name")
service, err := testClient.ChatV2.CreateService(serviceParams)
assert.Nil(t, err)
assert.NotNil(t, service)
userParams := &ChatV2.CreateUserParams{}
userParams.SetIdentity("user|identity&string")
user, err := testClient.ChatV2.CreateUser(*service.Sid, userParams)
assert.Nil(t, err)
assert.NotNil(t, user)
err = testClient.ChatV2.DeleteUser(*service.Sid, *user.Identity)
assert.Nil(t, err)
err = testClient.ChatV2.DeleteService(*service.Sid)
assert.Nil(t, err)
}
func TestListParams(t *testing.T) {
sinkConfig := map[string]interface{}{
"destination": "http://example.org/webhook",
"method": "post",
"batch_events": false,
}
sinkParams := &EventsV1.CreateSinkParams{}
sinkParams.SetSinkConfiguration(sinkConfig)
sinkParams.SetDescription("test sink")
sinkParams.SetSinkType("webhook")
sink, err := testClient.EventsV1.CreateSink(sinkParams)
assert.Nil(t, err)
assert.NotNil(t, sink)
types := []map[string]interface{}{
{
"type": "com.twilio.messaging.message.delivered",
},
{
"type": "com.twilio.messaging.message.sent",
},
}
subscriptionsParams := &EventsV1.CreateSubscriptionParams{}
subscriptionsParams.SetSinkSid(*sink.Sid)
subscriptionsParams.SetTypes(types)
subscriptionsParams.SetDescription("test subscription")
subscription, err := testClient.EventsV1.CreateSubscription(subscriptionsParams)
assert.Nil(t, err)
assert.NotNil(t, subscription)
// Clean up the resources we've created
err = testClient.EventsV1.DeleteSubscription(*subscription.Sid)
assert.Nil(t, err)
err = testClient.EventsV1.DeleteSink(*sink.Sid)
assert.Nil(t, err)
}
func TestListingAvailableNumber(t *testing.T) {
params := &Api.ListAvailablePhoneNumberTollFreeParams{}
params.SetLimit(2)
resp, err := testClient.Api.ListAvailablePhoneNumberTollFree("US", params)
assert.Nil(t, err)
assert.NotNil(t, resp)
assert.Equal(t, 2, len(resp))
}
func TestOauth_WithCreateMessage(t *testing.T) {
from = os.Getenv("TWILIO_FROM_NUMBER_OAUTH")
to = os.Getenv("TWILIO_TO_NUMBER_OAUTH")
var clientId = os.Getenv("TWILIO_CLIENT_ID")
var clientSecret = os.Getenv("TWILIO_CLIENT_SECRET")
var accountSid = os.Getenv("TWILIO_ACCOUNT_SID_OAUTH")
clientCredentialProvider := &ClientCredentialProvider{
GrantType: "client_credentials",
ClientId: clientId,
ClientSecret: clientSecret,
}
testClient = NewRestClientWithParams(
ClientParams{ClientCredentialProvider: clientCredentialProvider, AccountSid: accountSid},
)
params := &Api.CreateMessageParams{}
params.SetTo(to)
params.SetFrom(from)
params.SetBody("Hello there")
resp, err := testClient.Api.CreateMessage(params)
assert.Nil(t, err)
assert.NotNil(t, resp)
assert.Equal(t, "Hello there", *resp.Body)
}
func TestOauth_WithCachedToken(t *testing.T) {
from = os.Getenv("TWILIO_FROM_NUMBER_OAUTH")
to = os.Getenv("TWILIO_TO_NUMBER_OAUTH")
var clientId = os.Getenv("TWILIO_CLIENT_ID")
var clientSecret = os.Getenv("TWILIO_CLIENT_SECRET")
var accountSid = os.Getenv("TWILIO_ACCOUNT_SID_OAUTH")
clientCredentialProvider := &ClientCredentialProvider{
GrantType: "client_credentials",
ClientId: clientId,
ClientSecret: clientSecret,
}
testClient = NewRestClientWithParams(
ClientParams{ClientCredentialProvider: clientCredentialProvider, AccountSid: accountSid},
)
params := &Api.CreateMessageParams{}
params.SetTo(to)
params.SetFrom(from)
params.SetBody("Hello there")
resp, err := testClient.Api.CreateMessage(params)
assert.Nil(t, err)
assert.NotNil(t, resp)
assert.Equal(t, "Hello there", *resp.Body)
listParams := &Api.ListMessageParams{}
listParams.SetLimit(1)
response, err := testClient.Api.ListMessage(listParams)
assert.Nil(t, err)
assert.NotNil(t, response)
assert.Equal(t, 1, len(response))
}
func TestTokenAuth_FetchToken(t *testing.T) {
var grantType = "client_credentials"
var clientId = os.Getenv("TWILIO_CLIENT_ID")
var clientSecret = os.Getenv("TWILIO_CLIENT_SECRET")
params := &IamV1.CreateTokenParams{
GrantType: &grantType,
ClientId: &clientId,
ClientSecret: &clientSecret,
Code: nil,
RedirectUri: nil,
Audience: nil,
RefreshToken: nil,
Scope: nil,
}
resp, err := testClient.IamV1.CreateToken(params)
assert.Nil(t, err)
assert.NotNil(t, resp)
}
func TestTokenAuthFetchTokenException(t *testing.T) {
var grantType = "client_credentials"
var clientId = os.Getenv("TWILIO_CLIENT_ID")
var clientSecret = os.Getenv("TWILIO_CLIENT_SECRET") + "invalid"
params := &IamV1.CreateTokenParams{
GrantType: &grantType,
ClientId: &clientId,
ClientSecret: &clientSecret,
}
resp, err := testClient.IamV1.CreateToken(params)
assert.NotNil(t, 403, err.(*client.TwilioRestError).Status)
assert.Nil(t, resp)
}