Skip to content
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
2 changes: 1 addition & 1 deletion lib/courrier/email/providers/mailgun.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def content_type = "multipart/form-data"

def default_headers
{
"Authorization" => "Basic #{Base64.strict_encode64("api:#{@api_key}")}"
"Authorization" => "Basic #{["api:#{@api_key}"].pack("m0")}"
}
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/courrier/email/providers/mailjet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def body

def default_headers
{
"Authorization" => "Basic " + Base64.strict_encode64("#{@api_key}:#{@provider_options.api_secret}")
"Authorization" => "Basic #{["#{@api_key}:#{@provider_options.api_secret}"].pack("m0")}"
}
end

Expand Down
86 changes: 86 additions & 0 deletions test/courrier/email/providers/mailgun_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
require "test_helper"

module Courrier::Email::Providers
class MailgunTest < Minitest::Test
def setup
email = TestEmail.new(
from: "devs@railsdesigner.com",
to: "first@example.com, second@example.com",
reply_to: "support@railsdesigner.com"
)

@provider = provider_for(email)
end

def test_formats_transactional_email
assert_equal(
{
"from" => "devs@railsdesigner.com",
"to" => "first@example.com, second@example.com",
"h:Reply-To" => "support@railsdesigner.com",
"subject" => "Test Subject",
"text" => "Test Body",
"html" => "<p>Test HTML Body</p>"
},
@provider.body
)
end

def test_omits_reply_to_when_not_set
body = provider_for(TestEmail.new(from: "devs@railsdesigner.com", to: "first@example.com")).body

refute_includes body.keys, "h:Reply-To"
end

def test_builds_endpoint_url_from_domain
assert_equal "https://api.mailgun.net/v3/railsdesigner.com/messages", @provider.send(:endpoint_url)
end

def test_requires_a_domain
provider = Mailgun.new(
api_key: "test_key",
options: TestEmail.new(from: "devs@railsdesigner.com", to: "first@example.com").options,
provider_options: Courrier::Configuration::ProviderConfig.new
)

error = assert_raises(Courrier::ArgumentError) { provider.send(:endpoint_url) }

assert_match(/domain/, error.message)
end

def test_authenticates_with_api_key
assert_equal(
{"Authorization" => "Basic YXBpOnRlc3Rfa2V5"},
@provider.send(:default_headers)
)
end

def test_submits_as_multipart_form_data
assert_equal "multipart/form-data", @provider.send(:content_type)
end

def test_is_available_through_provider_registry
mock_provider = Minitest::Mock.new
mock_provider.expect(:deliver, nil)

Mailgun.stub :new, mock_provider do
Courrier::Email::Provider.new(
provider: "mailgun",
api_key: "test_key",
options: @provider.instance_variable_get(:@options)
).deliver
end

mock_provider.verify
end

private

def provider_for(email)
provider_options = Courrier::Configuration::ProviderConfig.new
provider_options.domain = "railsdesigner.com"

Mailgun.new(api_key: "test_key", options: email.options, provider_options: provider_options)
end
end
end
70 changes: 70 additions & 0 deletions test/courrier/email/providers/mailjet_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
require "test_helper"

module Courrier::Email::Providers
class MailjetTest < Minitest::Test
def setup
email = TestEmail.new(
from: "devs@railsdesigner.com",
to: "first@example.com",
reply_to: "support@railsdesigner.com"
)

@provider = provider_for(email)
end

def test_formats_transactional_email
assert_equal(
{
"Messages" => [
{
"From" => {"Email" => "devs@railsdesigner.com"},
"To" => [{"Email" => "first@example.com"}],
"ReplyTo" => {"Email" => "support@railsdesigner.com"},
"Subject" => "Test Subject",
"TextPart" => "Test Body",
"HTMLPart" => "<p>Test HTML Body</p>"
}
]
},
@provider.body
)
end

def test_omits_reply_to_when_not_set
body = provider_for(TestEmail.new(from: "devs@railsdesigner.com", to: "first@example.com")).body

refute_includes body["Messages"].first.keys, "ReplyTo"
end

def test_authenticates_with_api_key_and_secret
assert_equal(
{"Authorization" => "Basic dGVzdF9rZXk6dGVzdF9zZWNyZXQ="},
@provider.send(:default_headers)
)
end

def test_is_available_through_provider_registry
mock_provider = Minitest::Mock.new
mock_provider.expect(:deliver, nil)

Mailjet.stub :new, mock_provider do
Courrier::Email::Provider.new(
provider: "mailjet",
api_key: "test_key",
options: @provider.instance_variable_get(:@options)
).deliver
end

mock_provider.verify
end

private

def provider_for(email)
provider_options = Courrier::Configuration::ProviderConfig.new
provider_options.api_secret = "test_secret"

Mailjet.new(api_key: "test_key", options: email.options, provider_options: provider_options)
end
end
end
Loading