Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mailchimp unsubscribe webhook #5804

Merged
merged 5 commits into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines +6 to +8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from what i can tell, we're not using most of these keys; what do we need them for? (just trying to get context on this)

Copy link
Contributor Author

@citizen428 citizen428 Feb 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the mapping of option names in the SiteConfig model to newsletter types. We're using it here:

    list_id = params.dig(:data, :list_id)
    key = LIST_MAPPINGS.keys.detect { |k| SiteConfig.public_send(k) == list_id }

Once we have mapped from list id to attribute name, we use the latter in the update user.update(email_type => false).

Does that clear it up?

}.freeze

def create
not_authorized unless valid_secret?
user = User.find_by!(email: params.dig(:data, :email))
user.update(email_type => false)
vaidehijoshi marked this conversation as resolved.
Show resolved Hide resolved
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
4 changes: 4 additions & 0 deletions app/models/site_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class SiteConfig < RailsSettings::Base
field :mailchimp_tag_moderators_id, type: :string, default: ""
field :mailchimp_community_moderators_id, type: :string, default: ""

# Mailchimp webhook secret. Part of the callback URL in the Mailchimp settings.
# <https://mailchimp.com/developer/guides/about-webhooks/#Webhooks_security>
field :mailchimp_incoming_webhook_secret, type: :string, default: ""

# Email digest frequency
field :periodic_email_digest_max, type: :integer, default: 0
field :periodic_email_digest_min, type: :integer, default: 2
Expand Down
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@
resources :reads, only: [:create]
end

namespace :incoming_webhooks do
post "/mailchimp/:secret/unsubscribe", to: "mailchimp_unsubscribes#create", as: :mailchimp_unsubscribe
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably change this at one point and use routing DSL trickery, but for now this seems good enough.

end

resources :messages, only: [:create]
resources :chat_channels, only: %i[index show create update]
resources :chat_channel_memberships, only: %i[create update destroy]
Expand Down
29 changes: 29 additions & 0 deletions spec/requests/incoming_webhooks/mailchimp/unsubscribe_spec.rb
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's a typo here but I don't think it should hold the PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean that it should say incoming_webhooks? Because I don't really see a typo, but that could also be because I knew what I wanted to write 😉

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, i didn't know about the from(true).to(false) syntax! that's so much nicer than asserting the value of email_newsletter on the user before making the post request, and then checking it again afterwards!! ✨

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's also by, which can specify an amount of change, i.e.

expect do
  User.create(...)
end.to change(User, :count).by(1)

end
end
end