Skip to content

Commit

Permalink
Dynamic sendgrid key (#523)
Browse files Browse the repository at this point in the history
Closes #522

What changed?
============

Adds the ability to configure dynamic sendgrid keys: 

```
api_key: "my_api_key"
  # or {:system, "SENDGRID_API_KEY"},
  # or {ModuleName, :method_name, []}
```
  • Loading branch information
spunkedy authored Aug 24, 2020
1 parent 9ff6723 commit 4d20168
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 22 deletions.
7 changes: 6 additions & 1 deletion lib/bamboo/adapters/send_grid_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ defmodule Bamboo.SendGridAdapter do
# In config/config.exs, or config.prod.exs, etc.
config :my_app, MyApp.Mailer,
adapter: Bamboo.SendGridAdapter,
api_key: "my_api_key" # or {:system, "SENDGRID_API_KEY"},
api_key: "my_api_key"
# or {:system, "SENDGRID_API_KEY"},
# or {ModuleName, :method_name, []}
hackney_opts: [
recv_timeout: :timer.minutes(1)
]
Expand All @@ -32,6 +34,7 @@ defmodule Bamboo.SendGridAdapter do
defmodule MyApp.Mailer do
use Bamboo.Mailer, otp_app: :my_app
end
"""

@service_name "SendGrid"
Expand Down Expand Up @@ -73,6 +76,8 @@ defmodule Bamboo.SendGridAdapter do
api_key =
case Map.get(config, :api_key) do
{:system, var} -> System.get_env(var)
{module_name, method_name, args} -> apply(module_name, method_name, args)
fun when is_function(fun) -> fun.()
key -> key
end

Expand Down
68 changes: 47 additions & 21 deletions test/lib/bamboo/adapters/send_grid_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@ defmodule Bamboo.SendGridAdapterTest do
use ExUnit.Case
alias Bamboo.Email
alias Bamboo.SendGridAdapter

alias Bamboo.Test.User

@config %{adapter: SendGridAdapter, api_key: "123_abc"}
@good_api_key "123_abc"
@config %{adapter: SendGridAdapter, api_key: @good_api_key}
@config_with_bad_key %{adapter: SendGridAdapter, api_key: nil}
@config_with_env_var_key %{adapter: SendGridAdapter, api_key: {:system, "SENDGRID_API"}}
@config_with_sandbox_enabled %{adapter: SendGridAdapter, api_key: "123_abc", sandbox: true}
@config_with_env_var_tuple %{
adapter: SendGridAdapter,
api_key: {Bamboo.SendGridAdapterTest, :sendgrid_secret, []}
}
@config_with_env_var_tuple_direct %{
adapter: SendGridAdapter,
api_key: &Bamboo.SendGridAdapterTest.sendgrid_secret/0
}

@config_with_sandbox_enabled %{adapter: SendGridAdapter, api_key: @good_api_key, sandbox: true}

defmodule FakeSendgrid do
use Plug.Router
Expand Down Expand Up @@ -56,6 +65,12 @@ defmodule Bamboo.SendGridAdapterTest do
end
end

@doc """
This is a private function that is referenced in `Bamboo.SendGridAdapterTest`
to test the config usage example of having a dynamic key
"""
def sendgrid_secret(), do: @good_api_key

setup do
FakeSendgrid.start_server(self())

Expand All @@ -66,33 +81,44 @@ defmodule Bamboo.SendGridAdapterTest do
:ok
end

test "raises if the api key is nil" do
assert_raise ArgumentError, ~r/no API key set/, fn ->
new_email(from: "foo@bar.com") |> SendGridAdapter.deliver(@config_with_bad_key)
describe "API key section" do
test "raises if the api key is nil" do
assert_raise ArgumentError, ~r/no API key set/, fn ->
new_email(from: "foo@bar.com") |> SendGridAdapter.deliver(@config_with_bad_key)
end

assert_raise ArgumentError, ~r/no API key set/, fn ->
SendGridAdapter.handle_config(%{})
end
end

assert_raise ArgumentError, ~r/no API key set/, fn ->
SendGridAdapter.handle_config(%{})
test "can have a tuple resolution" do
config = SendGridAdapter.handle_config(@config_with_env_var_tuple)
assert config[:api_key] == @good_api_key
end
end

test "can read the api key from an ENV var" do
System.put_env("SENDGRID_API", "123_abc")
test "can have an anonymous function resolution" do
config = SendGridAdapter.handle_config(@config_with_env_var_tuple_direct)
assert config[:api_key] == @good_api_key
end

config = SendGridAdapter.handle_config(@config_with_env_var_key)
test "can read the api key from an ENV var" do
System.put_env("SENDGRID_API", @good_api_key)
config = SendGridAdapter.handle_config(@config_with_env_var_key)

assert config[:api_key] == "123_abc"
end
assert config[:api_key] == @good_api_key
end

test "raises if an invalid ENV var is used for the API key" do
System.delete_env("SENDGRID_API")
test "raises if an invalid ENV var is used for the API key" do
System.delete_env("SENDGRID_API")

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

assert_raise ArgumentError, ~r/no API key set/, fn ->
SendGridAdapter.handle_config(@config_with_env_var_key)
assert_raise ArgumentError, ~r/no API key set/, fn ->
SendGridAdapter.handle_config(@config_with_env_var_key)
end
end
end

Expand Down

0 comments on commit 4d20168

Please sign in to comment.