-
Notifications
You must be signed in to change notification settings - Fork 462
/
twilio_spec.rb
393 lines (351 loc) · 15.9 KB
/
twilio_spec.rb
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
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
require 'twilio-ruby'
require 'fakeweb'
require 'rack'
describe Twilio::REST::Client do
before :all do
FakeWeb.register_uri(:any, %r/http:\/\/api.twilio.com\//, :body => '{"message": "You tried to reach Twilio"}')
end
it 'should set up a new client instance with the given sid and token' do
twilio = Twilio::REST::Client.new('someSid', 'someToken')
twilio.account_sid.should == 'someSid'
twilio.instance_variable_get('@auth_token').should == 'someToken'
end
it 'should set up the proper default http ssl connection' do
twilio = Twilio::REST::Client.new('someSid', 'someToken')
connection = twilio.instance_variable_get('@connection')
connection.address.should == 'api.twilio.com'
connection.port.should == 443
connection.use_ssl?.should == true
end
it 'should set up the requested ssl verification ca_file if provided' do
twilio = Twilio::REST::Client.new('someSid', 'someToken', :ssl_ca_file => '/path/to/ca/file')
connection = twilio.instance_variable_get('@connection')
connection.ca_file.should == '/path/to/ca/file'
end
it 'should set up the proper http ssl connection when a different domain is given' do
twilio = Twilio::REST::Client.new('someSid', 'someToken', :host => 'api.faketwilio.com')
connection = twilio.instance_variable_get('@connection')
connection.address.should == 'api.faketwilio.com'
connection.port.should == 443
connection.use_ssl?.should == true
end
it 'should adjust the open and read timeouts on the underlying Net::HTTP object when asked' do
timeout = rand(30)
twilio = Twilio::REST::Client.new('someSid', 'someToken', :timeout => timeout)
connection = twilio.instance_variable_get('@connection')
connection.port.should == 443
connection.use_ssl?.should == true
connection.open_timeout.should == timeout
connection.read_timeout.should == timeout
end
it 'should set up the proper http ssl connection when a proxy_host is given' do
twilio = Twilio::REST::Client.new('someSid', 'someToken', :host => 'api.faketwilio.com', :proxy_addr => 'localhost')
connection = twilio.instance_variable_get('@connection')
connection.proxy?.should == true
connection.proxy_address.should == 'localhost'
connection.proxy_port.should == 80
connection.address.should == 'api.faketwilio.com'
connection.port.should == 443
connection.use_ssl?.should == true
end
it 'should set up the proper http ssl connection when a proxy_host and proxy_port are given' do
twilio = Twilio::REST::Client.new('someSid', 'someToken', :host => 'api.faketwilio.com', :proxy_addr => 'localhost', :proxy_port => 13128)
connection = twilio.instance_variable_get('@connection')
connection.proxy?.should == true
connection.proxy_address.should == 'localhost'
connection.proxy_port.should == 13128
connection.address.should == 'api.faketwilio.com'
connection.port.should == 443
connection.use_ssl?.should == true
end
it 'should set up an accounts resources object' do
twilio = Twilio::REST::Client.new('someSid', 'someToken')
twilio.respond_to?(:accounts).should == true
twilio.accounts.instance_variable_get('@uri').should == '/2010-04-01/Accounts'
end
it 'should set up an account object with the given sid' do
twilio = Twilio::REST::Client.new('someSid', 'someToken')
twilio.respond_to?(:account).should == true
twilio.account.instance_variable_get('@uri').should == '/2010-04-01/Accounts/someSid'
end
it 'should convert all parameter names to Twilio-style names' do
twilio = Twilio::REST::Client.new('someSid', 'someToken')
untwilified = {:sms_url => 'someUrl', 'voiceFallbackUrl' => 'anotherUrl',
'Status_callback' => 'yetAnotherUrl'}
twilified = {:SmsUrl => 'someUrl', :VoiceFallbackUrl => 'anotherUrl',
:StatusCallback => 'yetAnotherUrl'}
twilio.instance_eval do
twilify(untwilified).should == twilified
end
end
end
describe Twilio::REST::InstanceResource do
it 'should set up an internal reference to the uri and client' do
resource = Twilio::REST::InstanceResource.new('some/uri', 'someClient')
resource.instance_variable_get('@uri').should == 'some/uri'
resource.instance_variable_get('@client').should == 'someClient'
end
it 'should set up object properties if passed' do
params = {'SomeKey' => 'someValue'}
resource = Twilio::REST::InstanceResource.new('uri', 'client', params)
resource.some_key.should == 'someValue'
end
end
describe Twilio::REST::Account do
it 'should set up an incoming phone numbers resources object' do
account = Twilio::REST::Account.new('someUri', 'someClient')
account.respond_to?(:incoming_phone_numbers).should == true
account.incoming_phone_numbers.instance_variable_get('@uri').should == 'someUri/IncomingPhoneNumbers'
end
it 'should set up an available phone numbers resources object' do
account = Twilio::REST::Account.new('someUri', 'someClient')
account.respond_to?(:available_phone_numbers).should == true
account.available_phone_numbers.instance_variable_get('@uri').should == 'someUri/AvailablePhoneNumbers'
end
it 'should set up an outgoing caller ids resources object' do
account = Twilio::REST::Account.new('someUri', 'someClient')
account.respond_to?(:outgoing_caller_ids).should == true
account.outgoing_caller_ids.instance_variable_get('@uri').should == 'someUri/OutgoingCallerIds'
end
it 'should set up a calls resources object' do
account = Twilio::REST::Account.new('someUri', 'someClient')
account.respond_to?(:calls).should == true
account.calls.instance_variable_get('@uri').should == 'someUri/Calls'
end
it 'should set up a conferences resources object' do
account = Twilio::REST::Account.new('someUri', 'someClient')
account.respond_to?(:conferences).should == true
account.conferences.instance_variable_get('@uri').should == 'someUri/Conferences'
end
it 'should set up a sms resource object' do
account = Twilio::REST::Account.new('someUri', 'someClient')
account.respond_to?(:sms).should == true
account.sms.instance_variable_get('@uri').should == 'someUri/SMS'
end
it 'should set up a recordings resources object' do
account = Twilio::REST::Account.new('someUri', 'someClient')
account.respond_to?(:recordings).should == true
account.recordings.instance_variable_get('@uri').should == 'someUri/Recordings'
end
it 'should set up a transcriptions resources object' do
account = Twilio::REST::Account.new('someUri', 'someClient')
account.respond_to?(:transcriptions).should == true
account.transcriptions.instance_variable_get('@uri').should == 'someUri/Transcriptions'
end
it 'should set up a notifications resources object' do
account = Twilio::REST::Account.new('someUri', 'someClient')
account.respond_to?(:notifications).should == true
account.notifications.instance_variable_get('@uri').should == 'someUri/Notifications'
end
end
describe Twilio::REST::Call do
it 'should set up a recordings resources object' do
call = Twilio::REST::Call.new('someUri', 'someClient')
call.respond_to?(:recordings).should == true
call.recordings.instance_variable_get('@uri').should == 'someUri/Recordings'
end
it 'should set up a notifications resources object' do
call = Twilio::REST::Call.new('someUri', 'someClient')
call.respond_to?(:notifications).should == true
call.notifications.instance_variable_get('@uri').should == 'someUri/Notifications'
end
end
describe Twilio::REST::Conference do
it 'should set up a participants resources object' do
call = Twilio::REST::Conference.new('someUri', 'someClient')
call.respond_to?(:participants).should == true
call.participants.instance_variable_get('@uri').should == 'someUri/Participants'
end
end
describe Twilio::REST::Recording do
it 'should set up a transcriptions resources object' do
call = Twilio::REST::Recording.new('someUri', 'someClient')
call.respond_to?(:transcriptions).should == true
call.transcriptions.instance_variable_get('@uri').should == 'someUri/Transcriptions'
end
end
describe Twilio::Util::RequestValidator do
it 'should properly validate a Twilio Voice request' do
token = '2bd9e9638872de601313dc77410d3b23'
validator = Twilio::Util::RequestValidator.new token
url = 'http://twiliotests.heroku.com/validate/voice'
params = {
'ToState' => 'California',
'CalledState' => 'California',
'Direction' => 'inbound',
'FromState' => 'CA',
'AccountSid' => 'ACba8bc05eacf94afdae398e642c9cc32d',
'Caller' => '+14153595711',
'CallerZip' => '94108',
'CallerCountry' => 'US',
'From' => '+14153595711',
'FromCity' => 'SAN FRANCISCO',
'CallerCity' => 'SAN FRANCISCO',
'To' => '+14157669926',
'FromZip' => '94108',
'FromCountry' => 'US',
'ToCity' => '',
'CallStatus' => 'ringing',
'CalledCity' => '',
'CallerState' => 'CA',
'CalledZip' => '',
'ToZip' => '',
'ToCountry' => 'US',
'CallSid' => 'CA136d09cd59a3c0ec8dbff44da5c03f31',
'CalledCountry' => 'US',
'Called' => '+14157669926',
'ApiVersion' => '2010-04-01',
'ApplicationSid' => 'AP44efecad51364e80b133bb7c07eb8204'
}
signature = 'oVb2kXoVy8GEfwBDjR8bk/ZZ6eA='
validator.validate(url, params, signature).should == true
end
it 'should properly validate a Twilio SMS request' do
token = '2bd9e9638872de601313dc77410d3b23'
validator = Twilio::Util::RequestValidator.new token
url = 'http://twiliotests.heroku.com/validate/sms'
params = {
'ToState' => 'CA',
'FromState' => 'CA',
'AccountSid' => 'ACba8bc05eacf94afdae398e642c9cc32d',
'SmsMessageSid' => 'SM2003cbd5e6a3701999aa3e5f20ff2787',
'Body' => 'Orly',
'From' => '+14159354345',
'FromCity' => 'SAN FRANCISCO',
'SmsStatus' => 'received',
'FromZip' => '94107',
'FromCountry' => 'US',
'To' => '+14158141819',
'ToCity' => 'SAN FRANCISCO',
'ToZip' => '94105',
'ToCountry' => 'US',
'ApiVersion' => '2010-04-01',
'SmsSid' => 'SM2003cbd5e6a3701999aa3e5f20ff2787'
}
signature = 'mxeiv65lEe0b8L6LdVw2jgJi8yw='
validator.validate(url, params, signature).should == true
end
end
describe Twilio::Util::Capability do
before :each do
@capability = Twilio::Util::Capability.new 'myAccountSid', 'myAuthToken'
end
def queries(q)
q.scan(/scope:client:(incoming|outgoing)\?(\S+)/).map{|(type, query)| [type, Rack::Utils.parse_query(query)]}
end
it 'should return a valid jwt when #generate is called' do
token = @capability.generate
decoded = JWT.decode token, 'myAuthToken'
decoded['scope'].should_not be_nil
decoded['iss'].should_not be_nil
decoded['exp'].should_not be_nil
end
it 'should properly set the iss key in the payload' do
token = @capability.generate
decoded = JWT.decode token, 'myAuthToken'
decoded['iss'].should == 'myAccountSid'
end
it 'should properly set the exp key based on the default hour ttl' do
seconds = Time.now.to_i
token = @capability.generate
decoded = JWT.decode token, 'myAuthToken'
decoded['exp'].should == seconds + 3600
end
it 'should properly set the exp key based on the ttl passed to #generate' do
ttl = rand 10000
seconds = Time.now.to_i
token = @capability.generate ttl
decoded = JWT.decode token, 'myAuthToken'
decoded['exp'].should == seconds + ttl
end
it 'should generate a proper incoming client scope string' do
@capability.allow_client_incoming 'andrew'
token = @capability.generate
decoded = JWT.decode token, 'myAuthToken'
queries(decoded['scope']).should == [['incoming', {'clientName' => 'andrew'}]]
end
it 'should generate multiple proper incoming client scope strings' do
@capability.allow_client_incoming 'andrew'
@capability.allow_client_incoming 'bridget'
token = @capability.generate
decoded = JWT.decode token, 'myAuthToken'
queries(decoded['scope']).should == [
['incoming', {'clientName' => 'andrew'}],
['incoming', {'clientName' => 'bridget'}]
]
end
it 'should generate a proper outgoing client scope string' do
@capability.allow_client_outgoing 'myAppSid'
token = @capability.generate
decoded = JWT.decode token, 'myAuthToken'
queries(decoded['scope']).should == [['outgoing', {'appSid' => 'myAppSid'}]]
end
it 'should generate a proper outgoing client scope string with parameters' do
app_params_hash = {'key' => 'a value', :foo => 'bar/baz'}
@capability.allow_client_outgoing 'myAppSid', app_params_hash
app_params = @capability.instance_eval {url_encode(app_params_hash)}
params_hash = {'appSid' => 'myAppSid', 'appParams' => app_params}
params = @capability.instance_eval {url_encode(params_hash)}
token = @capability.generate
decoded = JWT.decode token, 'myAuthToken'
queries(decoded['scope']).should == [['outgoing', params_hash]]
end
it 'should generate a proper outgoing client scope string based on the ' +
'client name when calling #allow_client_incoming first' do
@capability.allow_client_incoming 'andrew'
@capability.allow_client_outgoing 'myAppSid'
token = @capability.generate
decoded = JWT.decode token, 'myAuthToken'
queries(decoded['scope']).should == [
['incoming', {'clientName' => 'andrew'}],
['outgoing', {'clientName' => 'andrew', 'appSid' => 'myAppSid'}]
]
end
it 'should generate a proper outgoing client scope string based on the ' +
'client name when calling #allow_client_incoming second' do
@capability.allow_client_outgoing 'myAppSid'
@capability.allow_client_incoming 'andrew'
token = @capability.generate
decoded = JWT.decode token, 'myAuthToken'
queries(decoded['scope']).should == [["incoming", {"clientName"=>"andrew"}], ["outgoing", {"clientName"=>"andrew", "appSid"=>"myAppSid"}]]
end
it 'should generate a proper outgoing client scope string with parameters ' +
'and a client name when calling #allow_client_incoming first' do
@capability.allow_client_incoming 'andrew'
app_params_hash = {'key' => 'a value', :foo => 'bar/baz'}
@capability.allow_client_outgoing 'myAppSid', app_params_hash
app_params = @capability.instance_eval {url_encode(app_params_hash)}
params_hash = {'appSid' => 'myAppSid', 'appParams' => app_params, 'clientName' => 'andrew'}
params = @capability.instance_eval {url_encode(params_hash)}
token = @capability.generate
decoded = JWT.decode token, 'myAuthToken'
scopes = queries(decoded['scope'])
scopes.shift.should == ["incoming", {"clientName"=>"andrew"}]
scope = scopes.shift
scope.first.should == 'outgoing'
Rack::Utils.parse_query(scope.last['appParams']).should == {'key' => 'a value', 'foo' => 'bar/baz'}
scope.last["clientName"].should == "andrew"
scope.last["appSid"].should == "myAppSid"
scopes.should be_empty
end
it 'should generate a proper outgoing client scope string with parameters ' +
'and a client name when calling #allow_client_incoming second' do
app_params_hash = {'key' => 'a value', :foo => 'bar/baz'}
@capability.allow_client_outgoing 'myAppSid', app_params_hash
@capability.allow_client_incoming 'andrew'
app_params = @capability.instance_eval {url_encode(app_params_hash)}
params_hash = {'appSid' => 'myAppSid', 'appParams' => app_params, 'clientName' => 'andrew'}
params = @capability.instance_eval {url_encode(params_hash)}
token = @capability.generate
decoded = JWT.decode token, 'myAuthToken'
scopes = queries(decoded['scope'])
scopes.shift.should == ["incoming", {"clientName"=>"andrew"}]
scope = scopes.shift
scope.first.should == 'outgoing'
Rack::Utils.parse_query(scope.last['appParams']).should == {'key' => 'a value', 'foo' => 'bar/baz'}
scope.last["clientName"].should == "andrew"
scope.last["appSid"].should == "myAppSid"
scopes.should be_empty
end
end