Skip to content

Commit

Permalink
Improve email validation regex
Browse files Browse the repository at this point in the history
  • Loading branch information
ku1ik committed May 14, 2024
1 parent 9cbcd1c commit d4c5042
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/asciinema/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Asciinema.Accounts do
alias Asciinema.{Fonts, Repo, Themes}
alias Ecto.Changeset

@valid_email_re ~r/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i
@valid_email_re ~r/^[A-Z0-9._%+-]+@([A-Z0-9-]+\.)+[A-Z]{2,}$/i
@valid_username_re ~r/^[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]$/

def fetch_user(id) do
Expand Down
14 changes: 10 additions & 4 deletions test/asciinema/accounts_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,23 @@ defmodule Asciinema.AccountsTest do
end

test "non-existing user, by email" do
assert Accounts.generate_login_url("new@example.com", true, Routes) ==
{:ok, {:signup, "http://signup", "new@example.com"}}
assert Accounts.generate_login_url("foo@example.com", true, Routes) ==
{:ok, {:signup, "http://signup", "foo@example.com"}}

assert Accounts.generate_login_url("foo@ex.ample.com", true, Routes) ==
{:ok, {:signup, "http://signup", "foo@ex.ample.com"}}
end

test "non-existing user, by email, when sign up is disabled" do
assert Accounts.generate_login_url("new@example.com", false, Routes) ==
assert Accounts.generate_login_url("foo@example.com", false, Routes) ==
{:error, :user_not_found}
end

test "non-existing user, by email, when email is invalid" do
assert Accounts.generate_login_url("new@", true, Routes) == {:error, :email_invalid}
assert Accounts.generate_login_url("foo@", true, Routes) == {:error, :email_invalid}

assert Accounts.generate_login_url("foo@ex.ample..com", true, Routes) ==
{:error, :email_invalid}
end

test "non-existing user, by username" do
Expand Down

0 comments on commit d4c5042

Please sign in to comment.