notification_mailer_spec.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe NotificationMailer do
  4. let(:receiver) { Fabricate(:user, account_attributes: { username: 'alice' }) }
  5. let(:sender) { Fabricate(:account, username: 'bob') }
  6. let(:foreign_status) { Fabricate(:status, account: sender, text: 'The body of the foreign status') }
  7. let(:own_status) { Fabricate(:status, account: receiver.account, text: 'The body of the own status') }
  8. shared_examples 'standard headers' do |type|
  9. it 'renders the email' do
  10. expect(mail)
  11. .to be_present
  12. .and(have_header('To', "#{receiver.account.username} <#{receiver.email}>"))
  13. .and(have_header('List-ID', "<#{type}.alice.cb6e6126.ngrok.io>"))
  14. .and(have_header('List-Unsubscribe', %r{<https://cb6e6126.ngrok.io/unsubscribe\?token=.+>}))
  15. .and(have_header('List-Unsubscribe', /&type=#{type}/))
  16. .and(have_header('List-Unsubscribe-Post', 'List-Unsubscribe=One-Click'))
  17. .and(deliver_to("#{receiver.account.username} <#{receiver.email}>"))
  18. .and(deliver_from('notifications@localhost'))
  19. end
  20. end
  21. shared_examples 'thread headers' do
  22. it 'renders the email with conversation thread headers' do
  23. conversation_header_regex = /<conversation-\d+.\d\d\d\d-\d\d-\d\d@cb6e6126.ngrok.io>/
  24. expect(mail)
  25. .to be_present
  26. .and(have_header('In-Reply-To', conversation_header_regex))
  27. .and(have_header('References', conversation_header_regex))
  28. end
  29. end
  30. describe 'mention' do
  31. let(:mention) { Mention.create!(account: receiver.account, status: foreign_status) }
  32. let(:notification) { Notification.create!(account: receiver.account, activity: mention) }
  33. let(:mail) { prepared_mailer_for(receiver.account).mention }
  34. include_examples 'localized subject', 'notification_mailer.mention.subject', name: 'bob'
  35. include_examples 'standard headers', 'mention'
  36. include_examples 'thread headers'
  37. it 'renders the email' do
  38. expect(mail)
  39. .to be_present
  40. .and(have_subject('You were mentioned by bob'))
  41. .and(have_body_text('You were mentioned by bob'))
  42. .and(have_body_text('The body of the foreign status'))
  43. end
  44. end
  45. describe 'follow' do
  46. let(:follow) { sender.follow!(receiver.account) }
  47. let(:notification) { Notification.create!(account: receiver.account, activity: follow) }
  48. let(:mail) { prepared_mailer_for(receiver.account).follow }
  49. include_examples 'localized subject', 'notification_mailer.follow.subject', name: 'bob'
  50. include_examples 'standard headers', 'follow'
  51. it 'renders the email' do
  52. expect(mail)
  53. .to be_present
  54. .and(have_subject('bob is now following you'))
  55. .and(have_body_text('bob is now following you'))
  56. end
  57. end
  58. describe 'favourite' do
  59. let(:favourite) { Favourite.create!(account: sender, status: own_status) }
  60. let(:notification) { Notification.create!(account: receiver.account, activity: favourite) }
  61. let(:mail) { prepared_mailer_for(own_status.account).favourite }
  62. include_examples 'localized subject', 'notification_mailer.favourite.subject', name: 'bob'
  63. include_examples 'standard headers', 'favourite'
  64. include_examples 'thread headers'
  65. it 'renders the email' do
  66. expect(mail)
  67. .to be_present
  68. .and(have_subject('bob favorited your post'))
  69. .and(have_body_text('Your post was favorited by bob'))
  70. .and(have_body_text('The body of the own status'))
  71. end
  72. end
  73. describe 'reblog' do
  74. let(:reblog) { Status.create!(account: sender, reblog: own_status) }
  75. let(:notification) { Notification.create!(account: receiver.account, activity: reblog) }
  76. let(:mail) { prepared_mailer_for(own_status.account).reblog }
  77. include_examples 'localized subject', 'notification_mailer.reblog.subject', name: 'bob'
  78. include_examples 'standard headers', 'reblog'
  79. include_examples 'thread headers'
  80. it 'renders the email' do
  81. expect(mail)
  82. .to be_present
  83. .and(have_subject('bob boosted your post'))
  84. .and(have_body_text('Your post was boosted by bob'))
  85. .and(have_body_text('The body of the own status'))
  86. end
  87. end
  88. describe 'follow_request' do
  89. let(:follow_request) { Fabricate(:follow_request, account: sender, target_account: receiver.account) }
  90. let(:notification) { Notification.create!(account: receiver.account, activity: follow_request) }
  91. let(:mail) { prepared_mailer_for(receiver.account).follow_request }
  92. include_examples 'localized subject', 'notification_mailer.follow_request.subject', name: 'bob'
  93. include_examples 'standard headers', 'follow_request'
  94. it 'renders the email' do
  95. expect(mail)
  96. .to be_present
  97. .and(have_subject('Pending follower: bob'))
  98. .and(have_body_text('bob has requested to follow you'))
  99. end
  100. end
  101. private
  102. def prepared_mailer_for(recipient)
  103. described_class.with(recipient: recipient, notification: notification)
  104. end
  105. end