twilio-ruby 5.3.1 → 5.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +17 -0
  3. data/README.md +2 -2
  4. data/lib/rack/twilio_webhook_authentication.rb +46 -0
  5. data/lib/twilio-ruby/rest/api/v2010/account/incoming_phone_number/local.rb +10 -1
  6. data/lib/twilio-ruby/rest/api/v2010/account/incoming_phone_number/mobile.rb +10 -1
  7. data/lib/twilio-ruby/rest/api/v2010/account/incoming_phone_number/toll_free.rb +10 -1
  8. data/lib/twilio-ruby/rest/api/v2010/account/incoming_phone_number.rb +22 -3
  9. data/lib/twilio-ruby/rest/api/v2010/account/message.rb +3 -1
  10. data/lib/twilio-ruby/rest/client.rb +7 -0
  11. data/lib/twilio-ruby/rest/preview/hosted_numbers/authorization_document.rb +417 -0
  12. data/lib/twilio-ruby/rest/preview/hosted_numbers/hosted_number_order.rb +8 -7
  13. data/lib/twilio-ruby/rest/preview/hosted_numbers.rb +16 -0
  14. data/lib/twilio-ruby/rest/preview.rb +9 -0
  15. data/lib/twilio-ruby/rest/proxy/v1/service/phone_number.rb +384 -0
  16. data/lib/twilio-ruby/rest/proxy/v1/service/session/interaction.rb +456 -0
  17. data/lib/twilio-ruby/rest/proxy/v1/service/session/participant/message_interaction.rb +465 -0
  18. data/lib/twilio-ruby/rest/proxy/v1/service/session/participant.rb +541 -0
  19. data/lib/twilio-ruby/rest/proxy/v1/service/session.rb +578 -0
  20. data/lib/twilio-ruby/rest/proxy/v1/service/short_code.rb +377 -0
  21. data/lib/twilio-ruby/rest/proxy/v1/service.rb +509 -0
  22. data/lib/twilio-ruby/rest/proxy/v1.rb +41 -0
  23. data/lib/twilio-ruby/rest/proxy.rb +44 -0
  24. data/lib/twilio-ruby/rest/wireless/v1/sim.rb +7 -0
  25. data/lib/twilio-ruby/security/request_validator.rb +1 -1
  26. data/lib/twilio-ruby/twiml/messaging_response.rb +5 -5
  27. data/lib/twilio-ruby/twiml/voice_response.rb +9 -7
  28. data/lib/twilio-ruby/util/configuration.rb +0 -3
  29. data/lib/twilio-ruby/version.rb +1 -1
  30. data/lib/twilio-ruby.rb +1 -0
  31. data/spec/integration/api/v2010/account/incoming_phone_number/local_spec.rb +2 -0
  32. data/spec/integration/api/v2010/account/incoming_phone_number/mobile_spec.rb +2 -0
  33. data/spec/integration/api/v2010/account/incoming_phone_number/toll_free_spec.rb +2 -0
  34. data/spec/integration/api/v2010/account/incoming_phone_number_spec.rb +4 -0
  35. data/spec/integration/preview/hosted_numbers/authorization_document_spec.rb +211 -0
  36. data/spec/integration/preview/hosted_numbers/hosted_number_order_spec.rb +35 -4
  37. data/spec/integration/proxy/v1/service/phone_number_spec.rb +176 -0
  38. data/spec/integration/proxy/v1/service/session/interaction_spec.rb +104 -0
  39. data/spec/integration/proxy/v1/service/session/participant/message_interaction_spec.rb +164 -0
  40. data/spec/integration/proxy/v1/service/session/participant_spec.rb +232 -0
  41. data/spec/integration/proxy/v1/service/session_spec.rb +225 -0
  42. data/spec/integration/proxy/v1/service/short_code_spec.rb +173 -0
  43. data/spec/integration/proxy/v1/service_spec.rb +200 -0
  44. data/spec/integration/wireless/v1/sim_spec.rb +6 -3
  45. data/spec/rack/twilio_webhook_authentication_spec.rb +106 -0
  46. data/spec/security/request_validator_spec.rb +28 -0
  47. data/spec/twiml/messaging_response_spec.rb +6 -6
  48. metadata +31 -2
