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/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/config/application.yml.example b/config/application.yml.example index f953cb4601..45fface302 100644 --- a/config/application.yml.example +++ b/config/application.yml.example @@ -5,8 +5,8 @@ SECRET_TOKEN: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx TIMEZONE: Melbourne -# Default country for dropdowns etc. -DEFAULT_COUNTRY: Australia +# 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 # Spree zone. 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 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? 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 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 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 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