defmodule LiberatorExampleWeb.PostControllerTest do
use LiberatorExampleWeb.ConnCase
alias LiberatorExample.Blog
alias LiberatorExample.Blog.Post
@create_attrs %{
content: "some content",
title: "some title"
}
@update_attrs %{
content: "some updated content",
title: "some updated title"
}
@invalid_attrs %{content: nil, title: nil}
def fixture(:post) do
{:ok, post} = Blog.create_post(@create_attrs)
post
end
setup %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end
describe "index" do
test "lists all posts", %{conn: conn} do
conn = get(conn, Routes.post_path(conn, :index))
assert json_response(conn, 200)["data"] == []
end
end
describe "create post" do
test "renders post when data is valid", %{conn: conn} do
conn = post(conn, Routes.post_path(conn, :create), post: @create_attrs)
assert %{"id" => id} = json_response(conn, 201)["data"]
conn = get(conn, Routes.post_path(conn, :show, id))
assert %{
"id" => id,
"content" => "some content",
"title" => "some title"
} = json_response(conn, 200)["data"]
end
test "renders errors when data is invalid", %{conn: conn} do
conn = post(conn, Routes.post_path(conn, :create), post: @invalid_attrs)
assert json_response(conn, 422)["errors"] != %{}
end
end
describe "update post" do
setup [:create_post]
test "renders post when data is valid", %{conn: conn, post: %Post{id: id} = post} do
conn = put(conn, Routes.post_path(conn, :update, post), post: @update_attrs)
assert %{"id" => ^id} = json_response(conn, 200)["data"]
conn = get(conn, Routes.post_path(conn, :show, id))
assert %{
"id" => id,
"content" => "some updated content",
"title" => "some updated title"
} = json_response(conn, 200)["data"]
end
test "renders errors when data is invalid", %{conn: conn, post: post} do
conn = put(conn, Routes.post_path(conn, :update, post), post: @invalid_attrs)
assert json_response(conn, 422)["errors"] != %{}
end
end
describe "delete post" do
setup [:create_post]
test "deletes chosen post", %{conn: conn, post: post} do
conn = delete(conn, Routes.post_path(conn, :delete, post))
assert response(conn, 204)
conn = get(conn, Routes.post_path(conn, :show, post))
assert response(conn, 404)
end
end
defp create_post(_) do
post = fixture(:post)
%{post: post}
end
end