Skip to content

Commit

Permalink
Update refactor to test emails
Browse files Browse the repository at this point in the history
Updates the AnnualLeaveRequestController tests
to check emails are sent to the correct person
with the correct template used.
  • Loading branch information
Jonathan Young committed Sep 13, 2023
1 parent 549d25b commit ccc7184
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 44 deletions.
4 changes: 2 additions & 2 deletions app/controllers/annual_leave_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
65 changes: 23 additions & 42 deletions spec/controllers/annual_leave_requests_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,29 @@
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: "[email protected]") }
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,
date_to: valid_request.date_to,
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

Expand All @@ -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

Expand Down Expand Up @@ -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: {
Expand All @@ -96,31 +79,29 @@
},
}

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: {
status: "denied",
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

0 comments on commit ccc7184

Please sign in to comment.