From 7512f5fafc35efa2bccb6644ee7f5b40b90d8e84 Mon Sep 17 00:00:00 2001 From: Jack S Date: Mon, 19 Jun 2023 15:37:10 +0100 Subject: [PATCH] wip --- app/models/user.rb | 6 ++++-- spec/models/user_spec.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index eda023519..792079cc9 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_save :send_data_protection_confirmation_reminder, if: :is_dpo_changed? + after_update :send_data_protection_confirmation_reminder, if: :is_dpo_changed? validates :organisation_id, presence: true @@ -186,8 +186,10 @@ protected private def send_data_protection_confirmation_reminder + # binding.pry + return unless is_dpo_changed? return unless is_dpo? - DataProtectionConfirmationMailer.send_confirmation_email(user) + DataProtectionConfirmationMailer.send_confirmation_email(user).deliver_later end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index b5580989a..9707067f8 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -369,4 +369,30 @@ RSpec.describe User, type: :model do end end end + + describe "#send_data_protection_confirmation_reminder" do + context "when updating to dpo" do + let!(:user) { create(:user, is_dpo: false) } + + it "sends the email" do + expect { user.update!(is_dpo: true) }.to enqueue_job(DataProtectionConfirmationMailer) + end + end + + context "when updating to non dpo" 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) + end + end + + context "when updating something else" do + let!(:user) { create(:user) } + + it "does not send the email" do + expect { user.update!(name: "foobar") }.not_to enqueue_job(DataProtectionConfirmationMailer) + end + end + end end