Compare commits

..

No commits in common. "8b527f618a6aa94723fc41c9197dbe78b7f3b7b3" and "c6e3a490d2f32c7edad598c82b98272f0c9771f3" have entirely different histories.

4 changed files with 17 additions and 42 deletions

View File

@ -1,18 +1,10 @@
defmodule BasicSignupAllowlist do defmodule BasicSignupAllowlist do
import FunCore.BasicSignupAllowlist import FunCore.BasicSignupAllowlist
@signup_allowlist_emails "SIGNUP_ALLOWLIST_EMAILS"
@moduledoc """ @moduledoc """
Checks if an email address is allowed based on the SIGNUP_ALLOWLIST_EMAILS env variable. Checks if an email address is allowed based on the SIGNUP_ALLOWLIST_EMAILS env variable.
""" """
@doc """
Returns the name of the environment variable used for the signup allowlist.
"""
@spec signup_allowlist_emails() :: String.t()
def signup_allowlist_emails, do: @signup_allowlist_emails
@doc """ @doc """
Checks if an email address is allowed based on the SIGNUP_ALLOWLIST_EMAILS env variable. Checks if an email address is allowed based on the SIGNUP_ALLOWLIST_EMAILS env variable.
@ -27,16 +19,15 @@ defmodule BasicSignupAllowlist do
## Examples ## Examples
iex> System.delete_env("SIGNUP_ALLOWLIST_EMAILS") iex> System.delete_env("SIGNUP_ALLOWLIST_EMAILS")
iex> BasicSignupAllowlist.signup_allowed?("joe@example.com") iex> BasicSignupAllowlist.mail_allowlisted("joe@example.com")
false false
iex> System.put_env("SIGNUP_ALLOWLIST_EMAILS", "*") iex> System.put_env("SIGNUP_ALLOWLIST_EMAILS","*")
iex> BasicSignupAllowlist.signup_allowed?("joe@example.com") iex> BasicSignupAllowlist.mail_allowlisted("joe@example.com")
true true
""" """
@spec signup_allowed?(String.t()) :: boolean() def mail_allowlisted(email) do
def signup_allowed?(email) do env_value = System.get_env("SIGNUP_ALLOWLIST_EMAILS")
env_value = System.get_env(@signup_allowlist_emails) mail_allowlisted_fun(env_value, email)
signup_allowed_fun(env_value, email)
end end
end end

View File

@ -1,31 +1,15 @@
defmodule FunCore.BasicSignupAllowlist do defmodule FunCore.BasicSignupAllowlist do
@moduledoc """
Functional core for email allowlist checking logic.
"""
@doc """
Normalizes an email address by trimming whitespace and converting to lowercase.
"""
@spec normalize(String.t()) :: String.t()
def normalize(email) do def normalize(email) do
email |> String.trim() |> String.downcase() email |> String.trim() |> String.downcase()
end end
@doc """
Converts a comma-separated string of email addresses into a normalized list.
"""
@spec addresses_as_list(String.t()) :: [String.t()]
def addresses_as_list(addresses_str) do def addresses_as_list(addresses_str) do
addresses_str addresses_str
|> String.split(",") |> String.split(",")
|> Enum.map(&normalize/1) |> Enum.map(&normalize/1)
end end
@doc """ def mail_allowlisted_fun(signups_allowed, email_received) do
Checks if an email is allowed based on the allowlist configuration.
"""
@spec signup_allowed_fun(String.t() | nil, String.t()) :: boolean()
def signup_allowed_fun(signups_allowed, email_received) do
case signups_allowed do case signups_allowed do
nil -> false nil -> false
"*" -> true "*" -> true

View File

@ -3,27 +3,27 @@ defmodule BasicSignupAllowlistTest do
doctest BasicSignupAllowlist doctest BasicSignupAllowlist
defp allow_signups_for(allowlist) do defp allow_signups_for(allowlist) do
System.put_env(BasicSignupAllowlist.signup_allowlist_emails(), allowlist) System.put_env("SIGNUP_ALLOWLIST_EMAILS", allowlist)
end end
describe "setup in describe block" do describe "setup in describe block" do
setup do setup do
on_exit(fn -> on_exit(fn ->
# resetting values did not work, so 'just' set it to the effect without this module configured. # resetting values did not work, so 'just' set it to the effect without this module configured.
System.put_env(BasicSignupAllowlist.signup_allowlist_emails(), "*") System.put_env("SIGNUP_ALLOWLIST_EMAILS", "*")
end) end)
:ok :ok
end end
test "When not set, not allowlisted" do test "When not set, not allowlisted" do
System.delete_env(BasicSignupAllowlist.signup_allowlist_emails()) System.delete_env("SIGNUP_ALLOWLIST_EMAILS")
refute BasicSignupAllowlist.signup_allowed?("joe@example.com") refute BasicSignupAllowlist.mail_allowlisted("joe@example.com")
end end
test "When set to star, allowlisted" do test "When set to star, allowlisted" do
allow_signups_for("*") allow_signups_for("*")
assert BasicSignupAllowlist.signup_allowed?("joe@example.com") assert BasicSignupAllowlist.mail_allowlisted("joe@example.com")
end end
end end
end end

View File

@ -16,25 +16,25 @@ defmodule FunCore.BasicSignupAllowlistTest do
describe "Not allowlisted when allowed list is" do describe "Not allowlisted when allowed list is" do
test "not set" do test "not set" do
refute(signup_allowed_fun(nil, "joe@example.com")) refute(mail_allowlisted_fun(nil, "joe@example.com"))
end end
test "empty" do test "empty" do
refute(signup_allowed_fun("j", "joe@example.com")) refute(mail_allowlisted_fun("j", "joe@example.com"))
end end
end end
describe "Allowlisted when" do describe "Allowlisted when" do
test "*" do test "*" do
assert(signup_allowed_fun("*", "jane@example.com")) assert(mail_allowlisted_fun("*", "jane@example.com"))
end end
test "Multiple set and one match" do test "Multiple set and one match" do
assert(signup_allowed_fun("joe@example.com, jane@example.com", "jane@example.com")) assert(mail_allowlisted_fun("joe@example.com, jane@example.com", "jane@example.com"))
end end
test "Matches with different casings" do test "Matches with different casings" do
assert(signup_allowed_fun("joe@Example.com, jane@example.com", "Joe@example.com")) assert(mail_allowlisted_fun("joe@Example.com, jane@example.com", "Joe@example.com"))
end end
end end
end end