From b3935315ceb6ad40de8905ff0b78ca81925aa080 Mon Sep 17 00:00:00 2001 From: Aaron Spencer Date: Tue, 13 Jun 2023 17:35:15 +0100 Subject: [PATCH] CLDC-2328: Email test refactor. --- spec/features/user_spec.rb | 66 ++++++++++++++++++++++---- spec/requests/users_controller_spec.rb | 2 +- 2 files changed, 57 insertions(+), 11 deletions(-) diff --git a/spec/features/user_spec.rb b/spec/features/user_spec.rb index ad24bdfb8..50d15dd57 100644 --- a/spec/features/user_spec.rb +++ b/spec/features/user_spec.rb @@ -453,9 +453,13 @@ RSpec.describe "User Features" do end end - context "when reinviting a user after initial confirmation email has been sent" do - let!(:other_user) { FactoryBot.create(:user, name: "new user", organisation: user.organisation, email: "new_user@example.com", initial_confirmation_sent: true) } + end + + context "when signed in as support" do + let!(:user) { FactoryBot.create(:user, :support) } + let!(:other_user) { FactoryBot.create(:user, name: "new user", organisation: user.organisation, email: "new_user@example.com", confirmation_token: "abc") } + context "when reinviting a user before initial confirmation email has been sent" do let(:personalisation) do { name: "new user", @@ -466,25 +470,66 @@ RSpec.describe "User Features" do end before do - other_user.update!(confirmation_token: "abc") - visit(lettings_logs_path) - fill_in("user[email]", with: user.email) - fill_in("user[password]", with: "pAssword1") - click_button("Sign in") - visit("/users/#{other_user.id}") + other_user.update!(initial_confirmation_sent: false) + allow(user).to receive(:need_two_factor_authentication?).and_return(false) + sign_in(user) + visit(user_path(user.id)) + end + + it "sends initial confirmable template email when the resend invite link is clicked" do + other_user.legacy_users.destroy_all + visit(user_path(other_user)) + expect(notify_client).to receive(:send_email).with(email_address: "new_user@example.com", template_id: User::CONFIRMABLE_TEMPLATE_ID, personalisation:).once + click_button("Resend invite link") + end + end + + context "when reinviting a user after initial confirmation email has been sent" do + let(:personalisation) do + { + name: "new user", + email: "new_user@example.com", + organisation: other_user.organisation.name, + link: include("/account/confirmation?confirmation_token=#{other_user.confirmation_token}"), + } + end + + before do + other_user.update!(initial_confirmation_sent: true) + allow(user).to receive(:need_two_factor_authentication?).and_return(false) + sign_in(user) + visit(user_path(user.id)) end it "sends and email when the resend invite link is clicked" do other_user.legacy_users.destroy_all visit(user_path(other_user)) expect(notify_client).to receive(:send_email).with(email_address: "new_user@example.com", template_id: User::RECONFIRMABLE_TEMPLATE_ID, personalisation:).once - click_link("Resend invite link") + click_button("Resend invite link") + end + end + + context "when reinviting a legacy user" do + let(:personalisation) do + { + name: "new user", + email: "new_user@example.com", + organisation: other_user.organisation.name, + link: include("/account/confirmation?confirmation_token=#{other_user.confirmation_token}"), + } + end + + before do + other_user.update!(initial_confirmation_sent: true) + allow(user).to receive(:need_two_factor_authentication?).and_return(false) + sign_in(user) + visit(user_path(user.id)) end it "sends beta onboarding email to be sent when user is legacy" do visit(user_path(other_user)) expect(notify_client).to receive(:send_email).with(email_address: "new_user@example.com", template_id: User::BETA_ONBOARDING_TEMPLATE_ID, personalisation:).once - click_link("Resend invite link") + click_button("Resend invite link") end end end @@ -685,5 +730,6 @@ RSpec.describe "User Features" do end end end + end end diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb index c20449bab..7b0c93826 100644 --- a/spec/requests/users_controller_spec.rb +++ b/spec/requests/users_controller_spec.rb @@ -145,7 +145,7 @@ RSpec.describe UsersController, type: :request do end it "does not allow resending invitation emails" do - expect(page).not_to have_link("Resend invite link", href: "/users/#{other_user.id}/resend-invite") + expect(page).not_to have_link("Resend invite link") end end end