Skip to content

Commit 5f2ddfc

Browse files
committed
Refactored signature encryption in unsubscribe and stub the encryption in tests to return a simple valid value
1 parent e9f94e9 commit 5f2ddfc

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

app/controllers/emails_controller.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class EmailsController < ApplicationController
22
def unsubscribe
3-
Rails.logger.info("Mailgun Unsubscribe: #{params.inspect}")
3+
puts("Mailgun Unsubscribe: #{params.inspect}")
44
if mailgun?(ENV['MAILGUN_API_KEY'], params['token'], params['timestamp'], params['signature'])
55
if params[:email_type] == Notifier::WELCOME_EVENT
66
user = User.where(email: params[:recipient]).first
@@ -30,9 +30,12 @@ def delivered
3030
protected
3131

3232
def mailgun?(api_key, token, timestamp, signature)
33-
return signature == OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'),
34-
api_key,
35-
'%s%s' % [timestamp, token])
33+
encrypted = encrypt_signature(api_key, timestamp, token)
34+
return signature == encrypted
35+
end
36+
37+
def encrypt_signature(api_key, timestamp, token)
38+
OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), api_key, '%s%s' % [timestamp, token])
3639
end
3740

3841
end

spec/controllers/emails_controller_spec.rb

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
describe EmailsController do
22
let(:mailgun_params) { {
3-
"domain" => ENV['MAILGUN_DOMAIN'],
4-
"tag" => "*",
5-
"recipient" => "someone@example.com",
6-
"event" => "unsubscribed",
7-
"email_type" => Notifier::ACTIVITY_EVENT,
8-
"timestamp" => "1327043027",
9-
"token" => ENV['MAILGUN_TOKEN'],
10-
"signature" => ENV['MAILGUN_SIGNATURE'],
11-
"controller" => "emails",
12-
"action" => "unsubscribe"} }
3+
'domain' => ENV['MAILGUN_DOMAIN'],
4+
'tag' => '*',
5+
'recipient' => 'someone@example.com',
6+
'event' => 'unsubscribed',
7+
'email_type' => Notifier::ACTIVITY_EVENT,
8+
'timestamp' => '1327043027',
9+
'token' => ENV['MAILGUN_TOKEN'],
10+
'signature' => ENV['MAILGUN_SIGNATURE'],
11+
'controller' => 'emails',
12+
'action' => 'unsubscribe'}
13+
}
1314

1415
it 'unsubscribes member from notifications when they unsubscribe from a notification email on mailgun' do
1516
user = Fabricate(:user, email: 'someone@example.com')
1617
user.notify_on_award.should == true
18+
EmailsController.any_instance.should_receive(:encrypt_signature).and_return(ENV['MAILGUN_SIGNATURE'])
1719
post :unsubscribe, mailgun_params
1820
user.reload
1921
user.notify_on_award.should == false
@@ -24,6 +26,7 @@
2426
user = Fabricate(:user, email: 'someone@example.com')
2527
new_params = mailgun_params
2628
new_params["email_type"] = Notifier::WELCOME_EVENT
29+
EmailsController.any_instance.should_receive(:encrypt_signature).and_return(ENV['MAILGUN_SIGNATURE'])
2730
post :unsubscribe, mailgun_params
2831
user.reload
2932
user.notify_on_award.should == true

0 commit comments

Comments
 (0)