Skip to content

Commit 1fa4072

Browse files
author
David Zuckerman
committed
Calling deliver_later for forms instead of deliver_now
1 parent 17bbc97 commit 1fa4072

File tree

7 files changed

+113
-42
lines changed

7 files changed

+113
-42
lines changed

app/mailers/request_mailer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class RequestMailer < ApplicationMailer
88

99
# Sends the AffiliateBorrowRequestForm
1010
def affiliate_borrow_request_form_email(borrow_request)
11-
@borrow_request = borrow_request
11+
@borrow_request = Struct.new(borrow_request)
1212

1313
mail(to: borrow_request.department_head_email)
1414
end

app/models/affiliate_borrow_request_form.rb

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1+
# app/models/affiliate_borrow_request_form.rb
12
class AffiliateBorrowRequestForm < Form
2-
attr_accessor(
3-
:department_head_email,
4-
:department_head_name,
5-
:department_name,
6-
:employee_email,
7-
:employee_id,
8-
:employee_name,
9-
:employee_personal_email,
10-
:employee_phone,
11-
:employee_preferred_name,
12-
:employee_address
13-
)
3+
ATTRIBUTES = %i[
4+
department_head_email
5+
department_head_name
6+
department_name
7+
employee_email
8+
employee_id
9+
employee_name
10+
employee_personal_email
11+
employee_phone
12+
employee_preferred_name
13+
employee_address
14+
].freeze
15+
16+
attr_accessor(*ATTRIBUTES)
1417

1518
validates :department_name,
1619
presence: true
@@ -39,9 +42,14 @@ class AffiliateBorrowRequestForm < Form
3942
validates :employee_address,
4043
presence: true
4144

42-
private
45+
# Return a serializable hash for deliver_later
46+
def to_h
47+
ATTRIBUTES.index_with { |attr| public_send(attr) }
48+
end
49+
50+
def submit!
51+
raise ActiveModel::ValidationError, self unless valid?
4352

44-
def submit
45-
RequestMailer.affiliate_borrow_request_form_email(self).deliver_now
53+
RequestMailer.affiliate_borrow_request_form_email(to_h).deliver_later
4654
end
4755
end

app/models/doemoff_patron_email_form.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ class DoemoffPatronEmailForm < Form
1515
private
1616

1717
def submit
18-
RequestMailer.doemoff_patron_email(self).deliver_now
18+
RequestMailer.doemoff_patron_email(:patron_email, :patron_message, :sender, :recipient_email).deliver_later
1919
end
2020
end

app/models/efees_invoice.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def encode
4141

4242
def submit!
4343
# Send the email with the link to the user!
44-
RequestMailer.efee_invoice_email(self).deliver_now
44+
RequestMailer.efee_invoice_email(alma_id).deliver_later
4545
end
4646

4747
private

spec/models/affiliate_borrow_request_form_spec.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
require 'rails_helper'
22

