Skip to content

Commit

Permalink
refacto_test(accounts): reworked controller tests after guardian impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabrielparizet committed Jul 3, 2024
1 parent 0e1e92e commit e988325
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 22 deletions.
10 changes: 7 additions & 3 deletions lib/book_my_gigs/accounts/storage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions lib/book_my_gigs/auth_error_handler.ex
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/book_my_gigs/guardian.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
defmodule BookMyGigs.Guardian do
@moduledoc """
Guardian implementation module
"""
use Guardian, otp_app: :book_my_gigs

alias BookMyGigs.Accounts
Expand Down
9 changes: 0 additions & 9 deletions lib/book_my_gigs_web/accounts/accounts_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 7 additions & 10 deletions test/book_my_gigs_web/accounts/accounts_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e988325

Please sign in to comment.