Compare commits

...

6 Commits

Author SHA1 Message Date
464b96b656 match different casings 2025-09-14 16:46:57 +01:00
5aefc18098 Whitelist two addresses
Intermediate steps not committed - addresses_as_list, because long stack
trace in output. turned out 'nil' case needs to be explicit now. I
prefer the small functions though instead of whole implementation in
case statement.
2025-09-14 16:39:56 +01:00
50500060d5 Factor out describe block for allowed list empty etc. 2025-09-14 16:23:20 +01:00
e30a92193d empty covered by not equals - not allowed 2025-09-14 16:13:52 +01:00
b5e308aec6 whitelist on exact match 2025-09-14 16:12:33 +01:00
f333ea1ed0 when not defined, not allowed 2025-09-14 16:06:50 +01:00
2 changed files with 67 additions and 2 deletions

View File

@ -1,10 +1,18 @@
defmodule BasicSignupWhitelist do
@moduledoc """
Documentation for `BasicSignupWhitelist`.
Checks if an email address is allowed based on the SIGNUP_ALLOWED_EMAILS env variable.
"""
@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

View 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