mirror of
https://github.com/kevin-DL/phoenix_api_template.git
synced 2026-01-11 18:54:33 +00:00
Create user
Create profile added some validation
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
defmodule PhoenixApiTemplateWeb.ProfileControllerTest do
|
||||
use PhoenixApiTemplateWeb.ConnCase
|
||||
|
||||
import PhoenixApiTemplate.ProfilesFixtures
|
||||
|
||||
alias PhoenixApiTemplate.Profiles.Profile
|
||||
|
||||
@create_attrs %{
|
||||
display_name: "some display_name"
|
||||
}
|
||||
@update_attrs %{
|
||||
display_name: "some updated display_name"
|
||||
}
|
||||
@invalid_attrs %{display_name: nil}
|
||||
|
||||
setup %{conn: conn} do
|
||||
{:ok, conn: put_req_header(conn, "accept", "application/json")}
|
||||
end
|
||||
|
||||
describe "index" do
|
||||
test "lists all profiles", %{conn: conn} do
|
||||
conn = get(conn, Routes.profile_path(conn, :index))
|
||||
assert json_response(conn, 200)["data"] == []
|
||||
end
|
||||
end
|
||||
|
||||
describe "create profile" do
|
||||
test "renders profile when data is valid", %{conn: conn} do
|
||||
conn = post(conn, Routes.profile_path(conn, :create), profile: @create_attrs)
|
||||
assert %{"id" => id} = json_response(conn, 201)["data"]
|
||||
|
||||
conn = get(conn, Routes.profile_path(conn, :show, id))
|
||||
|
||||
assert %{
|
||||
"id" => ^id,
|
||||
"display_name" => "some display_name"
|
||||
} = json_response(conn, 200)["data"]
|
||||
end
|
||||
|
||||
test "renders errors when data is invalid", %{conn: conn} do
|
||||
conn = post(conn, Routes.profile_path(conn, :create), profile: @invalid_attrs)
|
||||
assert json_response(conn, 422)["errors"] != %{}
|
||||
end
|
||||
end
|
||||
|
||||
describe "update profile" do
|
||||
setup [:create_profile]
|
||||
|
||||
test "renders profile when data is valid", %{conn: conn, profile: %Profile{id: id} = profile} do
|
||||
conn = put(conn, Routes.profile_path(conn, :update, profile), profile: @update_attrs)
|
||||
assert %{"id" => ^id} = json_response(conn, 200)["data"]
|
||||
|
||||
conn = get(conn, Routes.profile_path(conn, :show, id))
|
||||
|
||||
assert %{
|
||||
"id" => ^id,
|
||||
"display_name" => "some updated display_name"
|
||||
} = json_response(conn, 200)["data"]
|
||||
end
|
||||
|
||||
test "renders errors when data is invalid", %{conn: conn, profile: profile} do
|
||||
conn = put(conn, Routes.profile_path(conn, :update, profile), profile: @invalid_attrs)
|
||||
assert json_response(conn, 422)["errors"] != %{}
|
||||
end
|
||||
end
|
||||
|
||||
describe "delete profile" do
|
||||
setup [:create_profile]
|
||||
|
||||
test "deletes chosen profile", %{conn: conn, profile: profile} do
|
||||
conn = delete(conn, Routes.profile_path(conn, :delete, profile))
|
||||
assert response(conn, 204)
|
||||
|
||||
assert_error_sent 404, fn ->
|
||||
get(conn, Routes.profile_path(conn, :show, profile))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp create_profile(_) do
|
||||
profile = profile_fixture()
|
||||
%{profile: profile}
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,88 @@
|
||||
defmodule PhoenixApiTemplateWeb.UserControllerTest do
|
||||
use PhoenixApiTemplateWeb.ConnCase
|
||||
|
||||
import PhoenixApiTemplate.AccountsFixtures
|
||||
|
||||
alias PhoenixApiTemplate.Accounts.User
|
||||
|
||||
@create_attrs %{
|
||||
email: "some email",
|
||||
hashed_password: "some hashed_password"
|
||||
}
|
||||
@update_attrs %{
|
||||
email: "some updated email",
|
||||
hashed_password: "some updated hashed_password"
|
||||
}
|
||||
@invalid_attrs %{email: nil, hashed_password: nil}
|
||||
|
||||
setup %{conn: conn} do
|
||||
{:ok, conn: put_req_header(conn, "accept", "application/json")}
|
||||
end
|
||||
|
||||
describe "index" do
|
||||
test "lists all users", %{conn: conn} do
|
||||
conn = get(conn, Routes.user_path(conn, :index))
|
||||
assert json_response(conn, 200)["data"] == []
|
||||
end
|
||||
end
|
||||
|
||||
describe "create user" do
|
||||
test "renders user when data is valid", %{conn: conn} do
|
||||
conn = post(conn, Routes.user_path(conn, :create), user: @create_attrs)
|
||||
assert %{"id" => id} = json_response(conn, 201)["data"]
|
||||
|
||||
conn = get(conn, Routes.user_path(conn, :show, id))
|
||||
|
||||
assert %{
|
||||
"id" => ^id,
|
||||
"email" => "some email",
|
||||
"hashed_password" => "some hashed_password"
|
||||
} = json_response(conn, 200)["data"]
|
||||
end
|
||||
|
||||
test "renders errors when data is invalid", %{conn: conn} do
|
||||
conn = post(conn, Routes.user_path(conn, :create), user: @invalid_attrs)
|
||||
assert json_response(conn, 422)["errors"] != %{}
|
||||
end
|
||||
end
|
||||
|
||||
describe "update user" do
|
||||
setup [:create_user]
|
||||
|
||||
test "renders user when data is valid", %{conn: conn, user: %User{id: id} = user} do
|
||||
conn = put(conn, Routes.user_path(conn, :update, user), user: @update_attrs)
|
||||
assert %{"id" => ^id} = json_response(conn, 200)["data"]
|
||||
|
||||
conn = get(conn, Routes.user_path(conn, :show, id))
|
||||
|
||||
assert %{
|
||||
"id" => ^id,
|
||||
"email" => "some updated email",
|
||||
"hashed_password" => "some updated hashed_password"
|
||||
} = json_response(conn, 200)["data"]
|
||||
end
|
||||
|
||||
test "renders errors when data is invalid", %{conn: conn, user: user} do
|
||||
conn = put(conn, Routes.user_path(conn, :update, user), user: @invalid_attrs)
|
||||
assert json_response(conn, 422)["errors"] != %{}
|
||||
end
|
||||
end
|
||||
|
||||
describe "delete user" do
|
||||
setup [:create_user]
|
||||
|
||||
test "deletes chosen user", %{conn: conn, user: user} do
|
||||
conn = delete(conn, Routes.user_path(conn, :delete, user))
|
||||
assert response(conn, 204)
|
||||
|
||||
assert_error_sent 404, fn ->
|
||||
get(conn, Routes.user_path(conn, :show, user))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp create_user(_) do
|
||||
user = user_fixture()
|
||||
%{user: user}
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user