user_mailer_spec.rb 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe UserMailer, type: :mailer do
  4. let(:receiver) { Fabricate(:user) }
  5. shared_examples 'localized subject' do |*args, **kwrest|
  6. it 'renders subject localized for the locale of the receiver' do
  7. locale = I18n.available_locales.sample
  8. receiver.update!(locale: locale)
  9. expect(mail.subject).to eq I18n.t(*args, **kwrest.merge(locale: locale))
  10. end
  11. it 'renders subject localized for the default locale if the locale of the receiver is unavailable' do
  12. receiver.update!(locale: nil)
  13. expect(mail.subject).to eq I18n.t(*args, **kwrest.merge(locale: I18n.default_locale))
  14. end
  15. end
  16. describe 'confirmation_instructions' do
  17. let(:mail) { UserMailer.confirmation_instructions(receiver, 'spec') }
  18. it 'renders confirmation instructions' do
  19. receiver.update!(locale: nil)
  20. expect(mail.body.encoded).to include I18n.t('devise.mailer.confirmation_instructions.title')
  21. expect(mail.body.encoded).to include 'spec'
  22. expect(mail.body.encoded).to include Rails.configuration.x.local_domain
  23. end
  24. include_examples 'localized subject',
  25. 'devise.mailer.confirmation_instructions.subject',
  26. instance: Rails.configuration.x.local_domain
  27. end
  28. describe 'reconfirmation_instructions' do
  29. let(:mail) { UserMailer.confirmation_instructions(receiver, 'spec') }
  30. it 'renders reconfirmation instructions' do
  31. receiver.update!(email: 'new-email@example.com', locale: nil)
  32. expect(mail.body.encoded).to include I18n.t('devise.mailer.reconfirmation_instructions.title')
  33. expect(mail.body.encoded).to include 'spec'
  34. expect(mail.body.encoded).to include Rails.configuration.x.local_domain
  35. expect(mail.subject).to eq I18n.t('devise.mailer.reconfirmation_instructions.subject',
  36. instance: Rails.configuration.x.local_domain,
  37. locale: I18n.default_locale)
  38. end
  39. end
  40. describe 'reset_password_instructions' do
  41. let(:mail) { UserMailer.reset_password_instructions(receiver, 'spec') }
  42. it 'renders reset password instructions' do
  43. receiver.update!(locale: nil)
  44. expect(mail.body.encoded).to include I18n.t('devise.mailer.reset_password_instructions.title')
  45. expect(mail.body.encoded).to include 'spec'
  46. end
  47. include_examples 'localized subject',
  48. 'devise.mailer.reset_password_instructions.subject'
  49. end
  50. describe 'password_change' do
  51. let(:mail) { UserMailer.password_change(receiver) }
  52. it 'renders password change notification' do
  53. receiver.update!(locale: nil)
  54. expect(mail.body.encoded).to include I18n.t('devise.mailer.password_change.title')
  55. end
  56. include_examples 'localized subject',
  57. 'devise.mailer.password_change.subject'
  58. end
  59. describe 'email_changed' do
  60. let(:mail) { UserMailer.email_changed(receiver) }
  61. it 'renders email change notification' do
  62. receiver.update!(locale: nil)
  63. expect(mail.body.encoded).to include I18n.t('devise.mailer.email_changed.title')
  64. end
  65. include_examples 'localized subject',
  66. 'devise.mailer.email_changed.subject'
  67. end
  68. describe 'warning' do
  69. let(:strike) { Fabricate(:account_warning, target_account: receiver.account, text: 'dont worry its just the testsuite', action: 'suspend') }
  70. let(:mail) { UserMailer.warning(receiver, strike) }
  71. it 'renders warning notification' do
  72. receiver.update!(locale: nil)
  73. expect(mail.body.encoded).to include I18n.t("user_mailer.warning.title.suspend", acct: receiver.account.acct)
  74. expect(mail.body.encoded).to include strike.text
  75. end
  76. end
  77. end