Skip to content

Commit

Permalink
Update twilio-ruby from 5.7.2 to 5.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
monfresh committed Apr 21, 2018
1 parent 7cee896 commit 10524a3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ GEM
fakefs (0.13.3)
faker (1.8.7)
i18n (>= 0.7)
faraday (0.14.0)
faraday (0.15.0)
multipart-post (>= 1.2, < 3)
fasterer (0.4.1)
colorize (~> 0.7)
Expand Down Expand Up @@ -593,7 +593,7 @@ GEM
thread_safe (0.3.6)
tilt (2.0.8)
timecop (0.9.1)
twilio-ruby (5.7.2)
twilio-ruby (5.8.0)
faraday (~> 0.9)
jwt (>= 1.5, <= 2.5)
nokogiri (>= 1.6, < 2.0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,9 @@ def index
end

it 'flashes an sms error when twilio responds with an sms error' do
twilio_error = Twilio::REST::RestError.new('', TwilioService::SMS_ERROR_CODE, '400')
twilio_error = Twilio::REST::RestError.new(
'', FakeTwilioErrorResponse.new(TwilioService::SMS_ERROR_CODE)
)

allow(SmsOtpSenderJob).to receive(:perform_now).and_raise(twilio_error)
get :send_code, params: { otp_delivery_selection_form: { otp_delivery_preference: 'sms' } }
Expand All @@ -255,7 +257,9 @@ def index
end

it 'flashes an invalid error when twilio responds with an invalid error' do
twilio_error = Twilio::REST::RestError.new('', TwilioService::INVALID_ERROR_CODE, '400')
twilio_error = Twilio::REST::RestError.new(
'', FakeTwilioErrorResponse.new(TwilioService::INVALID_ERROR_CODE)
)

allow(SmsOtpSenderJob).to receive(:perform_now).and_raise(twilio_error)
get :send_code, params: { otp_delivery_selection_form: { otp_delivery_preference: 'sms' } }
Expand All @@ -265,7 +269,7 @@ def index

it 'flashes an error when twilio responds with an invalid calling area error' do
twilio_error = Twilio::REST::RestError.new(
'', TwilioService::INVALID_CALLING_AREA_ERROR_CODE, '400'
'', FakeTwilioErrorResponse.new(TwilioService::INVALID_CALLING_AREA_ERROR_CODE)
)

allow(VoiceOtpSenderJob).to receive(:perform_now).and_raise(twilio_error)
Expand All @@ -277,7 +281,7 @@ def index

it 'flashes an error when twilio responds with an invalid voice number' do
twilio_error = Twilio::REST::RestError.new(
'', TwilioService::INVALID_VOICE_NUMBER_ERROR_CODE, '400'
'', FakeTwilioErrorResponse.new(TwilioService::INVALID_VOICE_NUMBER_ERROR_CODE)
)

allow(VoiceOtpSenderJob).to receive(:perform_now).and_raise(twilio_error)
Expand All @@ -288,7 +292,9 @@ def index
end

it 'flashes a failed to send error when twilio responds with an unknown error' do
twilio_error = Twilio::REST::RestError.new('', '', '400')
twilio_error = Twilio::REST::RestError.new(
'', FakeTwilioErrorResponse.new
)

allow(SmsOtpSenderJob).to receive(:perform_now).and_raise(twilio_error)
get :send_code, params: { otp_delivery_selection_form: { otp_delivery_preference: 'sms' } }
Expand All @@ -298,7 +304,9 @@ def index

it 'records an analytics event when Twilio responds with an error' do
stub_analytics
twilio_error = Twilio::REST::RestError.new('error message', '', '400')
twilio_error = Twilio::REST::RestError.new(
'error message', FakeTwilioErrorResponse.new
)
allow(SmsOtpSenderJob).to receive(:perform_now).and_raise(twilio_error)
analytics_hash = {
success: true,
Expand All @@ -309,12 +317,13 @@ def index
country_code: '1',
area_code: '202',
}
twilio_error = "[HTTP 400] : error message\n\n"

expect(@analytics).to receive(:track_event).
with(Analytics::OTP_DELIVERY_SELECTION, analytics_hash)

expect(@analytics).to receive(:track_event).
with(Analytics::TWILIO_PHONE_VALIDATION_FAILED, error: 'error message', code: '')
with(Analytics::TWILIO_PHONE_VALIDATION_FAILED, error: twilio_error, code: '')

get :send_code, params: { otp_delivery_selection_form: { otp_delivery_preference: 'sms' } }
end
Expand Down
4 changes: 3 additions & 1 deletion spec/features/users/sign_up_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@
end

scenario 'renders an error when twilio api responds with an error' do
twilio_error = Twilio::REST::RestError.new('', TwilioService::SMS_ERROR_CODE, '400')
twilio_error = Twilio::REST::RestError.new(
'', FakeTwilioErrorResponse.new(TwilioService::SMS_ERROR_CODE)
)

allow(SmsOtpSenderJob).to receive(:perform_now).and_raise(twilio_error)
sign_up_and_set_password
Expand Down
17 changes: 13 additions & 4 deletions spec/services/twilio_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,17 @@
raw_message = 'Unable to create record: Account not authorized to call +123456789012.'
error_code = '21215'
status_code = 400
sanitized_message = 'Unable to create record: Account not authorized to call +12345#######.'
sanitized_message = "[HTTP #{status_code}] #{error_code} : Unable to create record: Account " \
"not authorized to call +12345#######.\n\n"

service = TwilioService.new

raw_error = Twilio::REST::RestError.new(
raw_message, FakeTwilioErrorResponse.new(error_code)
)

expect(service.send(:client).calls).to receive(:create).
and_raise(Twilio::REST::RestError.new(raw_message, error_code, status_code))
and_raise(raw_error)

expect { service.place_call(to: '+123456789012', url: 'https://twimlet.com') }.
to raise_error(Twilio::REST::RestError, sanitized_message)
Expand Down Expand Up @@ -116,12 +121,16 @@
raw_message = "The 'To' number +1 (888) 555-5555 is not a valid phone number"
error_code = '21211'
status_code = 400
sanitized_message = "The 'To' number +1 (888) 5##-#### is not a valid phone number"
sanitized_message = "[HTTP #{status_code}] #{error_code} : The 'To' " \
"number +1 (888) 5##-#### is not a valid phone number\n\n"

service = TwilioService.new
raw_error = Twilio::REST::RestError.new(
raw_message, FakeTwilioErrorResponse.new(error_code)
)

expect(service.send(:client).messages).to receive(:create).
and_raise(Twilio::REST::RestError.new(raw_message, error_code, status_code))
and_raise(raw_error)

expect { service.send_sms(to: '+1 (888) 555-5555', body: 'test') }.
to raise_error(Twilio::REST::RestError, sanitized_message)
Expand Down
15 changes: 15 additions & 0 deletions spec/support/fake_twilio_error_response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class FakeTwilioErrorResponse
attr_reader :code

def initialize(code = '')
@code = code
end

def status_code
400
end

def body
{ 'code' => code }
end
end

0 comments on commit 10524a3

Please sign in to comment.