Skip to content

Commit ebc8222

Browse files
author
David Zuckerman
committed
Added missing tests for ActionMailer
1 parent eab0984 commit ebc8222

5 files changed

Lines changed: 324 additions & 43 deletions

spec/jobs/bibliographic_job_spec.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,60 @@
3535

3636
end
3737

38+
describe 'email notifications' do
39+
let(:mailer_double) { instance_double(ActionMailer::MessageDelivery, deliver_now: true) }
40+
41+
context 'when job succeeds' do
42+
it 'sends completion email with attachments' do
43+
fake_attachments = {
44+
"fake_completed.csv" => { mime_type: 'text/csv', content: 'csvdata' }
45+
}
46+
47+
allow_any_instance_of(BibliographicJob)
48+
.to receive(:generate_attatchments)
49+
.and_return(fake_attachments)
50+
51+
expect(RequestMailer)
52+
.to receive(:bibliographic_email)
53+
.with(
54+
email,
55+
fake_attachments,
56+
'Host Bibliographic Upload - Completed',
57+
'When there is an attached log file, please review unusual MMS ID information.'
58+
)
59+
.and_return(mailer_double)
60+
61+
BibliographicJob.perform_now(host_bib_task)
62+
63+
expect(host_bib_task.reload.status).to eq('succeeded')
64+
end
65+
end
66+
67+
context 'when job fails' do
68+
it 'sends failure email' do
69+
host_bib = host_bib_task.host_bibs.create(mms_id: mms_id1, marc_status: 'pending')
70+
71+
allow(Bibliographic::HostBib)
72+
.to receive(:create_linked_bibs)
73+
.with(host_bib)
74+
.and_raise(StandardError.new('Error'))
75+
76+
expect(RequestMailer)
77+
.to receive(:bibliographic_email)
78+
.with(
79+
email,
80+
[],
81+
'Host Bibliographic Upload - Failed',
82+
'Host Bibliographic upload failed, please reach out to our support team.'
83+
)
84+
.and_return(mailer_double)
85+
86+
expect {
87+
BibliographicJob.perform_now(host_bib_task)
88+
}.to raise_error(StandardError)
89+
90+
expect(host_bib_task.reload.status).to eq('failed')
91+
end
92+
end
93+
end
3894
end
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe PatronNoteJobBase, type: :job do
4+
let(:patron_id) { '12345' }
5+
let(:mailer_prefix) { 'test_prefix' }
6+
let(:note_txt) { 'Test note text' }
7+
8+
let(:job) do
9+
described_class.new(mailer_prefix:, note_txt:)
10+
end
11+
12+
let(:patron) do
13+
instance_double(
14+
Alma::User,
15+
email: 'mrperson@test.com',
16+
id: patron_id,
17+
name: 'Bud Powell'
18+
)
19+
end
20+
21+
let(:mail_double) do
22+
instance_double(ActionMailer::MessageDelivery, deliver_now: true)
23+
end
24+
25+
before do
26+
allow(Alma::User).to receive(:find_if_active).with(patron_id).and_return(patron)
27+
28+
allow(patron).to receive(:delete_note)
29+
allow(patron).to receive(:add_note)
30+
allow(patron).to receive(:save)
31+
end
32+
33+
describe '#perform' do
34+
it 'finds patron and adds note' do
35+
expect(patron).to receive(:delete_note).with(note_txt)
36+
expect(patron).to receive(:add_note).with(job.note)
37+
expect(patron).to receive(:save)
38+
39+
allow(RequestMailer)
40+
.to receive(:send)
41+
.and_return(mail_double)
42+
43+
job.perform(patron_id)
44+
end
45+
46+
it 'sends confirmation email' do
47+
expect(RequestMailer)
48+
.to receive(:send)
49+
.with("#{mailer_prefix}_confirmation_email", patron.email)
50+
.and_return(mail_double)
51+
52+
job.perform(patron_id)
53+
end
54+
55+
it 'formats note with date and tag' do
56+
allow(RequestMailer).to receive(:send).and_return(mail_double)
57+
58+
travel_to Date.new(2025, 1, 1) do
59+
job.perform(patron_id)
60+
expect(job.note).to eq("20250101 #{note_txt} [litscript]")
61+
end
62+
end
63+
end
64+
65+
describe 'when patron lookup fails' do
66+
it 'logs and raises error' do
67+
allow(Alma::User).to receive(:find_if_active).and_raise(StandardError.new('fail'))
68+
69+
expect(job).to receive(:log_error).at_least(:once)
70+
71+
expect {
72+
job.perform(patron_id)
73+
}.to raise_error(StandardError)
74+
end
75+
end
76+
77+
describe 'when add_note fails' do
78+
before do
79+
allow(RequestMailer).to receive(:send).and_return(mail_double)
80+
allow(patron).to receive(:add_note).and_raise(StandardError.new('boom'))
81+
end
82+
83+
it 'sends failure email' do
84+
expect(RequestMailer)
85+
.to receive(:send)
86+
.with(
87+
"#{mailer_prefix}_failure_email",
88+
patron.id,
89+
patron.name,
90+
job.note
91+
)
92+
.and_return(mail_double)
93+
94+
expect {
95+
job.perform(patron_id)
96+
}.to raise_error(StandardError)
97+
end
98+
99+
it 'logs error' do
100+
expect(job).to receive(:log_error).at_least(:once)
101+
102+
expect {
103+
job.perform(patron_id)
104+
}.to raise_error(StandardError)
105+
end
106+
end
107+
108+
describe 'when confirmation email fails' do
109+
it 'logs and raises error' do
110+
allow(RequestMailer)
111+
.to receive(:send)
112+
.and_raise(StandardError.new('mail fail'))
113+
114+
expect(job).to receive(:log_error).at_least(:once)
115+
116+
expect {
117+
job.perform(patron_id)
118+
}.to raise_error(StandardError)
119+
end
120+
end
121+
122+
describe 'when failure email itself fails' do
123+
before do
124+
allow(patron).to receive(:add_note).and_raise(StandardError.new('note fail'))
125+
allow(RequestMailer).to receive(:send).and_raise(StandardError.new('mail fail'))
126+
end
127+
128+
it 'logs and raises error' do
129+
expect(job).to receive(:log_error).at_least(:once)
130+
131+
expect {
132+
job.perform(patron_id)
133+
}.to raise_error(StandardError)
134+
end
135+
end
136+
end

