notification_mailer_spec.rb 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. require "rails_helper"
  2. RSpec.describe NotificationMailer, type: :mailer do
  3. let(:receiver) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) }
  4. let(:sender) { Fabricate(:account, username: 'bob') }
  5. let(:foreign_status) { Fabricate(:status, account: sender, text: 'The body of the foreign status') }
  6. let(:own_status) { Fabricate(:status, account: receiver.account, text: 'The body of the own status') }
  7. shared_examples 'localized subject' do |*args, **kwrest|
  8. it 'renders subject localized for the locale of the receiver' do
  9. locale = I18n.available_locales.sample
  10. receiver.update!(locale: locale)
  11. expect(mail.subject).to eq I18n.t(*args, kwrest.merge(locale: locale))
  12. end
  13. it 'renders subject localized for the default locale if the locale of the receiver is unavailable' do
  14. receiver.update!(locale: nil)
  15. expect(mail.subject).to eq I18n.t(*args, kwrest.merge(locale: I18n.default_locale))
  16. end
  17. end
  18. describe "mention" do
  19. let(:mention) { Mention.create!(account: receiver.account, status: foreign_status) }
  20. let(:mail) { NotificationMailer.mention(receiver.account, Notification.create!(account: receiver.account, activity: mention)) }
  21. include_examples 'localized subject', 'notification_mailer.mention.subject', name: 'bob'
  22. it "renders the headers" do
  23. expect(mail.subject).to eq("You were mentioned by bob")
  24. expect(mail.to).to eq([receiver.email])
  25. end
  26. it "renders the body" do
  27. expect(mail.body.encoded).to match("You were mentioned by bob")
  28. expect(mail.body.encoded).to include 'The body of the foreign status'
  29. end
  30. end
  31. describe "follow" do
  32. let(:follow) { sender.follow!(receiver.account) }
  33. let(:mail) { NotificationMailer.follow(receiver.account, Notification.create!(account: receiver.account, activity: follow)) }
  34. include_examples 'localized subject', 'notification_mailer.follow.subject', name: 'bob'
  35. it "renders the headers" do
  36. expect(mail.subject).to eq("bob is now following you")
  37. expect(mail.to).to eq([receiver.email])
  38. end
  39. it "renders the body" do
  40. expect(mail.body.encoded).to match("bob is now following you")
  41. end
  42. end
  43. describe "favourite" do
  44. let(:favourite) { Favourite.create!(account: sender, status: own_status) }
  45. let(:mail) { NotificationMailer.favourite(own_status.account, Notification.create!(account: receiver.account, activity: favourite)) }
  46. include_examples 'localized subject', 'notification_mailer.favourite.subject', name: 'bob'
  47. it "renders the headers" do
  48. expect(mail.subject).to eq("bob favourited your status")
  49. expect(mail.to).to eq([receiver.email])
  50. end
  51. it "renders the body" do
  52. expect(mail.body.encoded).to match("Your status was favourited by bob")
  53. expect(mail.body.encoded).to include 'The body of the own status'
  54. end
  55. end
  56. describe "reblog" do
  57. let(:reblog) { Status.create!(account: sender, reblog: own_status) }
  58. let(:mail) { NotificationMailer.reblog(own_status.account, Notification.create!(account: receiver.account, activity: reblog)) }
  59. include_examples 'localized subject', 'notification_mailer.reblog.subject', name: 'bob'
  60. it "renders the headers" do
  61. expect(mail.subject).to eq("bob boosted your status")
  62. expect(mail.to).to eq([receiver.email])
  63. end
  64. it "renders the body" do
  65. expect(mail.body.encoded).to match("Your status was boosted by bob")
  66. expect(mail.body.encoded).to include 'The body of the own status'
  67. end
  68. end
  69. describe 'follow_request' do
  70. let(:follow_request) { Fabricate(:follow_request, account: sender, target_account: receiver.account) }
  71. let(:mail) { NotificationMailer.follow_request(receiver.account, Notification.create!(account: receiver.account, activity: follow_request)) }
  72. include_examples 'localized subject', 'notification_mailer.follow_request.subject', name: 'bob'
  73. it 'renders the headers' do
  74. expect(mail.subject).to eq('Pending follower: bob')
  75. expect(mail.to).to eq([receiver.email])
  76. end
  77. it 'renders the body' do
  78. expect(mail.body.encoded).to match("bob has requested to follow you")
  79. end
  80. end
  81. describe 'digest' do
  82. before do
  83. mention = Fabricate(:mention, account: receiver.account, status: foreign_status)
  84. Fabricate(:notification, account: receiver.account, activity: mention)
  85. sender.follow!(receiver.account)
  86. end
  87. context do
  88. let!(:mail) { NotificationMailer.digest(receiver.account, since: 5.days.ago) }
  89. include_examples 'localized subject', 'notification_mailer.digest.subject', count: 1, name: 'bob'
  90. it 'renders the headers' do
  91. expect(mail.subject).to match('notification since your last')
  92. expect(mail.to).to eq([receiver.email])
  93. end
  94. it 'renders the body' do
  95. expect(mail.body.encoded).to match('brief summary')
  96. expect(mail.body.encoded).to include 'The body of the foreign status'
  97. expect(mail.body.encoded).to include sender.username
  98. end
  99. end
  100. it 'includes activities since the date specified by :since option' do
  101. receiver.update!(last_emailed_at: '2000-02-01T00:00:00Z', current_sign_in_at: '2000-03-01T00:00:00Z')
  102. mail = NotificationMailer.digest(receiver.account, since: Time.parse('2000-01-01T00:00:00Z'))
  103. expect(mail.body.encoded).to include 'Jan 01, 2000, 00:00'
  104. end
  105. it 'includes activities since the receiver was last emailed if :since option is unavailable' do
  106. receiver.update!(last_emailed_at: '2000-02-01T00:00:00Z', current_sign_in_at: '2000-03-01T00:00:00Z')
  107. mail = NotificationMailer.digest(receiver.account)
  108. expect(mail.body.encoded).to include 'Feb 01, 2000, 00:00'
  109. end
  110. it 'includes activities since the receiver last signed in if :since option and the last emailed date are unavailable' do
  111. receiver.update!(last_emailed_at: nil, current_sign_in_at: '2000-03-01T00:00:00Z')
  112. mail = NotificationMailer.digest(receiver.account)
  113. expect(mail.body.encoded).to include 'Mar 01, 2000, 00:00'
  114. end
  115. end
  116. end