Skip to content

Commit

Permalink
Merge 918ccb4 into fa41ceb
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleVsteger committed Apr 14, 2020
2 parents fa41ceb + 918ccb4 commit 4ff4f1a
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/unplug/predicates/app_config_in.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Unplug.Predicates.AppConfigIn do
@moduledoc """
Given an application and a key, execute the plug if the configured value
matches the expected value.
Usage:
```elixir
plug Unplug,
if: {Unplug.Predicates.AppConfigIn, {:my_app, :some_config, [:enabled, :enabled_again]}},
do: MyApp.Plug
```
"""

@behaviour Unplug.Predicate

@impl true
def call(_conn, {app, key, expected_values}) when is_list(expected_values) do
Application.get_env(app, key) in expected_values
end
end
20 changes: 20 additions & 0 deletions lib/unplug/predicates/app_config_not_in.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Unplug.Predicates.AppConfigNotIn do
@moduledoc """
Given an application and a key, execute the plug if the configured value
matches the expected value.
Usage:
```elixir
plug Unplug,
if: {Unplug.Predicates.AppConfigNotIn, {:my_app, :some_config, [:enabled, :enabled_again]}},
do: MyApp.Plug
```
"""

@behaviour Unplug.Predicate

@impl true
def call(_conn, {app, key, expected_values}) when is_list(expected_values) do
Application.get_env(app, key) not in expected_values
end
end
20 changes: 20 additions & 0 deletions lib/unplug/predicates/env_var_in.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Unplug.Predicates.EnvVarIn do
@moduledoc """
Given an environment variable, execute the plug if the configured value
matches the expected value.
Usage:
```elixir
plug Unplug,
if: {Unplug.Predicates.EnvVarIn, {"ENABLE_DEBUG", ["true", "1"]}},
do: MyApp.Plug
```
"""

@behaviour Unplug.Predicate

@impl true
def call(_conn, {env_var, expected_values}) when is_list(expected_values) do
System.get_env(env_var) in expected_values
end
end
20 changes: 20 additions & 0 deletions lib/unplug/predicates/env_var_not_in.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Unplug.Predicates.EnvVarNotIn do
@moduledoc """
Given an environment variable, execute the plug if the configured value
matches the expected value.
Usage:
```elixir
plug Unplug,
if: {Unplug.Predicates.EnvVarNotIn, {"ENABLE_DEBUG", ["true", "1"]}},
do: MyApp.Plug
```
"""

@behaviour Unplug.Predicate

@impl true
def call(_conn, {env_var, expected_values}) when is_list(expected_values) do
System.get_env(env_var) not in expected_values
end
end
27 changes: 27 additions & 0 deletions test/predicates/app_config_in_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule Unplug.Predicates.AppConfigInTest do
use ExUnit.Case, async: false
use Plug.Test

test "should return true if the application config key is in the list" do
Application.put_env(:unplug, :app_config_test, "some_config_string")
Application.put_env(:unplug, :app_config_test_two, "some_other_config_string")
conn = conn(:get, "/")

assert Unplug.Predicates.AppConfigIn.call(
conn,
{:unplug, :app_config_test, ["some_config_string", "some_other_string"]}
)

assert Unplug.Predicates.AppConfigIn.call(
conn,
{:unplug, :app_config_test_two, ["some_config_string", "some_other_config_string"]}
)
end

test "should return false if the application config key is NOT in the list" do
Application.put_env(:unplug, :app_config_test, "some_config_string")
conn = conn(:get, "/")

refute Unplug.Predicates.AppConfigIn.call(conn, {:unplug, :app_config_test, ["invalid"]})
end
end
27 changes: 27 additions & 0 deletions test/predicates/app_config_not_in_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule Unplug.Predicates.AppConfigNotInTest do
use ExUnit.Case, async: false
use Plug.Test

test "should return true if the application config key is NOT in the list" do
Application.put_env(:unplug, :app_config_test, "some_config_string")
Application.put_env(:unplug, :app_config_test_two, "some_other_config_string")
conn = conn(:get, "/")

refute Unplug.Predicates.AppConfigNotIn.call(
conn,
{:unplug, :app_config_test, ["some_config_string", "some_other_string"]}
)

refute Unplug.Predicates.AppConfigNotIn.call(
conn,
{:unplug, :app_config_test_two, ["some_config_string", "some_other_config_string"]}
)
end

test "should return false if the application config key is in the list" do
Application.put_env(:unplug, :app_config_test, "some_config_string")
conn = conn(:get, "/")

assert Unplug.Predicates.AppConfigNotIn.call(conn, {:unplug, :app_config_test, ["invalid"]})
end
end
18 changes: 18 additions & 0 deletions test/predicates/env_var_in_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule Unplug.Predicates.EnvVarInTest do
use ExUnit.Case, async: false
use Plug.Test

test "should return true if the env var is in the list of acceptable keys" do
System.put_env("UNPLUG_ENV_VAR", "some_config_string")
conn = conn(:get, "/")

assert Unplug.Predicates.EnvVarIn.call(conn, {"UNPLUG_ENV_VAR", ["some_config_string", "some_other_config_string"]})
end

test "should return false if the env var is not in the list of acceptable keys" do
System.put_env("UNPLUG_ENV_VAR", "some_config_string")
conn = conn(:get, "/")

refute Unplug.Predicates.EnvVarIn.call(conn, {"UNPLUG_ENV_VAR", ["invalid", "still_invalid"]})
end
end
21 changes: 21 additions & 0 deletions test/predicates/env_var_not_in_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule Unplug.Predicates.EnvVarNotInTest do
use ExUnit.Case, async: false
use Plug.Test

test "should return false if the env var is in the list of acceptable keys" do
System.put_env("UNPLUG_ENV_VAR", "some_config_string")
conn = conn(:get, "/")

refute Unplug.Predicates.EnvVarNotIn.call(
conn,
{"UNPLUG_ENV_VAR", ["some_config_string", "some_other_config_string"]}
)
end

test "should return true if the env var is not in the list of acceptable keys" do
System.put_env("UNPLUG_ENV_VAR", "some_config_string")
conn = conn(:get, "/")

assert Unplug.Predicates.EnvVarNotIn.call(conn, {"UNPLUG_ENV_VAR", ["invalid", "still_invalid"]})
end
end

0 comments on commit 4ff4f1a

Please sign in to comment.