@@ -0,0 +1,173 @@
1
+ ##
2
+ # This code was generated by
3
+ # \ / _ _ _| _ _
4
+ # | (_)\/(_)(_|\/| |(/_ v1.0.0
5
+ # / /
6
+
7
+ require 'spec_helper.rb'
8
+
9
+ describe 'ShortCode' do
10
+ it "can create" do
11
+ @holodeck.mock(Twilio::Response.new(500, ''))
12
+
13
+ expect {
14
+ @client.proxy.v1.services("KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") \
15
+ .short_codes.create(sid: "SCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
16
+ }.to raise_exception(Twilio::REST::TwilioError)
17
+
18
+ values = {
19
+ 'Sid' => "SCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
20
+ }
21
+ expect(
22
+ @holodeck.has_request?(Holodeck::Request.new(
23
+ method: 'post',
24
+ url: 'https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/ShortCodes',
25
+ data: values,
26
+ ))).to eq(true)
27
+ end
28
+
29
+ it "receives create responses" do
30
+ @holodeck.mock(Twilio::Response.new(
31
+ 201,
32
+ %q[
33
+ {
34
+ "sid": "SCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
35
+ "account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
36
+ "service_sid": "KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
37
+ "date_created": "2015-07-30T20:00:00Z",
38
+ "date_updated": "2015-07-30T20:00:00Z",
39
+ "short_code": "12345",
40
+ "iso_country": "US",
41
+ "capabilities": [],
42
+ "url": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/ShortCodes/SCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
43
+ }
44
+ ]
45
+ ))
46
+
47
+ actual = @client.proxy.v1.services("KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") \
48
+ .short_codes.create(sid: "SCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
49
+
50
+ expect(actual).to_not eq(nil)
51
+ end
52
+
53
+ it "can delete" do
54
+ @holodeck.mock(Twilio::Response.new(500, ''))
55
+
56
+ expect {
57
+ @client.proxy.v1.services("KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") \
58
+ .short_codes("SCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").delete()
59
+ }.to raise_exception(Twilio::REST::TwilioError)
60
+
61
+ values = {}
62
+ expect(
63
+ @holodeck.has_request?(Holodeck::Request.new(
64
+ method: 'delete',
65
+ url: 'https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/ShortCodes/SCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
66
+ ))).to eq(true)
67
+ end
68
+
69
+ it "receives delete responses" do
70
+ @holodeck.mock(Twilio::Response.new(
71
+ 204,
72
+ nil,
73
+ ))
74
+
75
+ actual = @client.proxy.v1.services("KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") \
76
+ .short_codes("SCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").delete()
77
+
78
+ expect(actual).to eq(true)
79
+ end
80
+
81
+ it "can read" do
82
+ @holodeck.mock(Twilio::Response.new(500, ''))
83
+
84
+ expect {
85
+ @client.proxy.v1.services("KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") \
86
+ .short_codes.list()
87
+ }.to raise_exception(Twilio::REST::TwilioError)
88
+
89
+ values = {}
90
+ expect(
91
+ @holodeck.has_request?(Holodeck::Request.new(
92
+ method: 'get',
93
+ url: 'https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/ShortCodes',
94
+ ))).to eq(true)
95
+ end
96
+
97
+ it "receives read_full responses" do
98
+ @holodeck.mock(Twilio::Response.new(
99
+ 200,
100
+ %q[
101
+ {
102
+ "meta": {
103
+ "page": 0,
104
+ "page_size": 50,
105
+ "first_page_url": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/ShortCodes?PageSize=50&Page=0",
106
+ "previous_page_url": null,
107
+ "next_page_url": null,
108
+ "key": "short_codes",
109
+ "url": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/ShortCodes?PageSize=50&Page=0"
110
+ },
111
+ "short_codes": [
112
+ {
113
+ "account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
114
+ "service_sid": "KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
115
+ "sid": "SCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
116
+ "date_created": "2015-07-30T20:00:00Z",
117
+ "date_updated": "2015-07-30T20:00:00Z",
118
+ "short_code": "12345",
119
+ "iso_country": "US",
120
+ "capabilities": [],
121
+ "url": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/ShortCodes/SCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
122
+ }
123
+ ]
124
+ }
125
+ ]
126
+ ))
127
+
128
+ actual = @client.proxy.v1.services("KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") \
129
+ .short_codes.list()
130
+
131
+ expect(actual).to_not eq(nil)
132
+ end
133
+
134
+ it "can fetch" do
135
+ @holodeck.mock(Twilio::Response.new(500, ''))
136
+
137
+ expect {
138
+ @client.proxy.v1.services("KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") \
139
+ .short_codes("SCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").fetch()
140
+ }.to raise_exception(Twilio::REST::TwilioError)
141
+
142
+ values = {}
143
+ expect(
144
+ @holodeck.has_request?(Holodeck::Request.new(
145
+ method: 'get',
146
+ url: 'https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/ShortCodes/SCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
147
+ ))).to eq(true)
148
+ end
149
+
150
+ it "receives fetch responses" do
151
+ @holodeck.mock(Twilio::Response.new(
152
+ 200,
153
+ %q[
154
+ {
155
+ "sid": "SCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
156
+ "account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
157
+ "service_sid": "KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
158
+ "date_created": "2015-07-30T20:00:00Z",
159
+ "date_updated": "2015-07-30T20:00:00Z",
160
+ "short_code": "12345",
161
+ "iso_country": "US",
162
+ "capabilities": [],
163
+ "url": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/ShortCodes/SCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
164
+ }
165
+ ]
166
+ ))
167
+
168
+ actual = @client.proxy.v1.services("KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") \
169
+ .short_codes("SCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").fetch()
170
+
171
+ expect(actual).to_not eq(nil)
172
+ end
173
+ end
@@ -0,0 +1,200 @@
1
+ ##
2
+ # This code was generated by
3
+ # \ / _ _ _| _ _
4
+ # | (_)\/(_)(_|\/| |(/_ v1.0.0
5
+ # / /
6
+
7
+ require 'spec_helper.rb'
8
+
9
+ describe 'Service' do
10
+ it "can fetch" do
11
+ @holodeck.mock(Twilio::Response.new(500, ''))
12
+
13
+ expect {
14
+ @client.proxy.v1.services("KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").fetch()
15
+ }.to raise_exception(Twilio::REST::TwilioError)
16
+
17
+ values = {}
18
+ expect(
19
+ @holodeck.has_request?(Holodeck::Request.new(
20
+ method: 'get',
21
+ url: 'https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
22
+ ))).to eq(true)
23
+ end
24
+
25
+ it "receives fetch responses" do
26
+ @holodeck.mock(Twilio::Response.new(
27
+ 200,
28
+ %q[
29
+ {
30
+ "sid": "KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
31
+ "account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
32
+ "friendly_name": "friendly_name",
33
+ "default_ttl": 3600,
34
+ "callback_url": "http://www.example.com",
35
+ "date_created": "2015-07-30T20:00:00Z",
36
+ "date_updated": "2015-07-30T20:00:00Z",
37
+ "url": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
38
+ "links": {
39
+ "sessions": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Sessions",
40
+ "phone_numbers": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/PhoneNumbers",
41
+ "short_codes": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/ShortCodes"
42
+ }
43
+ }
44
+ ]
45
+ ))
46
+
47
+ actual = @client.proxy.v1.services("KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").fetch()
48
+
49
+ expect(actual).to_not eq(nil)
50
+ end
51
+
52
+ it "can read" do
53
+ @holodeck.mock(Twilio::Response.new(500, ''))
54
+
55
+ expect {
56
+ @client.proxy.v1.services.list()
57
+ }.to raise_exception(Twilio::REST::TwilioError)
58
+
59
+ values = {}
60
+ expect(
61
+ @holodeck.has_request?(Holodeck::Request.new(
62
+ method: 'get',
63
+ url: 'https://proxy.twilio.com/v1/Services',
64
+ ))).to eq(true)
65
+ end
66
+
67
+ it "receives read_empty responses" do
68
+ @holodeck.mock(Twilio::Response.new(
69
+ 200,
70
+ %q[
71
+ {
72
+ "services": [],
73
+ "meta": {
74
+ "first_page_url": "https://proxy.twilio.com/v1/Services?PageSize=50&Page=0",
75
+ "key": "services",
76
+ "next_page_url": null,
77
+ "page": 0,
78
+ "page_size": 50,
79
+ "previous_page_url": null,
80
+ "url": "https://proxy.twilio.com/v1/Services?PageSize=50&Page=0"
81
+ }
82
+ }
83
+ ]
84
+ ))
85
+
86
+ actual = @client.proxy.v1.services.list()
87
+
88
+ expect(actual).to_not eq(nil)
89
+ end
90
+
91
+ it "can create" do
92
+ @holodeck.mock(Twilio::Response.new(500, ''))
93
+
94
+ expect {
95
+ @client.proxy.v1.services.create()
96
+ }.to raise_exception(Twilio::REST::TwilioError)
97
+
98
+ values = {}
99
+ expect(
100
+ @holodeck.has_request?(Holodeck::Request.new(
101
+ method: 'post',
102
+ url: 'https://proxy.twilio.com/v1/Services',
103
+ ))).to eq(true)
104
+ end
105
+
106
+ it "receives create responses" do
107
+ @holodeck.mock(Twilio::Response.new(
108
+ 201,
109
+ %q[
110
+ {
111
+ "sid": "KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
112
+ "account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
113
+ "friendly_name": "friendly_name",
114
+ "default_ttl": 3600,
115
+ "callback_url": "http://www.example.com",
116
+ "date_created": "2015-07-30T20:00:00Z",
117
+ "date_updated": "2015-07-30T20:00:00Z",
118
+ "url": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
119
+ "links": {
120
+ "sessions": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Sessions",
121
+ "phone_numbers": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/PhoneNumbers",
122
+ "short_codes": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/ShortCodes"
123
+ }
124
+ }
125
+ ]
126
+ ))
127
+
128
+ actual = @client.proxy.v1.services.create()
129
+
130
+ expect(actual).to_not eq(nil)
131
+ end
132
+
133
+ it "can delete" do
134
+ @holodeck.mock(Twilio::Response.new(500, ''))
135
+
136
+ expect {
137
+ @client.proxy.v1.services("KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").delete()
138
+ }.to raise_exception(Twilio::REST::TwilioError)
139
+
140
+ values = {}
141
+ expect(
142
+ @holodeck.has_request?(Holodeck::Request.new(
143
+ method: 'delete',
144
+ url: 'https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
145
+ ))).to eq(true)
146
+ end
147
+
148
+ it "receives delete responses" do
149
+ @holodeck.mock(Twilio::Response.new(
150
+ 204,
151
+ nil,
152
+ ))
153
+
154
+ actual = @client.proxy.v1.services("KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").delete()
155
+
156
+ expect(actual).to eq(true)
157
+ end
158
+
159
+ it "can update" do
160
+ @holodeck.mock(Twilio::Response.new(500, ''))
161
+
162
+ expect {
163
+ @client.proxy.v1.services("KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").update()
164
+ }.to raise_exception(Twilio::REST::TwilioError)
165
+
166
+ values = {}
167
+ expect(
168
+ @holodeck.has_request?(Holodeck::Request.new(
169
+ method: 'post',
170
+ url: 'https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
171
+ ))).to eq(true)
172
+ end
173
+
174
+ it "receives update responses" do
175
+ @holodeck.mock(Twilio::Response.new(
176
+ 200,
177
+ %q[
178
+ {
179
+ "sid": "KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
180
+ "account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
181
+ "friendly_name": "friendly_name",
182
+ "default_ttl": 3600,
183
+ "callback_url": "http://www.example.com",
184
+ "date_created": "2015-07-30T20:00:00Z",
185
+ "date_updated": "2015-07-30T20:00:00Z",
186
+ "url": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
187
+ "links": {
188
+ "sessions": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Sessions",
189
+ "phone_numbers": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/PhoneNumbers",
190
+ "short_codes": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/ShortCodes"
191
+ }
192
+ }
193
+ ]
194
+ ))
195
+
196
+ actual = @client.proxy.v1.services("KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").update()
197
+
198
+ expect(actual).to_not eq(nil)
199
+ end
200
+ end
@@ -52,7 +52,8 @@ describe 'Sim' do
52
52
  "iccid": "iccid",
53
53
  "e_id": "e_id",
54
54
  "status": "new",
55
- "url": "https://wireless.twilio.com/v1/Sims/DEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
55
+ "url": "https://wireless.twilio.com/v1/Sims/DEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
56
+ "ip_address": "192.168.1.1"
56
57
  }
57
58
  ]
