This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
defmodule LinkShortenerWeb.Api.V1.LinkControllerTest do
|
||||
use LinkShortenerWeb.ConnCase
|
||||
|
||||
import LinkShortener.LinksFixtures
|
||||
|
||||
alias LinkShortener.Links.Link
|
||||
alias LinkShortener.Links
|
||||
|
||||
@create_attrs %{
|
||||
name: "some link name",
|
||||
url: "https://gitlab.com/KKlochko/link_shortener",
|
||||
shorten: "git_repo",
|
||||
}
|
||||
|
||||
@update_attrs %{
|
||||
name: "some updated link name",
|
||||
url: "https://gitlab.com/KKlochko/link_shortener2",
|
||||
shorten: "new_git_repo",
|
||||
}
|
||||
|
||||
@invalid_attrs %{
|
||||
name: nil,
|
||||
url: nil,
|
||||
shorten: nil,
|
||||
}
|
||||
|
||||
setup %{conn: conn} do
|
||||
{:ok, conn: put_req_header(conn, "accept", "application/json")}
|
||||
end
|
||||
|
||||
describe "index" do
|
||||
test "lists all links", %{conn: conn} do
|
||||
conn = get(conn, ~p"/api/v1/links")
|
||||
assert json_response(conn, 200)["data"] == []
|
||||
end
|
||||
end
|
||||
|
||||
describe "create link" do
|
||||
test "renders link when data is valid", %{conn: conn} do
|
||||
conn = post(conn, ~p"/api/v1/links", link: @create_attrs)
|
||||
assert %{"id" => id} = json_response(conn, 201)["data"]
|
||||
|
||||
conn = get(conn, ~p"/api/v1/links/#{id}")
|
||||
|
||||
assert %{
|
||||
"id" => ^id,
|
||||
"name" => "some link name",
|
||||
"url" => "https://gitlab.com/KKlochko/link_shortener",
|
||||
"shorten" => "git_repo",
|
||||
} = json_response(conn, 200)["data"]
|
||||
end
|
||||
|
||||
test "renders errors when data is invalid", %{conn: conn} do
|
||||
conn = post(conn, ~p"/api/v1/links", link: @invalid_attrs)
|
||||
assert json_response(conn, 422)["errors"] != %{}
|
||||
end
|
||||
end
|
||||
|
||||
describe "update link" do
|
||||
setup [:create_link]
|
||||
|
||||
test "renders link when data is valid", %{conn: conn, link: %Link{id: id} = link} do
|
||||
conn = put(conn, ~p"/api/v1/links/#{id}", link: @update_attrs)
|
||||
assert %{"id" => ^id} = json_response(conn, 200)["data"]
|
||||
|
||||
conn = get(conn, ~p"/api/v1/links/#{id}")
|
||||
|
||||
assert %{
|
||||
"id" => ^id,
|
||||
"name" => "some updated link name",
|
||||
"url" => "https://gitlab.com/KKlochko/link_shortener2",
|
||||
"shorten" => "new_git_repo",
|
||||
} = json_response(conn, 200)["data"]
|
||||
end
|
||||
|
||||
test "renders errors when data is invalid", %{conn: conn, link: %Link{id: id} = link} do
|
||||
conn = put(conn, ~p"/api/v1/links/#{id}", link: @invalid_attrs)
|
||||
assert json_response(conn, 422)["errors"] != %{}
|
||||
end
|
||||
end
|
||||
|
||||
describe "delete link" do
|
||||
setup [:create_link]
|
||||
|
||||
test "deletes chosen link", %{conn: conn, link: %Link{id: id} = link} do
|
||||
conn = delete(conn, ~p"/api/v1/links/#{id}")
|
||||
assert response(conn, 204)
|
||||
|
||||
assert_error_sent 404, fn ->
|
||||
conn = get(conn, ~p"/api/v1/links/#{id}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp create_link(_) do
|
||||
link = link_fixture()
|
||||
%{link: link}
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user