user_mailer_spec.rb 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.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)
  10. .to be_present
  11. .and(have_body_text(I18n.t('devise.mailer.confirmation_instructions.title')))
  12. .and(have_body_text('spec'))
  13. .and(have_body_text(Rails.configuration.x.local_domain))
  14. end
  15. include_examples 'localized subject',
  16. 'devise.mailer.confirmation_instructions.subject',
  17. instance: Rails.configuration.x.local_domain
  18. end
  19. describe '#reconfirmation_instructions' do
  20. let(:mail) { described_class.confirmation_instructions(receiver, 'spec') }
  21. it 'renders reconfirmation instructions' do
  22. receiver.update!(email: 'new-email@example.com', locale: nil)
  23. expect(mail)
  24. .to be_present
  25. .and(have_body_text(I18n.t('devise.mailer.reconfirmation_instructions.title')))
  26. .and(have_body_text('spec'))
  27. .and(have_body_text(Rails.configuration.x.local_domain))
  28. end
  29. include_examples 'localized subject',
  30. 'devise.mailer.confirmation_instructions.subject',
  31. instance: Rails.configuration.x.local_domain
  32. end
  33. describe '#reset_password_instructions' do
  34. let(:mail) { described_class.reset_password_instructions(receiver, 'spec') }
  35. it 'renders reset password instructions' do
  36. receiver.update!(locale: nil)
  37. expect(mail)
  38. .to be_present
  39. .and(have_body_text(I18n.t('devise.mailer.reset_password_instructions.title')))
  40. .and(have_body_text('spec'))
  41. end
  42. include_examples 'localized subject',
  43. 'devise.mailer.reset_password_instructions.subject'
  44. end
  45. describe '#password_change' do
  46. let(:mail) { described_class.password_change(receiver) }
  47. it 'renders password change notification' do
  48. receiver.update!(locale: nil)
  49. expect(mail)
  50. .to be_present
  51. .and(have_body_text(I18n.t('devise.mailer.password_change.title')))
  52. end
  53. include_examples 'localized subject',
  54. 'devise.mailer.password_change.subject'
  55. end
  56. describe '#email_changed' do
  57. let(:mail) { described_class.email_changed(receiver) }
  58. it 'renders email change notification' do
  59. receiver.update!(locale: nil)
  60. expect(mail)
  61. .to be_present
  62. .and(have_body_text(I18n.t('devise.mailer.email_changed.title')))
  63. end
  64. include_examples 'localized subject',
  65. 'devise.mailer.email_changed.subject'
  66. end
  67. describe '#warning' do
  68. let(:strike) { Fabricate(:account_warning, target_account: receiver.account, text: 'dont worry its just the testsuite', action: 'suspend') }
  69. let(:mail) { described_class.warning(receiver, strike) }
  70. it 'renders warning notification' do
  71. receiver.update!(locale: nil)
  72. expect(mail)
  73. .to be_present
  74. .and(have_body_text(I18n.t('user_mailer.warning.title.suspend', acct: receiver.account.acct)))
  75. .and(have_body_text(strike.text))
  76. end
  77. end
  78. describe '#webauthn_credential_deleted' do
  79. let(:credential) { Fabricate(:webauthn_credential, user_id: receiver.id) }
  80. let(:mail) { described_class.webauthn_credential_deleted(receiver, credential) }
  81. it 'renders webauthn credential deleted notification' do
  82. receiver.update!(locale: nil)
  83. expect(mail)
  84. .to be_present
  85. .and(have_body_text(I18n.t('devise.mailer.webauthn_credential.deleted.title')))
  86. end
  87. include_examples 'localized subject',
  88. 'devise.mailer.webauthn_credential.deleted.subject'
  89. end
  90. describe '#suspicious_sign_in' do
  91. let(:ip) { '192.168.0.1' }
  92. let(:agent) { 'NCSA_Mosaic/2.0 (Windows 3.1)' }
  93. let(:timestamp) { Time.now.utc }
  94. let(:mail) { described_class.suspicious_sign_in(receiver, ip, agent, timestamp) }
  95. it 'renders suspicious sign in notification' do
  96. receiver.update!(locale: nil)
  97. expect(mail)
  98. .to be_present
  99. .and(have_body_text(I18n.t('user_mailer.suspicious_sign_in.explanation')))
  100. end
  101. include_examples 'localized subject',
  102. 'user_mailer.suspicious_sign_in.subject'
  103. end
  104. describe '#failed_2fa' do
  105. let(:ip) { '192.168.0.1' }
  106. let(:agent) { 'NCSA_Mosaic/2.0 (Windows 3.1)' }
  107. let(:timestamp) { Time.now.utc }
  108. let(:mail) { described_class.failed_2fa(receiver, ip, agent, timestamp) }
  109. it 'renders failed 2FA notification' do
  110. receiver.update!(locale: nil)
  111. expect(mail)
  112. .to be_present
  113. .and(have_body_text(I18n.t('user_mailer.failed_2fa.explanation')))
  114. end
  115. include_examples 'localized subject',
  116. 'user_mailer.failed_2fa.subject'
  117. end
  118. describe '#appeal_approved' do
  119. let(:appeal) { Fabricate(:appeal, account: receiver.account, approved_at: Time.now.utc) }
  120. let(:mail) { described_class.appeal_approved(receiver, appeal) }
  121. it 'renders appeal_approved notification' do
  122. expect(mail)
  123. .to be_present
  124. .and(have_subject(I18n.t('user_mailer.appeal_approved.subject', date: I18n.l(appeal.created_at))))
  125. .and(have_body_text(I18n.t('user_mailer.appeal_approved.title')))
  126. end
  127. end
  128. describe '#appeal_rejected' do
  129. let(:appeal) { Fabricate(:appeal, account: receiver.account, rejected_at: Time.now.utc) }
  130. let(:mail) { described_class.appeal_rejected(receiver, appeal) }
  131. it 'renders appeal_rejected notification' do
  132. expect(mail)
  133. .to be_present
  134. .and(have_subject(I18n.t('user_mailer.appeal_rejected.subject', date: I18n.l(appeal.created_at))))
  135. .and(have_body_text(I18n.t('user_mailer.appeal_rejected.title')))
  136. end
  137. end
  138. describe '#two_factor_enabled' do
  139. let(:mail) { described_class.two_factor_enabled(receiver) }
  140. it 'renders two_factor_enabled mail' do
  141. expect(mail)
  142. .to be_present
  143. .and(have_subject(I18n.t('devise.mailer.two_factor_enabled.subject')))
  144. .and(have_body_text(I18n.t('devise.mailer.two_factor_enabled.explanation')))
  145. end
  146. end
  147. describe '#two_factor_disabled' do
  148. let(:mail) { described_class.two_factor_disabled(receiver) }
  149. it 'renders two_factor_disabled mail' do
  150. expect(mail)
  151. .to be_present
  152. .and(have_subject(I18n.t('devise.mailer.two_factor_disabled.subject')))
  153. .and(have_body_text(I18n.t('devise.mailer.two_factor_disabled.explanation')))
  154. end
  155. end
  156. describe '#webauthn_enabled' do
  157. let(:mail) { described_class.webauthn_enabled(receiver) }
  158. it 'renders webauthn_enabled mail' do
  159. expect(mail)
  160. .to be_present
  161. .and(have_subject(I18n.t('devise.mailer.webauthn_enabled.subject')))
  162. .and(have_body_text(I18n.t('devise.mailer.webauthn_enabled.explanation')))
  163. end
  164. end
  165. describe '#webauthn_disabled' do
  166. let(:mail) { described_class.webauthn_disabled(receiver) }
  167. it 'renders webauthn_disabled mail' do
  168. expect(mail)
  169. .to be_present
  170. .and(have_subject(I18n.t('devise.mailer.webauthn_disabled.subject')))
  171. .and(have_body_text(I18n.t('devise.mailer.webauthn_disabled.explanation')))
  172. end
  173. end
  174. describe '#two_factor_recovery_codes_changed' do
  175. let(:mail) { described_class.two_factor_recovery_codes_changed(receiver) }
  176. it 'renders two_factor_recovery_codes_changed mail' do
  177. expect(mail)
  178. .to be_present
  179. .and(have_subject(I18n.t('devise.mailer.two_factor_recovery_codes_changed.subject')))
  180. .and(have_body_text(I18n.t('devise.mailer.two_factor_recovery_codes_changed.explanation')))
  181. end
  182. end
  183. describe '#webauthn_credential_added' do
  184. let(:credential) { Fabricate.build(:webauthn_credential) }
  185. let(:mail) { described_class.webauthn_credential_added(receiver, credential) }
  186. it 'renders webauthn_credential_added mail' do
  187. expect(mail)
  188. .to be_present
  189. .and(have_subject(I18n.t('devise.mailer.webauthn_credential.added.subject')))
  190. .and(have_body_text(I18n.t('devise.mailer.webauthn_credential.added.explanation')))
  191. end
  192. end
  193. describe '#welcome' do
  194. let(:mail) { described_class.welcome(receiver) }
  195. before do
  196. # This is a bit hacky and low-level but this allows stubbing trending tags
  197. tag_ids = Fabricate.times(5, :tag).pluck(:id)
  198. allow(Trends.tags).to receive(:query).and_return(instance_double(Trends::Query, allowed: Tag.where(id: tag_ids)))
  199. end
  200. it 'renders welcome mail' do
  201. expect(mail)
  202. .to be_present
  203. .and(have_subject(I18n.t('user_mailer.welcome.subject')))
  204. .and(have_body_text(I18n.t('user_mailer.welcome.explanation')))
  205. end
  206. end
  207. describe '#backup_ready' do
  208. let(:backup) { Fabricate(:backup) }
  209. let(:mail) { described_class.backup_ready(receiver, backup) }
  210. it 'renders backup_ready mail' do
  211. expect(mail)
  212. .to be_present
  213. .and(have_subject(I18n.t('user_mailer.backup_ready.subject')))
  214. .and(have_body_text(I18n.t('user_mailer.backup_ready.explanation')))
  215. end
  216. end
  217. end