From adb34b00338eb8588919193177c53a13f239dbf9 Mon Sep 17 00:00:00 2001 From: Jack S Date: Mon, 19 Jun 2023 16:14:55 +0100 Subject: [PATCH] Send confirmation email to user --- app/models/user.rb | 7 ++++--- spec/models/user_spec.rb | 21 ++++++++++++++++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 792079cc9..f6e90aca7 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -21,7 +21,7 @@ class User < ApplicationRecord validates :password, confirmation: { if: :password_required? } validates :password, length: { within: Devise.password_length, allow_blank: true } - after_update :send_data_protection_confirmation_reminder, if: :is_dpo_changed? + after_validation :send_data_protection_confirmation_reminder, if: :is_dpo_changed? validates :organisation_id, presence: true @@ -186,10 +186,11 @@ protected private def send_data_protection_confirmation_reminder - # binding.pry + return unless FeatureToggle.new_data_protection_confirmation? + return unless persisted? return unless is_dpo_changed? return unless is_dpo? - DataProtectionConfirmationMailer.send_confirmation_email(user).deliver_later + DataProtectionConfirmationMailer.send_confirmation_email(self).deliver_later end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 9707067f8..69c5f8a7e 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -375,7 +375,22 @@ RSpec.describe User, type: :model do let!(:user) { create(:user, is_dpo: false) } it "sends the email" do - expect { user.update!(is_dpo: true) }.to enqueue_job(DataProtectionConfirmationMailer) + expect { user.update!(is_dpo: true) }.to enqueue_job(ActionMailer::MailDeliveryJob).with( + "DataProtectionConfirmationMailer", + "send_confirmation_email", + "deliver_now", + args: [user], + ) + end + + context "when feature flag disabled" do + before do + allow(FeatureToggle).to receive(:new_data_protection_confirmation?).and_return(false) + end + + it "does not send the email" do + expect { user.update!(is_dpo: true) }.not_to enqueue_job(ActionMailer::MailDeliveryJob) + end end end @@ -383,7 +398,7 @@ RSpec.describe User, type: :model do let!(:user) { create(:user, is_dpo: true) } it "does not send the email" do - expect { user.update!(is_dpo: false) }.not_to enqueue_job(DataProtectionConfirmationMailer) + expect { user.update!(is_dpo: false) }.not_to enqueue_job(ActionMailer::MailDeliveryJob) end end @@ -391,7 +406,7 @@ RSpec.describe User, type: :model do let!(:user) { create(:user) } it "does not send the email" do - expect { user.update!(name: "foobar") }.not_to enqueue_job(DataProtectionConfirmationMailer) + expect { user.update!(name: "foobar") }.not_to enqueue_job(ActionMailer::MailDeliveryJob) end end end