-
-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate URLs for emails inside email templates, using verified routes
- Loading branch information
Showing
15 changed files
with
78 additions
and
114 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
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
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
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
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
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
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
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
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
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
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
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
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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
defmodule Asciinema.AccountsTest do | ||
import Asciinema.Fixtures | ||
import Asciinema.Factory | ||
use Asciinema.DataCase | ||
use Oban.Testing, repo: Asciinema.Repo | ||
alias Asciinema.Accounts | ||
|
@@ -10,81 +10,72 @@ defmodule Asciinema.AccountsTest do | |
end | ||
|
||
test "valid token" do | ||
token = Accounts.signup_token("[email protected]") | ||
{:ok, {:signup, token, _email}} = Accounts.generate_login_token("[email protected]") | ||
assert {:ok, "[email protected]"} = Accounts.verify_signup_token(token) | ||
end | ||
end | ||
|
||
describe "generate_login_url/3" do | ||
defmodule Routes do | ||
def signup_url(_), do: "http://signup" | ||
def login_url(_), do: "http://login" | ||
end | ||
|
||
test "existing user, by email" do | ||
user = fixture(:user) | ||
insert(:user, email: "[email protected]") | ||
|
||
assert Accounts.generate_login_url(user.email, true, Routes) == | ||
{:ok, {:login, "http://login", user.email}} | ||
assert {:ok, {:login, _token, "[email protected]"}} = | ||
Accounts.generate_login_token("[email protected]") | ||
end | ||
|
||
test "existing user, by username" do | ||
user = fixture(:user) | ||
insert(:user, username: "test", email: "[email protected]") | ||
|
||
assert Accounts.generate_login_url(user.username, true, Routes) == | ||
{:ok, {:login, "http://login", user.email}} | ||
assert {:ok, {:login, _token, "[email protected]"}} = Accounts.generate_login_token("test") | ||
end | ||
|
||
test "non-existing user, by email" do | ||
assert Accounts.generate_login_url("[email protected]", true, Routes) == | ||
{:ok, {:signup, "http://signup", "[email protected]"}} | ||
assert {:ok, {:signup, _token, "[email protected]"}} = | ||
Accounts.generate_login_token("[email protected]") | ||
|
||
assert Accounts.generate_login_url("[email protected]", true, Routes) == | ||
{:ok, {:signup, "http://signup", "[email protected]"}} | ||
assert {:ok, {:signup, _token, "[email protected]"}} = | ||
Accounts.generate_login_token("[email protected]") | ||
end | ||
|
||
test "non-existing user, by email, when sign up is disabled" do | ||
assert Accounts.generate_login_url("[email protected]", false, Routes) == | ||
{:error, :user_not_found} | ||
assert Accounts.generate_login_token("[email protected]", false) == {:error, :user_not_found} | ||
end | ||
|
||
test "non-existing user, by email, when email is invalid" do | ||
assert Accounts.generate_login_url("foo@", true, Routes) == {:error, :email_invalid} | ||
|
||
assert Accounts.generate_login_url("[email protected]", true, Routes) == | ||
{:error, :email_invalid} | ||
assert Accounts.generate_login_token("foo@") == {:error, :email_invalid} | ||
assert Accounts.generate_login_token("[email protected]") == {:error, :email_invalid} | ||
end | ||
|
||
test "non-existing user, by username" do | ||
assert Accounts.generate_login_url("idontexist", true, Routes) == {:error, :user_not_found} | ||
assert Accounts.generate_login_token("idontexist") == {:error, :user_not_found} | ||
end | ||
end | ||
|
||
describe "update_user/2" do | ||
setup do | ||
%{user: fixture(:user)} | ||
end | ||
|
||
def success(user, attrs) do | ||
assert {:ok, user} = Accounts.update_user(user, attrs) | ||
test "success" do | ||
user = insert(:user) | ||
|
||
user | ||
end | ||
|
||
test "success", %{user: user} do | ||
assert success(user, %{email: "[email protected]"}) | ||
assert success(user, %{email: "[email protected]"}).email == "[email protected]" | ||
assert success(user, %{username: "newone"}) | ||
end | ||
|
||
def assert_validation_error(user, attrs) do | ||
assert {:error, %Ecto.Changeset{}} = Accounts.update_user(user, attrs) | ||
end | ||
test "validation failures" do | ||
user = insert(:user) | ||
|
||
test "validation failures", %{user: user} do | ||
assert_validation_error(user, %{email: "newone.com"}) | ||
assert_validation_error(user, %{email: ""}) | ||
assert_validation_error(user, %{username: ""}) | ||
end | ||
|
||
defp success(user, attrs) do | ||
assert {:ok, user} = Accounts.update_user(user, attrs) | ||
|
||
user | ||
end | ||
|
||
defp assert_validation_error(user, attrs) do | ||
assert {:error, %Ecto.Changeset{}} = Accounts.update_user(user, attrs) | ||
end | ||
end | ||
end |
Oops, something went wrong.