2018-08-01 14:45:18 +02:00
|
|
|
defmodule EventosWeb.FollowerControllerTest do
|
|
|
|
use EventosWeb.ConnCase
|
|
|
|
|
|
|
|
alias Eventos.Actors
|
|
|
|
alias Eventos.Actors.Follower
|
|
|
|
import Eventos.Factory
|
|
|
|
|
|
|
|
@create_attrs %{approved: true, score: 42}
|
|
|
|
@update_attrs %{approved: false, score: 43}
|
|
|
|
@invalid_attrs %{approved: nil, score: nil}
|
|
|
|
|
|
|
|
setup %{conn: conn} do
|
|
|
|
actor = insert(:actor)
|
|
|
|
target_actor = insert(:actor)
|
|
|
|
|
|
|
|
{:ok,
|
|
|
|
conn: put_req_header(conn, "accept", "application/json"),
|
|
|
|
actor: actor,
|
2018-08-03 10:19:28 +02:00
|
|
|
target_actor: target_actor}
|
2018-08-01 14:45:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "create follower" do
|
|
|
|
test "renders follower when data is valid", %{
|
|
|
|
conn: conn,
|
|
|
|
actor: actor,
|
|
|
|
target_actor: target_actor
|
|
|
|
} do
|
|
|
|
create_attrs =
|
|
|
|
@create_attrs
|
|
|
|
|> Map.put(:actor_id, actor.id)
|
|
|
|
|> Map.put(:target_actor_id, target_actor.id)
|
|
|
|
|
|
|
|
conn = post(conn, follower_path(conn, :create), follower: create_attrs)
|
|
|
|
assert %{"id" => id} = json_response(conn, 201)["data"]
|
|
|
|
|
|
|
|
conn = get(conn, follower_path(conn, :show, id))
|
|
|
|
assert json_response(conn, 200)["data"] == %{"id" => id, "approved" => true, "score" => 42}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "renders errors when data is invalid", %{conn: conn} do
|
|
|
|
conn = post(conn, follower_path(conn, :create), follower: @invalid_attrs)
|
|
|
|
assert json_response(conn, 422)["errors"] != %{}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "update follower" do
|
2018-08-03 10:16:22 +02:00
|
|
|
setup [:create_follower]
|
2018-08-03 10:19:28 +02:00
|
|
|
|
2018-08-01 14:45:18 +02:00
|
|
|
test "renders follower when data is valid", %{
|
|
|
|
conn: conn,
|
|
|
|
follower: %Follower{id: id} = follower
|
|
|
|
} do
|
|
|
|
conn = put(conn, follower_path(conn, :update, follower), follower: @update_attrs)
|
|
|
|
assert %{"id" => ^id} = json_response(conn, 200)["data"]
|
|
|
|
|
|
|
|
conn = get(conn, follower_path(conn, :show, id))
|
|
|
|
assert json_response(conn, 200)["data"] == %{"id" => id, "approved" => false, "score" => 43}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "renders errors when data is invalid", %{conn: conn, follower: follower} do
|
|
|
|
conn = put(conn, follower_path(conn, :update, follower), follower: @invalid_attrs)
|
|
|
|
assert json_response(conn, 422)["errors"] != %{}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "delete follower" do
|
2018-08-03 10:16:22 +02:00
|
|
|
setup [:create_follower]
|
2018-08-03 10:19:28 +02:00
|
|
|
|
2018-08-01 14:45:18 +02:00
|
|
|
test "deletes chosen follower", %{conn: conn, follower: follower} do
|
|
|
|
conn = delete(conn, follower_path(conn, :delete, follower))
|
|
|
|
assert response(conn, 204)
|
|
|
|
|
|
|
|
assert_error_sent(404, fn ->
|
|
|
|
get(conn, follower_path(conn, :show, follower))
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
2018-08-03 10:16:22 +02:00
|
|
|
|
|
|
|
defp create_follower(%{actor: actor, target_actor: target_actor}) do
|
|
|
|
follower = insert(:follower, actor: actor, target_actor: target_actor)
|
|
|
|
[follower: follower]
|
|
|
|
end
|
2018-08-01 14:45:18 +02:00
|
|
|
end
|