From db2cec7706b9ceb105d8bc767d45de38f5d85dff Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Fri, 21 Dec 2018 11:45:55 +0100 Subject: [PATCH] Test paginated event list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Introduce an event number limit for…limits Signed-off-by: Thomas Citharel --- lib/mobilizon_web/resolvers/event.ex | 10 ++- .../resolvers/event_resolver_test.exs | 78 +++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/lib/mobilizon_web/resolvers/event.ex b/lib/mobilizon_web/resolvers/event.ex index 9c98d1bc1..239f97e0b 100644 --- a/lib/mobilizon_web/resolvers/event.ex +++ b/lib/mobilizon_web/resolvers/event.ex @@ -4,10 +4,18 @@ defmodule MobilizonWeb.Resolvers.Event do alias Mobilizon.Actors alias Mobilizon.Events.Event - def list_events(_parent, %{page: page, limit: limit}, _resolution) do + # We limit the max number of events that can be retrieved + @event_max_limit 100 + + def list_events(_parent, %{page: page, limit: limit}, _resolution) + when limit < @event_max_limit do {:ok, Mobilizon.Events.list_events(page, limit)} end + def list_events(_parent, %{page: page, limit: limit}, _resolution) do + {:error, :events_max_limit_reached} + end + def find_event(_parent, %{uuid: uuid}, _resolution) do case Mobilizon.Events.get_event_full_by_uuid(uuid) do nil -> diff --git a/test/mobilizon_web/resolvers/event_resolver_test.exs b/test/mobilizon_web/resolvers/event_resolver_test.exs index 898d3e0e0..5af5f12e9 100644 --- a/test/mobilizon_web/resolvers/event_resolver_test.exs +++ b/test/mobilizon_web/resolvers/event_resolver_test.exs @@ -163,5 +163,83 @@ defmodule MobilizonWeb.Resolvers.EventResolverTest do assert hd(tl(json_response(res, 200)["data"]["search"]))["preferredUsername"] == actor.preferred_username end + + 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 end end