Create user

Create profile
added some validation
This commit is contained in:
2023-02-17 08:29:33 +00:00
parent 57dae2bee3
commit bb1f5d74d8
19 changed files with 797 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
defmodule PhoenixApiTemplate.AccountsTest do
use PhoenixApiTemplate.DataCase
alias PhoenixApiTemplate.Accounts
describe "users" do
alias PhoenixApiTemplate.Accounts.User
import PhoenixApiTemplate.AccountsFixtures
@invalid_attrs %{email: nil, hashed_password: nil}
test "list_users/0 returns all users" do
user = user_fixture()
assert Accounts.list_users() == [user]
end
test "get_user!/1 returns the user with given id" do
user = user_fixture()
assert Accounts.get_user!(user.id) == user
end
test "create_user/1 with valid data creates a user" do
valid_attrs = %{email: "some email", hashed_password: "some hashed_password"}
assert {:ok, %User{} = user} = Accounts.create_user(valid_attrs)
assert user.email == "some email"
assert user.hashed_password == "some hashed_password"
end
test "create_user/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Accounts.create_user(@invalid_attrs)
end
test "update_user/2 with valid data updates the user" do
user = user_fixture()
update_attrs = %{email: "some updated email", hashed_password: "some updated hashed_password"}
assert {:ok, %User{} = user} = Accounts.update_user(user, update_attrs)
assert user.email == "some updated email"
assert user.hashed_password == "some updated hashed_password"
end
test "update_user/2 with invalid data returns error changeset" do
user = user_fixture()
assert {:error, %Ecto.Changeset{}} = Accounts.update_user(user, @invalid_attrs)
assert user == Accounts.get_user!(user.id)
end
test "delete_user/1 deletes the user" do
user = user_fixture()
assert {:ok, %User{}} = Accounts.delete_user(user)
assert_raise Ecto.NoResultsError, fn -> Accounts.get_user!(user.id) end
end
test "change_user/1 returns a user changeset" do
user = user_fixture()
assert %Ecto.Changeset{} = Accounts.change_user(user)
end
end
end

View File

@@ -0,0 +1,59 @@
defmodule PhoenixApiTemplate.ProfilesTest do
use PhoenixApiTemplate.DataCase
alias PhoenixApiTemplate.Profiles
describe "profiles" do
alias PhoenixApiTemplate.Profiles.Profile
import PhoenixApiTemplate.ProfilesFixtures
@invalid_attrs %{display_name: nil}
test "list_profiles/0 returns all profiles" do
profile = profile_fixture()
assert Profiles.list_profiles() == [profile]
end
test "get_profile!/1 returns the profile with given id" do
profile = profile_fixture()
assert Profiles.get_profile!(profile.id) == profile
end
test "create_profile/1 with valid data creates a profile" do
valid_attrs = %{display_name: "some display_name"}
assert {:ok, %Profile{} = profile} = Profiles.create_profile(valid_attrs)
assert profile.display_name == "some display_name"
end
test "create_profile/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Profiles.create_profile(@invalid_attrs)
end
test "update_profile/2 with valid data updates the profile" do
profile = profile_fixture()
update_attrs = %{display_name: "some updated display_name"}
assert {:ok, %Profile{} = profile} = Profiles.update_profile(profile, update_attrs)
assert profile.display_name == "some updated display_name"
end
test "update_profile/2 with invalid data returns error changeset" do
profile = profile_fixture()
assert {:error, %Ecto.Changeset{}} = Profiles.update_profile(profile, @invalid_attrs)
assert profile == Profiles.get_profile!(profile.id)
end
test "delete_profile/1 deletes the profile" do
profile = profile_fixture()
assert {:ok, %Profile{}} = Profiles.delete_profile(profile)
assert_raise Ecto.NoResultsError, fn -> Profiles.get_profile!(profile.id) end
end
test "change_profile/1 returns a profile changeset" do
profile = profile_fixture()
assert %Ecto.Changeset{} = Profiles.change_profile(profile)
end
end
end

View File

@@ -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

View File

@@ -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

View File

@@ -0,0 +1,26 @@
defmodule PhoenixApiTemplate.AccountsFixtures do
@moduledoc """
This module defines test helpers for creating
entities via the `PhoenixApiTemplate.Accounts` context.
"""
@doc """
Generate a unique user email.
"""
def unique_user_email, do: "some email#{System.unique_integer([:positive])}"
@doc """
Generate a user.
"""
def user_fixture(attrs \\ %{}) do
{:ok, user} =
attrs
|> Enum.into(%{
email: unique_user_email(),
hashed_password: "some hashed_password"
})
|> PhoenixApiTemplate.Accounts.create_user()
user
end
end

View File

@@ -0,0 +1,20 @@
defmodule PhoenixApiTemplate.ProfilesFixtures do
@moduledoc """
This module defines test helpers for creating
entities via the `PhoenixApiTemplate.Profiles` context.
"""
@doc """
Generate a profile.
"""
def profile_fixture(attrs \\ %{}) do
{:ok, profile} =
attrs
|> Enum.into(%{
display_name: "some display_name"
})
|> PhoenixApiTemplate.Profiles.create_profile()
profile
end
end