2018-05-30 14:27:21 +02:00
|
|
|
defmodule EventosWeb.BotControllerTest do
|
|
|
|
use EventosWeb.ConnCase
|
|
|
|
|
2018-06-14 17:25:55 +02:00
|
|
|
import Eventos.Factory
|
|
|
|
|
2018-05-30 14:27:21 +02:00
|
|
|
alias Eventos.Actors
|
|
|
|
alias Eventos.Actors.Bot
|
|
|
|
|
2018-06-14 17:25:55 +02:00
|
|
|
@create_attrs %{source: "some source", type: "some type", name: "some name"}
|
2018-07-27 10:45:35 +02:00
|
|
|
@update_attrs %{
|
|
|
|
source: "some updated source",
|
|
|
|
type: "some updated type",
|
|
|
|
name: "some updated name"
|
|
|
|
}
|
2018-06-14 17:25:55 +02:00
|
|
|
@invalid_attrs %{source: nil, type: nil, name: nil}
|
2018-05-30 14:27:21 +02:00
|
|
|
|
|
|
|
setup %{conn: conn} do
|
2018-07-10 10:00:07 +02:00
|
|
|
user = insert(:user)
|
|
|
|
actor = insert(:actor, user: user)
|
2018-06-14 17:25:55 +02:00
|
|
|
{:ok, conn: put_req_header(conn, "accept", "application/json"), user: user}
|
2018-05-30 14:27:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "index" do
|
|
|
|
test "lists all bots", %{conn: conn} do
|
2018-07-27 10:45:35 +02:00
|
|
|
conn = get(conn, bot_path(conn, :index))
|
2018-05-30 14:27:21 +02:00
|
|
|
assert json_response(conn, 200)["data"] == []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "create bot" do
|
2018-06-14 17:25:55 +02:00
|
|
|
test "renders bot when data is valid", %{conn: conn, user: user} do
|
|
|
|
conn = auth_conn(conn, user)
|
2018-07-27 10:45:35 +02:00
|
|
|
conn = post(conn, bot_path(conn, :create), bot: @create_attrs)
|
2018-05-30 14:27:21 +02:00
|
|
|
assert %{"id" => id} = json_response(conn, 201)["data"]
|
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
conn = get(conn, bot_path(conn, :show, id))
|
|
|
|
|
2018-05-30 14:27:21 +02:00
|
|
|
assert json_response(conn, 200)["data"] == %{
|
2018-07-27 10:45:35 +02:00
|
|
|
"id" => id,
|
|
|
|
"source" => "some source",
|
|
|
|
"type" => "some type"
|
|
|
|
}
|
2018-05-30 14:27:21 +02:00
|
|
|
end
|
|
|
|
|
2018-06-14 17:25:55 +02:00
|
|
|
test "renders errors when data is invalid", %{conn: conn, user: user} do
|
|
|
|
conn = auth_conn(conn, user)
|
2018-07-27 10:45:35 +02:00
|
|
|
conn = post(conn, bot_path(conn, :create), bot: @invalid_attrs)
|
2018-05-30 14:27:21 +02:00
|
|
|
assert json_response(conn, 422)["errors"] != %{}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "update bot" do
|
|
|
|
setup [:create_bot]
|
|
|
|
|
2018-06-14 17:25:55 +02:00
|
|
|
test "renders bot when data is valid", %{conn: conn, bot: %Bot{id: id} = bot, user: user} do
|
|
|
|
conn = auth_conn(conn, user)
|
2018-07-27 10:45:35 +02:00
|
|
|
conn = put(conn, bot_path(conn, :update, bot), bot: @update_attrs)
|
2018-05-30 14:27:21 +02:00
|
|
|
assert %{"id" => ^id} = json_response(conn, 200)["data"]
|
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
conn = get(conn, bot_path(conn, :show, id))
|
|
|
|
|
2018-05-30 14:27:21 +02:00
|
|
|
assert json_response(conn, 200)["data"] == %{
|
2018-07-27 10:45:35 +02:00
|
|
|
"id" => id,
|
|
|
|
"source" => "some updated source",
|
|
|
|
"type" => "some updated type"
|
|
|
|
}
|
2018-05-30 14:27:21 +02:00
|
|
|
end
|
|
|
|
|
2018-06-14 17:25:55 +02:00
|
|
|
test "renders errors when data is invalid", %{conn: conn, bot: bot, user: user} do
|
|
|
|
conn = auth_conn(conn, user)
|
2018-07-27 10:45:35 +02:00
|
|
|
conn = put(conn, bot_path(conn, :update, bot), bot: @invalid_attrs)
|
2018-05-30 14:27:21 +02:00
|
|
|
assert json_response(conn, 422)["errors"] != %{}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "delete bot" do
|
|
|
|
setup [:create_bot]
|
|
|
|
|
2018-06-14 17:25:55 +02:00
|
|
|
test "deletes chosen bot", %{conn: conn, bot: bot, user: user} do
|
|
|
|
conn = auth_conn(conn, user)
|
2018-07-27 10:45:35 +02:00
|
|
|
conn = delete(conn, bot_path(conn, :delete, bot))
|
2018-05-30 14:27:21 +02:00
|
|
|
assert response(conn, 204)
|
2018-07-27 10:45:35 +02:00
|
|
|
|
|
|
|
assert_error_sent(404, fn ->
|
|
|
|
get(conn, bot_path(conn, :show, bot))
|
|
|
|
end)
|
2018-05-30 14:27:21 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp create_bot(_) do
|
2018-06-14 17:25:55 +02:00
|
|
|
bot = insert(:bot)
|
2018-05-30 14:27:21 +02:00
|
|
|
{:ok, bot: bot}
|
|
|
|
end
|
|
|
|
end
|