From 54824d2edb9f425d82cceacf8031833d4fe50f4a Mon Sep 17 00:00:00 2001 From: daniel_sp Date: Sat, 30 Sep 2023 18:24:13 +0100 Subject: [PATCH 1/2] feat: add auth controller tests --- config/test.exs | 3 +- .../controllers/auth_controller_test.exs | 132 ++++++++++++++++++ test/support/factories/accounts_factory.ex | 1 + 3 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 test/bokken_web/controllers/auth_controller_test.exs 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..acc7fbd4 --- /dev/null +++ b/test/bokken_web/controllers/auth_controller_test.exs @@ -0,0 +1,132 @@ +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 = %{ + email: "example@mail.com", + password: "password1234", + role: "guardian" + } + + 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 = %{ + email: "example@mail.com", + password: "password1234", + 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) + end + end + + describe "create" do + setup [:login_as_guardian] + + test "create new guardian", %{conn: conn} do + params = %{ + first_name: "Daniel", + last_name: "Pereira", + email: "random@gmail.com", + role: "guardian", + mobile: "929 066 896" + } + + 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 } From fe426b192997ac689fc9af6dad722a59d0cac51d Mon Sep 17 00:00:00 2001 From: daniel_sp Date: Thu, 5 Oct 2023 19:24:38 +0100 Subject: [PATCH 2/2] Add suggested changes --- .../controllers/auth_controller_test.exs | 26 +++++-------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/test/bokken_web/controllers/auth_controller_test.exs b/test/bokken_web/controllers/auth_controller_test.exs index acc7fbd4..eed701bc 100644 --- a/test/bokken_web/controllers/auth_controller_test.exs +++ b/test/bokken_web/controllers/auth_controller_test.exs @@ -7,13 +7,9 @@ defmodule BokkenWeb.AuthControllerTest do {:ok, conn: put_req_header(conn, "accept", "application/json")} end - describe "sign_up " do + describe "sign_up" do test "sign_up new user when data is valid", %{conn: conn} do - user_params = %{ - email: "example@mail.com", - password: "password1234", - role: "guardian" - } + user_params = params_for(:user) conn = post(conn, ~p"/api/auth/sign_up", user_params) @@ -21,11 +17,7 @@ defmodule BokkenWeb.AuthControllerTest do end test "sign_up new user when data is invalid", %{conn: conn} do - user_params = %{ - email: "example@mail.com", - password: "password1234", - role: "random" - } + user_params = params_for(:user, %{role: "random"}) conn = post(conn, ~p"/api/auth/sign_up", user_params) @@ -33,7 +25,7 @@ defmodule BokkenWeb.AuthControllerTest do end end - describe "sign_in " do + describe "sign_in" do test "sign_in user when data is valid", %{conn: conn} do user = insert(:user) @@ -92,7 +84,7 @@ defmodule BokkenWeb.AuthControllerTest do conn = put(conn, ~p"/api/auth/me", user: user_params) - assert json_response(conn, 200) + assert json_response(conn, 200)["verified"] == false end end @@ -100,13 +92,7 @@ defmodule BokkenWeb.AuthControllerTest do setup [:login_as_guardian] test "create new guardian", %{conn: conn} do - params = %{ - first_name: "Daniel", - last_name: "Pereira", - email: "random@gmail.com", - role: "guardian", - mobile: "929 066 896" - } + params = params_for(:guardian) conn = post(conn, ~p"/api/auth/me", user: params)