Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Mailchimp unsubscribe webhook (#5804) [deploy]
- Loading branch information
1 parent
0119918
commit 050a2e2
Showing
4 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
app/controllers/incoming_webhooks/mailchimp_unsubscribes_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class IncomingWebhooks::MailchimpUnsubscribesController < ApplicationController | ||
class InvalidListID < StandardError; end | ||
|
||
LIST_MAPPINGS = { | ||
mailchimp_newsletter_id: :email_newsletter, | ||
mailchimp_sustaining_members_id: :email_membership_newsletter, | ||
mailchimp_tag_moderators_id: :email_tag_mod_newsletter, | ||
mailchimp_community_moderators_id: :email_community_mod_newsletter | ||
}.freeze | ||
|
||
def create | ||
not_authorized unless valid_secret? | ||
user = User.find_by!(email: params.dig(:data, :email)) | ||
user.update(email_type => false) | ||
end | ||
|
||
private | ||
|
||
def valid_secret? | ||
params[:secret] == SiteConfig.mailchimp_incoming_webhook_secret | ||
end | ||
|
||
def email_type | ||
list_id = params.dig(:data, :list_id) | ||
key = LIST_MAPPINGS.keys.detect { |k| SiteConfig.public_send(k) == list_id } | ||
raise InvalidListID unless key | ||
|
||
LIST_MAPPINGS[key] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
spec/requests/incoming_webhooks/mailchimp/unsubscribe_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe "IncomingWebhooks::MailchimpUnsubscribesController", type: :request do | ||
let(:user) { create(:user, email_digest_periodic: true) } | ||
|
||
describe "POST /webhooks/mailchimp/:secret/unsubscribe" do | ||
let(:secret) { "secret" } | ||
let(:list_id) { "1234" } | ||
let(:params) { { data: { email: user.email, list_id: list_id } } } | ||
|
||
before do | ||
allow(SiteConfig).to receive(:mailchimp_incoming_webhook_secret).and_return(secret) | ||
end | ||
|
||
it "return not authorized if the secret is incorrect" do | ||
expect do | ||
post "/incoming_webhooks/mailchimp/wrong_secret/unsubscribe", params: params | ||
end.to raise_error(Pundit::NotAuthorizedError) | ||
end | ||
|
||
it "unsubscribes the user if the secret is correct" do | ||
SiteConfig.mailchimp_newsletter_id = list_id | ||
|
||
expect do | ||
post "/incoming_webhooks/mailchimp/#{secret}/unsubscribe", params: params | ||
end.to change { user.reload.email_newsletter }.from(true).to(false) | ||
end | ||
end | ||
end |