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

Allow to set mailgun config from env #363

Merged
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
29 changes: 22 additions & 7 deletions lib/bamboo/adapters/mailgun_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ defmodule Bamboo.MailgunAdapter do
# In config/config.exs, or config.prod.exs, etc.
config :my_app, MyApp.Mailer,
adapter: Bamboo.MailgunAdapter,
api_key: "my_api_key",
domain: "your.domain"
api_key: "my_api_key" # or {:system, "MAILGUN_API_KEY"},
domain: "your.domain" # or {:system, "MAILGUN_DOMAIN"}

# Define a Mailer. Maybe in lib/my_app/mailer.ex
defmodule MyApp.Mailer do
Expand All @@ -28,13 +28,27 @@ defmodule Bamboo.MailgunAdapter do

@doc false
def handle_config(config) do
for setting <- [:api_key, :domain] do
if config[setting] in [nil, ""] do
raise_missing_setting_error(config, setting)
end
config
|> Map.put(:api_key, get_setting(config, :api_key))
|> Map.put(:domain, get_setting(config, :domain))
end

defp get_setting(config, key) do
config[key]
|> case do
{:system, var} ->
System.get_env(var)

value ->
value
end
|> case do
value when value in [nil, ""] ->
raise_missing_setting_error(config, key)

config
value ->
value
end
end

defp raise_missing_setting_error(config, setting) do
Expand All @@ -49,6 +63,7 @@ defmodule Bamboo.MailgunAdapter do

def deliver(email, config) do
body = to_mailgun_body(email)
config = handle_config(config)

case :hackney.post(full_uri(config), headers(email, config), body, [:with_body]) do
{:ok, status, _headers, response} when status > 299 ->
Expand Down
44 changes: 44 additions & 0 deletions test/lib/bamboo/adapters/mailgun_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ defmodule Bamboo.MailgunAdapterTest do
alias Bamboo.MailgunAdapter

@config %{adapter: MailgunAdapter, api_key: "dummyapikey", domain: "test.tt"}
@config_with_env_var_key %{adapter: MailgunAdapter,
api_key: {:system, "MAILGUN_API_KEY"}, domain: {:system, "MAILGUN_DOMAIN"}}

defmodule FakeMailgun do
use Plug.Router
Expand Down Expand Up @@ -62,6 +64,19 @@ defmodule Bamboo.MailgunAdapterTest do
:ok
end

test "can read the settings from an ENV var" do
System.put_env("MAILGUN_API_KEY", "env_api_key")
System.put_env("MAILGUN_DOMAIN", "env_domain")

config = MailgunAdapter.handle_config(@config_with_env_var_key)

assert config[:api_key] == "env_api_key"
assert config[:domain] == "env_domain"

System.delete_env("MAILGUN_API_KEY")
System.delete_env("MAILGUN_DOMAIN")
end

test "raises if the api key is nil" do
assert_raise ArgumentError, ~r/no api_key set/, fn ->
MailgunAdapter.handle_config(%{domain: "test.tt"})
Expand All @@ -74,6 +89,35 @@ defmodule Bamboo.MailgunAdapterTest do
end
end

test "raises if an invalid ENV var is used for the api_key" do
System.put_env("MAILGUN_DOMAIN", "env_domain")

assert_raise ArgumentError, ~r/no api_key set/, fn ->
new_email(from: "foo@bar.com") |> MailgunAdapter.deliver(@config_with_env_var_key)
end

assert_raise ArgumentError, ~r/no api_key set/, fn ->
MailgunAdapter.handle_config(@config_with_env_var_key)

end
System.delete_env("MAILGUN_DOMAIN")
end


test "raises if an invalid ENV var is used for the domain" do
System.put_env("MAILGUN_API_KEY", "env_api_key")

assert_raise ArgumentError, ~r/no domain set/, fn ->
new_email(from: "foo@bar.com") |> MailgunAdapter.deliver(@config_with_env_var_key)
end

assert_raise ArgumentError, ~r/no domain set/, fn ->
MailgunAdapter.handle_config(@config_with_env_var_key)

end
System.delete_env("MAILGUN_API_KEY")
end

test "deliver/2 sends the to the right url" do
new_email() |> MailgunAdapter.deliver(@config)

Expand Down