user_mailer_spec.rb 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe UserMailer do
  4. let(:receiver) { Fabricate(:user) }
  5. describe '#confirmation_instructions' do
  6. let(:mail) { described_class.confirmation_instructions(receiver, 'spec') }
  7. it 'renders confirmation instructions' do
  8. receiver.update!(locale: nil)
  9. expect(mail.body.encoded).to include I18n.t('devise.mailer.confirmation_instructions.title')
  10. expect(mail.body.encoded).to include 'spec'
  11. expect(mail.body.encoded).to include Rails.configuration.x.local_domain
  12. end
  13. include_examples 'localized subject',
  14. 'devise.mailer.confirmation_instructions.subject',
  15. instance: Rails.configuration.x.local_domain
  16. end
  17. describe '#reconfirmation_instructions' do
  18. let(:mail) { described_class.confirmation_instructions(receiver, 'spec') }
  19. it 'renders reconfirmation instructions' do
  20. receiver.update!(email: 'new-email@example.com', locale: nil)
  21. expect(mail.body.encoded).to include I18n.t('devise.mailer.reconfirmation_instructions.title')
  22. expect(mail.body.encoded).to include 'spec'
  23. expect(mail.body.encoded).to include Rails.configuration.x.local_domain
  24. expect(mail.subject).to eq I18n.t('devise.mailer.reconfirmation_instructions.subject',
  25. instance: Rails.configuration.x.local_domain,
  26. locale: I18n.default_locale)
  27. end
  28. end
  29. describe '#reset_password_instructions' do
  30. let(:mail) { described_class.reset_password_instructions(receiver, 'spec') }
  31. it 'renders reset password instructions' do
  32. receiver.update!(locale: nil)
  33. expect(mail.body.encoded).to include I18n.t('devise.mailer.reset_password_instructions.title')
  34. expect(mail.body.encoded).to include 'spec'
  35. end
  36. include_examples 'localized subject',
  37. 'devise.mailer.reset_password_instructions.subject'
  38. end
  39. describe '#password_change' do
  40. let(:mail) { described_class.password_change(receiver) }
  41. it 'renders password change notification' do
  42. receiver.update!(locale: nil)
  43. expect(mail.body.encoded).to include I18n.t('devise.mailer.password_change.title')
  44. end
  45. include_examples 'localized subject',
  46. 'devise.mailer.password_change.subject'
  47. end
  48. describe '#email_changed' do
  49. let(:mail) { described_class.email_changed(receiver) }
  50. it 'renders email change notification' do
  51. receiver.update!(locale: nil)
  52. expect(mail.body.encoded).to include I18n.t('devise.mailer.email_changed.title')
  53. end
  54. include_examples 'localized subject',
  55. 'devise.mailer.email_changed.subject'
  56. end
  57. describe '#warning' do
  58. let(:strike) { Fabricate(:account_warning, target_account: receiver.account, text: 'dont worry its just the testsuite', action: 'suspend') }
  59. let(:mail) { described_class.warning(receiver, strike) }
  60. it 'renders warning notification' do
  61. receiver.update!(locale: nil)
  62. expect(mail.body.encoded).to include I18n.t('user_mailer.warning.title.suspend', acct: receiver.account.acct)
  63. expect(mail.body.encoded).to include strike.text
  64. end
  65. end
  66. describe '#webauthn_credential_deleted' do
  67. let(:credential) { Fabricate(:webauthn_credential, user_id: receiver.id) }
  68. let(:mail) { described_class.webauthn_credential_deleted(receiver, credential) }
  69. it 'renders webauthn credential deleted notification' do
  70. receiver.update!(locale: nil)
  71. expect(mail.body.encoded).to include I18n.t('devise.mailer.webauthn_credential.deleted.title')
  72. end
  73. include_examples 'localized subject',
  74. 'devise.mailer.webauthn_credential.deleted.subject'
  75. end
  76. describe '#suspicious_sign_in' do
  77. let(:ip) { '192.168.0.1' }
  78. let(:agent) { 'NCSA_Mosaic/2.0 (Windows 3.1)' }
  79. let(:timestamp) { Time.now.utc }
  80. let(:mail) { described_class.suspicious_sign_in(receiver, ip, agent, timestamp) }
  81. it 'renders suspicious sign in notification' do
  82. receiver.update!(locale: nil)
  83. expect(mail.body.encoded).to include I18n.t('user_mailer.suspicious_sign_in.explanation')
  84. end
  85. include_examples 'localized subject',
  86. 'user_mailer.suspicious_sign_in.subject'
  87. end
  88. describe '#appeal_approved' do
  89. let(:appeal) { Fabricate(:appeal, account: receiver.account, approved_at: Time.now.utc) }
  90. let(:mail) { described_class.appeal_approved(receiver, appeal) }
  91. it 'renders appeal_approved notification' do
  92. expect(mail.subject).to eq I18n.t('user_mailer.appeal_approved.subject', date: I18n.l(appeal.created_at))
  93. expect(mail.body.encoded).to include I18n.t('user_mailer.appeal_approved.title')
  94. end
  95. end
  96. describe '#appeal_rejected' do
  97. let(:appeal) { Fabricate(:appeal, account: receiver.account, rejected_at: Time.now.utc) }
  98. let(:mail) { described_class.appeal_rejected(receiver, appeal) }
  99. it 'renders appeal_rejected notification' do
  100. expect(mail.subject).to eq I18n.t('user_mailer.appeal_rejected.subject', date: I18n.l(appeal.created_at))
  101. expect(mail.body.encoded).to include I18n.t('user_mailer.appeal_rejected.title')
  102. end
  103. end
  104. describe '#two_factor_enabled' do
  105. let(:mail) { described_class.two_factor_enabled(receiver) }
  106. it 'renders two_factor_enabled mail' do
  107. expect(mail.subject).to eq I18n.t('devise.mailer.two_factor_enabled.subject')
  108. expect(mail.body.encoded).to include I18n.t('devise.mailer.two_factor_enabled.explanation')
  109. end
  110. end
  111. describe '#two_factor_disabled' do
  112. let(:mail) { described_class.two_factor_disabled(receiver) }
  113. it 'renders two_factor_disabled mail' do
  114. expect(mail.subject).to eq I18n.t('devise.mailer.two_factor_disabled.subject')
  115. expect(mail.body.encoded).to include I18n.t('devise.mailer.two_factor_disabled.explanation')
  116. end
  117. end
  118. describe '#webauthn_enabled' do
  119. let(:mail) { described_class.webauthn_enabled(receiver) }
  120. it 'renders webauthn_enabled mail' do
  121. expect(mail.subject).to eq I18n.t('devise.mailer.webauthn_enabled.subject')
  122. expect(mail.body.encoded).to include I18n.t('devise.mailer.webauthn_enabled.explanation')
  123. end
  124. end
  125. describe '#webauthn_disabled' do
  126. let(:mail) { described_class.webauthn_disabled(receiver) }
  127. it 'renders webauthn_disabled mail' do
  128. expect(mail.subject).to eq I18n.t('devise.mailer.webauthn_disabled.subject')
  129. expect(mail.body.encoded).to include I18n.t('devise.mailer.webauthn_disabled.explanation')
  130. end
  131. end
  132. describe '#two_factor_recovery_codes_changed' do
  133. let(:mail) { described_class.two_factor_recovery_codes_changed(receiver) }
  134. it 'renders two_factor_recovery_codes_changed mail' do
  135. expect(mail.subject).to eq I18n.t('devise.mailer.two_factor_recovery_codes_changed.subject')
  136. expect(mail.body.encoded).to include I18n.t('devise.mailer.two_factor_recovery_codes_changed.explanation')
  137. end
  138. end
  139. describe '#webauthn_credential_added' do
  140. let(:credential) { Fabricate.build(:webauthn_credential) }
  141. let(:mail) { described_class.webauthn_credential_added(receiver, credential) }
  142. it 'renders webauthn_credential_added mail' do
  143. expect(mail.subject).to eq I18n.t('devise.mailer.webauthn_credential.added.subject')
  144. expect(mail.body.encoded).to include I18n.t('devise.mailer.webauthn_credential.added.explanation')
  145. end
  146. end
  147. describe '#welcome' do
  148. let(:mail) { described_class.welcome(receiver) }
  149. it 'renders welcome mail' do
  150. expect(mail.subject).to eq I18n.t('user_mailer.welcome.subject')
  151. expect(mail.body.encoded).to include I18n.t('user_mailer.welcome.explanation')
  152. end
  153. end
  154. describe '#backup_ready' do
  155. let(:backup) { Fabricate(:backup) }
  156. let(:mail) { described_class.backup_ready(receiver, backup) }
  157. it 'renders backup_ready mail' do
  158. expect(mail.subject).to eq I18n.t('user_mailer.backup_ready.subject')
  159. expect(mail.body.encoded).to include I18n.t('user_mailer.backup_ready.explanation')
  160. end
  161. end
  162. end