Skip to content

Commit

Permalink
Refactor RSpec mail deliveries
Browse files Browse the repository at this point in the history
Extract deliveries into helper method which stores and clear the mail
queue. This gives us one place to update to perform delayed mail
delivery via ActiveJob.
  • Loading branch information
gbp committed Jul 31, 2024
1 parent 6e8b6f7 commit 3d7e9cf
Show file tree
Hide file tree
Showing 44 changed files with 223 additions and 362 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@
end

it 'sends a response email to the user who requested the change' do
deliveries = ActionMailer::Base.deliveries
mail = deliveries.first

expect(deliveries.size).to eq(1)
mail = deliveries.first
expect(mail.subject).to eq('Your request')
expect(mail.to).to eq([add_request.get_user_email])
expect(mail.body).to match(/Thanks but no/)
Expand All @@ -63,7 +61,7 @@
end

it 'no email is sent to the user who requested the change' do
expect(ActionMailer::Base.deliveries).to be_empty
expect(deliveries).to be_empty
end

it 'notifies the admin the request is closed' do
Expand Down
6 changes: 2 additions & 4 deletions spec/controllers/admin_public_body_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,8 @@
end

it 'should send a response to the requesting user' do
deliveries = ActionMailer::Base.deliveries
expect(deliveries.size).to eq(1)
mail = deliveries[0]
mail = deliveries.first
expect(mail.subject).to eq('Adding a new body')
expect(mail.to).to eq([@change_request.get_user_email])
expect(mail.body).
Expand Down Expand Up @@ -610,9 +609,8 @@
end

it 'should send a response to the requesting user' do
deliveries = ActionMailer::Base.deliveries
expect(deliveries.size).to eq(1)
mail = deliveries[0]
mail = deliveries.first
expect(mail.subject).to eq('Body update')
expect(mail.to).to eq([@change_request.get_user_email])
expect(mail.body).to match(/Done./)
Expand Down
3 changes: 1 addition & 2 deletions spec/controllers/admin_request_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,8 @@
info_request.reload
expect(info_request.prominence).to eq("requester_only")
expect(info_request.described_state).to eq("vexatious")
deliveries = ActionMailer::Base.deliveries
expect(deliveries.size).to eq(1)
mail = deliveries[0]
mail = deliveries.first
expect(mail.body).to match(/Foo/)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@

it 'emails the pro contact address with the request' do
post :create, params: { account_request: account_request_params }
expect(ActionMailer::Base.deliveries.size).to eq 1
mail = ActionMailer::Base.deliveries.first
expect(deliveries.size).to eq 1
mail = deliveries.first
expect(mail.to.first).to eq AlaveteliConfiguration.pro_contact_email
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@

it 'does not send a notification' do
action
expect(ActionMailer::Base.deliveries).to be_empty
expect(deliveries).to be_empty
end
end

Expand All @@ -280,7 +280,7 @@

it 'sends a notification' do
action
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to match(/Batch rate limit hit/)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
end

it 'sends an exception email' do
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to match(/Stripe::RateLimitError/)
end

Expand All @@ -137,7 +137,7 @@
end

it 'sends an exception email' do
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to match(/Stripe::InvalidRequestError/)
end

Expand All @@ -154,7 +154,7 @@
end

it 'sends an exception email' do
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to match(/Stripe::AuthenticationError/)
end

Expand All @@ -171,7 +171,7 @@
end

it 'sends an exception email' do
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to match(/Stripe::APIConnectionError/)
end

Expand All @@ -188,7 +188,7 @@
end

it 'sends an exception email' do
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to match(/Stripe::StripeError/)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def send_request
expected = '(Stripe::SignatureVerificationError) "Unable to extract ' \
'timestamp and signatures'
post :receive, params: payload
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to include(expected)
end

Expand All @@ -115,7 +115,7 @@ def send_request
it 'sends an exception email' do
expected = '(Stripe::SignatureVerificationError) "No signatures ' \
'found matching the expected'
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to include(expected)
end

Expand Down Expand Up @@ -155,7 +155,7 @@ def send_request

it 'sends an exception email' do
expected = 'Timestamp outside the tolerance zone'
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to include(expected)
end
end
Expand All @@ -176,7 +176,7 @@ def send_request
it 'sends an exception email' do
expected = 'AlaveteliPro::StripeWebhooksController::' \
'MissingTypeStripeWebhookError'
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to include(expected)
end
end
Expand Down Expand Up @@ -262,7 +262,7 @@ def send_request
end

it 'notifies the user that their payment failed' do
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to match(/Payment failed/)
expect(mail.to).to include(user.email)
end
Expand Down
34 changes: 17 additions & 17 deletions spec/controllers/alaveteli_pro/subscriptions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@
end

