2017-12-08 09:58:14 +01:00
|
|
|
defmodule EventosWeb.CategoryControllerTest do
|
|
|
|
use EventosWeb.ConnCase
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
import Eventos.Factory
|
|
|
|
|
2017-12-08 09:58:14 +01:00
|
|
|
alias Eventos.Events
|
2018-01-13 23:33:03 +01:00
|
|
|
alias Eventos.Events.Category
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
@create_attrs %{description: "some description", picture: "some picture", title: "some title"}
|
|
|
|
@update_attrs %{description: "some updated description", picture: "some updated picture", title: "some updated title"}
|
|
|
|
@invalid_attrs %{description: nil, picture: nil, title: nil}
|
2017-12-08 09:58:14 +01:00
|
|
|
|
|
|
|
def fixture(:category) do
|
|
|
|
{:ok, category} = Events.create_category(@create_attrs)
|
|
|
|
category
|
|
|
|
end
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
setup %{conn: conn} do
|
2018-07-10 10:00:07 +02:00
|
|
|
user = insert(:user)
|
|
|
|
actor = insert(:actor, user: user)
|
2018-01-13 23:33:03 +01:00
|
|
|
{:ok, conn: conn, user: user}
|
|
|
|
end
|
|
|
|
|
2017-12-08 09:58:14 +01:00
|
|
|
describe "index" do
|
|
|
|
test "lists all categories", %{conn: conn} do
|
|
|
|
conn = get conn, category_path(conn, :index)
|
2018-01-13 23:33:03 +01:00
|
|
|
assert json_response(conn, 200)["data"] == []
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "create category" do
|
2018-01-13 23:33:03 +01:00
|
|
|
test "renders category when data is valid", %{conn: conn, user: user} do
|
|
|
|
conn = auth_conn(conn, user)
|
2017-12-08 09:58:14 +01:00
|
|
|
conn = post conn, category_path(conn, :create), category: @create_attrs
|
2018-01-13 23:33:03 +01:00
|
|
|
assert %{"id" => id} = json_response(conn, 201)["data"]
|
2017-12-08 09:58:14 +01:00
|
|
|
|
|
|
|
conn = get conn, category_path(conn, :show, id)
|
2018-01-13 23:33:03 +01:00
|
|
|
assert json_response(conn, 200)["data"] == %{
|
|
|
|
"id" => id,
|
|
|
|
"description" => "some description",
|
|
|
|
"picture" => "some picture",
|
|
|
|
"title" => "some title"}
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
test "renders errors when data is invalid", %{conn: conn, user: user} do
|
|
|
|
conn = auth_conn(conn, user)
|
2017-12-08 09:58:14 +01:00
|
|
|
conn = post conn, category_path(conn, :create), category: @invalid_attrs
|
2018-01-13 23:33:03 +01:00
|
|
|
assert json_response(conn, 422)["errors"] != %{}
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "update category" do
|
|
|
|
setup [:create_category]
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
test "renders category when data is valid", %{conn: conn, category: %Category{id: id} = category, user: user} do
|
|
|
|
conn = auth_conn(conn, user)
|
2017-12-08 09:58:14 +01:00
|
|
|
conn = put conn, category_path(conn, :update, category), category: @update_attrs
|
2018-01-13 23:33:03 +01:00
|
|
|
assert %{"id" => ^id} = json_response(conn, 200)["data"]
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
conn = get conn, category_path(conn, :show, id)
|
|
|
|
assert json_response(conn, 200)["data"] == %{
|
|
|
|
"id" => id,
|
|
|
|
"description" => "some updated description",
|
|
|
|
"picture" => "some updated picture",
|
|
|
|
"title" => "some updated title"}
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
test "renders errors when data is invalid", %{conn: conn, category: category, user: user} do
|
|
|
|
conn = auth_conn(conn, user)
|
2017-12-08 09:58:14 +01:00
|
|
|
conn = put conn, category_path(conn, :update, category), category: @invalid_attrs
|
2018-01-13 23:33:03 +01:00
|
|
|
assert json_response(conn, 422)["errors"] != %{}
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "delete category" do
|
|
|
|
setup [:create_category]
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
test "deletes chosen category", %{conn: conn, category: category, user: user} do
|
|
|
|
conn = auth_conn(conn, user)
|
2017-12-08 09:58:14 +01:00
|
|
|
conn = delete conn, category_path(conn, :delete, category)
|
2018-01-13 23:33:03 +01:00
|
|
|
assert response(conn, 204)
|
2017-12-08 09:58:14 +01:00
|
|
|
assert_error_sent 404, fn ->
|
|
|
|
get conn, category_path(conn, :show, category)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp create_category(_) do
|
|
|
|
category = fixture(:category)
|
|
|
|
{:ok, category: category}
|
|
|
|
end
|
|
|
|
end
|