From 327d388d0a8477e7f22e95eaee335574048c1fd4 Mon Sep 17 00:00:00 2001 From: Jonathan Young Date: Fri, 8 Sep 2023 15:20:13 +0100 Subject: [PATCH] Update refactor to test emails Updates the AnnualLeaveRequestController tests to check emails are sent to the correct person with the correct template used. --- .../annual_leave_requests_controller.rb | 4 +- .../annual_leave_requests_controller_spec.rb | 65 +++++++------------ 2 files changed, 25 insertions(+), 44 deletions(-) diff --git a/app/controllers/annual_leave_requests_controller.rb b/app/controllers/annual_leave_requests_controller.rb index 23f2fbb..84d4a03 100644 --- a/app/controllers/annual_leave_requests_controller.rb +++ b/app/controllers/annual_leave_requests_controller.rb @@ -15,7 +15,7 @@ def create @annual_leave_request = current_user.annual_leave_requests.build(annual_leave_request_params) if @annual_leave_request.save - notify_client.send_email(new_request_email_hash) + @email_response_notification = notify_client.send_email(new_request_email_hash) redirect_to annual_leave_request_confirmation_path else render "new" @@ -39,7 +39,7 @@ def update_status @annual_leave_request = line_reports_leave_requests.find(params[:annual_leave_request_id]) if @annual_leave_request.update(annual_leave_request_params) - notify_client.send_email(status_update.email_hash) + @email_response_notification = notify_client.send_email(status_update.email_hash) redirect_to status_update.confirmation_page_path else render status_update.action diff --git a/spec/controllers/annual_leave_requests_controller_spec.rb b/spec/controllers/annual_leave_requests_controller_spec.rb index 6691725..1889fbf 100644 --- a/spec/controllers/annual_leave_requests_controller_spec.rb +++ b/spec/controllers/annual_leave_requests_controller_spec.rb @@ -1,27 +1,17 @@ RSpec.describe AnnualLeaveRequestsController do let(:user) { create(:user, line_manager_id: line_manager.id) } + let(:user_full_name) { "#{user.given_name} #{user.family_name}" } let(:line_manager) { create(:user, email: "line_manager@digital.cabinet-office.gov.uk") } - let(:notify_fake_client) { instance_double(Notifications::Client, send_email: "FakeNotificationResponse") } + let(:notify_test_client) { Notifications::Client.new(ENV["NOTIFY_TEST_API_KEY"]) } describe "POST create" do setup do - allow(controller).to receive(:notify_client).and_return(notify_fake_client) + allow(controller).to receive(:notify_client).and_return(notify_test_client) sign_in user end it "emails line manager and redirects to confirmation page if annual leave request valid" do valid_request = build(:annual_leave_request, user_id: user.id) - new_request_email_hash = { - email_address: line_manager.email, - template_id: "1587d50b-c12e-4698-b10e-cf414de26f36", - personalisation: { - line_manager_name: "#{line_manager.given_name} #{line_manager.family_name}", - name: "#{user.given_name} #{user.family_name}", - date_from: valid_request.date_from.to_fs(:rfc822), - date_to: valid_request.date_to.to_fs(:rfc822), - days_required: valid_request.days_required, - }, - } post :create, params: { annual_leave_request: { date_from: valid_request.date_from, @@ -29,7 +19,11 @@ days_required: valid_request.days_required, } } - expect(notify_fake_client).to have_received(:send_email).with(new_request_email_hash) + email_response_notification = assigns(:email_response_notification) + email_response = notify_test_client.get_notification(email_response_notification.id) + + expect(email_response.email_address).to eq(line_manager.email) + expect(email_response.subject).to eq("GOV.UK Holiday Logger – #{user_full_name} – New Annual Leave Request") expect(response).to redirect_to(annual_leave_request_confirmation_path) end @@ -48,7 +42,7 @@ let(:leave_request) { create(:annual_leave_request, user_id: user.id) } setup do - allow(controller).to receive(:notify_client).and_return(notify_fake_client) + allow(controller).to receive(:notify_client).and_return(notify_test_client) sign_in line_manager end @@ -76,18 +70,7 @@ expect(response).to render_template(:deny) end - it "sends an email to the line report when status is updated to approved" do - approved_request_email_hash = { - email_address: user.email, - template_id: "34542d49-8b91-412c-9393-c186a04a7d1c", - personalisation: { - line_manager_name: "#{line_manager.given_name} #{line_manager.family_name}", - name: "#{user.given_name} #{user.family_name}", - date_from: leave_request.date_from.to_fs(:rfc822), - date_to: leave_request.date_to.to_fs(:rfc822), - }, - } - + it "sends an email to the line report and redirects to confirmatioon page when status is updated to 'approved'" do patch :update_status, params: { annual_leave_request_id: leave_request.id, annual_leave_request: { @@ -96,10 +79,15 @@ }, } - expect(notify_fake_client).to have_received(:send_email).with(approved_request_email_hash) + email_response_notification = assigns(:email_response_notification) + email_response = notify_test_client.get_notification(email_response_notification.id) + + expect(email_response.email_address).to eq(user.email) + expect(email_response.subject).to eq("GOV.UK Holiday Logger – Annual Leave Request Approved") + expect(response).to redirect_to(confirm_annual_leave_request_approval_path) end - it "sends an email to the line report when status is updated to 'denied'" do + it "sends an email to the line report and redirects to confirmatioon page when status is updated to 'denied'" do patch :update_status, params: { annual_leave_request_id: leave_request.id, annual_leave_request: { @@ -107,20 +95,13 @@ denial_reason: "some valid reason", }, } - leave_request.reload - denied_request_email_hash = { - email_address: user.email, - template_id: "ec9035df-9c98-4e0e-8826-47768c311745", - personalisation: { - line_manager_name: "#{line_manager.given_name} #{line_manager.family_name}", - name: "#{user.given_name} #{user.family_name}", - date_from: leave_request.date_from.to_fs(:rfc822), - date_to: leave_request.date_to.to_fs(:rfc822), - denial_reason: leave_request.denial_reason, - }, - } - expect(notify_fake_client).to have_received(:send_email).with(denied_request_email_hash) + email_response_notification = assigns(:email_response_notification) + email_response = notify_test_client.get_notification(email_response_notification.id) + + expect(email_response.email_address).to eq(user.email) + expect(email_response.subject).to eq("GOV.UK Holiday Logger – Annual Leave Request Denied") + expect(response).to redirect_to(confirm_annual_leave_request_denial_path) end end end