notification_mailer_spec.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. require "rails_helper"
  2. RSpec.describe NotificationMailer, type: :mailer do
  3. let(:receiver) { Fabricate(:user) }
  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 post")
  49. expect(mail.to).to eq([receiver.email])
  50. end
  51. it "renders the body" do
  52. expect(mail.body.encoded).to match("Your post 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 post")
  62. expect(mail.to).to eq([receiver.email])
  63. end
  64. it "renders the body" do
  65. expect(mail.body.encoded).to match("Your post 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. end