From d4cf44a6dd09eac9b66bcae448abefd9d02ac2a8 Mon Sep 17 00:00:00 2001 From: Paul Mackay Date: Sun, 29 Mar 2015 17:06:05 +0100 Subject: [PATCH 1/8] Use COUNTRY_CODE instead of COUNTRY in application.yml. --- config/application.yml.example | 2 +- config/initializers/spree.rb | 2 +- ...50936_add_address_instances_to_existing_enterprise_groups.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/config/application.yml.example b/config/application.yml.example index f953cb4601..9eba8eb6ee 100644 --- a/config/application.yml.example +++ b/config/application.yml.example @@ -6,7 +6,7 @@ SECRET_TOKEN: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx TIMEZONE: Melbourne # Default country for dropdowns etc. -DEFAULT_COUNTRY: Australia +DEFAULT_COUNTRY_CODE: AU # Locale for translation. LOCALE: en # Spree zone. diff --git a/config/initializers/spree.rb b/config/initializers/spree.rb index a28763f289..a6a045222d 100644 --- a/config/initializers/spree.rb +++ b/config/initializers/spree.rb @@ -17,7 +17,7 @@ Spree.config do |config| config.checkout_zone = ENV["CHECKOUT_ZONE"] config.currency = ENV['CURRENCY'] if Spree::Country.table_exists? - country = Spree::Country.find_by_name(ENV["DEFAULT_COUNTRY"]) + country = Spree::Country.find_by_iso(ENV['DEFAULT_COUNTRY_CODE']) config.default_country_id = country.id if country.present? else config.default_country_id = 12 # Australia diff --git a/db/migrate/20150115050936_add_address_instances_to_existing_enterprise_groups.rb b/db/migrate/20150115050936_add_address_instances_to_existing_enterprise_groups.rb index 23fc038ce8..3b93ee6386 100644 --- a/db/migrate/20150115050936_add_address_instances_to_existing_enterprise_groups.rb +++ b/db/migrate/20150115050936_add_address_instances_to_existing_enterprise_groups.rb @@ -1,6 +1,6 @@ class AddAddressInstancesToExistingEnterpriseGroups < ActiveRecord::Migration def change - country = Spree::Country.find_by_name(ENV['DEFAULT_COUNTRY']) + country = Spree::Country.find_by_iso(ENV['DEFAULT_COUNTRY_CODE']) state = country.states.first EnterpriseGroup.all.each do |g| next if g.address.present? From 998288e21f92e714a947852fe4dae8b76c953293 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Tue, 21 Apr 2015 15:37:36 +1000 Subject: [PATCH 2/8] Keep failed jobs around for debugging. Limit max runtime to 15 mins (we're only sending emails at present). Notify bugsnag of errors in jobs. --- config/initializers/delayed_job.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/config/initializers/delayed_job.rb b/config/initializers/delayed_job.rb index b95d718c6d..8fc9aa8ec7 100644 --- a/config/initializers/delayed_job.rb +++ b/config/initializers/delayed_job.rb @@ -1 +1,14 @@ Delayed::Worker.logger = Logger.new(Rails.root.join('log', 'delayed_job.log')) +Delayed::Worker.destroy_failed_jobs = false +Delayed::Worker.max_run_time = 15.minutes + +# Notify bugsnag when a job fails +# Code adapted from http://trevorturk.com/2011/01/25/notify-hoptoad-if-theres-an-exception-in-delayedjob/ +class Delayed::Worker + alias_method :original_handle_failed_job, :handle_failed_job + + def handle_failed_job(job, error) + Bugsnag.notify(error) + original_handle_failed_job(job, error) + end +end From 9d225142c80dd5cf0efeeb392d00c3a0253332a0 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Tue, 21 Apr 2015 15:44:01 +1000 Subject: [PATCH 3/8] Send password reset instructions asynchronously --- app/models/spree/user_decorator.rb | 2 ++ spec/features/consumer/authentication_spec.rb | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb index b700fae3f0..8298627547 100644 --- a/app/models/spree/user_decorator.rb +++ b/app/models/spree/user_decorator.rb @@ -1,4 +1,6 @@ Spree.user_class.class_eval do + handle_asynchronously :send_reset_password_instructions + has_many :enterprise_roles, :dependent => :destroy has_many :enterprises, through: :enterprise_roles has_many :owned_enterprises, class_name: 'Enterprise', foreign_key: :owner_id, inverse_of: :owner diff --git a/spec/features/consumer/authentication_spec.rb b/spec/features/consumer/authentication_spec.rb index e3fa340909..d0302bcd6f 100644 --- a/spec/features/consumer/authentication_spec.rb +++ b/spec/features/consumer/authentication_spec.rb @@ -81,9 +81,11 @@ feature "Authentication", js: true do scenario "resetting password" do fill_in "Your email", with: user.email - click_reset_password_button - page.should have_reset_password - ActionMailer::Base.deliveries.last.subject.should =~ /Password Reset/ + expect do + click_reset_password_button + page.should have_reset_password + end.to enqueue_job Delayed::PerformableMethod + Delayed::Job.last.payload_object.method_name.should == :send_reset_password_instructions_without_delay end end end From 700cb73b8fa4ceb4653a655d8de1a1cba45857aa Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Tue, 21 Apr 2015 16:19:02 +1000 Subject: [PATCH 4/8] Send enterprise confirmation emails asynchronously --- app/models/enterprise.rb | 2 ++ spec/models/enterprise_spec.rb | 16 ++++++++-------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 9b81405489..1aa76b88a8 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -8,6 +8,8 @@ class Enterprise < ActiveRecord::Base preference :shopfront_taxon_order, :string, default: "" devise :confirmable, reconfirmable: true, confirmation_keys: [ :id, :email ] + handle_asynchronously :send_confirmation_instructions + handle_asynchronously :send_on_create_confirmation_instructions self.inheritance_column = nil diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index 97a4b988d8..69ba68569f 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -10,10 +10,10 @@ describe Enterprise do context "when the email address has not already been confirmed" do it "sends a confirmation email" do - mail_message = double "Mail::Message" - expect(EnterpriseMailer).to receive(:confirmation_instructions).and_return mail_message - mail_message.should_receive :deliver - create(:enterprise, owner: user, email: "unknown@email.com", confirmed_at: nil ) + expect do + create(:enterprise, owner: user, email: "unknown@email.com", confirmed_at: nil ) + end.to enqueue_job Delayed::PerformableMethod + Delayed::Job.last.payload_object.method_name.should == :send_on_create_confirmation_instructions_without_delay end it "does not send a welcome email" do @@ -41,10 +41,10 @@ describe Enterprise do let!(:enterprise) { create(:enterprise, owner: user) } it "when the email address has not already been confirmed" do - mail_message = double "Mail::Message" - expect(EnterpriseMailer).to receive(:confirmation_instructions).and_return mail_message - mail_message.should_receive :deliver - enterprise.update_attributes(email: "unknown@email.com") + expect do + enterprise.update_attributes(email: "unknown@email.com") + end.to enqueue_job Delayed::PerformableMethod + Delayed::Job.last.payload_object.method_name.should == :send_confirmation_instructions_without_delay end it "when the email address has already been confirmed" do From b2717ffca0fd07300414fc9c03f47122e6d4b385 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Tue, 21 Apr 2015 16:40:45 +1000 Subject: [PATCH 5/8] Use db:test:load instead of db:test:prepare in CI to avoid conflicts between branches --- script/run_tests.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/script/run_tests.sh b/script/run_tests.sh index 461ca4ecd8..be33fb1bfe 100755 --- a/script/run_tests.sh +++ b/script/run_tests.sh @@ -11,8 +11,8 @@ fi echo "--- Bundling" bundle install -echo "--- Preparing test database" -bundle exec rake db:test:prepare +echo "--- Loading test database" +bundle exec rake db:test:load echo "--- Running tests" bundle exec rspec spec --format progress From 7b8938b5f659bbcc2c3bcc2fda27176609a3abca Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 22 Apr 2015 07:32:22 +1000 Subject: [PATCH 6/8] Run password reset job for spec that requires it --- spec/controllers/user_passwords_controller_spec.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spec/controllers/user_passwords_controller_spec.rb b/spec/controllers/user_passwords_controller_spec.rb index 686d46656e..59fadf8e99 100644 --- a/spec/controllers/user_passwords_controller_spec.rb +++ b/spec/controllers/user_passwords_controller_spec.rb @@ -32,7 +32,10 @@ describe UserPasswordsController do end it "renders Darkswarm" do + clear_jobs user.send_reset_password_instructions + flush_jobs # Send the reset password instructions + user.reload spree_get :edit, reset_password_token: user.reset_password_token response.should render_template "user_passwords/edit" end From 3b61d7a1e0cad0019c4f954e39c1196466ae7abd Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 29 Apr 2015 14:12:08 +1000 Subject: [PATCH 7/8] Add ref for country ISO codoes --- config/application.yml.example | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/application.yml.example b/config/application.yml.example index 9eba8eb6ee..45fface302 100644 --- a/config/application.yml.example +++ b/config/application.yml.example @@ -5,7 +5,7 @@ SECRET_TOKEN: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx TIMEZONE: Melbourne -# Default country for dropdowns etc. +# Default country for dropdowns etc. See for codes: http://en.wikipedia.org/wiki/ISO_3166-1 DEFAULT_COUNTRY_CODE: AU # Locale for translation. LOCALE: en From f4df227ef02d2f9c6712926957fd614a0f6f1d92 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 30 Apr 2015 13:54:27 +1000 Subject: [PATCH 8/8] Buildkite should now support Fuubar --- script/ci/run_tests.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/ci/run_tests.sh b/script/ci/run_tests.sh index 189875f1e1..5539935a8b 100755 --- a/script/ci/run_tests.sh +++ b/script/ci/run_tests.sh @@ -16,4 +16,4 @@ echo "--- Loading test database" bundle exec rake db:test:load echo "--- Running tests" -bundle exec rspec spec --format progress +bundle exec rspec spec