it 'sends an exception email' do
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to match(/Stripe::RateLimitError/)
end

Expand All @@ -307,7 +307,7 @@
end

it 'sends an exception email' do
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to match(/Stripe::InvalidRequestError/)
end

Expand All @@ -332,7 +332,7 @@
end

it 'sends an exception email' do
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to match(/Stripe::AuthenticationError/)
end

Expand All @@ -357,7 +357,7 @@
end

it 'sends an exception email' do
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to match(/Stripe::APIConnectionError/)
end

Expand All @@ -382,7 +382,7 @@
end

it 'sends an exception email' do
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to match(/Stripe::StripeError/)
end

Expand All @@ -407,7 +407,7 @@
end

it 'does not sends an exception email' do
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail).to be_nil
end

Expand All @@ -432,7 +432,7 @@
end

it 'does not sends an exception email' do
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail).to be_nil
end

Expand Down Expand Up @@ -632,7 +632,7 @@

it 'sends an exception email' do
authorise
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to match(/Stripe::RateLimitError/)
end
end
Expand All @@ -647,7 +647,7 @@

it 'sends an exception email' do
authorise
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to match(/Stripe::InvalidRequestError/)
end
end
Expand All @@ -662,7 +662,7 @@

it 'sends an exception email' do
authorise
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to match(/Stripe::AuthenticationError/)
end
end
Expand All @@ -677,7 +677,7 @@

it 'sends an exception email' do
authorise
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to match(/Stripe::APIConnectionError/)
end
end
Expand All @@ -692,7 +692,7 @@

it 'sends an exception email' do
authorise
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to match(/Stripe::StripeError/)
end
end
Expand Down Expand Up @@ -914,7 +914,7 @@
end

it 'sends an exception email' do
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to match(/Stripe::RateLimitError/)
end

Expand All @@ -935,7 +935,7 @@
end

it 'sends an exception email' do
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to match(/Stripe::InvalidRequestError/)
end

Expand All @@ -956,7 +956,7 @@
end

it 'sends an exception email' do
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to match(/Stripe::AuthenticationError/)
end

Expand All @@ -977,7 +977,7 @@
end

it 'sends an exception email' do
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to match(/Stripe::APIConnectionError/)
end

Expand All @@ -998,7 +998,7 @@
end

it 'sends an exception email' do
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to match(/Stripe::StripeError/)
end

Expand Down
10 changes: 4 additions & 6 deletions spec/controllers/classifications_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ def post_status(status, message: nil)
it 'should send an email to the requester letting them know someone
has updated the status of their request' do
post_status('rejected')
deliveries = ActionMailer::Base.deliveries
expect(deliveries.size).to eq(1)
expect(deliveries.first.subject).
mail = deliveries.first
expect(mail.subject).
to match('Someone has updated the status of your request')
end

Expand Down Expand Up @@ -134,7 +134,6 @@ def post_status(status, message: nil)
'requires admin and one to the requester noting the status ' \
'change' do
post_status('requires_admin', message: 'a message')
deliveries = ActionMailer::Base.deliveries
expect(deliveries.size).to eq(2)
requires_admin_mail = deliveries.first
status_update_mail = deliveries.second
Expand Down Expand Up @@ -344,15 +343,14 @@ def post_status(status, message: nil)

info_request.reload
expect(info_request.described_state).not_to eq('requires_admin')
expect(ActionMailer::Base.deliveries).to be_empty
expect(deliveries).to be_empty
end

context 'message is included when classifying as requires_admin' do
it 'should send an email including the message' do
post_status('requires_admin', message: 'Something weird happened')
deliveries = ActionMailer::Base.deliveries
expect(deliveries.size).to eq(1)
mail = deliveries[0]
mail = deliveries.first
expect(mail.body).to match(/as needing admin/)
expect(mail.body).to match(/Something weird happened/)
end
Expand Down
6 changes: 3 additions & 3 deletions spec/controllers/comment_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
expect(comment_array.size).to eq(1)
comment = comment_array[0]

expect(ActionMailer::Base.deliveries.size).to eq(0)
expect(deliveries).to be_empty

expect(response).to redirect_to(
controller: 'request',
Expand Down Expand Up @@ -257,7 +257,7 @@
submitted_comment: 1,
preview: 0
}
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to match(/spam annotation from user #{ user.id }/)
end

Expand Down Expand Up @@ -329,7 +329,7 @@
submitted_comment: 1,
preview: 0
}
mail = ActionMailer::Base.deliveries.first
mail = deliveries.first
expect(mail.subject).to match(/spam annotation from user #{ user.id }/)
end

Expand Down
Loading

0 comments on commit 3d7e9cf

Please sign in to comment.