diff --git a/app/models/user.rb b/app/models/user.rb index 5424a307a..7b921573a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -621,6 +621,15 @@ class User < ApplicationRecord "User:#{id}" end + protected + + # Send emails asynchronously + # see https://github.com/heartcombo/devise#activejob-integration + def send_devise_notification(notification, *args) + message = devise_mailer.public_send(notification, self, *args) + message.deliver_later + end + private def sync_relevant_profile_fields_to_user_settings_table(users_setting_record) diff --git a/spec/requests/admin/invitations_spec.rb b/spec/requests/admin/invitations_spec.rb index 15a52101e..6cd5c4a20 100644 --- a/spec/requests/admin/invitations_spec.rb +++ b/spec/requests/admin/invitations_spec.rb @@ -30,6 +30,15 @@ RSpec.describe "/admin/invitations", type: :request do expect(User.last.registered).to be false end + it "enqueues an invitation email to be sent", :aggregate_failures do + assert_enqueued_with(job: Devise.mailer.delivery_job) do + post admin_invitations_path, + params: { user: { email: "hey#{rand(1000)}@email.co", name: "Roger #{rand(1000)}" } } + end + + expect(enqueued_jobs.first[:args]).to match(array_including("invitation_instructions")) + end + it "does not create an invitation if a user with that email exists" do expect do post admin_invitations_path, diff --git a/spec/system/user_logs_in_with_password_spec.rb b/spec/system/user_logs_in_with_password_spec.rb index 7a594f152..39a36bac0 100644 --- a/spec/system/user_logs_in_with_password_spec.rb +++ b/spec/system/user_logs_in_with_password_spec.rb @@ -28,12 +28,14 @@ RSpec.describe "Authenticating with a password" do expect(page).to have_text("Invalid Email or password.") end - it "sends an email with the unlock link if the uset gets locked out" do + it "sends an email with the unlock link if the uset gets locked out", :aggregate_failures do allow(User).to receive(:maximum_attempts).and_return(1) - expect do + assert_enqueued_with(job: Devise.mailer.delivery_job) do submit_login_form(user.email, "wr0ng") - end.to change { Devise.mailer.deliveries.count }.by(1) + end + + expect(enqueued_jobs.first[:args]).to match(array_including("unlock_instructions")) end end