notification_mailer_spec.rb 4.8 KB

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