diff --git a/config/test.exs b/config/test.exs index 2d677af1..a18b3bea 100644 --- a/config/test.exs +++ b/config/test.exs @@ -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", diff --git a/test/bokken_web/controllers/auth_controller_test.exs b/test/bokken_web/controllers/auth_controller_test.exs new file mode 100644 index 00000000..eed701bc --- /dev/null +++ b/test/bokken_web/controllers/auth_controller_test.exs @@ -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 diff --git a/test/support/factories/accounts_factory.ex b/test/support/factories/accounts_factory.ex index 7c883b6f..c0aaa5b7 100644 --- a/test/support/factories/accounts_factory.ex +++ b/test/support/factories/accounts_factory.ex @@ -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!", role: sequence(:role, ["organizer", "guardian", "mentor", "ninja"]), active: true }