-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fa41ceb
commit 918ccb4
Showing
8 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |