Skip to content

Commit

Permalink
Merge pull request #973 from thecartercenter/13.25.1
Browse files Browse the repository at this point in the history
Upgrade Twilio gem to v6
  • Loading branch information
cooperka authored Jan 17, 2024
2 parents d9c7498 + 47a00eb commit f126966
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ gem "spreadsheet" # For XLSForm export
gem "term-ansicolor", "~> 1.3"
gem "terrapin", "~> 0.6.0"
gem "thor", "~> 1.0"
gem "twilio-ruby", "~> 4.2" # Does not use semver after v5, watch out!
gem "twilio-ruby", "~> 6.9.0" # Does not use semver after v5, watch out!

# JS/CSS
gem "bootstrap", "~> 4.3"
Expand Down
13 changes: 6 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ GEM
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (2.6.3)
jwt (1.5.6)
jwt (2.7.1)
launchy (2.5.2)
addressable (~> 2.8)
letter_opener (1.8.1)
Expand Down Expand Up @@ -342,7 +342,6 @@ GEM
minitest (5.20.0)
mocha (1.16.1)
msgpack (1.7.2)
multi_json (1.15.0)
multipart-post (2.3.0)
net-http-persistent (4.0.2)
connection_pool (~> 2.2)
Expand Down Expand Up @@ -574,10 +573,10 @@ GEM
timeout (0.4.0)
tins (1.32.1)
sync
twilio-ruby (4.13.0)
builder (>= 2.1.2)
jwt (~> 1.0)
multi_json (>= 1.3.0)
twilio-ruby (6.9.0)
faraday (>= 0.9, < 3.0)
jwt (>= 1.5, < 3.0)
nokogiri (>= 1.6, < 2.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
uglifier (4.2.0)
Expand Down Expand Up @@ -728,7 +727,7 @@ DEPENDENCIES
terrapin (~> 0.6.0)
thor (~> 1.0)
timecop (= 0.9.6)
twilio-ruby (~> 4.2)
twilio-ruby (~> 6.9.0)
uglifier (~> 4.2)
vcr (~> 6.0)
versionist (~> 2.0)
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
13.25
13.25.1
4 changes: 2 additions & 2 deletions app/models/sms/adapters/twilio_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def receive(request)

def validate(request)
params = request.request_parameters.merge(request.query_parameters)
validator = Twilio::Util::RequestValidator.new(config.twilio_auth_token)
validator = Twilio::Security::RequestValidator.new(config.twilio_auth_token)
return if Rails.env.test?
return if validator.validate(request.original_url, params, request.headers["X-Twilio-Signature"])
raise Sms::Error, "Could not validate incoming Twilio message from #{params[:From]}"
Expand All @@ -79,7 +79,7 @@ def send_message_for_each_recipient(message)
errors = []
(numbers = message.recipient_numbers).each_with_index do |number, index|
send_message(number, message.body)
rescue Twilio::REST::RequestError => e
rescue Twilio::REST::RestError => e
errors << e.to_s
# Check if creating the first 3 messages, or ALL the messages, all failed
if errors.size == numbers.size || errors.size == 3 && index == 2
Expand Down
26 changes: 18 additions & 8 deletions spec/models/sms/adapters/twilio_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@
twilio_auth_token: "12121212121212121212121212121212", incoming_sms_numbers: [])
end
let(:adapter) { Sms::Adapters::Factory.instance.create("Twilio", config: mission_config) }
let(:fake_response) { {} }
let(:fake_body) { {} }
let(:twilio_error_msg) { "[HTTP 123] Stub : A Twilio error\nStub\nStub\nStub\n\n" }

before do
allow(adapter).to receive(:client).and_return(client)

allow(fake_response).to receive(:status_code).and_return(123)
allow(fake_response).to receive(:body).and_return(fake_body)
allow(fake_body).to receive(:fetch).and_return("Stub")
end

it_behaves_like "all adapters that can deliver messages"
Expand Down Expand Up @@ -69,7 +76,7 @@
before do
allow(msg).to receive(:recipient_numbers) { numbers }
allow(messages).to receive(:create) do |params|
raise Twilio::REST::RequestError, "A Twilio error" if failing_numbers.include?(params[:to])
raise Twilio::REST::RestError.new("A Twilio error", fake_response) if failing_numbers.include?(params[:to])
end
end

Expand All @@ -78,8 +85,7 @@

it "has at least one success and raises Sms::Adapters::PartialSendError" do
expect(messages).to receive(:create).with(hash_including(to: "+34"))
expect { delivery }.to raise_error(Sms::Adapters::PartialSendError,
"A Twilio error\nA Twilio error\nA Twilio error")
expect { delivery }.to raise_error(Sms::Adapters::PartialSendError, repeated_error_msg(3))
end
end

Expand All @@ -88,8 +94,7 @@

it "raises Sms::Adapters::FatalSendError" do
expect(messages).not_to receive(:create).with(hash_including(to: "+78"))
expect { delivery }.to raise_error(Sms::Adapters::FatalSendError,
"A Twilio error\nA Twilio error\nA Twilio error")
expect { delivery }.to raise_error(Sms::Adapters::FatalSendError, repeated_error_msg(3))
end
end

Expand All @@ -98,7 +103,7 @@
let(:failing_numbers) { ["+12", "+34"] }

it "raises Sms::Adapters::FatalSendError" do
expect { delivery }.to raise_error(Sms::Adapters::FatalSendError, "A Twilio error\nA Twilio error")
expect { delivery }.to raise_error(Sms::Adapters::FatalSendError, repeated_error_msg(2))
end
end

Expand All @@ -107,7 +112,7 @@
let(:failing_numbers) { ["+12"] }

it "raises Sms::Adapters::FatalSendError" do
expect { delivery }.to raise_error(Sms::Adapters::FatalSendError, "A Twilio error")
expect { delivery }.to raise_error(Sms::Adapters::FatalSendError, repeated_error_msg(1))
end
end
end
Expand All @@ -119,7 +124,7 @@ def twilio_request(options = {})
params = (options[:params] || {}).with_indifferent_access

signature = options[:signature] || begin
validator = Twilio::Util::RequestValidator.new(mission_config.twilio_auth_token)
validator = Twilio::Security::RequestValidator.new(mission_config.twilio_auth_token)
validator.build_signature_for(url, params)
end

Expand All @@ -129,4 +134,9 @@ def twilio_request(options = {})
double(headers: headers, params: params, original_url: url,
request_parameters: params, query_parameters: {}.freeze)
end

# Return the error message N times joined with "\n".
def repeated_error_msg(times)
("#{twilio_error_msg}\n" * (times - 1)) + twilio_error_msg
end
end

0 comments on commit f126966

Please sign in to comment.