Skip to content

Commit

Permalink
test(users): user controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabrielparizet committed Nov 11, 2024
1 parent 30f1f96 commit 38f9249
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 25 deletions.
16 changes: 13 additions & 3 deletions lib/book_my_gigs/users.ex
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ defmodule BookMyGigs.Users do
end

def create_user(%{"user" => user_params}, account_id) do
user_params
|> Storage.create_user(account_id)
|> to_context_struct()
case Storage.create_user(user_params, account_id) do
{:ok, user} -> {:ok, to_context_struct(user)}
{:error, :account_not_found} -> {:error, :account_not_found}
{:error, changeset} -> {:error, changeset_errors(changeset)}
end
end

def update_user(
Expand Down Expand Up @@ -252,4 +254,12 @@ defmodule BookMyGigs.Users do
end
end)
end

defp changeset_errors(changeset) do
Ecto.Changeset.traverse_errors(changeset, fn {msg, opts} ->
Enum.reduce(opts, msg, fn {key, value}, acc ->
String.replace(acc, "%{#{key}}", to_string(value))
end)
end)
end
end
7 changes: 4 additions & 3 deletions lib/book_my_gigs/users/storage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ defmodule BookMyGigs.Users.Storage do

case Repo.insert(changeset) do
{:ok, user} ->
user
|> Repo.preload(:location)
|> Repo.preload(:genres)
{:ok,
user
|> Repo.preload(:location)
|> Repo.preload(:genres)}

{:error, changeset} ->
{:error, changeset}
Expand Down
4 changes: 2 additions & 2 deletions lib/book_my_gigs_web/events/events_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ defmodule BookMyGigsWeb.EventsController do
|> put_resp_content_type("application/json")
|> send_resp(200, Jason.encode!(event))

{:error, changeset} ->
{:error, changeset_errors} ->
conn
|> put_resp_content_type("application/json")
|> send_resp(422, Jason.encode!(%{error: changeset}))
|> send_resp(422, Jason.encode!(%{error: changeset_errors}))
end
end

Expand Down
24 changes: 16 additions & 8 deletions lib/book_my_gigs_web/users/users_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,22 @@ defmodule BookMyGigsWeb.UsersController do
def create(conn, params) do
account_id = conn.private[:guardian_default_resource].id

user =
params
|> Users.create_user(account_id)
|> Jason.encode!()

conn
|> put_resp_content_type("application/json")
|> send_resp(200, user)
case Users.create_user(params, account_id) do
{:ok, user} ->
conn
|> put_resp_content_type("application/json")
|> send_resp(200, Jason.encode!(user))

{:error, :account_not_found} ->
conn
|> put_resp_content_type("application/json")
|> send_resp(401, Jason.encode!(%{error: "Unauthorized"}))

{:error, changeset_errors} ->
conn
|> put_resp_content_type("application/json")
|> send_resp(422, Jason.encode!(%{error: changeset_errors}))
end
end

operation(:update,
Expand Down
63 changes: 54 additions & 9 deletions test/book_my_gigs_web/users/users_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,62 @@ defmodule BookMyGigsWeb.Users.UsersControllerTest do
{:ok, user} = Users.get_user_by_account_id(account.id)

assert json_data == %{
"id" => user.id,
"location" => nil,
"username" => "johndo2000",
"account_id" => account.id,
"genres" => [],
"first_name" => "John",
"last_name" => "Doe",
"birthday" => "2000-01-01"
}
"id" => user.id,
"location" => nil,
"username" => "johndo2000",
"account_id" => account.id,
"genres" => [],
"first_name" => "John",
"last_name" => "Doe",
"birthday" => "2000-01-01"
}

TestAssertions.assert_schema(user_payload, "User response", api_spec)
end

test "fails to create a user when a username is already taken", %{conn: conn} do
account_1 =
%Accounts.Storage.Account{
email: "test@gmail.com",
password: "ThisIsMyPassword123"
}
|> Repo.insert!()

_user_1 =
%Users.Storage.User{
account_id: account_1.id,
username: "johndo2000",
first_name: "John",
last_name: "Doe",
birthday: ~D[1969-08-26]
}
|> Repo.insert!()

account_2 =
%Accounts.Storage.Account{
email: "test2@gmail.com",
password: "ThisIsMyPassword123"
}
|> Repo.insert!()

user2_payload = %{
"user" => %{
"username" => "johndo2000",
"first_name" => "John",
"last_name" => "Doe",
"birthday" => "01/01/2000"
}
}

conn_out =
conn
|> authenticate_user(account_2)
|> put_req_header("content-type", "application/json")
|> post("/api/users", user2_payload)

json_data = json_response(conn_out, 422)

assert json_data == %{"error" => %{"username" => ["has already been taken"]}}
end
end
end

0 comments on commit 38f9249

Please sign in to comment.