2018-11-06 10:30:27 +01:00
|
|
|
defmodule MobilizonWeb.Resolvers.EventResolverTest do
|
|
|
|
use MobilizonWeb.ConnCase
|
2019-01-25 13:59:58 +01:00
|
|
|
alias Mobilizon.Events
|
2018-11-06 10:30:27 +01:00
|
|
|
alias MobilizonWeb.AbsintheHelpers
|
|
|
|
import Mobilizon.Factory
|
|
|
|
|
2019-01-24 15:23:27 +01:00
|
|
|
@event %{
|
|
|
|
description: "some body",
|
|
|
|
title: "some title",
|
2019-02-14 14:19:55 +01:00
|
|
|
begins_on: DateTime.utc_now() |> DateTime.truncate(:second),
|
2019-01-24 15:23:27 +01:00
|
|
|
uuid: "b5126423-f1af-43e4-a923-002a03003ba4",
|
|
|
|
url: "some url"
|
|
|
|
}
|
2018-11-06 10:30:27 +01:00
|
|
|
|
|
|
|
setup %{conn: conn} do
|
2019-01-25 13:59:58 +01:00
|
|
|
user = insert(:user)
|
|
|
|
actor = insert(:actor, user: user, preferred_username: "test")
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2018-11-29 17:43:22 +01:00
|
|
|
{:ok, conn: conn, actor: actor, user: user}
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "Event Resolver" do
|
|
|
|
test "find_event/3 returns an event", context do
|
|
|
|
category = insert(:category)
|
|
|
|
|
|
|
|
event =
|
|
|
|
@event
|
|
|
|
|> Map.put(:organizer_actor_id, context.actor.id)
|
|
|
|
|> Map.put(:category_id, category.id)
|
|
|
|
|
|
|
|
{:ok, event} = Events.create_event(event)
|
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
event(uuid: "#{event.uuid}") {
|
|
|
|
uuid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "event"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["event"]["uuid"] == to_string(event.uuid)
|
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
event(uuid: "bad uuid") {
|
|
|
|
uuid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "event"))
|
|
|
|
|
|
|
|
assert [%{"message" => "Argument \"uuid\" has invalid value \"bad uuid\"."}] =
|
2018-11-12 18:17:53 +01:00
|
|
|
json_response(res, 200)["errors"]
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
|
2018-11-29 17:43:22 +01:00
|
|
|
test "create_event/3 creates an event", %{conn: conn, actor: actor, user: user} do
|
2018-11-06 10:30:27 +01:00
|
|
|
category = insert(:category)
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
createEvent(
|
|
|
|
title: "come to my event",
|
|
|
|
description: "it will be fine",
|
2019-02-14 14:19:55 +01:00
|
|
|
begins_on: "#{
|
|
|
|
DateTime.utc_now() |> DateTime.truncate(:second) |> DateTime.to_iso8601()
|
|
|
|
}",
|
2019-01-24 15:23:27 +01:00
|
|
|
organizer_actor_id: "#{actor.id}",
|
2019-01-14 17:48:08 +01:00
|
|
|
category: "#{category.title}"
|
2018-11-06 10:30:27 +01:00
|
|
|
) {
|
|
|
|
title,
|
|
|
|
uuid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
2018-11-29 17:43:22 +01:00
|
|
|
|> auth_conn(user)
|
2018-11-06 10:30:27 +01:00
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["createEvent"]["title"] == "come to my event"
|
|
|
|
end
|
|
|
|
|
2018-12-21 11:45:55 +01:00
|
|
|
test "list_events/3 returns events", context do
|
|
|
|
event = insert(:event)
|
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
events {
|
|
|
|
uuid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "event"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["events"] |> Enum.map(& &1["uuid"]) == [event.uuid]
|
|
|
|
|
|
|
|
Enum.each(0..15, fn _ ->
|
|
|
|
insert(:event)
|
|
|
|
end)
|
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
events {
|
|
|
|
uuid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "event"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["events"] |> length == 10
|
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
events(page: 2) {
|
|
|
|
uuid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "event"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["events"] |> length == 7
|
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
events(page: 2, limit: 15) {
|
|
|
|
uuid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "event"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["events"] |> length == 2
|
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
events(page: 3, limit: 15) {
|
|
|
|
uuid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "event"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["events"] |> length == 0
|
|
|
|
end
|
2019-01-14 15:56:07 +01:00
|
|
|
|
|
|
|
test "list_events/3 doesn't list private events", context do
|
|
|
|
insert(:event, visibility: :private)
|
|
|
|
insert(:event, visibility: :unlisted)
|
2019-02-01 12:33:15 +01:00
|
|
|
insert(:event, visibility: :restricted)
|
2019-01-14 15:56:07 +01:00
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
events {
|
|
|
|
uuid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "event"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["events"] |> Enum.map(& &1["uuid"]) == []
|
|
|
|
end
|
|
|
|
|
|
|
|
test "find_event/3 returns an unlisted event", context do
|
|
|
|
event = insert(:event, visibility: :unlisted)
|
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
event(uuid: "#{event.uuid}") {
|
|
|
|
uuid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "event"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["event"]["uuid"] == to_string(event.uuid)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "find_event/3 doesn't return a private event", context do
|
|
|
|
event = insert(:event, visibility: :private)
|
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
event(uuid: "#{event.uuid}") {
|
|
|
|
uuid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "event"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["errors"] |> hd |> Map.get("message") ==
|
|
|
|
"Event with UUID #{event.uuid} not found"
|
|
|
|
end
|
2019-01-25 17:06:57 +01:00
|
|
|
|
|
|
|
test "delete_event/3 deletes an event", %{conn: conn, user: user, actor: actor} do
|
|
|
|
event = insert(:event, organizer_actor: actor)
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
deleteEvent(
|
|
|
|
actor_id: #{actor.id},
|
|
|
|
event_id: #{event.id}
|
|
|
|
) {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["errors"] == nil
|
|
|
|
assert json_response(res, 200)["data"]["deleteEvent"]["id"] == event.id
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] =~ "not found"
|
|
|
|
end
|
2019-02-01 09:52:36 +01:00
|
|
|
|
|
|
|
test "delete_event/3 should check the user is authenticated", %{conn: conn, actor: actor} do
|
|
|
|
event = insert(:event, organizer_actor: actor)
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
deleteEvent(
|
|
|
|
actor_id: #{actor.id},
|
|
|
|
event_id: #{event.id}
|
|
|
|
) {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] =~ "logged-in"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "delete_event/3 should check the actor id is owned by the user", %{
|
|
|
|
conn: conn,
|
|
|
|
user: user,
|
|
|
|
actor: actor
|
|
|
|
} do
|
|
|
|
event = insert(:event, organizer_actor: actor)
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
deleteEvent(
|
|
|
|
actor_id: 1042,
|
|
|
|
event_id: #{event.id}
|
|
|
|
) {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] =~ "not owned"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "delete_event/3 should check the event can be deleted by the user", %{
|
|
|
|
conn: conn,
|
|
|
|
user: user,
|
|
|
|
actor: actor
|
|
|
|
} do
|
|
|
|
actor2 = insert(:actor)
|
|
|
|
event = insert(:event, organizer_actor: actor2)
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
deleteEvent(
|
|
|
|
actor_id: #{actor.id},
|
|
|
|
event_id: #{event.id}
|
|
|
|
) {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] =~ "cannot delete"
|
|
|
|
end
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
end
|