Skip to content

Commit beb390b

Browse files
Cover mailer in unit tests
System tests are for asserting things that the user sees when interacting with the app. These mailer assertions are better tested somewhere else such as the requests specs.
1 parent 1f7d793 commit beb390b

File tree

2 files changed

+113
-49
lines changed

2 files changed

+113
-49
lines changed

spec/requests/volunteers_spec.rb

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,23 @@
358358
end
359359

360360
describe "PATCH /resend_invitation" do
361-
before { sign_in admin }
361+
it "resends an invitation email as an admin" do
362+
sign_in admin
363+
364+
expect(volunteer.invitation_created_at.present?).to eq(false)
365+
366+
get resend_invitation_volunteer_path(volunteer)
367+
volunteer.reload
368+
369+
expect(volunteer.invitation_created_at.present?).to eq(true)
370+
expect(Devise.mailer.deliveries.count).to eq(1)
371+
expect(Devise.mailer.deliveries.first.subject).to eq(I18n.t("devise.mailer.invitation_instructions.subject"))
372+
expect(response).to redirect_to(edit_volunteer_path(volunteer))
373+
end
374+
375+
it "resends an invitation email as a supervisor" do
376+
sign_in supervisor
362377

363-
it "resends an invitation email" do
364378
expect(volunteer.invitation_created_at.present?).to eq(false)
365379

366380
get resend_invitation_volunteer_path(volunteer)
@@ -373,6 +387,98 @@
373387
end
374388
end
375389

390+
describe "PATCH /reminder" do
391+
describe "as admin" do
392+
it "emails the volunteer" do
393+
organization = create(:casa_org)
394+
admin = create(:casa_admin, casa_org_id: organization.id)
395+
supervisor = build(:supervisor, casa_org: organization)
396+
volunteer = create(:volunteer, supervisor: supervisor, casa_org_id: organization.id)
397+
398+
sign_in admin
399+
400+
patch reminder_volunteer_path(volunteer)
401+
402+
email = ActionMailer::Base.deliveries.last
403+
expect(email).not_to be_nil
404+
expect(email.to).to eq [volunteer.email]
405+
expect(email.subject).to eq("Reminder to input case contacts")
406+
end
407+
408+
it "cc's their supervisor and admin when the 'with_cc` param is present" do
409+
admin = create(:casa_admin, casa_org_id: organization.id)
410+
supervisor = build(:supervisor, casa_org: organization)
411+
volunteer = create(:volunteer, supervisor: supervisor, casa_org_id: organization.id)
412+
413+
sign_in admin
414+
415+
patch reminder_volunteer_path(volunteer), params: {
416+
with_cc: true
417+
}
418+
419+
email = ActionMailer::Base.deliveries.last
420+
expect(email).not_to be_nil
421+
expect(email.to).to eq [volunteer.email]
422+
expect(email.subject).to eq("Reminder to input case contacts")
423+
expect(email.cc).to include(volunteer.supervisor.email)
424+
expect(email.cc).to include(admin.email)
425+
end
426+
end
427+
428+
describe "as supervisor" do
429+
it "emails the volunteer" do
430+
organization = create(:casa_org)
431+
supervisor = build(:supervisor, casa_org: organization)
432+
volunteer = create(:volunteer, supervisor: supervisor, casa_org_id: organization.id)
433+
434+
sign_in supervisor
435+
436+
patch reminder_volunteer_path(volunteer)
437+
438+
email = ActionMailer::Base.deliveries.last
439+
expect(email).not_to be_nil
440+
expect(email.to).to eq [volunteer.email]
441+
expect(email.subject).to eq("Reminder to input case contacts")
442+
end
443+
444+
it "cc's their supervisor when the 'with_cc` param is present" do
445+
organization = create(:casa_org)
446+
supervisor = build(:supervisor, casa_org: organization)
447+
volunteer = create(:volunteer, supervisor: supervisor, casa_org_id: organization.id)
448+
449+
sign_in supervisor
450+
451+
patch reminder_volunteer_path(volunteer), params: {
452+
with_cc: true
453+
}
454+
455+
email = ActionMailer::Base.deliveries.last
456+
expect(email).not_to be_nil
457+
expect(email.to).to eq [volunteer.email]
458+
expect(email.subject).to eq("Reminder to input case contacts")
459+
expect(email.cc).to eq([supervisor.email])
460+
end
461+
end
462+
463+
it "emails the volunteer without a supervisor" do
464+
organization = create(:casa_org)
465+
volunteer_without_supervisor = create(:volunteer)
466+
supervisor = build(:supervisor, casa_org: organization)
467+
468+
sign_in supervisor
469+
470+
patch reminder_volunteer_path(volunteer_without_supervisor), params: {
471+
with_cc: true
472+
}
473+
474+
email = ActionMailer::Base.deliveries.last
475+
expect(email).not_to be_nil
476+
expect(email.to).to eq [volunteer_without_supervisor.email]
477+
expect(email.subject).to eq("Reminder to input case contacts")
478+
expect(email.cc).to be_empty
479+
end
480+
end
481+
376482
describe "POST /send_reactivation_alert" do
377483
before do
378484
sign_in admin

