0
0
Fork 0

Use capture_emails helper to improve email assertions in specs (#29245)

This commit is contained in:
Matt Jankowski 2024-02-19 10:57:47 -05:00 committed by GitHub
parent 86627ea2e4
commit 64f9939e39
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 97 additions and 47 deletions

View file

@ -35,7 +35,7 @@ RSpec.describe Admin::Disputes::AppealsController do
let(:current_user) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')) }
it 'redirects back to the strike page and notifies target account about approved appeal', :sidekiq_inline do
subject
emails = capture_emails { subject }
expect(response)
.to redirect_to(disputes_strike_path(appeal.strike))
@ -43,9 +43,13 @@ RSpec.describe Admin::Disputes::AppealsController do
expect(target_account.reload)
.to_not be_suspended
expect(UserMailer.deliveries.size).to eq(1)
expect(UserMailer.deliveries.first.to.first).to eq(target_account.user.email)
expect(UserMailer.deliveries.first.subject).to eq(I18n.t('user_mailer.appeal_approved.subject', date: I18n.l(appeal.created_at)))
expect(emails.size)
.to eq(1)
expect(emails.first)
.to have_attributes(
to: contain_exactly(target_account.user.email),
subject: eq(I18n.t('user_mailer.appeal_approved.subject', date: I18n.l(appeal.created_at)))
)
end
end
@ -55,14 +59,19 @@ RSpec.describe Admin::Disputes::AppealsController do
let(:current_user) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')) }
it 'redirects back to the strike page and notifies target account about rejected appeal', :sidekiq_inline do
subject
emails = capture_emails { subject }
expect(response)
.to redirect_to(disputes_strike_path(appeal.strike))
expect(UserMailer.deliveries.size).to eq(1)
expect(UserMailer.deliveries.first.to.first).to eq(target_account.user.email)
expect(UserMailer.deliveries.first.subject).to eq(I18n.t('user_mailer.appeal_rejected.subject', date: I18n.l(appeal.created_at)))
expect(emails.size)
.to eq(1)
expect(emails.first)
.to have_attributes(
to: contain_exactly(target_account.user.email),
subject: eq(I18n.t('user_mailer.appeal_rejected.subject', date: I18n.l(appeal.created_at)))
)
end
end
end

View file

@ -5,6 +5,8 @@ require 'rails_helper'
describe Admin::ResetsController do
render_views
subject { post :create, params: { account_id: account.id } }
let(:account) { Fabricate(:account) }
before do
@ -13,11 +15,11 @@ describe Admin::ResetsController do
describe 'POST #create', :sidekiq_inline do
it 'redirects to admin accounts page' do
expect do
post :create, params: { account_id: account.id }
end.to change(Devise.mailer.deliveries, :size).by(2)
emails = capture_emails { subject }
expect(Devise.mailer.deliveries).to have_attributes(
expect(emails.size)
.to eq(2)
expect(emails).to have_attributes(
first: have_attributes(
to: include(account.user.email),
subject: I18n.t('devise.mailer.password_change.subject')

View file

@ -124,7 +124,7 @@ RSpec.describe Auth::SessionsController do
end
it 'logs the user in and sends suspicious email and redirects home', :sidekiq_inline do
subject
emails = capture_emails { subject }
expect(response)
.to redirect_to(root_path)
@ -132,9 +132,13 @@ RSpec.describe Auth::SessionsController do
expect(controller.current_user)
.to eq user
expect(UserMailer.deliveries.size).to eq(1)
expect(UserMailer.deliveries.first.to.first).to eq(user.email)
expect(UserMailer.deliveries.first.subject).to eq(I18n.t('user_mailer.suspicious_sign_in.subject'))
expect(emails.size)
.to eq(1)
expect(emails.first)
.to have_attributes(
to: contain_exactly(user.email),
subject: eq(I18n.t('user_mailer.suspicious_sign_in.subject'))
)
end
end
@ -260,21 +264,27 @@ RSpec.describe Auth::SessionsController do
end
it 'does not log the user in, sets a flash message, and sends a suspicious sign in email', :sidekiq_inline do
Auth::SessionsController::MAX_2FA_ATTEMPTS_PER_HOUR.times do
post :create, params: { user: { otp_attempt: '1234' } }, session: { attempt_user_id: user.id, attempt_user_updated_at: user.updated_at.to_s }
expect(controller.current_user).to be_nil
emails = capture_emails do
Auth::SessionsController::MAX_2FA_ATTEMPTS_PER_HOUR.times do
post :create, params: { user: { otp_attempt: '1234' } }, session: { attempt_user_id: user.id, attempt_user_updated_at: user.updated_at.to_s }
expect(controller.current_user).to be_nil
end
post :create, params: { user: { otp_attempt: user.current_otp } }, session: { attempt_user_id: user.id, attempt_user_updated_at: user.updated_at.to_s }
end
post :create, params: { user: { otp_attempt: user.current_otp } }, session: { attempt_user_id: user.id, attempt_user_updated_at: user.updated_at.to_s }
expect(controller.current_user)
.to be_nil
expect(flash[:alert])
.to match I18n.t('users.rate_limited')
expect(UserMailer.deliveries.size).to eq(1)
expect(UserMailer.deliveries.first.to.first).to eq(user.email)
expect(UserMailer.deliveries.first.subject).to eq(I18n.t('user_mailer.failed_2fa.subject'))
expect(emails.size)
.to eq(1)
expect(emails.first)
.to have_attributes(
to: contain_exactly(user.email),
subject: eq(I18n.t('user_mailer.failed_2fa.subject'))
)
end
end

View file

@ -18,9 +18,15 @@ RSpec.describe Disputes::AppealsController do
let(:params) { { strike_id: strike.id, appeal: { text: 'Foo' } } }
it 'notifies staff about new appeal and redirects back to strike page', :sidekiq_inline do
subject
emails = capture_emails { subject }
expect(ActionMailer::Base.deliveries.first.to).to eq([admin.email])
expect(emails.size)
.to eq(1)
expect(emails.first)
.to have_attributes(
to: contain_exactly(admin.email),
subject: eq(I18n.t('admin_mailer.new_appeal.subject', username: current_user.account.acct, instance: Rails.configuration.x.local_domain))
)
expect(response).to redirect_to(disputes_strike_path(strike.id))
end
end
@ -31,9 +37,9 @@ RSpec.describe Disputes::AppealsController do
let(:params) { { strike_id: strike.id, appeal: { text: '' } } }
it 'does not send email and renders strike show page', :sidekiq_inline do
subject
emails = capture_emails { subject }
expect(ActionMailer::Base.deliveries.size).to eq(0)
expect(emails).to be_empty
expect(response).to render_template('disputes/strikes/show')
end
end