Skip to content
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

test: add auth controller tests #198

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ config :bokken, BokkenWeb.Endpoint,
port: String.to_integer(System.get_env("PORT", "4002"))
],
secret_key_base: "UYOacKoTtE8G5zQ4bjnfor+cxMxtRf3wOhpmYHPuMZDgrqtzzwXdt9uMfTb9wsSl",
server: true
server: true,
frontend_url: "http://localhost:3000"

config :bokken, Bokken.Authorization,
issuer: "bokken",
Expand Down
118 changes: 118 additions & 0 deletions test/bokken_web/controllers/auth_controller_test.exs
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: "[email protected]"
}

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: "[email protected]",
mobile: "929 066 896"
}
danielsp45 marked this conversation as resolved.
Show resolved Hide resolved

conn = post(conn, ~p"/api/auth/me", %{ninja_id: ninja.id, user: params})

assert json_response(conn, 201)
end
end
end
1 change: 1 addition & 0 deletions test/support/factories/accounts_factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defmodule Bokken.Factories.AccountFactory do
%User{
email: sequence(:email, &"email-#{&1}@mail.com"),
password_hash: Argon2.hash_pwd_salt("password1234!"),
password: "password1234!",
danielsp45 marked this conversation as resolved.
Show resolved Hide resolved
role: sequence(:role, ["organizer", "guardian", "mentor", "ninja"]),
active: true
}
Expand Down
Loading