58
59
  ))
@@ -133,7 +134,8 @@ describe 'Sim' do
133
134
  "voice_fallback_url": "http://www.example.com",
134
135
  "voice_method": "http_method",
135
136
  "voice_url": "http://www.example.com",
136
- "url": "https://wireless.twilio.com/v1/Sims/DEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
137
+ "url": "https://wireless.twilio.com/v1/Sims/DEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
138
+ "ip_address": "192.168.1.30"
137
139
  }
138
140
  ],
139
141
  "meta": {
@@ -199,7 +201,8 @@ describe 'Sim' do
199
201
  "voice_fallback_url": "http://www.example.com",
200
202
  "voice_method": "http_method",
201
203
  "voice_url": "http://www.example.com",
202
- "url": "https://wireless.twilio.com/v1/Sims/DEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
204
+ "url": "https://wireless.twilio.com/v1/Sims/DEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
205
+ "ip_address": "192.168.1.30"
203
206
  }
204
207
  ]
205
208
  ))
@@ -0,0 +1,106 @@
1
+ require 'spec_helper'
2
+ require 'rack/mock'
3
+
4
+ describe Rack::TwilioWebhookAuthentication do
5
+ before do
6
+ @app = ->(_env) { [200, { 'Content-Type' => 'text/plain' }, ['Hello']] }
7
+ end
8
+
9
+ describe 'new' do
10
+ it 'should initialize with an app, auth token and a path' do
11
+ expect do
12
+ Rack::TwilioWebhookAuthentication.new(@app, 'ABC', /\/voice/)
13
+ end.not_to raise_error
14
+ end
15
+
16
+ it 'should initialize with an app, auth token and paths' do
17
+ expect do
18
+ Rack::TwilioWebhookAuthentication.new(@app, 'ABC', /\/voice/, /\/sms/)
19
+ end.not_to raise_error
20
+ end
21
+
22
+ it 'should initialize with an app, dynamic token and paths' do
23
+ expect do
24
+ Rack::TwilioWebhookAuthentication.new(@app, nil, /\/voice/, /\/sms/)
25
+ end.not_to raise_error
26
+ end
27
+ end
28
+
29
+ describe 'calling against one path with dynamic auth token' do
30
+ it 'should allow a request through if it validates' do
31
+ auth_token = 'qwerty'
32
+ account_sid = 12_345
33
+ expect_any_instance_of(Rack::Request).to receive(:post?).and_return(true)
34
+ expect_any_instance_of(Rack::Request).to receive(:POST).and_return({ 'AccountSid' => account_sid })
35
+ @middleware = Rack::TwilioWebhookAuthentication.new(@app, nil, /\/voice/) { |asid| auth_token }
36
+ request_validator = double('RequestValidator')
37
+ expect(Twilio::Security::RequestValidator).to receive(:new).with(auth_token).and_return(request_validator)
38
+ expect(request_validator).to receive(:validate).and_return(true)
39
+ request = Rack::MockRequest.env_for('/voice')
40
+ status, headers, body = @middleware.call(request)
41
+ expect(status).to be(200)
42
+ end
43
+ end
44
+
45
+ describe 'calling against one path' do
46
+ before do
47
+ @middleware = Rack::TwilioWebhookAuthentication.new(@app, 'ABC', /\/voice/)
48
+ end
49
+
50
+ it 'should not intercept when the path doesn\'t match' do
51
+ expect(Twilio::Security::RequestValidator).to_not receive(:validate)
52
+ request = Rack::MockRequest.env_for('/sms')
53
+ status, headers, body = @middleware.call(request)
54
+ expect(status).to be(200)
55
+ end
56
+
57
+ it 'should allow a request through if it validates' do
58
+ expect_any_instance_of(Twilio::Security::RequestValidator).to(
59
+ receive(:validate).and_return(true)
60
+ )
61
+ request = Rack::MockRequest.env_for('/voice')
62
+ status, headers, body = @middleware.call(request)
63
+ expect(status).to be(200)
64
+ end
65
+
66
+ it 'should short circuit a request to 403 if it does not validate' do
67
+ expect_any_instance_of(Twilio::Security::RequestValidator).to(
68
+ receive(:validate).and_return(false)
69
+ )
70
+ request = Rack::MockRequest.env_for('/voice')
71
+ status, headers, body = @middleware.call(request)
72
+ expect(status).to be(403)
73
+ end
74
+ end
75
+
76
+ describe 'calling against many paths' do
77
+ before do
78
+ @middleware = Rack::TwilioWebhookAuthentication.new(@app, 'ABC', /\/voice/, /\/sms/)
79
+ end
80
+
81
+ it 'should not intercept when the path doesn\'t match' do
82
+ expect(Twilio::Security::RequestValidator).to_not receive(:validate)
83
+ request = Rack::MockRequest.env_for('icesms')
84
+ status, headers, body = @middleware.call(request)
85
+ expect(status).to be(200)
86
+ end
87
+
88
+ it 'shold allow a request through if it validates' do
89
+ expect_any_instance_of(Twilio::Security::RequestValidator).to(
90
+ receive(:validate).and_return(true)
91
+ )
92
+ request = Rack::MockRequest.env_for('/sms')
93
+ status, headers, body = @middleware.call(request)
94
+ expect(status).to be(200)
95
+ end
96
+
97
+ it 'should short circuit a request to 403 if it does not validate' do
98
+ expect_any_instance_of(Twilio::Security::RequestValidator).to(
99
+ receive(:validate).and_return(false)
100
+ )
101
+ request = Rack::MockRequest.env_for('/sms')
102
+ status, headers, body = @middleware.call(request)
103
+ expect(status).to be(403)
104
+ end
105
+ end
106
+ end