mailers.rb 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # frozen_string_literal: true
  2. RSpec.shared_examples 'localized subject' do |*args, **kwrest|
  3. it 'renders subject localized for the locale of the receiver' do
  4. locale = :de
  5. receiver.update!(locale: locale)
  6. expect(mail.subject).to eq I18n.t(*args, **kwrest.merge(locale: locale))
  7. end
  8. it 'renders subject localized for the default locale if the locale of the receiver is unavailable' do
  9. receiver.update!(locale: nil)
  10. expect(mail.subject).to eq I18n.t(*args, **kwrest.merge(locale: I18n.default_locale))
  11. end
  12. end
  13. RSpec::Matchers.define :have_thread_headers do
  14. match(notify_expectation_failures: true) do |mail|
  15. expect(mail)
  16. .to be_present
  17. .and(have_header('In-Reply-To', conversation_header_regex))
  18. .and(have_header('References', conversation_header_regex))
  19. end
  20. def conversation_header_regex = /<conversation-\d+.\d\d\d\d-\d\d-\d\d@cb6e6126.ngrok.io>/
  21. end
  22. RSpec::Matchers.define :have_standard_headers do |type|
  23. chain :for do |user|
  24. @user = user
  25. end
  26. match(notify_expectation_failures: true) do |mail|
  27. expect(mail)
  28. .to be_present
  29. .and(have_header('To', "#{@user.account.username} <#{@user.email}>"))
  30. .and(have_header('List-ID', "<#{type}.#{@user.account.username}.#{Rails.configuration.x.local_domain}>"))
  31. .and(have_header('List-Unsubscribe', %r{<https://#{Rails.configuration.x.local_domain}/unsubscribe\?token=.+>}))
  32. .and(have_header('List-Unsubscribe', /&type=#{type}/))
  33. .and(have_header('List-Unsubscribe-Post', 'List-Unsubscribe=One-Click'))
  34. .and(deliver_to("#{@user.account.username} <#{@user.email}>"))
  35. .and(deliver_from(Rails.configuration.action_mailer.default_options[:from]))
  36. end
  37. end