3+
ActiveJob::Base.queue_adapter = :test
4+
35
describe AffiliateBorrowRequestForm do
6+
include ActiveJob::TestHelper
7+
include ActionMailer::TestHelper
8+
49
it 'validates the form' do
510
tests = [
611
{
@@ -90,10 +95,8 @@
9095
employee_address: '123 North St, Berkeley, CA 94707'
9196
)
9297

93-
expect { form.submit! }.to(change { ActionMailer::Base.deliveries.count }.by(1))
94-
last_email = ActionMailer::Base.deliveries.last
95-
expect(last_email.subject).to eq('UC Berkeley Borrowing Card Requested')
96-
expect(last_email.to).to include('jeff@wilco.com')
98+
expect { form.submit! }.to have_enqueued_job(ActionMailer::MailDeliveryJob)
99+
97100
end
98101

99102
describe :model_name do

spec/models/doemoff_patron_email_form_spec.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
attr_reader :form
66

7-
# verify params are valid
7+
# verify params are valid and it sends an email
88
describe 'is valid' do
99
describe 'form' do
1010
before do
@@ -20,8 +20,16 @@
2020
end
2121

2222
it 'sends an email' do
23-
expect { @form.submit! }.to change { ActionMailer::Base.deliveries.count }.by(1)
23+
expect do
24+
@form.submit!
25+
end.to have_enqueued_job(ActionMailer::MailDeliveryJob).with(
26+
'RequestMailer',
27+
'doemoff_patron_email',
28+
'deliver_now',
29+
any_args
30+
)
2431
end
32+
2533
end
2634
end
2735

spec/models/efees_invoice_spec.rb

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,84 @@
11
require 'rails_helper'
22

33
describe EfeesInvoice do
4+
include ActiveJob::TestHelper
5+
46
let(:alma_api_key) { 'fake-api-key' }
5-
let(:request_headers) { { 'Accept' => 'application/json', 'Authorization' => "apikey #{alma_api_key}" } }
7+
let(:alma_id) { '10335026' }
8+
let(:request_headers) do
9+
{
10+
'Accept' => 'application/json',
11+
'Authorization' => "apikey #{alma_api_key}"
12+
}
13+
end
614

7-
before do
8-
alma_id = '10335026'
15+
context 'JWT generation' do
16+
before do
17+
allow(Rails.application.config)
18+
.to receive(:alma_api_key)
19+
.and_return(alma_api_key)
920

10-
allow(Rails.application.config).to receive(:alma_api_key).and_return(alma_api_key)
21+
stub_request(
22+
:get,
23+
"https://api-na.hosted.exlibrisgroup.com/almaws/v1/users/#{alma_id}?expand=fees&view=full"
24+
)
25+
.with(headers: request_headers)
26+
.to_return(
27+
status: 200,
28+
body: File.new('spec/data/fees/efee-lookup-data.json')
29+
)
1130

12-
stub_request(:get, "https://api-na.hosted.exlibrisgroup.com/almaws/v1/users/#{alma_id}?expand=fees&view=full")
13-
.with(headers: request_headers)
14-
.to_return(status: 200, body: File.new('spec/data/fees/efee-lookup-data.json'))
31+
stub_request(
32+
:get,
33+
"https://api-na.hosted.exlibrisgroup.com/almaws/v1/users/#{alma_id}/fees"
34+
)
35+
.with(headers: request_headers)
36+
.to_return(
37+
status: 200,
38+
body: File.new('spec/data/fees/efee-lookup-fees.json')
39+
)
40+
end
1541

16-
stub_request(:get, "https://api-na.hosted.exlibrisgroup.com/almaws/v1/users/#{alma_id}/fees")
17-
.with(headers: request_headers)
18-
.to_return(status: 200, body: File.new('spec/data/fees/efee-lookup-fees.json'))
42+
it 'encodes the user info into a jwt' do
43+
invoice = EfeesInvoice.new(alma_id)
44+
decoded_token = EfeesInvoice.decode(invoice.jwt)
1945

20-
@invoice = EfeesInvoice.new(alma_id)
46+
expect(decoded_token[0]).to have_key('userName')
47+
expect(decoded_token[0]['userName']).to eq(alma_id)
48+
end
2149
end
2250

23-
it 'encodes the user info into a jwt' do
24-
decoded_token = EfeesInvoice.decode(@invoice.jwt)
25-
expect(decoded_token[0] { userName }).to have_key('userName')
26-
expect(decoded_token[0] { userName }).to have_value('10335026')
27-
end
51+
describe '#submit!' do
52+
let(:alma_id) { '123456' }
53+
54+
let(:user_double) do
55+
double(
56+
id: alma_id,
57+
email: 'test@example.com',
58+
name: 'Test User',
59+
fees: 100.0
60+
)
61+
end
62+
63+
before do
64+
allow(Alma::User)
65+
.to receive(:find_if_exists)
66+
.with(alma_id)
67+
.and_return(user_double)
68+
end
69+
70+
it 'enqueues the efee invoice email' do
71+
invoice = EfeesInvoice.new(alma_id)
2872

29-
it 'submits the request mailer' do # rubocop:disable RSpec/NoExpectationExample
30-
@invoice.submit!
73+
expect do
74+
invoice.submit!
75+
end.to have_enqueued_job(ActionMailer::MailDeliveryJob)
76+
.with(
77+
'RequestMailer',
78+
'efee_invoice_email',
79+
'deliver_now',
80+
{ args: [alma_id] }
81+
)
82+
end
3183
end
3284
end

0 commit comments

Comments
 (0)