Skip to content

Commit

Permalink
make api_key overridable in Application env
Browse files Browse the repository at this point in the history
  • Loading branch information
msimonborg committed Mar 14, 2019
1 parent c15fde8 commit 74dd648
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lib/open_states/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ defmodule OpenStates.Application do
use Receiver, as: :credentials

def start(_type, _args) do
api_key = Application.get_env(:open_states, :api_key, System.get_env("OPENSTATES_API_KEY"))
start_credentials(fn -> api_key end)
set_api_key_from_env()

children = []

Expand All @@ -18,4 +17,11 @@ defmodule OpenStates.Application do

@spec api_key() :: String.t()
def api_key, do: get_credentials()

@spec set_api_key_from_env() :: :ok
def set_api_key_from_env do
start_credentials()
api_key = Application.get_env(:open_states, :api_key, System.get_env("OPENSTATES_API_KEY"))
update_credentials(fn _ -> api_key end)
end
end
15 changes: 14 additions & 1 deletion test/open_states_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule OpenStatesTest do
use ExUnit.Case, async: true
use ExUnit.Case, async: false
import Mock
doctest OpenStates

Expand All @@ -11,6 +11,19 @@ defmodule OpenStatesTest do
assert OpenStates.api_key() == System.get_env("OPENSTATES_API_KEY")
end

test "api_key/0 can be overriden in Application config" do
original = OpenStates.api_key()
Application.put_env(:open_states, :api_key, "hello")
:ok = OpenStates.Application.set_api_key_from_env()

assert OpenStates.api_key() == "hello"

Application.put_env(:open_states, :api_key, original)
:ok = OpenStates.Application.set_api_key_from_env()

assert OpenStates.api_key() == original
end

test "headers/0 returns the API query headers" do
assert OpenStates.headers() == ["X-API-KEY": OpenStates.api_key()]
end
Expand Down

0 comments on commit 74dd648

Please sign in to comment.