Skip to content

Commit

Permalink
Fix the scope of variables/mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwr18 committed Dec 5, 2023
1 parent 3fa9341 commit 5a58fbd
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions spec/requests/whats_app/webhook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,21 +258,6 @@

describe 'given a known contributor' do
let!(:contributor) { create(:contributor, whats_app_phone_number: whats_app_phone_number) }
let!(:request) do
create(:request, title: 'I failed to send', text: 'Hey {{FIRST_NAME}}, because it was sent outside the allowed window')
end
let(:valid_account_sid) { 'VALID_ACCOUNT_SID' }
let(:valid_api_key_sid) { 'VALID_API_KEY_SID' }
let(:valid_api_key_secret) { 'VALID_API_KEY_SECRET' }
let(:mock_twilio_rest_client) { instance_double(Twilio::REST::Client) }
let(:messages_double) { double(Twilio::REST::Api::V2010::AccountContext::MessageInstance, body: body_text) }
let(:body_text) { 'no message with this text saved' }

before do
allow(Twilio::REST::Client).to receive(:new).and_return(mock_twilio_rest_client)
allow(mock_twilio_rest_client).to receive(:messages).with('someSid').and_return(messages_double)
allow(messages_double).to receive(:fetch).and_return(messages_double)
end

describe 'given a failed message delivery' do
it 'reports the error with the error message' do
Expand Down Expand Up @@ -301,11 +286,23 @@
end

context 'due to a freeform message not allowed error' do
let!(:request) do
create(:request, title: 'I failed to send', text: 'Hey {{FIRST_NAME}}, because it was sent outside the allowed window')
end
let(:valid_account_sid) { 'VALID_ACCOUNT_SID' }
let(:valid_api_key_sid) { 'VALID_API_KEY_SID' }
let(:valid_api_key_secret) { 'VALID_API_KEY_SECRET' }
let(:mock_twilio_rest_client) { instance_double(Twilio::REST::Client) }
let(:messages_double) { double(Twilio::REST::Api::V2010::AccountContext::MessageInstance, body: body_text) }
let(:body_text) { 'no message with this text saved' }
let(:freeform_message_not_allowed_error_message) do
'Twilio Error: Failed to send freeform message because you are outside the allowed window.'
end

before do
allow(Twilio::REST::Client).to receive(:new).and_return(mock_twilio_rest_client)
allow(mock_twilio_rest_client).to receive(:messages).with('someSid').and_return(messages_double)
allow(messages_double).to receive(:fetch).and_return(messages_double)
params['ErrorCode'] = '63016'
params['ErrorMessage'] = freeform_message_not_allowed_error_message
end
Expand Down

0 comments on commit 5a58fbd

Please sign in to comment.