Skip to content

Commit

Permalink
support using master key subaccount header
Browse files Browse the repository at this point in the history
  • Loading branch information
NotTVsFrank authored and dgoerlich committed Apr 11, 2016
1 parent 9e06253 commit 2e821d1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/sparkpost_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class Configuration
attr_accessor :transactional
attr_accessor :ip_pool

attr_accessor :subaccount

def initialize
set_defaults
end
Expand All @@ -46,6 +48,8 @@ def set_defaults

@transactional = false
@ip_pool = nil

@subaccount = nil
end
end
end
10 changes: 10 additions & 0 deletions lib/sparkpost_rails/delivery_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,16 @@ def prepare_api_headers_from sparkpost_data
"Authorization" => api_key,
"Content-Type" => "application/json"
}

if sparkpost_data.has_key?(:subaccount)
subaccount = sparkpost_data[:subaccount]
else
subaccount = SparkPostRails.configuration.subaccount
end

if subaccount
@headers["X-MSYS-SUBACCOUNT"] = subaccount.to_s
end
end

def post_to_api
Expand Down
44 changes: 43 additions & 1 deletion spec/subaccount_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
end

context "Subaccount API Key" do
it "handles uses supplied subaccount key instead of default API key" do
it "uses supplied subaccount key instead of default API key" do
SparkPostRails.configure do |c|
c.api_key = 'NEW_DEFAULT_API_KEY'
end
Expand All @@ -30,4 +30,46 @@
expect(@delivery_method.headers).to include("Authorization" => "NEW_DEFAULT_API_KEY")
end
end

context "Subaccount ID" do
it "accepts a subaccount ID in the configuration" do
SparkPostRails.configure do |c|
c.subaccount = 123
end

test_email = Mailer.test_email
@delivery_method.deliver!(test_email)

expect(@delivery_method.headers).to include("X-MSYS-SUBACCOUNT" => "123")
end

it "defaults to no subaccount ID in the configuration" do
expect(SparkPostRails.configuration.subaccount).to eq(nil)
end

it "accepts subaccount ID for an individual message" do
test_email = Mailer.test_email sparkpost_data: {subaccount: 456}
@delivery_method.deliver!(test_email)

expect(@delivery_method.headers).to include("X-MSYS-SUBACCOUNT" => "456")
end

it "uses subaccount ID on message instead of value in configuration" do
SparkPostRails.configure do |c|
c.subaccount = 123
end

test_email = Mailer.test_email sparkpost_data: {subaccount: 456}
@delivery_method.deliver!(test_email)

expect(@delivery_method.headers).to include("X-MSYS-SUBACCOUNT" => "456")
end

it "does not include the subaccount header when none is specified" do
test_email = Mailer.test_email
@delivery_method.deliver!(test_email)

expect(@delivery_method.headers.has_key?("X-MSYS-SUBACCOUNT")).to eq(false)
end
end
end

0 comments on commit 2e821d1

Please sign in to comment.