notification_mailer_spec.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe NotificationMailer do
  4. shared_examples 'delivery to non functional user' do
  5. context 'when user is not functional' do
  6. before { receiver.update(confirmed_at: nil) }
  7. it 'does not deliver mail' do
  8. emails = capture_emails { mail.deliver_now }
  9. expect(emails).to be_empty
  10. end
  11. end
  12. end
  13. shared_examples 'delivery without status' do
  14. context 'when notification target_status is missing' do
  15. before { allow(notification).to receive(:target_status).and_return(nil) }
  16. it 'does not deliver mail' do
  17. emails = capture_emails { mail.deliver_now }
  18. expect(emails).to be_empty
  19. end
  20. end
  21. end
  22. let(:receiver) { Fabricate(:user, account_attributes: { username: 'alice' }) }
  23. let(:sender) { Fabricate(:account, username: 'bob') }
  24. let(:foreign_status) { Fabricate(:status, account: sender, text: 'The body of the foreign status') }
  25. let(:own_status) { Fabricate(:status, account: receiver.account, text: 'The body of the own status') }
  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. it 'renders the email' do
  32. expect(mail)
  33. .to be_present
  34. .and(have_subject('You were mentioned by bob'))
  35. .and(have_body_text('You were mentioned by bob'))
  36. .and(have_body_text('The body of the foreign status'))
  37. .and have_thread_headers
  38. .and have_standard_headers('mention').for(receiver)
  39. end
  40. include_examples 'delivery to non functional user'
  41. include_examples 'delivery without status'
  42. end
  43. describe 'follow' do
  44. let(:follow) { sender.follow!(receiver.account) }
  45. let(:notification) { Notification.create!(account: receiver.account, activity: follow) }
  46. let(:mail) { prepared_mailer_for(receiver.account).follow }
  47. include_examples 'localized subject', 'notification_mailer.follow.subject', name: 'bob'
  48. it 'renders the email' do
  49. expect(mail)
  50. .to be_present
  51. .and(have_subject('bob is now following you'))
  52. .and(have_body_text('bob is now following you'))
  53. .and have_standard_headers('follow').for(receiver)
  54. end
  55. include_examples 'delivery to non functional user'
  56. end
  57. describe 'favourite' do
  58. let(:favourite) { Favourite.create!(account: sender, status: own_status) }
  59. let(:notification) { Notification.create!(account: receiver.account, activity: favourite) }
  60. let(:mail) { prepared_mailer_for(own_status.account).favourite }
  61. include_examples 'localized subject', 'notification_mailer.favourite.subject', name: 'bob'
  62. it 'renders the email' do
  63. expect(mail)
  64. .to be_present
  65. .and(have_subject('bob favorited your post'))
  66. .and(have_body_text('Your post was favorited by bob'))
  67. .and(have_body_text('The body of the own status'))
  68. .and have_thread_headers
  69. .and have_standard_headers('favourite').for(receiver)
  70. end
  71. include_examples 'delivery to non functional user'
  72. include_examples 'delivery without status'
  73. end
  74. describe 'reblog' do
  75. let(:reblog) { Status.create!(account: sender, reblog: own_status) }
  76. let(:notification) { Notification.create!(account: receiver.account, activity: reblog) }
  77. let(:mail) { prepared_mailer_for(own_status.account).reblog }
  78. include_examples 'localized subject', 'notification_mailer.reblog.subject', name: 'bob'
  79. it 'renders the email' do
  80. expect(mail)
  81. .to be_present
  82. .and(have_subject('bob boosted your post'))
  83. .and(have_body_text('Your post was boosted by bob'))
  84. .and(have_body_text('The body of the own status'))
  85. .and have_thread_headers
  86. .and have_standard_headers('reblog').for(receiver)
  87. end
  88. include_examples 'delivery to non functional user'
  89. include_examples 'delivery without status'
  90. end
  91. describe 'follow_request' do
  92. let(:follow_request) { Fabricate(:follow_request, account: sender, target_account: receiver.account) }
  93. let(:notification) { Notification.create!(account: receiver.account, activity: follow_request) }
  94. let(:mail) { prepared_mailer_for(receiver.account).follow_request }
  95. include_examples 'localized subject', 'notification_mailer.follow_request.subject', name: 'bob'
  96. it 'renders the email' do
  97. expect(mail)
  98. .to be_present
  99. .and(have_subject('Pending follower: bob'))
  100. .and(have_body_text('bob has requested to follow you'))
  101. .and have_standard_headers('follow_request').for(receiver)
  102. end
  103. include_examples 'delivery to non functional user'
  104. end
  105. private
  106. def prepared_mailer_for(recipient)
  107. described_class.with(recipient: recipient, notification: notification)
  108. end
  109. end