Compare commits
6 Commits
135448398d
...
464b96b656
| Author | SHA1 | Date | |
|---|---|---|---|
| 464b96b656 | |||
| 5aefc18098 | |||
| 50500060d5 | |||
| e30a92193d | |||
| b5e308aec6 | |||
| f333ea1ed0 |
@ -1,10 +1,18 @@
|
|||||||
defmodule BasicSignupWhitelist do
|
defmodule BasicSignupWhitelist do
|
||||||
@moduledoc """
|
@moduledoc """
|
||||||
Documentation for `BasicSignupWhitelist`.
|
Checks if an email address is allowed based on the SIGNUP_ALLOWED_EMAILS env variable.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
false
|
Checks if an email address is allowed based on the SIGNUP_ALLOWED_EMAILS env variable.
|
||||||
|
|
||||||
|
Rules:
|
||||||
|
- Returns true if ALLOWED_EMAILS == "*" (all emails allowed)
|
||||||
|
- Returns true if email matches any entry in the comma-separated list
|
||||||
|
- Returns false if:
|
||||||
|
* Environment variable doesn't exist
|
||||||
|
* Environment variable is empty string
|
||||||
|
* Email not found in whitelist
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
|
|||||||
57
test/fun_core/basic_signup_whitelist_test.exs
Normal file
57
test/fun_core/basic_signup_whitelist_test.exs
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
defmodule FunCore.BasicSignupWhitelistTest do
|
||||||
|
use ExUnit.Case
|
||||||
|
|
||||||
|
defp normalize(email) do
|
||||||
|
email |> String.trim() |> String.downcase()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp addresses_as_list(addresses_str) do
|
||||||
|
addresses_str
|
||||||
|
|> String.split(",")
|
||||||
|
|> Enum.map(&normalize/1)
|
||||||
|
end
|
||||||
|
|
||||||
|
def mail_whitelisted_fun(signups_allowed, email_received) do
|
||||||
|
case signups_allowed do
|
||||||
|
nil -> false
|
||||||
|
"*" -> true
|
||||||
|
list_str -> normalize(email_received) in addresses_as_list(list_str)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "addresses_as_list" do
|
||||||
|
assert addresses_as_list("joe@example.com, jane@example.com") == [
|
||||||
|
"joe@example.com",
|
||||||
|
"jane@example.com"
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
test "jane in list" do
|
||||||
|
lst = addresses_as_list("joe@example.com, jane@example.com")
|
||||||
|
assert "jane@example.com" in lst
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "Not whitelisted when allowed list is" do
|
||||||
|
test "not set" do
|
||||||
|
refute(mail_whitelisted_fun(nil, "joe@example.com"))
|
||||||
|
end
|
||||||
|
|
||||||
|
test "empty" do
|
||||||
|
refute(mail_whitelisted_fun("j", "joe@example.com"))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "Whitelisted when" do
|
||||||
|
test "*" do
|
||||||
|
assert(mail_whitelisted_fun("*", "jane@example.com"))
|
||||||
|
end
|
||||||
|
|
||||||
|
test "Multiple set and one match" do
|
||||||
|
assert(mail_whitelisted_fun("joe@example.com, jane@example.com", "jane@example.com"))
|
||||||
|
end
|
||||||
|
|
||||||
|
test "Matches with different casings" do
|
||||||
|
assert(mail_whitelisted_fun("joe@Example.com, jane@example.com", "Joe@example.com"))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
x
Reference in New Issue
Block a user