diff --git a/spec/controllers/admin_public_body_change_requests_controller_spec.rb b/spec/controllers/admin_public_body_change_requests_controller_spec.rb index c7e94ea70c..9585bc0123 100644 --- a/spec/controllers/admin_public_body_change_requests_controller_spec.rb +++ b/spec/controllers/admin_public_body_change_requests_controller_spec.rb @@ -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/) @@ -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 diff --git a/spec/controllers/admin_public_body_controller_spec.rb b/spec/controllers/admin_public_body_controller_spec.rb index c8cf516faf..879ab54d4b 100644 --- a/spec/controllers/admin_public_body_controller_spec.rb +++ b/spec/controllers/admin_public_body_controller_spec.rb @@ -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). @@ -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./) diff --git a/spec/controllers/admin_request_controller_spec.rb b/spec/controllers/admin_request_controller_spec.rb index ffbec8bdc8..0546a0e37e 100644 --- a/spec/controllers/admin_request_controller_spec.rb +++ b/spec/controllers/admin_request_controller_spec.rb @@ -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 diff --git a/spec/controllers/alaveteli_pro/account_request_controller_spec.rb b/spec/controllers/alaveteli_pro/account_request_controller_spec.rb index 3460959c1b..3dfa38bee6 100644 --- a/spec/controllers/alaveteli_pro/account_request_controller_spec.rb +++ b/spec/controllers/alaveteli_pro/account_request_controller_spec.rb @@ -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 diff --git a/spec/controllers/alaveteli_pro/info_request_batches_controller_spec.rb b/spec/controllers/alaveteli_pro/info_request_batches_controller_spec.rb index 6ed5157dc7..2219bf6046 100644 --- a/spec/controllers/alaveteli_pro/info_request_batches_controller_spec.rb +++ b/spec/controllers/alaveteli_pro/info_request_batches_controller_spec.rb @@ -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 @@ -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 diff --git a/spec/controllers/alaveteli_pro/payment_methods_controller_spec.rb b/spec/controllers/alaveteli_pro/payment_methods_controller_spec.rb index d8c3bf88d1..8f5e716409 100644 --- a/spec/controllers/alaveteli_pro/payment_methods_controller_spec.rb +++ b/spec/controllers/alaveteli_pro/payment_methods_controller_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/spec/controllers/alaveteli_pro/stripe_webhooks_controller_spec.rb b/spec/controllers/alaveteli_pro/stripe_webhooks_controller_spec.rb index 3c3078b801..ee2fbd33eb 100644 --- a/spec/controllers/alaveteli_pro/stripe_webhooks_controller_spec.rb +++ b/spec/controllers/alaveteli_pro/stripe_webhooks_controller_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/spec/controllers/alaveteli_pro/subscriptions_controller_spec.rb b/spec/controllers/alaveteli_pro/subscriptions_controller_spec.rb index 4a548902a5..de003915fd 100644 --- a/spec/controllers/alaveteli_pro/subscriptions_controller_spec.rb +++ b/spec/controllers/alaveteli_pro/subscriptions_controller_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/spec/controllers/classifications_controller_spec.rb b/spec/controllers/classifications_controller_spec.rb index aea327dc94..df9125c5df 100644 --- a/spec/controllers/classifications_controller_spec.rb +++ b/spec/controllers/classifications_controller_spec.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/controllers/comment_controller_spec.rb b/spec/controllers/comment_controller_spec.rb index d5c13907b5..cfdaba6bf8 100644 --- a/spec/controllers/comment_controller_spec.rb +++ b/spec/controllers/comment_controller_spec.rb @@ -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', @@ -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 @@ -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 diff --git a/spec/controllers/followups_controller_spec.rb b/spec/controllers/followups_controller_spec.rb index e17833eb3c..d68c4a9f1b 100644 --- a/spec/controllers/followups_controller_spec.rb +++ b/spec/controllers/followups_controller_spec.rb @@ -364,9 +364,8 @@ } # check it worked - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first expect(mail.body).to match(/What a useless response! You suck./) expect(mail.to_addrs.first.to_s). to eq(request.public_body.request_email) @@ -541,8 +540,7 @@ def send_request request_url_title: closed_request.url_title, incoming_message_id: closed_request.incoming_messages[0].id } - deliveries = ActionMailer::Base.deliveries - expect(deliveries.size).to eq(0) + expect(deliveries).to be_empty expect(response).to render_template('new') @@ -577,7 +575,6 @@ def send_request end it "only delivers the message once" do - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) end diff --git a/spec/controllers/help_controller_spec.rb b/spec/controllers/help_controller_spec.rb index 95881ec827..516557dcad 100644 --- a/spec/controllers/help_controller_spec.rb +++ b/spec/controllers/help_controller_spec.rb @@ -141,10 +141,9 @@ } expect(response).to redirect_to(frontpage_path) - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - expect(deliveries[0].body).to include('really should know') - deliveries.clear + mail = deliveries.first + expect(mail.body).to include('really should know') end context 'the site is configured to require reCAPTCHA' do @@ -166,10 +165,7 @@ submitted_contact_form: 1 } expect(response).not_to redirect_to(frontpage_path) - - deliveries = ActionMailer::Base.deliveries - expect(deliveries.size).to eq(0) - deliveries.clear + expect(deliveries).to be_empty end end @@ -186,10 +182,7 @@ } expect(response).to redirect_to(frontpage_path) - - deliveries = ActionMailer::Base.deliveries - expect(deliveries.size).to eq(0) - deliveries.clear + expect(deliveries).to be_empty end it 'renders the form when no params are supplied' do diff --git a/spec/controllers/password_changes_controller_spec.rb b/spec/controllers/password_changes_controller_spec.rb index 3badb9c931..c6916857cc 100644 --- a/spec/controllers/password_changes_controller_spec.rb +++ b/spec/controllers/password_changes_controller_spec.rb @@ -151,9 +151,8 @@ password_change_user: { email: user.email } } - email = ActionMailer::Base.deliveries.last - expect(email.subject).to eq('Change your password on Alaveteli') - ActionMailer::Base.deliveries.clear + mail = deliveries.last + expect(mail.subject).to eq('Change your password on Alaveteli') end it 'does not send a confirmation email for an unknown email' do @@ -162,7 +161,7 @@ email: 'unknown-email@example.org' } } - expect(ActionMailer::Base.deliveries.size).to eq(0) + expect(deliveries).to be_empty end it 'renders the confirmation message' do diff --git a/spec/controllers/public_body_change_requests_controller_spec.rb b/spec/controllers/public_body_change_requests_controller_spec.rb index 75b7a4e78f..53dece79aa 100644 --- a/spec/controllers/public_body_change_requests_controller_spec.rb +++ b/spec/controllers/public_body_change_requests_controller_spec.rb @@ -38,9 +38,8 @@ public_body_change_request: @change_request_params } change_request_id = assigns[:change_request].id - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first expect(mail.subject).to match(/Add authority - New Body/) expect(mail.header['Reply-To'].to_s).to include(@email) expect(mail.to).to include('postmaster@localhost') @@ -108,9 +107,7 @@ expect(response).to redirect_to(frontpage_path) - deliveries = ActionMailer::Base.deliveries - expect(deliveries.size).to eq(0) - deliveries.clear + expect(deliveries).to be_empty end context 'when handling a request for an update to an existing authority' do @@ -133,9 +130,8 @@ public_body_change_request: @change_request_params } change_request_id = assigns[:change_request].id - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first expect(mail.subject). to match(/Update email address - #{@public_body.name}/) expect(mail.header['Reply-To'].to_s).to include(@email) diff --git a/spec/controllers/reports_controller_spec.rb b/spec/controllers/reports_controller_spec.rb index e4bee67f6b..bf678f530f 100644 --- a/spec/controllers/reports_controller_spec.rb +++ b/spec/controllers/reports_controller_spec.rb @@ -138,9 +138,8 @@ reason: "my reason", message: "It's just not" } - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first expect(mail.subject).to match(/attention_requested/) expect(mail.header['Reply-To'].to_s).to include(user.email) expect(mail.body).to include(user.name) @@ -249,10 +248,9 @@ reason: "my reason", message: "It's just not" } - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first expect(mail.subject).to match(/requires admin/) expect(mail.header['Reply-To'].to_s).to include(user.email) diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb index 97661fd5ba..f47c9cad8b 100644 --- a/spec/controllers/request_controller_spec.rb +++ b/spec/controllers/request_controller_spec.rb @@ -1007,9 +1007,8 @@ def send_request expect(om.body). to eq("This is a silly letter. It is too short to be interesting.") - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first expect(mail.body). to match(/This is a silly letter. It is too short to be interesting./) @@ -1369,7 +1368,7 @@ def send_request submitted_new_request: 1, preview: 0 } - mail = ActionMailer::Base.deliveries.first + mail = deliveries.first expect(mail.subject).to match(/Spam request from user #{ user.id }/) end end @@ -1398,7 +1397,7 @@ def send_request submitted_new_request: 1, preview: 0 } - mail = ActionMailer::Base.deliveries.first + mail = deliveries.first expect(mail.subject).to match(/Spam request from user #{ user.id }/) end end @@ -1422,7 +1421,7 @@ def send_request submitted_new_request: 1, preview: 0 } - mail = ActionMailer::Base.deliveries.first + mail = deliveries.first expect(mail.subject).to match(/Spam request from user #{ user.id }/) end @@ -1505,7 +1504,7 @@ def send_request submitted_new_request: 1, preview: 0 } - mail = ActionMailer::Base.deliveries.first + mail = deliveries.first expect(mail.subject).to match(/Spam request from user #{ user.id }/) end @@ -1563,7 +1562,7 @@ def send_request submitted_new_request: 1, preview: 0 } - mail = ActionMailer::Base.deliveries.first + mail = deliveries.first expect(mail.subject). to match(/\(ip_in_blocklist\) from User##{ user.id }/) end @@ -1647,7 +1646,7 @@ def send_request submitted_new_request: 1, preview: 0 } - mail = ActionMailer::Base.deliveries.first + mail = deliveries.first expect(mail.subject). to match(/\(ip_in_blocklist\) from User##{ user.id }/) end diff --git a/spec/controllers/user_controller_spec.rb b/spec/controllers/user_controller_spec.rb index 654dfeff75..5aa4066e40 100644 --- a/spec/controllers/user_controller_spec.rb +++ b/spec/controllers/user_controller_spec.rb @@ -729,9 +729,9 @@ def make_request } expect(response).to render_template('confirm') - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - expect(deliveries[0].body).to include("not reveal your email") + mail = deliveries.first + expect(mail.body).to include("not reveal your email") end it "should send confirmation mail in other languages or different locales" do @@ -746,9 +746,9 @@ def make_request } expect(response).to render_template('confirm') - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - expect(deliveries[0].body).to include("No revelaremos") + mail = deliveries.first + expect(mail.body).to include("No revelaremos") end context "filling in the form with an existing registered email" do @@ -763,12 +763,12 @@ def make_request } expect(response).to render_template('confirm') - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) + mail = deliveries.first # This text may span a line break, depending on the length of the # SITE_NAME - expect(deliveries[0].body).to match(/when\s+you\s+already\s+have\s+an/) + expect(mail.body).to match(/when\s+you\s+already\s+have\s+an/) end it "cope with trailing spaces in the email address" do @@ -782,12 +782,12 @@ def make_request } expect(response).to render_template('confirm') - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) + mail = deliveries.first # This text may span a line break, depending on the length of the # SITE_NAME - expect(deliveries[0].body).to match(/when\s+you\s+already\s+have\s+an/) + expect(mail.body).to match(/when\s+you\s+already\s+have\s+an/) end it "should create a new PostRedirect if the old one has expired" do @@ -1101,8 +1101,7 @@ def make_request expect(response).to render_template('signchangeemail') expect(assigns[:signchangeemail].errors[:password]).not_to be_nil - deliveries = ActionMailer::Base.deliveries - expect(deliveries.size).to eq(0) + expect(deliveries).to be_empty end it "should be an error if old email is wrong, everything else right" do @@ -1124,8 +1123,7 @@ def make_request expect(response).to render_template('signchangeemail') expect(assigns[:signchangeemail].errors[:old_email]).not_to be_nil - deliveries = ActionMailer::Base.deliveries - expect(deliveries.size).to eq(0) + expect(deliveries).to be_empty end it "should work even if the old email had a case difference" do @@ -1165,9 +1163,8 @@ def make_request expect(response).to render_template('signchangeemail_confirm') - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first expect(mail.body).to include("perhaps you, just tried to change their") expect(mail.to).to eq([ 'silly@localhost' ]) diff --git a/spec/controllers/user_profile/about_me_controller_spec.rb b/spec/controllers/user_profile/about_me_controller_spec.rb index dc0048e2c9..0ab6fcd7ab 100644 --- a/spec/controllers/user_profile/about_me_controller_spec.rb +++ b/spec/controllers/user_profile/about_me_controller_spec.rb @@ -279,7 +279,7 @@ about_me: '[HD] Watch Jason Bourne Online free MOVIE Full-HD' } } - mail = ActionMailer::Base.deliveries.first + mail = deliveries.first expect(mail.subject). to match(/Spam about me text from user #{ user.id }/) end diff --git a/spec/controllers/users/messages_controller_spec.rb b/spec/controllers/users/messages_controller_spec.rb index ed7a86d070..d72a9e8606 100644 --- a/spec/controllers/users/messages_controller_spec.rb +++ b/spec/controllers/users/messages_controller_spec.rb @@ -8,8 +8,6 @@ before { sign_in sender } - after { ActionMailer::Base.deliveries.clear } - describe 'GET contact' do context 'when not signed in' do it 'redirects to signin page' do @@ -76,7 +74,7 @@ submitted_contact_form: 1 } - expect(ActionMailer::Base.deliveries).to be_empty + expect(deliveries).to be_empty expect(response).to render_template('users/messages/opted_out') end end @@ -114,9 +112,7 @@ submitted_contact_form: 1 } - deliveries = ActionMailer::Base.deliveries - expect(deliveries.size).to eq(0) - deliveries.clear + expect(deliveries).to be_empty end end @@ -131,9 +127,8 @@ } expect(response).to redirect_to(user_url(recipient)) - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first expect(mail.body). to include("Bob Smith has used #{site_name} " \ "to send you the message below") @@ -179,7 +174,8 @@ submitted_contact_form: 1 } - expect(ActionMailer::Base.deliveries.first.subject). + mail = deliveries.first + expect(mail.subject). to match(/spam user message from user #{ sender.id }/) end @@ -225,7 +221,8 @@ } expect(response).to redirect_to(user_url(recipient)) - expect(ActionMailer::Base.deliveries.first.subject).to match(/Dearest/) + mail = deliveries.first + expect(mail.subject).to match(/Dearest/) end end @@ -246,7 +243,8 @@ submitted_contact_form: 1 } - expect(ActionMailer::Base.deliveries.first.subject). + mail = deliveries.first + expect(mail.subject). to match(/spam user message from user #{ sender.id }/) end @@ -261,7 +259,8 @@ } expect(response).to redirect_to(user_url(recipient)) - expect(ActionMailer::Base.deliveries.last.subject).to match(/Dearest/) + mail = deliveries.last + expect(mail.subject).to match(/Dearest/) end end end diff --git a/spec/controllers/users/sessions_controller_spec.rb b/spec/controllers/users/sessions_controller_spec.rb index 3fdf6c1c11..48c6fb44ba 100644 --- a/spec/controllers/users/sessions_controller_spec.rb +++ b/spec/controllers/users/sessions_controller_spec.rb @@ -88,7 +88,7 @@ expect(session[:user_id]).to eq(users(:bob_smith_user).id) # response doesn't contain /en/ but redirect_to does... expect(response).to redirect_to(request_list_path(post_redirect: 1)) - expect(ActionMailer::Base.deliveries).to be_empty + expect(deliveries).to be_empty end it "should not log you in if you use an invalid PostRedirect token, and shouldn't give 500 error either" do @@ -287,7 +287,7 @@ def do_signin(email, password) it 'sends an exception notification' do do_signin(user.email, 'password1234') - mail = ActionMailer::Base.deliveries.first + mail = deliveries.first expect(mail.subject).to match(/signup from suspected spammer/) end @@ -310,7 +310,7 @@ def do_signin(email, password) } expect(response).to render_template('user/confirm') - expect(ActionMailer::Base.deliveries).not_to be_empty + expect(deliveries).not_to be_empty end # TODO: Extract to integration spec @@ -344,11 +344,10 @@ def do_signin(email, password) }, token: post_redirect.token } - expect(ActionMailer::Base.deliveries).not_to be_empty + expect(deliveries).not_to be_empty - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first mail.body.to_s =~ /(http:\/\/.*(\/c\/(.*)))/ mail_url = $1 mail_path = $2 @@ -388,11 +387,10 @@ def do_signin(email, password) }, token: post_redirect.token } - expect(ActionMailer::Base.deliveries).not_to be_empty + expect(deliveries).not_to be_empty - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first mail.body.to_s =~ /(http:\/\/.*(\/c\/(.*)))/ mail_url = $1 mail_path = $2 diff --git a/spec/integration/alaveteli_dsl.rb b/spec/integration/alaveteli_dsl.rb index 40da2ed5de..79db31c120 100644 --- a/spec/integration/alaveteli_dsl.rb +++ b/spec/integration/alaveteli_dsl.rb @@ -147,7 +147,6 @@ def last_holding_pen_mail end def confirmation_url_from_email - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) mail = deliveries.first mail.body.to_s =~ /(http:\/\/.*\/c\/.*)/ diff --git a/spec/integration/alaveteli_pro/request_creation_spec.rb b/spec/integration/alaveteli_pro/request_creation_spec.rb index 819a7107e7..f058857788 100644 --- a/spec/integration/alaveteli_pro/request_creation_spec.rb +++ b/spec/integration/alaveteli_pro/request_creation_spec.rb @@ -110,9 +110,8 @@ expect(info_request.outgoing_messages.length).to eq 1 expect(info_request.outgoing_messages.first.body).to eq "A very short letter." - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first expect(mail.body).to match(/A very short letter\./) expect(mail.subject).to match(/Freedom of Information request - Does the pro request form work\?/) expect(mail.to).to eq([public_body.request_email]) diff --git a/spec/integration/change_email_address_spec.rb b/spec/integration/change_email_address_spec.rb index 7a4b265e4a..9de1ef4544 100644 --- a/spec/integration/change_email_address_spec.rb +++ b/spec/integration/change_email_address_spec.rb @@ -14,7 +14,7 @@ expect(page).to have_content('Now check your email!') - mail = ActionMailer::Base.deliveries.first + mail = deliveries.first expect(mail.body).to include("confirm that you want to change") expect(mail.to).to eq([ 'newbob@localhost' ]) diff --git a/spec/integration/classify_request_spec.rb b/spec/integration/classify_request_spec.rb index dc12ce406e..1c81e36eeb 100644 --- a/spec/integration/classify_request_spec.rb +++ b/spec/integration/classify_request_spec.rb @@ -55,6 +55,8 @@ "try and fix it up." end + perform_enqueued_jobs + is_expected.to have_sent_email.matching_body(/as needing admin/) is_expected. to have_sent_email. diff --git a/spec/integration/errors_spec.rb b/spec/integration/errors_spec.rb index 6952dd4b38..d2d02ff807 100644 --- a/spec/integration/errors_spec.rb +++ b/spec/integration/errors_spec.rb @@ -1,12 +1,6 @@ require 'spec_helper' RSpec.describe "When errors occur" do - before(:each) do - # This should happen automatically before each test but doesn't with these integration - # tests for some reason. - ActionMailer::Base.deliveries = [] - end - context 'when considering all requests local (by default all in development)', local_requests: true do it 'should show a full trace for general errors' do @@ -68,9 +62,8 @@ it 'should notify of a general error' do allow(InfoRequest).to receive(:find_by_url_title!).and_raise("An example error") get "/request/example" - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first expect(mail.body).to match(/An example error/) end diff --git a/spec/integration/incoming_mail_spec.rb b/spec/integration/incoming_mail_spec.rb index 23f664d955..3276a94c61 100644 --- a/spec/integration/incoming_mail_spec.rb +++ b/spec/integration/incoming_mail_spec.rb @@ -7,9 +7,8 @@ it "receives incoming messages, sends email to requester, and shows them" do receive_incoming_mail('incoming-request-plain.email', email_to: info_request.incoming_email) - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first expect(mail.to).to eq([info_request.user.email]) expect(mail.body).to match(/You have a new response to the Freedom of Information request/) visit show_request_path info_request.url_title diff --git a/spec/integration/track_alerts_spec.rb b/spec/integration/track_alerts_spec.rb index 37fae2127a..102a327bec 100644 --- a/spec/integration/track_alerts_spec.rb +++ b/spec/integration/track_alerts_spec.rb @@ -31,9 +31,8 @@ TrackMailer.alert_tracks - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first expect(mail.body).to match(/Alter your subscription/) expect(mail.to_addrs.first.to_s).to include(user.email) mail.body.to_s =~ /(http:\/\/.*\/c\/(.*))/ @@ -55,10 +54,7 @@ expect(mail.body).to include(subscription_preferences_link) # Check nothing more is delivered if we try again - deliveries.clear - TrackMailer.alert_tracks - deliveries = ActionMailer::Base.deliveries - expect(deliveries.size).to eq(0) + expect { TrackMailer.alert_tracks }.to_not change { deliveries.size } end it "should send localised alerts" do @@ -83,8 +79,8 @@ destroy_and_rebuild_xapian_index TrackMailer.alert_tracks - deliveries = ActionMailer::Base.deliveries - mail = deliveries[0] + expect(deliveries.size).to eq(1) + mail = deliveries.first expect(mail.body).to include('el equipo de ') end end diff --git a/spec/lib/alaveteli_mail_poller_spec.rb b/spec/lib/alaveteli_mail_poller_spec.rb index 15eab823bd..6d5b4d1bfd 100644 --- a/spec/lib/alaveteli_mail_poller_spec.rb +++ b/spec/lib/alaveteli_mail_poller_spec.rb @@ -62,7 +62,7 @@ it 'sends an exception notification' do poller.poll_for_incoming - notification = ActionMailer::Base.deliveries.first + notification = deliveries.first expect(notification.subject). to eq('[ERROR] (Net::POPError) "Error code"') end @@ -77,7 +77,7 @@ it 'sends an exception notification' do poller.poll_for_incoming - notification = ActionMailer::Base.deliveries.first + notification = deliveries.first expect(notification.subject). to eq('[ERROR] (Net::POPError) "Error code"') end @@ -135,7 +135,7 @@ it 'sends an exception notification' do poller.poll_for_incoming - notification = ActionMailer::Base.deliveries.first + notification = deliveries.first expect(notification.subject). to eq('[ERROR] (ActiveRecord::StatementInvalid) "Deadlock"') end @@ -170,7 +170,7 @@ it 'sends an exception notification' do poller.poll_for_incoming - exception_notification = ActionMailer::Base.deliveries.first + exception_notification = deliveries.first expect(exception_notification.subject). to eq('[ERROR] (Net::POPError) "Error code"') end @@ -245,7 +245,7 @@ it 'sends an exception notification' do expect { poller.poll_for_incoming }.to_not raise_error - notification = ActionMailer::Base.deliveries.first + notification = deliveries.first expect(notification.subject). to eq('[ERROR] (Timeout::Error) "execution expired"') end diff --git a/spec/mailers/alaveteli_pro/embargo_mailer_spec.rb b/spec/mailers/alaveteli_pro/embargo_mailer_spec.rb index 0a2b2e9c97..0bc0920827 100644 --- a/spec/mailers/alaveteli_pro/embargo_mailer_spec.rb +++ b/spec/mailers/alaveteli_pro/embargo_mailer_spec.rb @@ -31,34 +31,30 @@ describe '.alert_expiring' do it 'only sends one email per user' do AlaveteliPro::EmbargoMailer.alert_expiring - mails = ActionMailer::Base.deliveries - expect(mails.size).to eq 2 - first_mail = mails.detect { |mail| mail.to == [pro_user.email] } - second_mail = mails.detect { |mail| mail.to == [pro_user_2.email] } + expect(deliveries.size).to eq(2) + first_mail = deliveries.detect { _1.to == [pro_user.email] } + second_mail = deliveries.detect { _1.to == [pro_user_2.email] } expect(first_mail).not_to be nil expect(second_mail).not_to be nil end it 'only sends an alert about an expiring embargo once' do AlaveteliPro::EmbargoMailer.alert_expiring - expect(ActionMailer::Base.deliveries.size).to eq 2 + expect(deliveries.size).to eq(2) - ActionMailer::Base.deliveries.clear - AlaveteliPro::EmbargoMailer.alert_expiring - expect(ActionMailer::Base.deliveries.size).to eq 0 + expect { AlaveteliPro::EmbargoMailer.alert_expiring }. + to_not change { deliveries.size } end it 'sends an alert about an expiring embargo extension' do AlaveteliPro::EmbargoMailer.alert_expiring - expect(ActionMailer::Base.deliveries.size).to eq 2 + expect(deliveries.size).to eq(2) - ActionMailer::Base.deliveries.clear expiring_3.embargo.extend(embargo_extension) travel_to(AlaveteliPro::Embargo.three_months_from_now - 3.days) do AlaveteliPro::EmbargoMailer.alert_expiring - mails = ActionMailer::Base.deliveries - expect(mails.detect { |mail| mail.to == [pro_user_2.email] }). - not_to be_nil + mail = deliveries.detect { _1.to == [pro_user_2.email] } + expect(mail).not_to be_nil end end @@ -102,8 +98,7 @@ AlaveteliPro::EmbargoMailer.alert_expiring - mails = ActionMailer::Base.deliveries - mail = mails.detect { |m| m.to == [pro_user_3.email] } + mail = deliveries.detect { _1.to == [pro_user_3.email] } expect(mail).to be nil end end @@ -165,21 +160,19 @@ describe '.alert_expired' do it 'only sends one email per user' do AlaveteliPro::EmbargoMailer.alert_expired - mails = ActionMailer::Base.deliveries - expect(mails.size).to eq 2 - first_mail = mails.detect { |mail| mail.to == [pro_user.email] } - second_mail = mails.detect { |mail| mail.to == [pro_user_2.email] } + expect(deliveries.size).to eq(2) + first_mail = deliveries.detect { _1.to == [pro_user.email] } + second_mail = deliveries.detect { _1.to == [pro_user_2.email] } expect(first_mail).not_to be nil expect(second_mail).not_to be nil end it 'only sends an alert about an expired embargo once' do AlaveteliPro::EmbargoMailer.alert_expired - expect(ActionMailer::Base.deliveries.size).to eq 2 + expect(deliveries.size).to eq(2) - ActionMailer::Base.deliveries.clear - AlaveteliPro::EmbargoMailer.alert_expired - expect(ActionMailer::Base.deliveries.size).to eq 0 + expect { AlaveteliPro::EmbargoMailer.alert_expired }. + to_not change { deliveries.size } end it 'creates UserInfoRequestSentAlert records for each expired request' do @@ -226,8 +219,7 @@ AlaveteliPro::EmbargoMailer.alert_expired - mails = ActionMailer::Base.deliveries - mail = mails.detect { |m| m.to == [pro_user_3.email] } + mail = deliveries.detect { _1.to == [pro_user_3.email] } expect(mail).to be nil end end diff --git a/spec/mailers/alaveteli_pro/metrics_mailer_spec.rb b/spec/mailers/alaveteli_pro/metrics_mailer_spec.rb index 4e8ae6b05c..0a4be9b566 100644 --- a/spec/mailers/alaveteli_pro/metrics_mailer_spec.rb +++ b/spec/mailers/alaveteli_pro/metrics_mailer_spec.rb @@ -36,9 +36,7 @@ subject { described_class.send_weekly_report(report) } it 'should deliver the weekly_report email' do - expect { subject }.to( - change(ActionMailer::Base.deliveries, :size).by(1) - ) + expect { subject }.to change { deliveries.size }.by(1) end end diff --git a/spec/mailers/alaveteli_pro/webhook_mailer_spec.rb b/spec/mailers/alaveteli_pro/webhook_mailer_spec.rb index fa4d8eaa0a..f1e64df6cd 100644 --- a/spec/mailers/alaveteli_pro/webhook_mailer_spec.rb +++ b/spec/mailers/alaveteli_pro/webhook_mailer_spec.rb @@ -11,9 +11,7 @@ context 'pro pricing enabled', feature: %i[pro_pricing] do context 'with pending notifications' do it 'should deliver digest email' do - expect { subject }.to( - change(ActionMailer::Base.deliveries, :size).by(1) - ) + expect { subject }.to change { deliveries.size }.by(1) end it 'should mark webhook as not pending' do diff --git a/spec/mailers/notification_mailer_spec.rb b/spec/mailers/notification_mailer_spec.rb index b785845828..cbd5a548f6 100644 --- a/spec/mailers/notification_mailer_spec.rb +++ b/spec/mailers/notification_mailer_spec.rb @@ -814,16 +814,14 @@ end it "sends a mail for each expected user" do - ActionMailer::Base.deliveries.clear - NotificationMailer.send_daily_notifications - expect(ActionMailer::Base.deliveries.size).to eq 2 + expect(deliveries.size).to eq(2) - first_mail = ActionMailer::Base.deliveries.first + first_mail = deliveries.first expect(first_mail.to).to eq([notification_1.user.email]) - second_mail = ActionMailer::Base.deliveries.second + second_mail = deliveries.second expect(second_mail.to).to eq([notification_2.user.email]) end @@ -983,16 +981,14 @@ end it "sends a mail for each unseen instant notification" do - ActionMailer::Base.deliveries.clear - NotificationMailer.send_instant_notifications - expect(ActionMailer::Base.deliveries.size).to eq 2 + expect(deliveries.size).to eq(2) - first_mail = ActionMailer::Base.deliveries.first + first_mail = deliveries.first expect(first_mail.to).to eq([notification_1.user.email]) - second_mail = ActionMailer::Base.deliveries.second + second_mail = deliveries.second expect(second_mail.to).to eq([notification_2.user.email]) end diff --git a/spec/mailers/request_mailer_spec.rb b/spec/mailers/request_mailer_spec.rb index 653db16fe0..93607365d1 100644 --- a/spec/mailers/request_mailer_spec.rb +++ b/spec/mailers/request_mailer_spec.rb @@ -4,7 +4,6 @@ describe "when receiving incoming mail" do before(:each) do load_raw_emails_data - ActionMailer::Base.deliveries = [] end it "should append it to the appropriate request" do @@ -15,11 +14,9 @@ expect(ir.incoming_messages.count).to eq(2) # one more arrives expect(ir.info_request_events[-1].incoming_message_id).not_to be_nil - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first expect(mail.to).to eq([ 'bob@localhost' ]) # to the user who sent fancy_dog_request - deliveries.clear end it "should append it to the appropriate request if there is only one guess of information request" do @@ -33,12 +30,10 @@ expect(ir.info_request_events[-1].incoming_message_id).not_to be_nil expect(ir.info_request_events[-2].params[:editor]).to eq("automatic") - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first # to the user who sent fancy_dog_request expect(mail.to).to eq(['bob@localhost']) - deliveries.clear end it "should append the email to each exact request address, unless that request has already received the email" do @@ -131,11 +126,9 @@ last_event = InfoRequest.holding_pen_request.incoming_messages[0].info_request.info_request_events.last expect(last_event.params[:rejected_reason]).to eq("Could not identify the request from the email address") - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first expect(mail.to).to eq([ AlaveteliConfiguration.contact_email ]) - deliveries.clear end it "puts messages with a malformed To: in the holding pen" do @@ -161,11 +154,9 @@ expect(ir.incoming_messages.count).to eq(2) # one more arrives expect(ir.info_request_events[-1].incoming_message_id).not_to be_nil - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first expect(mail.to).to eq([ 'bob@localhost' ]) # to the user who sent fancy_dog_request - deliveries.clear end it "should parse attachments from mails sent with apple mail" do @@ -191,11 +182,9 @@ expect(im.foi_attachments.size).to eq(6) # Clean up - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first expect(mail.to).to eq([ AlaveteliConfiguration.contact_email ]) - deliveries.clear end it "should store mail in holding pen and send to admin when the from email is empty and only authorities can reply" do @@ -213,11 +202,9 @@ last_event = InfoRequest.holding_pen_request.incoming_messages[0].info_request.info_request_events.last expect(last_event.params[:rejected_reason]).to match(/there is no "From" address/) - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first expect(mail.to).to eq([ AlaveteliConfiguration.contact_email ]) - deliveries.clear end it "should store mail in holding pen and send to admin when the from email is unknown and only authorities can reply" do @@ -235,11 +222,9 @@ last_event = InfoRequest.holding_pen_request.incoming_messages[0].info_request.info_request_events.last expect(last_event.params[:rejected_reason]).to match(/Only the authority can reply/) - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first expect(mail.to).to eq([ AlaveteliConfiguration.contact_email ]) - deliveries.clear end context "when sent from known spam address" do @@ -251,36 +236,28 @@ receive_incoming_mail('incoming-request-plain.email', email_to: @spam_address.email) - deliveries = ActionMailer::Base.deliveries - expect(deliveries.size).to eq(0) - deliveries.clear + expect(deliveries).to be_empty end it "recognises a spam address under the 'CC' header" do receive_incoming_mail('incoming-request-plain.email', email_cc: @spam_address.email) - deliveries = ActionMailer::Base.deliveries - expect(deliveries.size).to eq(0) - deliveries.clear + expect(deliveries).to be_empty end it "recognises a spam address under the 'BCC' header" do receive_incoming_mail('incoming-request-plain.email', email_bcc: @spam_address.email) - deliveries = ActionMailer::Base.deliveries - expect(deliveries.size).to eq(0) - deliveries.clear + expect(deliveries).to be_empty end it "recognises a spam email address under the 'envelope-to' header" do receive_incoming_mail('incoming-request-plain.email', email_envelope_to: @spam_address.email) - deliveries = ActionMailer::Base.deliveries - expect(deliveries.size).to eq(0) - deliveries.clear + expect(deliveries).to be_empty end end @@ -299,13 +276,11 @@ expect(ir.incoming_messages.count).to eq(1) # nothing should arrive # should be a message back to sender - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first expect(mail.to).to eq([ 'geraldinequango@localhost' ]) expect(mail.multipart?).to eq(false) expect(mail.body).to include("marked to no longer receive responses") - deliveries.clear end it "should return incoming mail to sender if not authority when a request is stopped for non-authority spam" do @@ -323,11 +298,9 @@ expect(ir.incoming_messages.count).to eq(2) # one more arrives # ... should get "responses arrived" message for original requester - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first expect(mail.to).to eq([ 'bob@localhost' ]) # to the user who sent fancy_dog_request - deliveries.clear # Test what happens if something arrives from another domain expect(ir.incoming_messages.count).to eq(2) # in fixture and above @@ -337,11 +310,9 @@ expect(ir.incoming_messages.count).to eq(2) # nothing should arrive # ... should be a bounce message back to sender - deliveries = ActionMailer::Base.deliveries - expect(deliveries.size).to eq(1) - mail = deliveries[0] + expect(deliveries.size).to eq(2) + mail = deliveries.second expect(mail.to).to eq([ 'dummy-address@dummy.localhost' ]) - deliveries.clear end it "discards rejected responses with a malformed From: when set to bounce" do @@ -356,9 +327,7 @@ email_from: "") expect(ir.incoming_messages.count).to eq(1) - deliveries = ActionMailer::Base.deliveries - expect(deliveries.size).to eq(0) - deliveries.clear + expect(deliveries).to be_empty end it "should send all new responses to holding pen if a request is marked to do so" do @@ -382,15 +351,12 @@ expect(last_event.params[:rejected_reason]).to match(/allow new responses from nobody/) # should be a message to admin regarding holding pen - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first expect(mail.to).to eq([ AlaveteliConfiguration.contact_email ]) - deliveries.clear end it "should destroy the messages sent to a request if marked to do so" do - ActionMailer::Base.deliveries.clear # mark request as anti-spam ir = info_requests(:fancy_dog_request) ir.allow_new_responses_from = 'nobody' @@ -407,8 +373,7 @@ expect(InfoRequest.holding_pen_request.incoming_messages.count).to eq(0) # should be no messages to anyone - deliveries = ActionMailer::Base.deliveries - expect(deliveries.size).to eq(0) + expect(deliveries).to be_empty end it "should not mutilate long URLs when trying to word wrap them" do @@ -429,6 +394,7 @@ info_request = FactoryBot.create(:waiting_clarification_info_request) info_request.update_column(:updated_at, Time.zone.now - 5.days) RequestMailer.alert_not_clarified_request + perform_enqueued_jobs end it 'does not render the footer partial for a non-user email' do @@ -473,8 +439,8 @@ def sent_alert_params(request, type) it 'sends the reminder' do old_request.create_embargo(publish_at: Time.zone.now + 3.days) send_alerts - deliveries = ActionMailer::Base.deliveries - mail = deliveries[0] + expect(deliveries.size).to eq(1) + mail = deliveries.first expect(mail.body).to match(/#{old_request.title}/) expect(mail.body).to match(/Letting everyone know whether you got the information/) end @@ -502,8 +468,8 @@ def sent_alert_params(request, type) it 'should send the reminder' do send_alerts - deliveries = ActionMailer::Base.deliveries - mail = deliveries[0] + expect(deliveries.size).to eq(1) + mail = deliveries.first expect(mail.body).to match(/Letting everyone know whether you got the information/) end end @@ -635,9 +601,8 @@ def sent_alert_params(request, type) RequestMailer.alert_new_response_reminders info_request = info_requests(:fancy_dog_request) - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(3) # sufficiently late it sends reminders too - mail = deliveries[0] + mail = deliveries.first expect(mail.body).to match(/To let everyone know/) expect(mail.to_addrs.first.to_s).to eq(info_request.user.email) @@ -723,7 +688,7 @@ def sent_alert_params(request, type) end def kitten_mails - ActionMailer::Base.deliveries.select { |mail| mail.body =~ /kitten/ } + deliveries.select { |mail| mail.body =~ /kitten/ } end it 'should not create HTML entities in the subject line' do @@ -770,10 +735,8 @@ def kitten_mails it "does not resend alerts to people who've already received them" do travel_to(31.days.from_now) do RequestMailer.alert_overdue_requests - expect(kitten_mails.size).to eq(1) - ActionMailer::Base.deliveries.clear - RequestMailer.alert_overdue_requests - expect(kitten_mails.size).to eq(0) + expect { RequestMailer.alert_overdue_requests }. + to_not change(kitten_mails, :size).from(1) end end @@ -806,7 +769,6 @@ def kitten_mails travel_to(31.days.from_now) do RequestMailer.alert_overdue_requests expect(kitten_mails.size).to eq(1) - ActionMailer::Base.deliveries.clear end end @@ -816,7 +778,7 @@ def kitten_mails travel_to(31.days.from_now) do RequestMailer.alert_overdue_requests - mails = ActionMailer::Base.deliveries.select do |mail| + mails = deliveries.select do |mail| mail.body =~ /#{info_request.title}/ end mail = mails[0] @@ -830,7 +792,7 @@ def kitten_mails travel_to(31.days.from_now) do RequestMailer.alert_overdue_requests - mails = ActionMailer::Base.deliveries.select do |mail| + mails = deliveries.select do |mail| mail.body =~ /#{info_request.title}/ end expect(mails).to be_empty @@ -872,7 +834,7 @@ def kitten_mails travel_to(61.days.from_now) do RequestMailer.alert_overdue_requests - mails = ActionMailer::Base.deliveries.select do |mail| + mails = deliveries.select do |mail| mail.body =~ /#{info_request.title}/ end mail = mails[0] @@ -888,7 +850,7 @@ def kitten_mails travel_to(61.days.from_now) do RequestMailer.alert_overdue_requests - mails = ActionMailer::Base.deliveries.select do |mail| + mails = deliveries.select do |mail| mail.body =~ /#{info_request.title}/ end expect(mails).to be_empty @@ -934,9 +896,8 @@ def force_updated_at_to_past(request) RequestMailer.alert_not_clarified_request - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first expect(mail.body).to match(/asked you to explain/) expect(mail.to_addrs.first.to_s).to eq(info_requests(:fancy_dog_request).user.email) mail.body.to_s =~ /(http:\/\/.*)/ @@ -970,7 +931,7 @@ def force_updated_at_to_past(request) force_updated_at_to_past(ir) RequestMailer.alert_not_clarified_request - expect(ActionMailer::Base.deliveries.size).to eq(0) + expect(deliveries).to be_empty end it "should not send an alert to banned users" do @@ -984,8 +945,7 @@ def force_updated_at_to_past(request) RequestMailer.alert_not_clarified_request - deliveries = ActionMailer::Base.deliveries - expect(deliveries.size).to eq(0) + expect(deliveries).to be_empty end it "should alert about embargoed requests" do @@ -995,9 +955,8 @@ def force_updated_at_to_past(request) RequestMailer.alert_not_clarified_request - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first expect(mail.body).to match(/asked you to explain/) expect(mail.to_addrs.first.to_s).to eq(info_request.user.email) end @@ -1010,8 +969,7 @@ def force_updated_at_to_past(request) RequestMailer.alert_not_clarified_request - deliveries = ActionMailer::Base.deliveries - expect(deliveries.size).to eq(0) + expect(deliveries).to be_empty end end @@ -1031,8 +989,8 @@ def force_updated_at_to_past(request) # send comment alert RequestMailer.alert_comment_on_request - deliveries = ActionMailer::Base.deliveries - mail = deliveries[0] + expect(deliveries.size).to eq(1) + mail = deliveries.first expect(mail.body).to match(/has annotated your/) expect(mail.to_addrs.first.to_s).to eq(info_requests(:fancy_dog_request).user.email) mail.body.to_s =~ /(http:\/\/.*)/ @@ -1040,10 +998,8 @@ def force_updated_at_to_past(request) expect(mail_url).to match("/request/why_do_you_have_such_a_fancy_dog#comment-#{new_comment.id}") # check if we send again, no more go out - deliveries.clear - RequestMailer.alert_comment_on_request - deliveries = ActionMailer::Base.deliveries - expect(deliveries.size).to eq(0) + expect { RequestMailer.alert_comment_on_request }. + to_not change { deliveries.size }.from(1) end it "should not send an alert when you comment on your own request" do @@ -1058,8 +1014,7 @@ def force_updated_at_to_past(request) # try to send comment alert RequestMailer.alert_comment_on_request - deliveries = ActionMailer::Base.deliveries - expect(deliveries.size).to eq(0) + expect(deliveries).to be_empty end it 'should not send an alert for a comment on an external request' do @@ -1068,8 +1023,7 @@ def force_updated_at_to_past(request) # try to send comment alert RequestMailer.alert_comment_on_request - deliveries = ActionMailer::Base.deliveries - expect(deliveries.size).to eq(0) + expect(deliveries).to be_empty end it "should send an alert when there are two new comments" do @@ -1080,9 +1034,8 @@ def force_updated_at_to_past(request) RequestMailer.alert_comment_on_request - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first expect(mail.body).to match(/There are 2 new annotations/) expect(mail.to_addrs.first.to_s).to eq(info_requests(:fancy_dog_request).user.email) mail.body.to_s =~ /(http:\/\/.*)/ @@ -1098,9 +1051,8 @@ def force_updated_at_to_past(request) RequestMailer.alert_comment_on_request - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first expect(mail.to_addrs.first.to_s).to eq(info_request.user.email) end end diff --git a/spec/mailers/survey_mailer_spec.rb b/spec/mailers/survey_mailer_spec.rb index 80dc47046f..335a1dccd1 100644 --- a/spec/mailers/survey_mailer_spec.rb +++ b/spec/mailers/survey_mailer_spec.rb @@ -5,7 +5,6 @@ before do allow(Survey).to receive(:enabled?).and_return(true) InfoRequest.destroy_all - ActionMailer::Base.deliveries = [] end def get_surveyable_request(user = nil) @@ -23,7 +22,7 @@ def get_surveyable_request(user = nil) it 'sends a survey alert' do get_surveyable_request SurveyMailer.alert_survey - expect(ActionMailer::Base.deliveries.size).to eq(1) + expect(deliveries.size).to eq(1) end it 'records the sending of the alert' do @@ -39,7 +38,7 @@ def get_surveyable_request(user = nil) request = get_surveyable_request get_surveyable_request(request.user) SurveyMailer.alert_survey - expect(ActionMailer::Base.deliveries.size).to eq(1) + expect(deliveries.size).to eq(1) end end @@ -49,7 +48,7 @@ def get_surveyable_request(user = nil) and_return(false) get_surveyable_request SurveyMailer.alert_survey - expect(ActionMailer::Base.deliveries.size).to eq(0) + expect(deliveries).to be_empty end end diff --git a/spec/mailers/track_mailer_spec.rb b/spec/mailers/track_mailer_spec.rb index c7e8c62c9a..0777df104e 100644 --- a/spec/mailers/track_mailer_spec.rb +++ b/spec/mailers/track_mailer_spec.rb @@ -186,7 +186,6 @@ @post_redirect = mock_model(PostRedirect, save!: true, email_token: "token") allow(PostRedirect).to receive(:new).and_return(@post_redirect) - ActionMailer::Base.deliveries = [] @user = mock_model(User, name_and_email: MailHandler.address_from_name_and_email('Tippy Test', 'tippy@localhost'), url_name: 'tippy_test' @@ -195,7 +194,6 @@ end it 'should deliver one email, with right headers' do - deliveries = ActionMailer::Base.deliveries if deliveries.size > 1 # debugging if there is an error deliveries.each do |d| $stderr.puts "------------------------------" @@ -204,18 +202,14 @@ end end expect(deliveries.size).to eq(1) - mail = deliveries[0] - + mail = deliveries.first expect(mail['Auto-Submitted'].to_s).to eq('auto-generated') expect(mail['Precedence'].to_s).to eq('bulk') - - deliveries.clear end it "does not include HTMLEntities in the subject line" do - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first expect(mail.subject).to eq "Your L'information email alert" end @@ -235,13 +229,9 @@ email: info_request.public_body.request_email ) - ActionMailer::Base.deliveries.clear update_xapian_index - TrackMailer.alert_tracks - deliveries = ActionMailer::Base.deliveries - expect(deliveries.size).to eq(0) - mail = deliveries[0] + expect { TrackMailer.alert_tracks }.to_not change { deliveries.size } end context "force ssl is off" do @@ -251,9 +241,8 @@ # wouldn't get reloaded it "should have http links in the email" do - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - mail = deliveries[0] + mail = deliveries.first expect(mail.body).to include("http://") expect(mail.body).not_to include("https://") diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index c111566ac1..e055b4919c 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -248,14 +248,14 @@ expected = "FOI response requires admin (waiting_response) " \ "- #{comment.info_request.title}" comment.report!("Vexatious comment", "Comment is bad, please hide", user) - notification = ActionMailer::Base.deliveries.last + notification = deliveries.last expect(notification.subject).to eq(expected) end it 'prepends the reason to the message before sending' do expected = "Reason: Vexatious comment\n\nComment is bad, please hide" comment.report!("Vexatious comment", "Comment is bad, please hide", user) - notification = ActionMailer::Base.deliveries.last + notification = deliveries.last expect(notification.body).to match(expected) end @@ -264,7 +264,7 @@ "The user wishes to draw attention to the comment: " \ "#{comment_url(comment, host: AlaveteliConfiguration.domain)}" comment.report!("Vexatious comment", "Comment is bad, please hide", user) - notification = ActionMailer::Base.deliveries.last + notification = deliveries.last expect(notification.body).to match(expected) end diff --git a/spec/models/incoming_message_spec.rb b/spec/models/incoming_message_spec.rb index a2f217bc08..ab46423ac4 100644 --- a/spec/models/incoming_message_spec.rb +++ b/spec/models/incoming_message_spec.rb @@ -615,10 +615,6 @@ load_raw_emails_data end - after(:all) do - ActionMailer::Base.deliveries.clear - end - it 'should correctly parse multipart mails with a linebreak in the boundary marker' do ir = info_requests(:fancy_dog_request) receive_incoming_mail('space-boundary.email', email_to: ir.incoming_email) @@ -702,7 +698,6 @@ end it "should load an email with funny MIME settings" do - ActionMailer::Base.deliveries.clear # just send it to the holding pen expect(InfoRequest.holding_pen_request.incoming_messages.count).to eq(0) receive_incoming_mail("humberside-police-odd-mime-type.email", @@ -710,9 +705,7 @@ expect(InfoRequest.holding_pen_request.incoming_messages.count).to eq(1) # clear the notification of new message in holding pen - deliveries = ActionMailer::Base.deliveries expect(deliveries.size).to eq(1) - deliveries.clear incoming_message = InfoRequest.holding_pen_request.incoming_messages[0] diff --git a/spec/models/info_request/response_rejection/bounce_spec.rb b/spec/models/info_request/response_rejection/bounce_spec.rb index 836d69ab02..90a28ea70e 100644 --- a/spec/models/info_request/response_rejection/bounce_spec.rb +++ b/spec/models/info_request/response_rejection/bounce_spec.rb @@ -80,10 +80,8 @@ described_class.new(*args).reject - expect(ActionMailer::Base.deliveries.first.to). - to eq(['sender@example.com']) - - ActionMailer::Base.deliveries.clear + mail = deliveries.first + expect(mail.to).to eq(['sender@example.com']) end end end diff --git a/spec/models/info_request_batch_spec.rb b/spec/models/info_request_batch_spec.rb index 4572a83659..73669e1ad7 100644 --- a/spec/models/info_request_batch_spec.rb +++ b/spec/models/info_request_batch_spec.rb @@ -319,18 +319,18 @@ it 'should send requests and notifications for only unsent batch requests' do InfoRequestBatch.send_batches - expect(ActionMailer::Base.deliveries.size).to eq(3) - first_email = ActionMailer::Base.deliveries.first - expect(first_email.to).to eq([first_public_body.request_email]) - expect(first_email.subject).to eq('Freedom of Information request - Example title') - - second_email = ActionMailer::Base.deliveries.second - expect(second_email.to).to eq([second_public_body.request_email]) - expect(second_email.subject).to eq('Freedom of Information request - Example title') - - third_email = ActionMailer::Base.deliveries.third - expect(third_email.to).to eq([info_request_batch.user.email]) - expect(third_email.subject).to eq('Your batch request "Example title" has been sent') + expect(deliveries.size).to eq(3) + first_mail = deliveries.first + expect(first_mail.to).to eq([first_public_body.request_email]) + expect(first_mail.subject).to eq('Freedom of Information request - Example title') + + second_mail = deliveries.second + expect(second_mail.to).to eq([second_public_body.request_email]) + expect(second_mail.subject).to eq('Freedom of Information request - Example title') + + third_mail = deliveries.third + expect(third_mail.to).to eq([info_request_batch.user.email]) + expect(third_mail.subject).to eq('Your batch request "Example title" has been sent') end it 'should set the sent_at value of the info request batch' do diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb index f31d012864..c6364122a0 100644 --- a/spec/models/info_request_spec.rb +++ b/spec/models/info_request_spec.rb @@ -569,10 +569,9 @@ email, raw_email = email_and_raw_email info_request.receive(email, raw_email) - notification = ActionMailer::Base.deliveries.last + notification = deliveries.last expect(notification.to).to include(info_request.user.email) - expect(ActionMailer::Base.deliveries.size).to eq(1) - ActionMailer::Base.deliveries.clear + expect(deliveries.size).to eq(1) end end @@ -582,7 +581,7 @@ email, raw_email = email_and_raw_email expect { info_request.receive(email, raw_email) }. - not_to change { ActionMailer::Base.deliveries.size } + not_to change { deliveries.size } expect { info_request.receive(email, raw_email) }. not_to change { Notification.count } end @@ -823,17 +822,15 @@ info_request = FactoryBot.create(:info_request, attrs) email, raw_email = email_and_raw_email(from: 'bounce@example.com') info_request.receive(email, raw_email) - bounce = ActionMailer::Base.deliveries.first + bounce = deliveries.first expect(bounce.to).to include('bounce@example.com') - ActionMailer::Base.deliveries.clear end it 'does not bounce responses to external requests' do info_request = FactoryBot.create(:external_request) email, raw_email = email_and_raw_email(from: 'bounce@example.com') info_request.receive(email, raw_email) - expect(ActionMailer::Base.deliveries).to be_empty - ActionMailer::Base.deliveries.clear + expect(deliveries).to be_empty end it 'discards rejected responses if the mail has no from address' do @@ -842,8 +839,7 @@ info_request = FactoryBot.create(:info_request, attrs) email, raw_email = email_and_raw_email(from: '') info_request.receive(email, raw_email) - expect(ActionMailer::Base.deliveries).to be_empty - ActionMailer::Base.deliveries.clear + expect(deliveries).to be_empty end it 'sends rejected responses to the holding pen' do @@ -855,8 +851,7 @@ expect(InfoRequest.holding_pen_request.incoming_messages.count).to eq(1) # Check that the notification that there's something new in the holding # has been sent - expect(ActionMailer::Base.deliveries.size).to eq(1) - ActionMailer::Base.deliveries.clear + expect(deliveries.size).to eq(1) end it 'discards rejected responses' do @@ -865,9 +860,8 @@ info_request = FactoryBot.create(:info_request, attrs) email, raw_email = email_and_raw_email info_request.receive(email, raw_email) - expect(ActionMailer::Base.deliveries).to be_empty + expect(deliveries).to be_empty expect(InfoRequest.holding_pen_request.incoming_messages.count).to eq(0) - ActionMailer::Base.deliveries.clear end it 'raises an error if there is an unknown handle_rejected_responses' do @@ -966,8 +960,7 @@ email_to: info_request.incoming_email, email_from: 'spammer@example.com') expect(info_request.reload.rejected_incoming_count).to eq(1) - expect(ActionMailer::Base.deliveries).to be_empty - ActionMailer::Base.deliveries.clear + expect(deliveries).to be_empty end it "delivers mail under the configured spam threshold" do @@ -992,8 +985,7 @@ email_to: info_request.incoming_email, email_from: 'spammer@example.com') expect(info_request.rejected_incoming_count).to eq(0) - expect(ActionMailer::Base.deliveries.size).to eq(1) - ActionMailer::Base.deliveries.clear + expect(deliveries.size).to eq(1) end it "delivers mail without a spam header" do @@ -1018,7 +1010,6 @@ email_from: 'spammer@example.com') expect(info_request.rejected_incoming_count).to eq(0) expect(info_request.incoming_messages.count).to eq(1) - ActionMailer::Base.deliveries.clear end context 'when email has already been received' do @@ -3497,8 +3488,6 @@ def create_old_unclassified_holding_pen end context "a series of require admin events on a request" do - before { ActionMailer::Base.deliveries.clear } - it "does not send multiple require admin reports" do # Request sent to old/invalid address request.log_event('sent', {}) @@ -3511,7 +3500,7 @@ def create_old_unclassified_holding_pen # Requester classifies as error_message request.log_event('status_update', {}) expect { request.set_described_state('error_message', request.user) }. - to change { ActionMailer::Base.deliveries.size }.by(1) + to change { deliveries.size }.by(1) # A second bounce is received request.awaiting_description = true @@ -3520,7 +3509,7 @@ def create_old_unclassified_holding_pen # Requester classifies as error_message again request.log_event('status_update', {}) expect { request.set_described_state('error_message', request.user) }. - to_not change { ActionMailer::Base.deliveries.size } + to_not change { deliveries.size } end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4b7e5f5719..d6c11afd6b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -94,12 +94,6 @@ FileUtils.rm_rf(Rails.root.join('tmp', 'storage')) end - # This is a workaround for a strange thing where ActionMailer::Base.deliveries isn't being - # cleared out correctly in controller specs. So, do it here for everything. - config.before(:each) do - ActionMailer::Base.deliveries = [] - end - config.before(:suite) do if ENV['ALAVETELI_USE_OINK'] oink_log = Rails.root + 'log/oink.log' diff --git a/spec/support/email_helpers.rb b/spec/support/email_helpers.rb index 9f7eab508c..57262e870c 100644 --- a/spec/support/email_helpers.rb +++ b/spec/support/email_helpers.rb @@ -80,3 +80,10 @@ def rebuild_raw_emails(info_request) im.save! end end + +def deliveries + @deliveries ||= [] + @deliveries += ActionMailer::Base.deliveries + ActionMailer::Base.deliveries.clear + @deliveries +end diff --git a/spec/support/shared_examples_for_mailers.rb b/spec/support/shared_examples_for_mailers.rb index c10cfbd11d..4a8243db23 100644 --- a/spec/support/shared_examples_for_mailers.rb +++ b/spec/support/shared_examples_for_mailers.rb @@ -2,6 +2,6 @@ it { is_expected.to be_nil } it 'should not deliver any mail' do - expect { subject }.to_not change(ActionMailer::Base.deliveries, :size) + expect { subject }.to_not change { deliveries.size } end end diff --git a/spec/support/shared_examples_for_network_errors_during_send.rb b/spec/support/shared_examples_for_network_errors_during_send.rb index d30e6f704c..90d87f7857 100644 --- a/spec/support/shared_examples_for_network_errors_during_send.rb +++ b/spec/support/shared_examples_for_network_errors_during_send.rb @@ -9,8 +9,7 @@ end it 'does not send the email' do - deliveries = ActionMailer::Base.deliveries - expect(deliveries.size).to eq(0) + expect(deliveries).to be_empty end it 'sets the described_state of the request to "error_message"' do