7 changed files with 165 additions and 28 deletions
@ -0,0 +1,62 @@ |
|||
defmodule DpsWeb.AuthorControllerTest do |
|||
use DpsWeb.ConnCase |
|||
|
|||
@create_attrs %{name: "Some Author"} |
|||
@invalid_attrs %{name: nil} |
|||
|
|||
describe "index" do |
|||
test "GET /authors", %{conn: conn} do |
|||
conn = get(conn, Routes.author_path(conn, :index)) |
|||
|
|||
assert html_response(conn, 200) =~ "Authors" |
|||
end |
|||
end |
|||
|
|||
describe "new author" do |
|||
test "is blocked with auth", %{conn: conn} do |
|||
conn = get(conn, Routes.author_path(conn, :new)) |
|||
assert response(conn, 401) =~ "Unauthorized" |
|||
end |
|||
|
|||
test "GET /authors/new", %{conn: conn} do |
|||
conn = |
|||
put_req_header(conn, "authorization", Plug.BasicAuth.encode_basic_auth("user", "secret")) |
|||
|
|||
conn = get(conn, Routes.author_path(conn, :new)) |
|||
|
|||
assert html_response(conn, 200) =~ "Add Author" |
|||
end |
|||
end |
|||
|
|||
describe "create author" do |
|||
test "is blocked with auth", %{conn: conn} do |
|||
conn = post(conn, Routes.author_path(conn, :create), author: @create_attrs) |
|||
assert response(conn, 401) =~ "Unauthorized" |
|||
end |
|||
|
|||
test "redirects to poem when data is valid", %{conn: conn} do |
|||
conn = |
|||
put_req_header(conn, "authorization", Plug.BasicAuth.encode_basic_auth("user", "secret")) |
|||
|
|||
conn = post(conn, Routes.author_path(conn, :create), author: @create_attrs) |
|||
|
|||
# Assert that we got redirected to create a new poem |
|||
poem_path = Routes.poem_path(conn, :new, %{author: 0}) |> String.slice(0..-2) |
|||
assert redirected_to(conn) |> String.starts_with?(poem_path) |
|||
id = redirected_to(conn) |> String.trim(poem_path) |
|||
assert redirected_to(conn) == Routes.poem_path(conn, :new, %{author: id}) |
|||
|
|||
# Assert get on author shows the newly created author |
|||
conn = get(conn, Routes.author_path(conn, :show, id)) |
|||
assert html_response(conn, 200) =~ @create_attrs.name |
|||
end |
|||
|
|||
test "renders errors when data is invalid", %{conn: conn} do |
|||
conn = |
|||
put_req_header(conn, "authorization", Plug.BasicAuth.encode_basic_auth("user", "secret")) |
|||
|
|||
conn = post(conn, Routes.author_path(conn, :create), author: @invalid_attrs) |
|||
assert html_response(conn, 200) =~ "Add Author" |
|||
end |
|||
end |
|||
end |
@ -1,13 +0,0 @@ |
|||
defmodule DpsWeb.PageControllerTest do |
|||
use DpsWeb.ConnCase |
|||
|
|||
test "GET /", %{conn: conn} do |
|||
conn = get(conn, "/") |
|||
assert html_response(conn, 200) =~ "Poems" |
|||
end |
|||
|
|||
test "GET /poems", %{conn: conn} do |
|||
conn = get(conn, "/poems") |
|||
assert html_response(conn, 200) =~ "Poems" |
|||
end |
|||
end |
@ -0,0 +1,72 @@ |
|||
defmodule DpsWeb.PoemControllerTest do |
|||
use DpsWeb.ConnCase |
|||
|
|||
@create_attrs %{ |
|||
title: "Some Title", |
|||
epigraph: "Some Epi", |
|||
content: "Some Content", |
|||
author_id: 1 |
|||
} |
|||
@invalid_attrs %{title: nil, epigraph: nil, content: nil, author_id: nil} |
|||
|
|||
defp random_string(length \\ 32) do |
|||
:crypto.strong_rand_bytes(length) |> Base.encode64() |> binary_part(0, length) |
|||
end |
|||
|
|||
describe "index" do |
|||
test "GET /poems", %{conn: conn} do |
|||
conn = get(conn, Routes.poem_path(conn, :index)) |
|||
assert html_response(conn, 200) =~ "poems" |
|||
end |
|||
end |
|||
|
|||
describe "new poem" do |
|||
test "is blocked with auth", %{conn: conn} do |
|||
conn = get(conn, Routes.poem_path(conn, :new)) |
|||
assert response(conn, 401) =~ "Unauthorized" |
|||
end |
|||
|
|||
test "GET /authors/new", %{conn: conn} do |
|||
conn = |
|||
put_req_header(conn, "authorization", Plug.BasicAuth.encode_basic_auth("user", "secret")) |
|||
|
|||
conn = get(conn, Routes.poem_path(conn, :new)) |
|||
|
|||
assert html_response(conn, 200) =~ "Add Poem" |
|||
end |
|||
end |
|||
|
|||
describe "create poem" do |
|||
test "renders errors when data is invalid", %{conn: conn} do |
|||
conn = put_req_header(conn, "authorization", Plug.BasicAuth.encode_basic_auth("user", "secret")) |
|||
|
|||
conn = post(conn, Routes.poem_path(conn, :create), poem: @invalid_attrs) |
|||
assert html_response(conn, 200) =~ "Add Poem" |
|||
end |
|||
|
|||
test "redirects to show when data is valid", %{conn: conn} do |
|||
{:ok, new_author} = Dps.Author.Query.create_author(%{name: random_string()}) |
|||
|
|||
conn = |
|||
put_req_header(conn, "authorization", Plug.BasicAuth.encode_basic_auth("user", "secret")) |
|||
|
|||
conn = |
|||
post(conn, Routes.poem_path(conn, :create), |
|||
poem: %{@create_attrs | author_id: new_author.id} |
|||
) |
|||
|
|||
assert %{id: id} = redirected_params(conn) |
|||
assert redirected_to(conn) == Routes.poem_path(conn, :show, id) |
|||
|
|||
conn = get(conn, Routes.poem_path(conn, :show, id)) |
|||
html = html_response(conn, 200) |
|||
|
|||
Map.to_list(@create_attrs) |
|||
|> Enum.all?(fn |
|||
{_, attr} when is_binary(attr) -> html =~ attr |
|||
_ -> true |
|||
end) |
|||
|> assert |
|||
end |
|||
end |
|||
end |
Loading…
Reference in new issue