You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
5.3 KiB

  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 = %i(de en).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 receiver last signed in' do
  101. receiver.update!(last_emailed_at: nil, current_sign_in_at: '2000-03-01T00:00:00Z')
  102. mail = NotificationMailer.digest(receiver.account)
  103. expect(mail.body.encoded).to include 'Mar 01, 2000, 00:00'
  104. end
  105. end
  106. end