-
Couldn't load subscription status.
- Fork 1
test: add auth controller tests #198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
danielsp45
wants to merge
3
commits into
main
Choose a base branch
from
dp/add-auth-controller-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,118 @@ | ||
| defmodule BokkenWeb.AuthControllerTest do | ||
| use BokkenWeb.ConnCase | ||
|
|
||
| import Bokken.Factory | ||
|
|
||
| setup %{conn: conn} do | ||
| {:ok, conn: put_req_header(conn, "accept", "application/json")} | ||
| end | ||
|
|
||
| describe "sign_up" do | ||
| test "sign_up new user when data is valid", %{conn: conn} do | ||
| user_params = params_for(:user) | ||
|
|
||
| conn = post(conn, ~p"/api/auth/sign_up", user_params) | ||
|
|
||
| assert json_response(conn, 201) | ||
| end | ||
|
|
||
| test "sign_up new user when data is invalid", %{conn: conn} do | ||
| user_params = params_for(:user, %{role: "random"}) | ||
|
|
||
| conn = post(conn, ~p"/api/auth/sign_up", user_params) | ||
|
|
||
| assert json_response(conn, 422) == %{"errors" => %{"role" => ["não é válido"]}} | ||
| end | ||
| end | ||
|
|
||
| describe "sign_in" do | ||
| test "sign_in user when data is valid", %{conn: conn} do | ||
| user = insert(:user) | ||
|
|
||
| conn = post(conn, ~p"/api/auth/sign_in", %{email: user.email, password: user.password}) | ||
|
|
||
| assert json_response(conn, 200) | ||
| end | ||
|
|
||
| test "sign_in user when data is invalid", %{conn: conn} do | ||
| user = insert(:user) | ||
|
|
||
| conn = post(conn, ~p"/api/auth/sign_in", %{email: user.email, password: "random1234"}) | ||
|
|
||
| assert json_response(conn, 404) == %{"errors" => %{"detail" => "Not Found"}} | ||
| end | ||
| end | ||
|
|
||
| describe "show" do | ||
| test "shows current user when logged in", %{conn: conn} do | ||
| user = insert(:user) | ||
|
|
||
| conn = post(conn, ~p"/api/auth/sign_in", %{email: user.email, password: user.password}) | ||
| conn = get(conn, ~p"/api/auth/me") | ||
|
|
||
| assert json_response(conn, 200) | ||
| end | ||
|
|
||
| test "throws error when not logged in", %{conn: conn} do | ||
| conn = get(conn, ~p"/api/auth/me") | ||
|
|
||
| assert json_response(conn, 401) == %{"error" => "unauthenticated"} | ||
| end | ||
| end | ||
|
|
||
| describe "sign_out" do | ||
| setup [:login_as_guardian] | ||
|
|
||
| test "sign_out user", %{conn: conn} do | ||
| insert(:user) | ||
|
|
||
| conn = | ||
| delete(conn, ~p"/api/auth/sign_out") | ||
| |> get(~p"/api/auth/me") | ||
|
|
||
| assert json_response(conn, 401) == %{"error" => "unauthenticated"} | ||
| end | ||
| end | ||
|
|
||
| describe "update" do | ||
| setup [:login_as_guardian] | ||
|
|
||
| test "update new user", %{conn: conn} do | ||
| user_params = %{ | ||
| email: "random@gmail.com" | ||
| } | ||
|
|
||
| conn = put(conn, ~p"/api/auth/me", user: user_params) | ||
|
|
||
| assert json_response(conn, 200)["verified"] == false | ||
| end | ||
| end | ||
|
|
||
| describe "create" do | ||
| setup [:login_as_guardian] | ||
|
|
||
| test "create new guardian", %{conn: conn} do | ||
| params = params_for(:guardian) | ||
|
|
||
| conn = post(conn, ~p"/api/auth/me", user: params) | ||
|
|
||
| assert json_response(conn, 201) | ||
| end | ||
|
|
||
| test "create new ninja account", %{conn: conn} do | ||
| user = conn.private.guardian_default_resource | ||
| ninja = insert(:ninja, %{guardian: user.guardian}) | ||
|
|
||
| params = %{ | ||
| first_name: "Daniel", | ||
| last_name: "Pereira", | ||
| email: "ninja@gmail.com", | ||
| mobile: "929 066 896" | ||
| } | ||
|
|
||
| conn = post(conn, ~p"/api/auth/me", %{ninja_id: ninja.id, user: params}) | ||
|
|
||
| assert json_response(conn, 201) | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.