|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe BetterTogether::AuthorshipMailer do |
| 6 | + before do |
| 7 | + configure_host_platform |
| 8 | + end |
| 9 | + |
| 10 | + describe '#authorship_changed_notification' do |
| 11 | + let(:page) { create(:better_together_page, title: 'Test Page') } |
| 12 | + let(:recipient) { create(:better_together_person) } |
| 13 | + let(:actor) { create(:better_together_person, name: 'John Doe') } |
| 14 | + |
| 15 | + context 'when author is added with actor name' do |
| 16 | + let(:mail) do |
| 17 | + described_class.with( |
| 18 | + page:, |
| 19 | + recipient:, |
| 20 | + action: 'added', |
| 21 | + actor_name: actor.name |
| 22 | + ).authorship_changed_notification |
| 23 | + end |
| 24 | + |
| 25 | + it 'renders the subject with actor name' do |
| 26 | + expect(mail.subject).to include('Test Page') |
| 27 | + expect(mail.subject).to include('John Doe') |
| 28 | + end |
| 29 | + |
| 30 | + it 'sends to the recipient email' do |
| 31 | + expect(mail.to).to eq([recipient.email]) |
| 32 | + end |
| 33 | + |
| 34 | + it 'includes page title in body' do |
| 35 | + expect(mail.body.encoded).to include('Test Page') |
| 36 | + end |
| 37 | + |
| 38 | + it 'includes actor name in body' do |
| 39 | + expect(mail.body.encoded).to include('John Doe') |
| 40 | + end |
| 41 | + |
| 42 | + it 'is deliverable' do |
| 43 | + expect { mail.deliver_now }.not_to raise_error |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + context 'when author is added without actor name' do |
| 48 | + let(:mail) do |
| 49 | + described_class.with( |
| 50 | + page:, |
| 51 | + recipient:, |
| 52 | + action: 'added' |
| 53 | + ).authorship_changed_notification |
| 54 | + end |
| 55 | + |
| 56 | + it 'renders the subject without actor name' do |
| 57 | + expect(mail.subject).to include('Test Page') |
| 58 | + end |
| 59 | + |
| 60 | + it 'sends to the recipient email' do |
| 61 | + expect(mail.to).to eq([recipient.email]) |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + context 'when author is removed with actor name' do |
| 66 | + let(:mail) do |
| 67 | + described_class.with( |
| 68 | + page:, |
| 69 | + recipient:, |
| 70 | + action: 'removed', |
| 71 | + actor_name: actor.name |
| 72 | + ).authorship_changed_notification |
| 73 | + end |
| 74 | + |
| 75 | + it 'renders the subject for removal with actor' do |
| 76 | + expect(mail.subject).to include('Test Page') |
| 77 | + expect(mail.subject).to include('John Doe') |
| 78 | + end |
| 79 | + |
| 80 | + it 'sends to the recipient email' do |
| 81 | + expect(mail.to).to eq([recipient.email]) |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + context 'when author is removed without actor name' do |
| 86 | + let(:mail) do |
| 87 | + described_class.with( |
| 88 | + page:, |
| 89 | + recipient:, |
| 90 | + action: 'removed' |
| 91 | + ).authorship_changed_notification |
| 92 | + end |
| 93 | + |
| 94 | + it 'renders the subject for removal without actor' do |
| 95 | + expect(mail.subject).to include('Test Page') |
| 96 | + end |
| 97 | + |
| 98 | + it 'sends to the recipient email' do |
| 99 | + expect(mail.to).to eq([recipient.email]) |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + context 'when actor_id is provided instead of actor_name' do |
| 104 | + let(:mail) do |
| 105 | + described_class.with( |
| 106 | + page:, |
| 107 | + recipient:, |
| 108 | + action: 'added', |
| 109 | + actor_id: actor.id |
| 110 | + ).authorship_changed_notification |
| 111 | + end |
| 112 | + |
| 113 | + it 'looks up actor by ID and includes name' do |
| 114 | + expect(mail.subject).to include(actor.name) |
| 115 | + end |
| 116 | + |
| 117 | + it 'includes actor name in body' do |
| 118 | + expect(mail.body.encoded).to include(actor.name) |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | + context 'when actor_id is invalid' do |
| 123 | + let(:mail) do |
| 124 | + described_class.with( |
| 125 | + page:, |
| 126 | + recipient:, |
| 127 | + action: 'added', |
| 128 | + actor_id: 'nonexistent-id' |
| 129 | + ).authorship_changed_notification |
| 130 | + end |
| 131 | + |
| 132 | + it 'handles gracefully without actor name' do |
| 133 | + expect(mail.subject).to include('Test Page') |
| 134 | + end |
| 135 | + end |
| 136 | + |
| 137 | + context 'locale handling' do |
| 138 | + it 'respects recipient locale' do |
| 139 | + recipient.update(locale: 'es') |
| 140 | + mail = described_class.with( |
| 141 | + page:, |
| 142 | + recipient:, |
| 143 | + action: 'added', |
| 144 | + actor_name: actor.name |
| 145 | + ).authorship_changed_notification |
| 146 | + |
| 147 | + expect(mail.body.encoded).to be_present |
| 148 | + end |
| 149 | + end |
| 150 | + |
| 151 | + context 'time zone handling' do |
| 152 | + it 'respects recipient time zone' do |
| 153 | + recipient.update(time_zone: 'America/New_York') |
| 154 | + mail = described_class.with( |
| 155 | + page:, |
| 156 | + recipient:, |
| 157 | + action: 'added', |
| 158 | + actor_name: actor.name |
| 159 | + ).authorship_changed_notification |
| 160 | + |
| 161 | + expect(mail.body.encoded).to be_present |
| 162 | + end |
| 163 | + end |
| 164 | + end |
| 165 | +end |
0 commit comments