spec/request/proxy_borrower_forms_request_spec.rb

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
require 'forms_helper'
22

33
describe 'Proxy Borrower Forms', type: :request do
4+
include ActiveJob::TestHelper
5+
after { clear_enqueued_jobs }
6+
47
attr_reader :patron_id
58
attr_reader :patron
69
attr_reader :user
@@ -185,9 +188,46 @@
185188
date_term: '',
186189
renewal: ''
187190
} })
188-
191+
189192
expect(response).to have_http_status :unprocessable_entity
190193
expect(response.body).to match(/Please correct these and resubmit the form/)
191194
end
195+
196+
it 'submission enqueues confirmation + alert emails' do
197+
expect {
198+
post(forms_proxy_borrower_request_dsp_path, params: {
199+
proxy_borrower_requests: {
200+
student_name: 'Veronica Names',
201+
dsp_rep: 'DSP Rep',
202+
research_last: 'last',
203+
research_first: 'first',
204+
date_term: Date.tomorrow,
205+
renewal: 0
206+
}
207+
})
208+
}.to have_enqueued_job(ActionMailer::MailDeliveryJob).exactly(2).times
209+
210+
expect(response).to have_http_status(:created)
211+
end
212+
213+
it 'Faculty submission enqueues confirmation + alert emails' do
214+
allow_any_instance_of(User).to receive(:ucb_faculty?).and_return(true)
215+
216+
expect {
217+
post(forms_proxy_borrower_request_faculty_path, params: {
218+
proxy_borrower_requests: {
219+
faculty_name: 'Thelonius Monk',
220+
department: 'History',
221+
research_last: 'Last',
222+
research_first: 'First',
223+
date_term: Date.tomorrow,
224+
renewal: 0
225+
}
226+
})
227+
}.to have_enqueued_job(ActionMailer::MailDeliveryJob).exactly(2).times
228+
229+
expect(response).to have_http_status(:created)
230+
end
231+
192232
end
193233
end

0 commit comments

Comments
 (0)