diff --git a/app/mailers/devise_notify_mailer.rb b/app/mailers/devise_notify_mailer.rb index b91db566f..58b26e82d 100644 --- a/app/mailers/devise_notify_mailer.rb +++ b/app/mailers/devise_notify_mailer.rb @@ -57,7 +57,7 @@ class DeviseNotifyMailer < Devise::Mailer def send_email_changed_to_old_email(record) # Do not send if user changed own email - return if record.versions.last.actor == record + return unless record.versions.last.actor != record && record.versions.last.changeset.key?("unconfirmed_email") && FeatureToggle.new_email_journey? return true if intercept_send?(record.email) @@ -72,8 +72,6 @@ class DeviseNotifyMailer < Devise::Mailer end def email_changed?(record) - # binding.pry - # record.versions.last.changeset.has_key? "unconfirmed_email" record.confirmable_template == User::CONFIRMABLE_TEMPLATE_ID && (record.unconfirmed_email.present? && record.unconfirmed_email != record.email) end diff --git a/app/services/feature_toggle.rb b/app/services/feature_toggle.rb index d7df829ad..aed14d3e6 100644 --- a/app/services/feature_toggle.rb +++ b/app/services/feature_toggle.rb @@ -33,4 +33,8 @@ class FeatureToggle def self.deduplication_flow_enabled? !Rails.env.production? && !Rails.env.staging? end + + def self.new_email_journey? + !Rails.env.production? + end end diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb index 984090863..be2e27692 100644 --- a/spec/requests/users_controller_spec.rb +++ b/spec/requests/users_controller_spec.rb @@ -1679,13 +1679,28 @@ RSpec.describe UsersController, type: :request do expect(page).to have_content(other_user.reload.email.to_s) end - context "when the support user tries to update the user’s password" do + context "when the support user tries to update the user’s password", :aggregate_failures do let(:params) do { - id: user.id, user: { password: new_name, password_confirmation: new_name, name: "new name" } + id: user.id, user: { password: new_name, password_confirmation: new_name, name: "new name", email: new_email } } end + let(:personalisation) do + { + name: params[:user][:name], + email: new_email, + organisation: other_user.organisation.name, + link: include("/account/confirmation?confirmation_token="), + } + end + + before do + other_user.legacy_users.destroy_all + + allow(FeatureToggle).to receive(:new_email_journey?).and_return(true) + end + it "does not update the password" do expect { patch "/users/#{other_user.id}", headers:, params: } .not_to change(other_user, :encrypted_password) @@ -1695,6 +1710,40 @@ RSpec.describe UsersController, type: :request do expect { patch "/users/#{other_user.id}", headers:, params: } .to change { other_user.reload.name }.from("Danny Rojas").to("new name") end + + it "allows changing email" do + expect { patch "/users/#{other_user.id}", headers:, params: } + .to change { other_user.reload.unconfirmed_email }.from(nil).to(new_email) + end + + it "sends a confirmation email to both emails" do + expect(notify_client).to receive(:send_email).with(email_address: other_user.email, template_id: User::CONFIRMABLE_TEMPLATE_ID, personalisation:).once + expect(notify_client).to receive(:send_email).with(email_address: new_email, template_id: User::CONFIRMABLE_TEMPLATE_ID, personalisation:).once + + patch "/users/#{other_user.id}", headers:, params: + end + + context "with new user email flow enabled" do + before do + allow(FeatureToggle).to receive(:new_email_journey?).and_return(true) + end + + it "sends a new flow emails" do + expect(notify_client).to receive(:send_email).with(email_address: other_user.email, template_id: User::CONFIRMABLE_TEMPLATE_ID, personalisation:).once + expect(notify_client).to receive(:send_email).with(email_address: new_email, template_id: User::CONFIRMABLE_TEMPLATE_ID, personalisation:).once + + expect(notify_client).to receive(:send_email).with( + email_address: other_user.email, + template_id: User::FOR_OLD_EMAIL_CHANGED_BY_OTHER_USER_TEMPLATE_ID, + personalisation: { + new_email:, + old_email: other_user.email, + }, + ).once + + patch "/users/#{other_user.id}", headers:, params: + end + end end end end