Skip to content

Commit

Permalink
\#4 switch key configuration to use authenticate_with_mandrill_keys!
Browse files Browse the repository at this point in the history
  • Loading branch information
tardate committed Sep 18, 2013
1 parent 3877548 commit 0121038
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
8 changes: 4 additions & 4 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ Mandrill now supports {webhook authentication}[http://help.mandrill.com/entries/

If you do not configure your webhook API key, then the handlers will continue to work fine - they just won't be authenticated.

To enable authentication, use the <tt>mandrill_webhook_keys</tt> method to set your API key. It is recommended you pull
To enable authentication, use the <tt>authenticate_with_mandrill_keys!</tt> method to set your API key. It is recommended you pull
your API keys from environment settings, or use some other means to avoid committing the API keys in your source code.

For example, to handle inbound email:

class InboxController < ApplicationController
include Mandrill::Rails::WebHookProcessor
mandrill_webhook_keys 'YOUR_MANDRILL_WEBHOOK_KEY'
authenticate_with_mandrill_keys! 'YOUR_MANDRILL_WEBHOOK_KEY'

def handle_inbound(event_payload)
# .. handler methods will only be called if authentication has succeeded.
Expand All @@ -138,11 +138,11 @@ For example, to handle inbound email:

Sometimes you may have more than one WebHook sending requests to a single controller, for example if you have one handling 'click' events, and another sending inbound email. Mandrill assigns separate API keys to each of these.

In this case, just add all the valid API keys you will allow with <tt>mandrill_webhook_keys</tt>, for example:
In this case, just add all the valid API keys you will allow with <tt>authenticate_with_mandrill_keys!</tt>, for example:

class InboxController < ApplicationController
include Mandrill::Rails::WebHookProcessor
mandrill_webhook_keys 'MANDRILL_CLICK_WEBHOOK_KEY', 'MANDRILL_INBOUND_WEBHOOK_KEY', 'ANOTHER_WEBHOOK_KEY'
authenticate_with_mandrill_keys! 'MANDRILL_CLICK_WEBHOOK_KEY', 'MANDRILL_INBOUND_WEBHOOK_KEY', 'ANOTHER_WEBHOOK_KEY'

def handle_inbound(event_payload)
end
Expand Down
10 changes: 8 additions & 2 deletions lib/mandrill-rails/web_hook_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ module Mandrill::Rails::WebHookProcessor
end

module ClassMethods
# Gets/sets the current Mandrill WebHook Authentication key(s).
# Returns the current WebHook key(s) as an Array if called with no parameters.
# If called with parameters, add the params to the WebHook key array.
# If called with nil as the parameters, clears the WebHook key array.
def mandrill_webhook_keys(*keys)
def authenticate_with_mandrill_keys!(*keys)
@mandrill_webhook_keys ||= []
if keys.present?
if keys.compact.present?
Expand All @@ -54,7 +55,12 @@ def mandrill_webhook_keys(*keys)
@mandrill_webhook_keys
end

# Command: directly assigns the WebHook key array to +keys+
# Gets the current Mandrill WebHook Authentication key(s).
def mandrill_webhook_keys
authenticate_with_mandrill_keys!
end

# Command: directly assigns the WebHook key array to +keys+.
def mandrill_webhook_keys=(keys)
@mandrill_webhook_keys = Array(keys)
end
Expand Down
29 changes: 22 additions & 7 deletions spec/mandrill-rails/web_hook_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def head(*args) ; end
let(:processor_class) { WebHookProcessorTestHarness }
let(:processor_instance) { processor_class.new }
before do
processor_class.mandrill_webhook_keys nil
processor_class.authenticate_with_mandrill_keys! nil
end

describe "##skip_before_filter settings" do
Expand All @@ -30,32 +30,47 @@ def head(*args) ; end
it { should eql([:authenticate_mandrill_request!, {:only=>[:create]}]) }
end

describe "##mandrill_webhook_keys" do
describe "##authenticate_with_mandrill_keys!" do
subject { processor_class.mandrill_webhook_keys }
it { should eql([]) }
context "when set with a single value" do
let(:key_a) { "key_a" }
before { processor_class.mandrill_webhook_keys key_a }
before { processor_class.authenticate_with_mandrill_keys! key_a }
it { should eql([key_a]) }
context "then called with nil" do
before { processor_class.mandrill_webhook_keys nil }
before { processor_class.authenticate_with_mandrill_keys! nil }
it { should eql([]) }
end
end
context "when set with a list of values" do
let(:key_a) { "key_a" }
let(:key_b) { "key_b" }
before { processor_class.mandrill_webhook_keys key_a, key_b }
before { processor_class.authenticate_with_mandrill_keys! key_a, key_b }
it { should eql([key_a,key_b]) }
end
context "when set with an explicit array of values" do
let(:key_a) { "key_a" }
let(:key_b) { "key_b" }
before { processor_class.mandrill_webhook_keys [key_a, key_b] }
before { processor_class.authenticate_with_mandrill_keys! [key_a, key_b] }
it { should eql([key_a,key_b]) }
end
end

describe "#mandrill_webhook_keys" do
subject { processor_class.mandrill_webhook_keys }
it { should eql([]) }
context "when set with mandrill_webhook_keys=" do
let(:expected_value) { [1,2,3] }
before { processor_class.mandrill_webhook_keys = expected_value }
it { should eql(expected_value) }
end
context "when set with authenticate_with_mandrill_keys!" do
let(:expected_value) { [4,5,6] }
before { processor_class.authenticate_with_mandrill_keys! expected_value }
it { should eql(expected_value) }
end
end


subject { processor_instance }

Expand Down Expand Up @@ -101,7 +116,7 @@ def head(*args) ; end
end
context "when authentication enabled" do
before do
processor_class.mandrill_webhook_keys mandrill_webhook_keys
processor_class.authenticate_with_mandrill_keys! mandrill_webhook_keys
end
context "with valid key" do
let(:mandrill_webhook_keys) { valid_webhook_key }
Expand Down

0 comments on commit 0121038

Please sign in to comment.