Misc spec/refactor to user mailer and user mailer spec (#27486)

main
Matt Jankowski 2023-10-27 05:57:16 -04:00 committed by GitHub
parent 37929b9707
commit 1f5187e2e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 51 deletions

View File

@ -20,7 +20,7 @@ class Admin::Disputes::AppealsController < Admin::BaseController
authorize @appeal, :approve? authorize @appeal, :approve?
log_action :reject, @appeal log_action :reject, @appeal
@appeal.reject!(current_account) @appeal.reject!(current_account)
UserMailer.appeal_rejected(@appeal.account.user, @appeal) UserMailer.appeal_rejected(@appeal.account.user, @appeal).deliver_later
redirect_to disputes_strike_path(@appeal.strike) redirect_to disputes_strike_path(@appeal.strike)
end end

View File

@ -8,13 +8,15 @@ class UserMailer < Devise::Mailer
helper :instance helper :instance
helper :statuses helper :statuses
helper :formatting helper :formatting
helper :routing
helper RoutingHelper before_action :set_instance
default to: -> { @resource.email }
def confirmation_instructions(user, token, *, **) def confirmation_instructions(user, token, *, **)
@resource = user @resource = user
@token = token @token = token
@instance = Rails.configuration.x.local_domain
return unless @resource.active_for_authentication? return unless @resource.active_for_authentication?
@ -28,185 +30,177 @@ class UserMailer < Devise::Mailer
def reset_password_instructions(user, token, *, **) def reset_password_instructions(user, token, *, **)
@resource = user @resource = user
@token = token @token = token
@instance = Rails.configuration.x.local_domain
return unless @resource.active_for_authentication? return unless @resource.active_for_authentication?
I18n.with_locale(locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('devise.mailer.reset_password_instructions.subject') mail subject: default_devise_subject
end end
end end
def password_change(user, *, **) def password_change(user, *, **)
@resource = user @resource = user
@instance = Rails.configuration.x.local_domain
return unless @resource.active_for_authentication? return unless @resource.active_for_authentication?
I18n.with_locale(locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('devise.mailer.password_change.subject') mail subject: default_devise_subject
end end
end end
def email_changed(user, *, **) def email_changed(user, *, **)
@resource = user @resource = user
@instance = Rails.configuration.x.local_domain
return unless @resource.active_for_authentication? return unless @resource.active_for_authentication?
I18n.with_locale(locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('devise.mailer.email_changed.subject') mail subject: default_devise_subject
end end
end end
def two_factor_enabled(user, *, **) def two_factor_enabled(user, *, **)
@resource = user @resource = user
@instance = Rails.configuration.x.local_domain
return unless @resource.active_for_authentication? return unless @resource.active_for_authentication?
I18n.with_locale(locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('devise.mailer.two_factor_enabled.subject') mail subject: default_devise_subject
end end
end end
def two_factor_disabled(user, *, **) def two_factor_disabled(user, *, **)
@resource = user @resource = user
@instance = Rails.configuration.x.local_domain
return unless @resource.active_for_authentication? return unless @resource.active_for_authentication?
I18n.with_locale(locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('devise.mailer.two_factor_disabled.subject') mail subject: default_devise_subject
end end
end end
def two_factor_recovery_codes_changed(user, *, **) def two_factor_recovery_codes_changed(user, *, **)
@resource = user @resource = user
@instance = Rails.configuration.x.local_domain
return unless @resource.active_for_authentication? return unless @resource.active_for_authentication?
I18n.with_locale(locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('devise.mailer.two_factor_recovery_codes_changed.subject') mail subject: default_devise_subject
end end
end end
def webauthn_enabled(user, *, **) def webauthn_enabled(user, *, **)
@resource = user @resource = user
@instance = Rails.configuration.x.local_domain
return unless @resource.active_for_authentication? return unless @resource.active_for_authentication?
I18n.with_locale(locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('devise.mailer.webauthn_enabled.subject') mail subject: default_devise_subject
end end
end end
def webauthn_disabled(user, *, **) def webauthn_disabled(user, *, **)
@resource = user @resource = user
@instance = Rails.configuration.x.local_domain
return unless @resource.active_for_authentication? return unless @resource.active_for_authentication?
I18n.with_locale(locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('devise.mailer.webauthn_disabled.subject') mail subject: default_devise_subject
end end
end end
def webauthn_credential_added(user, webauthn_credential) def webauthn_credential_added(user, webauthn_credential)
@resource = user @resource = user
@instance = Rails.configuration.x.local_domain
@webauthn_credential = webauthn_credential @webauthn_credential = webauthn_credential
return unless @resource.active_for_authentication? return unless @resource.active_for_authentication?
I18n.with_locale(locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('devise.mailer.webauthn_credential.added.subject') mail subject: I18n.t('devise.mailer.webauthn_credential.added.subject')
end end
end end
def webauthn_credential_deleted(user, webauthn_credential) def webauthn_credential_deleted(user, webauthn_credential)
@resource = user @resource = user
@instance = Rails.configuration.x.local_domain
@webauthn_credential = webauthn_credential @webauthn_credential = webauthn_credential
return unless @resource.active_for_authentication? return unless @resource.active_for_authentication?
I18n.with_locale(locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('devise.mailer.webauthn_credential.deleted.subject') mail subject: I18n.t('devise.mailer.webauthn_credential.deleted.subject')
end end
end end
def welcome(user) def welcome(user)
@resource = user @resource = user
@instance = Rails.configuration.x.local_domain
return unless @resource.active_for_authentication? return unless @resource.active_for_authentication?
I18n.with_locale(locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('user_mailer.welcome.subject') mail subject: default_i18n_subject
end end
end end
def backup_ready(user, backup) def backup_ready(user, backup)
@resource = user @resource = user
@instance = Rails.configuration.x.local_domain
@backup = backup @backup = backup
return unless @resource.active_for_authentication? return unless @resource.active_for_authentication?
I18n.with_locale(locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('user_mailer.backup_ready.subject') mail subject: default_i18n_subject
end end
end end
def warning(user, warning) def warning(user, warning)
@resource = user @resource = user
@warning = warning @warning = warning
@instance = Rails.configuration.x.local_domain
@statuses = @warning.statuses.includes(:account, :preloadable_poll, :media_attachments, active_mentions: [:account]) @statuses = @warning.statuses.includes(:account, :preloadable_poll, :media_attachments, active_mentions: [:account])
I18n.with_locale(locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t("user_mailer.warning.subject.#{@warning.action}", acct: "@#{user.account.local_username_and_domain}") mail subject: I18n.t("user_mailer.warning.subject.#{@warning.action}", acct: "@#{user.account.local_username_and_domain}")
end end
end end
def appeal_approved(user, appeal) def appeal_approved(user, appeal)
@resource = user @resource = user
@instance = Rails.configuration.x.local_domain
@appeal = appeal @appeal = appeal
I18n.with_locale(locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('user_mailer.appeal_approved.subject', date: l(@appeal.created_at)) mail subject: default_i18n_subject(date: l(@appeal.created_at))
end end
end end
def appeal_rejected(user, appeal) def appeal_rejected(user, appeal)
@resource = user @resource = user
@instance = Rails.configuration.x.local_domain
@appeal = appeal @appeal = appeal
I18n.with_locale(locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('user_mailer.appeal_rejected.subject', date: l(@appeal.created_at)) mail subject: default_i18n_subject(date: l(@appeal.created_at))
end end
end end
def suspicious_sign_in(user, remote_ip, user_agent, timestamp) def suspicious_sign_in(user, remote_ip, user_agent, timestamp)
@resource = user @resource = user
@instance = Rails.configuration.x.local_domain
@remote_ip = remote_ip @remote_ip = remote_ip
@user_agent = user_agent @user_agent = user_agent
@detection = Browser.new(user_agent) @detection = Browser.new(user_agent)
@timestamp = timestamp.to_time.utc @timestamp = timestamp.to_time.utc
I18n.with_locale(locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('user_mailer.suspicious_sign_in.subject') mail subject: default_i18n_subject
end end
end end
private private
def default_devise_subject
I18n.t(:subject, scope: ['devise.mailer', action_name])
end
def set_instance
@instance = Rails.configuration.x.local_domain
end
def locale def locale
@resource.locale.presence || I18n.default_locale @resource.locale.presence || I18n.default_locale
end end

View File

@ -64,6 +64,7 @@ ignore_unused:
- 'statuses.attached.*' - 'statuses.attached.*'
- 'move_handler.carry_{mutes,blocks}_over_text' - 'move_handler.carry_{mutes,blocks}_over_text'
- 'admin_mailer.*.subject' - 'admin_mailer.*.subject'
- 'user_mailer.*.subject'
- 'notification_mailer.*' - 'notification_mailer.*'
- 'imports.overwrite_preambles.{following,blocking,muting,domain_blocking,bookmarks,lists}_html' - 'imports.overwrite_preambles.{following,blocking,muting,domain_blocking,bookmarks,lists}_html'
- 'imports.preambles.{following,blocking,muting,domain_blocking,bookmarks,lists}_html' - 'imports.preambles.{following,blocking,muting,domain_blocking,bookmarks,lists}_html'

View File

@ -5,7 +5,7 @@ require 'rails_helper'
describe UserMailer do describe UserMailer do
let(:receiver) { Fabricate(:user) } let(:receiver) { Fabricate(:user) }
describe 'confirmation_instructions' do describe '#confirmation_instructions' do
let(:mail) { described_class.confirmation_instructions(receiver, 'spec') } let(:mail) { described_class.confirmation_instructions(receiver, 'spec') }
it 'renders confirmation instructions' do it 'renders confirmation instructions' do
@ -20,7 +20,7 @@ describe UserMailer do
instance: Rails.configuration.x.local_domain instance: Rails.configuration.x.local_domain
end end
describe 'reconfirmation_instructions' do describe '#reconfirmation_instructions' do
let(:mail) { described_class.confirmation_instructions(receiver, 'spec') } let(:mail) { described_class.confirmation_instructions(receiver, 'spec') }
it 'renders reconfirmation instructions' do it 'renders reconfirmation instructions' do
@ -34,7 +34,7 @@ describe UserMailer do
end end
end end
describe 'reset_password_instructions' do describe '#reset_password_instructions' do
let(:mail) { described_class.reset_password_instructions(receiver, 'spec') } let(:mail) { described_class.reset_password_instructions(receiver, 'spec') }
it 'renders reset password instructions' do it 'renders reset password instructions' do
@ -47,7 +47,7 @@ describe UserMailer do
'devise.mailer.reset_password_instructions.subject' 'devise.mailer.reset_password_instructions.subject'
end end
describe 'password_change' do describe '#password_change' do
let(:mail) { described_class.password_change(receiver) } let(:mail) { described_class.password_change(receiver) }
it 'renders password change notification' do it 'renders password change notification' do
@ -59,7 +59,7 @@ describe UserMailer do
'devise.mailer.password_change.subject' 'devise.mailer.password_change.subject'
end end
describe 'email_changed' do describe '#email_changed' do
let(:mail) { described_class.email_changed(receiver) } let(:mail) { described_class.email_changed(receiver) }
it 'renders email change notification' do it 'renders email change notification' do
@ -71,7 +71,7 @@ describe UserMailer do
'devise.mailer.email_changed.subject' 'devise.mailer.email_changed.subject'
end end
describe 'warning' do describe '#warning' do
let(:strike) { Fabricate(:account_warning, target_account: receiver.account, text: 'dont worry its just the testsuite', action: 'suspend') } let(:strike) { Fabricate(:account_warning, target_account: receiver.account, text: 'dont worry its just the testsuite', action: 'suspend') }
let(:mail) { described_class.warning(receiver, strike) } let(:mail) { described_class.warning(receiver, strike) }
@ -82,7 +82,7 @@ describe UserMailer do
end end
end end
describe 'webauthn_credential_deleted' do describe '#webauthn_credential_deleted' do
let(:credential) { Fabricate(:webauthn_credential, user_id: receiver.id) } let(:credential) { Fabricate(:webauthn_credential, user_id: receiver.id) }
let(:mail) { described_class.webauthn_credential_deleted(receiver, credential) } let(:mail) { described_class.webauthn_credential_deleted(receiver, credential) }
@ -95,7 +95,7 @@ describe UserMailer do
'devise.mailer.webauthn_credential.deleted.subject' 'devise.mailer.webauthn_credential.deleted.subject'
end end
describe 'suspicious_sign_in' do describe '#suspicious_sign_in' do
let(:ip) { '192.168.0.1' } let(:ip) { '192.168.0.1' }
let(:agent) { 'NCSA_Mosaic/2.0 (Windows 3.1)' } let(:agent) { 'NCSA_Mosaic/2.0 (Windows 3.1)' }
let(:timestamp) { Time.now.utc } let(:timestamp) { Time.now.utc }
@ -110,7 +110,7 @@ describe UserMailer do
'user_mailer.suspicious_sign_in.subject' 'user_mailer.suspicious_sign_in.subject'
end end
describe 'appeal_approved' do describe '#appeal_approved' do
let(:appeal) { Fabricate(:appeal, account: receiver.account, approved_at: Time.now.utc) } let(:appeal) { Fabricate(:appeal, account: receiver.account, approved_at: Time.now.utc) }
let(:mail) { described_class.appeal_approved(receiver, appeal) } let(:mail) { described_class.appeal_approved(receiver, appeal) }
@ -120,7 +120,7 @@ describe UserMailer do
end end
end end
describe 'appeal_rejected' do describe '#appeal_rejected' do
let(:appeal) { Fabricate(:appeal, account: receiver.account, rejected_at: Time.now.utc) } let(:appeal) { Fabricate(:appeal, account: receiver.account, rejected_at: Time.now.utc) }
let(:mail) { described_class.appeal_rejected(receiver, appeal) } let(:mail) { described_class.appeal_rejected(receiver, appeal) }
@ -130,7 +130,7 @@ describe UserMailer do
end end
end end
describe 'two_factor_enabled' do describe '#two_factor_enabled' do
let(:mail) { described_class.two_factor_enabled(receiver) } let(:mail) { described_class.two_factor_enabled(receiver) }
it 'renders two_factor_enabled mail' do it 'renders two_factor_enabled mail' do
@ -139,7 +139,7 @@ describe UserMailer do
end end
end end
describe 'two_factor_disabled' do describe '#two_factor_disabled' do
let(:mail) { described_class.two_factor_disabled(receiver) } let(:mail) { described_class.two_factor_disabled(receiver) }
it 'renders two_factor_disabled mail' do it 'renders two_factor_disabled mail' do
@ -148,7 +148,7 @@ describe UserMailer do
end end
end end
describe 'webauthn_enabled' do describe '#webauthn_enabled' do
let(:mail) { described_class.webauthn_enabled(receiver) } let(:mail) { described_class.webauthn_enabled(receiver) }
it 'renders webauthn_enabled mail' do it 'renders webauthn_enabled mail' do
@ -157,7 +157,7 @@ describe UserMailer do
end end
end end
describe 'webauthn_disabled' do describe '#webauthn_disabled' do
let(:mail) { described_class.webauthn_disabled(receiver) } let(:mail) { described_class.webauthn_disabled(receiver) }
it 'renders webauthn_disabled mail' do it 'renders webauthn_disabled mail' do
@ -166,7 +166,7 @@ describe UserMailer do
end end
end end
describe 'two_factor_recovery_codes_changed' do describe '#two_factor_recovery_codes_changed' do
let(:mail) { described_class.two_factor_recovery_codes_changed(receiver) } let(:mail) { described_class.two_factor_recovery_codes_changed(receiver) }
it 'renders two_factor_recovery_codes_changed mail' do it 'renders two_factor_recovery_codes_changed mail' do
@ -175,7 +175,7 @@ describe UserMailer do
end end
end end
describe 'webauthn_credential_added' do describe '#webauthn_credential_added' do
let(:credential) { Fabricate.build(:webauthn_credential) } let(:credential) { Fabricate.build(:webauthn_credential) }
let(:mail) { described_class.webauthn_credential_added(receiver, credential) } let(:mail) { described_class.webauthn_credential_added(receiver, credential) }
@ -184,4 +184,23 @@ describe UserMailer do
expect(mail.body.encoded).to include I18n.t('devise.mailer.webauthn_credential.added.explanation') expect(mail.body.encoded).to include I18n.t('devise.mailer.webauthn_credential.added.explanation')
end end
end end
describe '#welcome' do
let(:mail) { described_class.welcome(receiver) }
it 'renders welcome mail' do
expect(mail.subject).to eq I18n.t('user_mailer.welcome.subject')
expect(mail.body.encoded).to include I18n.t('user_mailer.welcome.explanation')
end
end
describe '#backup_ready' do
let(:backup) { Fabricate(:backup) }
let(:mail) { described_class.backup_ready(receiver, backup) }
it 'renders backup_ready mail' do
expect(mail.subject).to eq I18n.t('user_mailer.backup_ready.subject')
expect(mail.body.encoded).to include I18n.t('user_mailer.backup_ready.explanation')
end
end
end end