diff --git a/app/helpers/user_helper.rb b/app/helpers/user_helper.rb index 06fe2bc7d..bbcb0acae 100644 --- a/app/helpers/user_helper.rb +++ b/app/helpers/user_helper.rb @@ -56,4 +56,23 @@ module UserHelper user.errors.add(attribute, message) end end + + def display_pending_email_change_banner?(user) + user.unconfirmed_email.present? && user.email != user.unconfirmed_email + end + + def pending_email_change_title_text(current_user, user) + if current_user == user + "You have requested to change your email address to #{user.unconfirmed_email}." + else + "There has been a request to change this user’s email address to #{user.unconfirmed_email}." + end + end + + def pending_email_change_banner_text(current_user) + text = "A confirmation link has been sent to the new email address. The current email will continue to work until the change is confirmed." + text += " Deactivating this user will cancel the email change request." if current_user.support? || current_user.data_coordinator? + + text + end end diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 7f42107f8..a43b03f82 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -11,6 +11,15 @@ <% end %> <% end %> +<% if display_pending_email_change_banner?(@user) %> + <%= govuk_notification_banner(title_text: "Important") do %> +

+ <%= pending_email_change_title_text(current_user, @user) %> +

+ <%= pending_email_change_banner_text(current_user) %> + <% end %> +<% end %> +

diff --git a/spec/helpers/user_helper_spec.rb b/spec/helpers/user_helper_spec.rb index c88790aa0..eb0a672db 100644 --- a/spec/helpers/user_helper_spec.rb +++ b/spec/helpers/user_helper_spec.rb @@ -105,6 +105,32 @@ RSpec.describe UserHelper do end end + describe "display_pending_email_change_banner?" do + context "when the user doesn't have an unconfirmed email" do + let(:user) { FactoryBot.create(:user, :data_provider, unconfirmed_email: nil) } + + it "does not display pending email change banner" do + expect(display_pending_email_change_banner?(user)).to be false + end + end + + context "when the user has the same unconfirmed email as current email" do + let(:user) { FactoryBot.create(:user, :data_provider, unconfirmed_email: "updated_email@example.com", email: "updated_email@example.com") } + + it "does not display pending email change banner" do + expect(display_pending_email_change_banner?(user)).to be false + end + end + + context "when the user has a different unconfirmed email" do + let(:user) { FactoryBot.create(:user, :data_provider, unconfirmed_email: "updated_email@example.com", email: "old_email@example.com") } + + it "displays pending email change banner" do + expect(display_pending_email_change_banner?(user)).to be true + end + end + end + describe "organisation_change_confirmation_warning" do context "when user owns logs" do before do @@ -147,4 +173,39 @@ RSpec.describe UserHelper do end end end + + describe "pending_email_change_title_text" do + let(:user) { FactoryBot.create(:user, :data_provider, unconfirmed_email: "updated_email@example.com", email: "old_email@example.com") } + let(:current_user) { FactoryBot.create(:user, :support) } + + context "when viewing own profile" do + it "returns the correct text" do + expect(pending_email_change_title_text(user, user)).to eq("You have requested to change your email address to updated_email@example.com.") + end + end + + context "when viewing another user's profile" do + it "returns the correct text" do + expect(pending_email_change_title_text(current_user, user)).to eq("There has been a request to change this user’s email address to updated_email@example.com.") + end + end + end + + describe "pending_email_change_banner_text" do + context "with provider user" do + let(:user) { FactoryBot.create(:user, :data_provider) } + + it "returns the correct text" do + expect(pending_email_change_banner_text(user)).to eq("A confirmation link has been sent to the new email address. The current email will continue to work until the change is confirmed.") + end + end + + context "with support user" do + let(:user) { FactoryBot.create(:user, :support) } + + it "returns the correct text" do + expect(pending_email_change_banner_text(user)).to eq("A confirmation link has been sent to the new email address. The current email will continue to work until the change is confirmed. Deactivating this user will cancel the email change request.") + end + end + end end