2017-12-08 09:58:14 +01:00
|
|
|
defmodule EventosWeb.GroupControllerTest do
|
|
|
|
use EventosWeb.ConnCase
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
import Eventos.Factory
|
|
|
|
|
|
|
|
alias Eventos.Groups
|
|
|
|
alias Eventos.Groups.Group
|
2017-12-08 09:58:14 +01:00
|
|
|
|
|
|
|
@create_attrs %{description: "some description", suspended: true, title: "some title", uri: "some uri", url: "some url"}
|
|
|
|
@update_attrs %{description: "some updated description", suspended: false, title: "some updated title", uri: "some updated uri", url: "some updated url"}
|
|
|
|
@invalid_attrs %{description: nil, suspended: nil, title: nil, uri: nil, url: nil}
|
|
|
|
|
|
|
|
def fixture(:group) do
|
2018-01-13 23:33:03 +01:00
|
|
|
{:ok, group} = Groups.create_group(@create_attrs)
|
2017-12-08 09:58:14 +01:00
|
|
|
group
|
|
|
|
end
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
setup %{conn: conn} do
|
|
|
|
account = insert(:account)
|
|
|
|
user = insert(:user, account: account)
|
|
|
|
{:ok, conn: conn, user: user}
|
|
|
|
end
|
|
|
|
|
2017-12-08 09:58:14 +01:00
|
|
|
describe "index" do
|
|
|
|
test "lists all groups", %{conn: conn} do
|
|
|
|
conn = get conn, group_path(conn, :index)
|
2018-01-13 23:33:03 +01:00
|
|
|
assert json_response(conn, 200)["data"] == []
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "create group" do
|
2018-01-13 23:33:03 +01:00
|
|
|
test "renders group when data is valid", %{conn: conn, user: user} do
|
|
|
|
conn = auth_conn(conn, user)
|
2017-12-08 09:58:14 +01:00
|
|
|
conn = post conn, group_path(conn, :create), group: @create_attrs
|
2018-01-13 23:33:03 +01:00
|
|
|
assert %{"id" => id} = json_response(conn, 201)["data"]
|
2017-12-08 09:58:14 +01:00
|
|
|
|
|
|
|
conn = get conn, group_path(conn, :show, id)
|
2018-01-13 23:33:03 +01:00
|
|
|
assert json_response(conn, 200)["data"] == %{
|
|
|
|
"id" => id,
|
|
|
|
"description" => "some description",
|
|
|
|
"suspended" => true,
|
|
|
|
"title" => "some title",
|
2018-01-16 19:45:09 +01:00
|
|
|
"uri" => "h",
|
|
|
|
"url" => "h",
|
|
|
|
"events" => [],
|
|
|
|
"members" => []
|
|
|
|
}
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
test "renders errors when data is invalid", %{conn: conn, user: user} do
|
|
|
|
conn = auth_conn(conn, user)
|
2017-12-08 09:58:14 +01:00
|
|
|
conn = post conn, group_path(conn, :create), group: @invalid_attrs
|
2018-01-13 23:33:03 +01:00
|
|
|
assert json_response(conn, 422)["errors"] != %{}
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "update group" do
|
|
|
|
setup [:create_group]
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
test "renders group when data is valid", %{conn: conn, group: %Group{id: id} = group, user: user} do
|
|
|
|
conn = auth_conn(conn, user)
|
2017-12-08 09:58:14 +01:00
|
|
|
conn = put conn, group_path(conn, :update, group), group: @update_attrs
|
2018-01-13 23:33:03 +01:00
|
|
|
assert %{"id" => ^id} = json_response(conn, 200)["data"]
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
conn = get conn, group_path(conn, :show, id)
|
|
|
|
assert json_response(conn, 200)["data"] == %{
|
|
|
|
"id" => id,
|
|
|
|
"description" => "some updated description",
|
|
|
|
"suspended" => false,
|
|
|
|
"title" => "some updated title",
|
|
|
|
"uri" => "some updated uri",
|
2018-01-16 19:45:09 +01:00
|
|
|
"url" => "some updated url",
|
|
|
|
"events" => [],
|
|
|
|
"members" => []
|
|
|
|
}
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
test "renders errors when data is invalid", %{conn: conn, group: group, user: user} do
|
|
|
|
conn = auth_conn(conn, user)
|
2017-12-08 09:58:14 +01:00
|
|
|
conn = put conn, group_path(conn, :update, group), group: @invalid_attrs
|
2018-01-13 23:33:03 +01:00
|
|
|
assert json_response(conn, 422)["errors"] != %{}
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "delete group" do
|
|
|
|
setup [:create_group]
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
test "deletes chosen group", %{conn: conn, group: group, user: user} do
|
|
|
|
conn = auth_conn(conn, user)
|
2017-12-08 09:58:14 +01:00
|
|
|
conn = delete conn, group_path(conn, :delete, group)
|
2018-01-13 23:33:03 +01:00
|
|
|
assert response(conn, 204)
|
2017-12-08 09:58:14 +01:00
|
|
|
assert_error_sent 404, fn ->
|
|
|
|
get conn, group_path(conn, :show, group)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp create_group(_) do
|
|
|
|
group = fixture(:group)
|
|
|
|
{:ok, group: group}
|
|
|
|
end
|
2018-01-13 23:33:03 +01:00
|
|
|
|
|
|
|
defp auth_conn(conn, %Eventos.Accounts.User{} = user) do
|
|
|
|
{:ok, token, _claims} = EventosWeb.Guardian.encode_and_sign(user)
|
|
|
|
conn
|
|
|
|
|> put_req_header("authorization", "Bearer #{token}")
|
|
|
|
|> put_req_header("accept", "application/json")
|
|
|
|
end
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|