-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* First steps of the unit tests * Added changeset validation in tests * Updated test names and small stuff * Got some good stuff working * Actually have login tests working * Formatting
- Loading branch information
Showing
5 changed files
with
121 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
defmodule PerseusWeb.Resolvers.AuthTest do | ||
use Perseus.DataCase | ||
|
||
alias Perseus.Auth | ||
alias PerseusWeb.Resolvers | ||
|
||
import Perseus.AccountsFixtures | ||
|
||
describe "login_user/3" do | ||
setup do | ||
%{user: user_fixture()} | ||
end | ||
|
||
test "with valid context should login user", %{user: user} do | ||
result = Resolvers.Auth.login_user(nil, nil, %{context: %{email: user.email}}) | ||
|
||
# Assert that the resolver returns :ok and a session_token | ||
assert {:ok, %{session_token: session_token}} = result | ||
assert {:ok, ^user} = Auth.find_user(session_token) | ||
end | ||
|
||
test "with invalid email address should return error" do | ||
result = Resolvers.Auth.login_user(nil, nil, %{context: %{email: "invalid@example.com"}}) | ||
|
||
# Assert that the resolver returns :ok and a session_token | ||
assert {:error, "User not found"} = result | ||
end | ||
end | ||
|
||
describe "signup_user/3" do | ||
setup do | ||
%{user: valid_user_attributes()} | ||
end | ||
|
||
test "with valid data should signup user", %{user: user} do | ||
result = Resolvers.Auth.signup_user(nil, %{user: user}, %{context: %{email: user.email}}) | ||
|
||
assert {:ok, %{session: _, new_user: created_user}} = result | ||
assert created_user.email == user.email | ||
assert created_user.first_name == user.first_name | ||
assert created_user.last_name == user.last_name | ||
assert created_user.verified == false | ||
end | ||
|
||
test "with invalid data should return a changeset" do | ||
result = | ||
Resolvers.Auth.signup_user(nil, %{user: %{first_name: "test"}}, %{ | ||
context: %{email: "invalid"} | ||
}) | ||
|
||
assert {:error, %Ecto.Changeset{} = changeset} = result | ||
assert "can't be blank" in errors_on(changeset).last_name | ||
end | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
defmodule PerseusWeb.Schema.Mutations.AuthMutationsTest do | ||
alias Perseus.Utils.BinaryUtils | ||
alias Perseus.Auth | ||
use PerseusWeb.ConnCase, async: true | ||
|
||
import Perseus.AccountsFixtures | ||
|
||
# Define the query that you will be testing | ||
@login_query """ | ||
mutation startSession { | ||
logIn { | ||
sessionToken | ||
} | ||
} | ||
""" | ||
|
||
describe "login mutation" do | ||
setup do | ||
user = user_fixture() | ||
{:ok, token} = Auth.send_login_email(user.email) | ||
%{user: user, token: token} | ||
end | ||
|
||
test "should login when email is found", %{conn: conn, user: user, token: token} do | ||
conn = | ||
conn | ||
|> put_req_header("authorization", "MagicLink " <> token) | ||
|> put_req_header("x-custom-header", "custom-value") | ||
|> post("/api/graphql", %{query: @login_query}) | ||
|
||
assert json_response(conn, 200) | ||
response_data = json_response(conn, 200)["data"] | ||
|
||
assert %{ | ||
"logIn" => %{ | ||
"sessionToken" => session_token | ||
} | ||
} = response_data | ||
|
||
{:ok, decoded_session_token} = BinaryUtils.decode(session_token) | ||
assert {:ok, ^user} = Auth.find_user(decoded_session_token) | ||
end | ||
end | ||
end |
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