Skip to content

Commit

Permalink
Avoid infinite loop in postmark (#1944)
Browse files Browse the repository at this point in the history
if we try to send a message to an admin notifying them that a
contributor is inactive, but that admin has the same email as the
contributor who has unsubscribed, it gets stuck in an infinite loop

Fixes #1941
  • Loading branch information
mattwr18 authored Jul 23, 2024
1 parent a1ed5a0 commit ee6aa1c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/adapters/postmark_adapter/outbound.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def send_user_count_exceeds_plan_limit_message!(admin, organization)
end

def contributor_marked_as_inactive!(admin, contributor)
return unless admin&.email && admin&.admin? && contributor&.id
return unless admin&.email && admin&.admin? && contributor&.id && admin.email != contributor.email

with(admin: admin, contributor: contributor).contributor_marked_as_inactive_email.deliver_later
end
Expand Down
57 changes: 57 additions & 0 deletions spec/adapters/postmark_adapter/outbound_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,5 +288,62 @@
end
end
end

describe '::contributor_marked_as_inactive!' do
subject { described_class.contributor_marked_as_inactive!(admin, contributor) }

context 'no admin' do
let(:admin) { nil }
let(:contributor) { create(:contributor) }

it 'does not enqueue a Mailer' do
expect { subject }.not_to have_enqueued_job
end
end

context 'no contributor' do
let(:admin) { build(:user, admin: true) }
let(:contributor) { nil }

it 'does not enqueue a Mailer' do
expect { subject }.not_to have_enqueued_job
end
end

context 'user without an admin role' do
let(:admin) { build(:user) }
let(:contributor) { create(:contributor) }

it 'does not enqueue a Mailer' do
expect { subject }.not_to have_enqueued_job
end
end

context 'admin email equals contributor email' do
let(:admin) { create(:user, admin: true, email: 'my-email@example.org') }
let(:contributor) { create(:contributor, email: 'my-email@example.org') }

it 'does not enqueue a Mailer' do
expect { subject }.not_to have_enqueued_job
end
end

context 'with an admin and contributor without the same email address' do
let(:admin) { create(:user, admin: true, email: 'admin@example.org') }
let(:contributor) { create(:contributor, email: 'contributor@example.org') }

it 'enqueues a Mailer' do
expect { subject }.to have_enqueued_job.on_queue('default').with(
'PostmarkAdapter::Outbound',
'contributor_marked_as_inactive_email',
'deliver_now', # How ActionMailer works in test environment, even though in production we call deliver_later
{
params: { admin: admin, contributor: contributor },
args: []
}
)
end
end
end
end
end

0 comments on commit ee6aa1c

Please sign in to comment.