diff --git a/.gitignore b/.gitignore index e4d0dd1e..882798bd 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,5 @@ libpeerconnection.log # Ignore application configuration /config/application.yml + +/fixtures/vcr_cassettes \ No newline at end of file diff --git a/.rspec b/.rspec index 83e16f80..2f0d7151 100644 --- a/.rspec +++ b/.rspec @@ -1,2 +1,3 @@ --color --require spec_helper +--order random diff --git a/Gemfile b/Gemfile index cb0e4264..c90b62a2 100644 --- a/Gemfile +++ b/Gemfile @@ -56,11 +56,12 @@ group :test do gem 'simplecov' gem 'rspec-rails' - gem 'rspec-its' gem 'rspec-given' gem 'capybara' gem 'factory_girl_rails' gem 'database_cleaner' + gem 'vcr' + gem 'webmock' gem 'zonebie' gem 'timecop' end diff --git a/Gemfile.lock b/Gemfile.lock index 22bd26f4..113900a9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -97,6 +97,8 @@ GEM simplecov (~> 0.10.0) term-ansicolor (~> 1.3) thor (~> 0.19.1) + crack (0.4.2) + safe_yaml (~> 1.0.0) database_cleaner (1.5.1) debug_inspector (0.0.2) descendants_tracker (0.0.4) @@ -139,6 +141,7 @@ GEM haml (>= 4.0.6, < 5.0) html2haml (>= 1.0.1) railties (>= 4.0.1) + hashdiff (0.2.3) hitimes (1.2.3) html2haml (2.0.0) erubis (~> 2.7.0) @@ -252,9 +255,6 @@ GEM rspec-given (3.7.1) given_core (= 3.7.1) rspec (>= 2.14.0) - rspec-its (1.2.0) - rspec-core (>= 3.0.0) - rspec-expectations (>= 3.0.0) rspec-mocks (3.3.2) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.3.0) @@ -269,6 +269,7 @@ GEM rspec-support (3.3.0) ruby_parser (3.7.1) sexp_processor (~> 4.1) + safe_yaml (1.0.4) sass (3.4.19) sass-rails (5.0.4) railties (>= 4.0.0, < 5.0) @@ -337,6 +338,7 @@ GEM unf_ext unf_ext (0.0.7.1) uniform_notifier (1.9.0) + vcr (3.0.0) virtus (1.0.5) axiom-types (~> 0.1) coercible (~> 1.0) @@ -344,6 +346,10 @@ GEM equalizer (~> 0.0, >= 0.0.9) warden (1.2.3) rack (>= 1.0) + webmock (1.22.3) + addressable (>= 2.3.6) + crack (>= 0.3.2) + hashdiff xpath (2.0.0) nokogiri (~> 1.3) zonebie (0.5.1) @@ -381,7 +387,6 @@ DEPENDENCIES rails_12factor rollbar rspec-given - rspec-its rspec-rails sass-rails sidekiq @@ -394,7 +399,9 @@ DEPENDENCIES timecop twilio-ruby uglifier + vcr virtus + webmock zonebie BUNDLED WITH diff --git a/app/services/sms/receiver.rb b/app/services/sms/receiver.rb index 8a5da05a..da6ef5c0 100644 --- a/app/services/sms/receiver.rb +++ b/app/services/sms/receiver.rb @@ -12,7 +12,7 @@ def handle from:, body: twilio_account: twilio, user: user, number: from, - text: body, + text: body.strip, direction: :incoming response = begin diff --git a/spec.new/controllers/twilio_controller_spec.rb b/spec.new/controllers/twilio_controller_spec.rb index d81dccdc..b67cd5de 100644 --- a/spec.new/controllers/twilio_controller_spec.rb +++ b/spec.new/controllers/twilio_controller_spec.rb @@ -4,7 +4,7 @@ Given(:twilio) { TwilioAccount.first } Given(:phone) { FactoryGirl.create :phone } - context "with a valid sid" do + context "with a valid sid", :vcr do When(:result) { post :receive, AccountSid: twilio.sid, From: phone.number, To: twilio.number, Body: "help" } Then { result.status == 200 } @@ -12,7 +12,7 @@ And { SMS.outgoing.last.number == Phone.condense(phone.number) } end - context "with a new phone number" do + context "with a new phone number", :vcr do Given(:number) { "+1 (555) 555-0123" } When(:result) { post :receive, AccountSid: twilio.sid, From: number, To: twilio.number, Body: "help" } diff --git a/spec.new/features/sms_request_spec.rb b/spec.new/features/sms_request_spec.rb index e21f75da..c32fe4c5 100644 --- a/spec.new/features/sms_request_spec.rb +++ b/spec.new/features/sms_request_spec.rb @@ -1,7 +1,7 @@ require "rails_helper" describe "Ordering via sms" do - it "runs end-to-end" do + it "runs end-to-end", :vcr do phone = FactoryGirl.create :phone volunteer = phone.user country = volunteer.country diff --git a/spec.new/rails_helper.rb b/spec.new/rails_helper.rb index ee10a7c9..9f53d00b 100644 --- a/spec.new/rails_helper.rb +++ b/spec.new/rails_helper.rb @@ -27,6 +27,11 @@ # If you are not using ActiveRecord, you can remove this line. ActiveRecord::Migration.maintain_test_schema! +VCR.configure do |config| + config.cassette_library_dir = "fixtures/vcr_cassettes" + config.hook_into :webmock +end + RSpec.configure do |config| # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures # config.fixture_path = "#{::Rails.root}/spec/fixtures" @@ -59,4 +64,12 @@ # * for some reason, `only` seems to work here, but `except` is being ignored # DatabaseCleaner.clean_with :truncation, except: %w( countries supplies country_supplies twilio_account ) end + + # TODO: many of these tests don't really need to hit Twilio and should probably opt out + config.around :each, :vcr do |x| + name = x.full_description.gsub /\W+/, '-' + VCR.use_cassette name, re_record_interval: 1.week do + x.run + end + end end diff --git a/spec.new/services/sms_receiver_spec.rb b/spec.new/services/sms_receiver_spec.rb index c4dbfc97..82432558 100644 --- a/spec.new/services/sms_receiver_spec.rb +++ b/spec.new/services/sms_receiver_spec.rb @@ -5,14 +5,14 @@ Given(:phone) { FactoryGirl.create :phone } Given(:receiver) { SMS::Receiver.new sid: twilio.sid, to: twilio.number } - context "with a message" do + context "with a message", :vcr do When(:result) { receiver.handle from: phone.number, body: "got it" } Then { result == SMS.outgoing.last } And { result.text =~ /can't find any outstanding orders/i } end - context "with an empty message" do + context "with an empty message", :vcr do When(:result) { receiver.handle from: phone.number, body: "" } Then { result == SMS.outgoing.last }