spec/system/volunteers/edit_spec.rb

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115

116116
describe "updating a volunteer's email" do
117117
context "with a valid email" do
118-
it "sends volunteer a confirmation email and does not change the displayed email" do
118+
it "displays a success for sending an email confirmation and does not change the displayed email" do
119119
volunteer = create(:volunteer, supervisor: supervisor, casa_org: organization)
120120
old_email = volunteer.email
121121

@@ -124,16 +124,10 @@
124124

125125
fill_in "Email", with: "newemail@example.com"
126126
click_on "Submit"
127-
volunteer.reload
128-
129-
expect(ActionMailer::Base.deliveries.count).to eq(1)
130-
expect(ActionMailer::Base.deliveries.first).to be_a(Mail::Message)
131-
expect(ActionMailer::Base.deliveries.first.body.encoded)
132-
.to match("Click here to confirm your email")
133127

134128
expect(page).to have_text "Volunteer was successfully updated. Confirmation Email Sent."
135129
expect(page).to have_field("Email", with: old_email)
136-
expect(volunteer.unconfirmed_email).to eq("newemail@example.com")
130+
expect(volunteer.reload.unconfirmed_email).to eq("newemail@example.com")
137131
end
138132

139133
it "succesfully displays the new email once the user confirms" do
@@ -373,10 +367,6 @@
373367
click_on "Resend Invitation"
374368

375369
expect(page).to have_content("Invitation sent")
376-
377-
deliveries = ActionMailer::Base.deliveries
378-
expect(deliveries.count).to eq(1)
379-
expect(deliveries.last.subject).to have_text "CASA Console invitation instructions"
380370
end
381371
end
382372

@@ -389,10 +379,6 @@
389379
click_on "Resend Invitation"
390380

391381
expect(page).to have_content("Invitation sent")
392-
393-
deliveries = ActionMailer::Base.deliveries
394-
expect(deliveries.count).to eq(1)
395-
expect(deliveries.last.subject).to have_text "CASA Console invitation instructions"
396382
end
397383

398384
describe "Send Reactivation (SMS)" do
@@ -436,21 +422,7 @@
436422
uncheck "with_cc"
437423
click_on "Send Reminder"
438424

439-
expect(ActionMailer::Base.deliveries.count).to eq(1)
440-
expect(ActionMailer::Base.deliveries.first.cc).to be_empty
441-
end
442-
443-
it "emails volunteer and cc's the supervisor" do
444-
volunteer = create(:volunteer, supervisor: supervisor, casa_org_id: organization.id)
445-
446-
sign_in supervisor
447-
visit edit_volunteer_path(volunteer)
448-
449-
check "with_cc"
450-
click_on "Send Reminder"
451-
452-
expect(ActionMailer::Base.deliveries.count).to eq(1)
453-
expect(ActionMailer::Base.deliveries.first.cc).to include(volunteer.supervisor.email)
425+
expect(page).to have_content("Reminder sent to volunteer")
454426
end
455427

456428
it "emails the volunteer without a supervisor" do
@@ -464,8 +436,7 @@
464436
check "with_cc"
465437
click_on "Send Reminder"
466438

467-
expect(ActionMailer::Base.deliveries.count).to eq(1)
468-
expect(ActionMailer::Base.deliveries.first.cc).to be_empty
439+
expect(page).to have_content("Reminder sent to volunteer")
469440
end
470441
end
471442

@@ -481,20 +452,7 @@
481452

482453
click_on "Send Reminder"
483454

484-
expect(ActionMailer::Base.deliveries.count).to eq(1)
485-
end
486-
487-
it "emails the volunteer and cc's their supervisor and admin" do
488-
volunteer = create(:volunteer, supervisor: supervisor, casa_org_id: organization.id)
489-
490-
sign_in admin
491-
visit edit_volunteer_path(volunteer)
492-
check "with_cc"
493-
click_on "Send Reminder"
494-
495-
expect(ActionMailer::Base.deliveries.count).to eq(1)
496-
expect(ActionMailer::Base.deliveries.first.cc).to include(volunteer.supervisor.email)
497-
expect(ActionMailer::Base.deliveries.first.cc).to include(admin.email)
455+
expect(page).to have_content("Reminder sent to volunteer")
498456
end
499457
end
500458

0 commit comments

Comments
 (0)