From e988325d3a37833fc908aa9fa41e35f8611e9f39 Mon Sep 17 00:00:00 2001 From: Gabrielparizet Date: Wed, 3 Jul 2024 15:09:52 +0200 Subject: [PATCH] refacto_test(accounts): reworked controller tests after guardian impl --- lib/book_my_gigs/accounts/storage.ex | 10 +++++++--- lib/book_my_gigs/auth_error_handler.ex | 3 +++ lib/book_my_gigs/guardian.ex | 3 +++ .../accounts/accounts_controller.ex | 9 --------- .../accounts/accounts_controller_test.exs | 17 +++++++---------- 5 files changed, 20 insertions(+), 22 deletions(-) diff --git a/lib/book_my_gigs/accounts/storage.ex b/lib/book_my_gigs/accounts/storage.ex index dcce507..a35154f 100644 --- a/lib/book_my_gigs/accounts/storage.ex +++ b/lib/book_my_gigs/accounts/storage.ex @@ -26,11 +26,15 @@ defmodule BookMyGigs.Accounts.Storage do password: params["password"] } - changeset = Storage.Account.changeset(%Storage.Account{}, params) + changeset = + Storage.Account.changeset(%Storage.Account{}, params) case Repo.insert(changeset) do - {:ok, account} -> {:ok, to_context_struct(account)} - {:error, changeset} -> {:error, changeset} + {:ok, account} -> + {:ok, to_context_struct(account)} + + {:error, changeset} -> + {:error, changeset} end end diff --git a/lib/book_my_gigs/auth_error_handler.ex b/lib/book_my_gigs/auth_error_handler.ex index 9c77a9d..9d967dc 100644 --- a/lib/book_my_gigs/auth_error_handler.ex +++ b/lib/book_my_gigs/auth_error_handler.ex @@ -1,4 +1,7 @@ defmodule BookMyGigs.AuthErrorHandler do + @moduledoc """ + Module for handling protected routes errors + """ import Plug.Conn def auth_error(conn, {type, _reason}, _opts) do diff --git a/lib/book_my_gigs/guardian.ex b/lib/book_my_gigs/guardian.ex index 857e140..559e3e5 100644 --- a/lib/book_my_gigs/guardian.ex +++ b/lib/book_my_gigs/guardian.ex @@ -1,4 +1,7 @@ defmodule BookMyGigs.Guardian do + @moduledoc """ + Guardian implementation module + """ use Guardian, otp_app: :book_my_gigs alias BookMyGigs.Accounts diff --git a/lib/book_my_gigs_web/accounts/accounts_controller.ex b/lib/book_my_gigs_web/accounts/accounts_controller.ex index 055f92f..6d0346b 100644 --- a/lib/book_my_gigs_web/accounts/accounts_controller.ex +++ b/lib/book_my_gigs_web/accounts/accounts_controller.ex @@ -56,15 +56,6 @@ defmodule BookMyGigsWeb.AccountsController do |> put_resp_content_type("application/json") |> send_resp(400, Jason.encode!(%{error: reason})) end - - account = - params - |> Accounts.create_account() - |> Jason.encode!() - - conn - |> put_resp_content_type("application/json") - |> send_resp(201, account) end operation(:update, diff --git a/test/book_my_gigs_web/accounts/accounts_controller_test.exs b/test/book_my_gigs_web/accounts/accounts_controller_test.exs index 5013c4c..75cf123 100644 --- a/test/book_my_gigs_web/accounts/accounts_controller_test.exs +++ b/test/book_my_gigs_web/accounts/accounts_controller_test.exs @@ -9,14 +9,6 @@ defmodule BookMyGigsWeb.Accounts.AccountsControllerTest do alias BookMyGigs.Accounts.Storage alias OpenApiSpex.TestAssertions - setup do - case Ecto.Adapters.SQL.Sandbox.checkout(Repo) do - :ok -> :ok - {:already, :allowed} -> :ok - {:error, reason} -> raise "Failed to checkout sandbox: #{inspect(reason)}" - end - end - test "Get all accounts", %{conn: conn} do api_spec = ApiSpec.spec() @@ -59,10 +51,15 @@ defmodule BookMyGigsWeb.Accounts.AccountsControllerTest do |> put_req_header("content-type", "application/json") |> post("/api/accounts", account_payload) - json_data = json_response(conn_out, 201) + json_data = + json_response(conn_out, 201) assert json_data == %{ - "email" => "test@email.com" + "account" => %{ + "email" => "test@email.com", + "id" => json_data["account"]["id"] + }, + "token" => json_data["token"] } TestAssertions.assert_schema(json_data, "Account response", api_spec)