From 8bba35e60b0704cab775a69b9940bfc02c15480f Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Fri, 22 Feb 2019 18:07:20 +0100 Subject: [PATCH] Improve tests Signed-off-by: Thomas Citharel --- lib/mobilizon/actors/follower.ex | 4 -- lib/mobilizon_web/resolvers/person.ex | 18 +++--- lib/mobilizon_web/resolvers/tag.ex | 18 +++--- lib/mobilizon_web/schema.ex | 1 + lib/service/activity_pub/activity_pub.ex | 5 +- .../resolvers/person_resolver_test.exs | 63 +++++++++++++++++++ .../resolvers/tag_resolver_test.exs | 48 ++++++++++++++ 7 files changed, 131 insertions(+), 26 deletions(-) create mode 100644 test/mobilizon_web/resolvers/tag_resolver_test.exs diff --git a/lib/mobilizon/actors/follower.ex b/lib/mobilizon/actors/follower.ex index 9fcdfcdad..a25fec3a5 100644 --- a/lib/mobilizon/actors/follower.ex +++ b/lib/mobilizon/actors/follower.ex @@ -21,8 +21,4 @@ defmodule Mobilizon.Actors.Follower do |> validate_required([:score, :approved, :target_actor_id, :actor_id]) |> unique_constraint(:target_actor_id, name: :followers_actor_target_actor_unique_index) end - - def url(%Follower{id: id}) do - "#{MobilizonWeb.Endpoint.url()}/follow/#{id}/activity" - end end diff --git a/lib/mobilizon_web/resolvers/person.ex b/lib/mobilizon_web/resolvers/person.ex index 8a3d4ba75..819a78930 100644 --- a/lib/mobilizon_web/resolvers/person.ex +++ b/lib/mobilizon_web/resolvers/person.ex @@ -6,17 +6,6 @@ defmodule MobilizonWeb.Resolvers.Person do alias Mobilizon.Actors.{Actor, User} alias Mobilizon.Service.ActivityPub - @deprecated "Use find_person/3 or find_group/3 instead" - def find_actor(_parent, %{preferred_username: name}, _resolution) do - case ActivityPub.find_or_make_actor_from_nickname(name) do - {:ok, actor} -> - {:ok, actor} - - _ -> - {:error, "Actor with name #{name} not found"} - end - end - @doc """ Find a person """ @@ -65,6 +54,13 @@ defmodule MobilizonWeb.Resolvers.Person do end end + @doc """ + This function is used to create more identities from an existing user + """ + def create_person(_parent, _args, _resolution) do + {:error, "You need to be logged-in to create a new identity"} + end + @doc """ This function is used to register a person afterwards the user has been created (but not activated) """ diff --git a/lib/mobilizon_web/resolvers/tag.ex b/lib/mobilizon_web/resolvers/tag.ex index abc3861de..2de78e304 100644 --- a/lib/mobilizon_web/resolvers/tag.ex +++ b/lib/mobilizon_web/resolvers/tag.ex @@ -19,15 +19,15 @@ defmodule MobilizonWeb.Resolvers.Tag do {:ok, Mobilizon.Events.list_tags_for_event(id)} end - @doc """ - Retrieve the list of related tags for a given tag ID - """ - def get_related_tags(_parent, %{tag_id: tag_id}, _resolution) do - with %Tag{} = tag <- Mobilizon.Events.get_tag!(tag_id), - tags <- Mobilizon.Events.tag_neighbors(tag) do - {:ok, tags} - end - end + # @doc """ + # Retrieve the list of related tags for a given tag ID + # """ + # def get_related_tags(_parent, %{tag_id: tag_id}, _resolution) do + # with %Tag{} = tag <- Mobilizon.Events.get_tag!(tag_id), + # tags <- Mobilizon.Events.tag_neighbors(tag) do + # {:ok, tags} + # end + # end @doc """ Retrieve the list of related tags for a parent tag diff --git a/lib/mobilizon_web/schema.ex b/lib/mobilizon_web/schema.ex index fe8d31f7a..b3d741eb7 100644 --- a/lib/mobilizon_web/schema.ex +++ b/lib/mobilizon_web/schema.ex @@ -131,6 +131,7 @@ defmodule MobilizonWeb.Schema do import_fields(:event_queries) import_fields(:participant_queries) import_fields(:category_queries) + import_fields(:tag_queries) end @desc """ diff --git a/lib/service/activity_pub/activity_pub.ex b/lib/service/activity_pub/activity_pub.ex index 015f11c70..aeb46aa44 100644 --- a/lib/service/activity_pub/activity_pub.ex +++ b/lib/service/activity_pub/activity_pub.ex @@ -241,8 +241,9 @@ defmodule Mobilizon.Service.ActivityPub do # end def follow(%Actor{} = follower, %Actor{} = followed, activity_id \\ nil, local \\ true) do - with {:ok, %Follower{} = follow} <- Actor.follow(followed, follower, true), - activity_follow_id <- activity_id || Follower.url(follow), + with {:ok, %Follower{id: follow_id}} <- Actor.follow(followed, follower, true), + activity_follow_id <- + activity_id || "#{MobilizonWeb.Endpoint.url()}/follow/#{follow_id}/activity", data <- make_follow_data(followed, follower, activity_follow_id), {:ok, activity} <- insert(data, local), :ok <- maybe_federate(activity) do diff --git a/test/mobilizon_web/resolvers/person_resolver_test.exs b/test/mobilizon_web/resolvers/person_resolver_test.exs index de89869a4..d5dbf64cc 100644 --- a/test/mobilizon_web/resolvers/person_resolver_test.exs +++ b/test/mobilizon_web/resolvers/person_resolver_test.exs @@ -73,5 +73,68 @@ defmodule MobilizonWeb.Resolvers.PersonResolverTest do assert json_response(res, 200)["data"]["loggedPerson"]["preferredUsername"] == actor.preferred_username end + + test "create_person/3 creates a new identity", context do + user = insert(:user) + actor = insert(:actor, user: user) + + mutation = """ + mutation { + createPerson( + preferredUsername: "new_identity", + name: "secret person", + summary: "no-one will know who I am" + ) { + id, + preferredUsername + } + } + """ + + res = + context.conn + |> post("/api", AbsintheHelpers.mutation_skeleton(mutation)) + + assert json_response(res, 200)["data"]["createPerson"] == nil + + assert hd(json_response(res, 200)["errors"])["message"] == + "You need to be logged-in to create a new identity" + + res = + context.conn + |> auth_conn(user) + |> post("/api", AbsintheHelpers.mutation_skeleton(mutation)) + + assert json_response(res, 200)["data"]["createPerson"]["preferredUsername"] == + "new_identity" + + query = """ + { + identities { + avatarUrl, + preferredUsername, + } + } + """ + + res = + context.conn + |> get("/api", AbsintheHelpers.query_skeleton(query, "identities")) + + assert json_response(res, 200)["data"]["identities"] == nil + + assert hd(json_response(res, 200)["errors"])["message"] == + "You need to be logged-in to view your list of identities" + + res = + context.conn + |> auth_conn(user) + |> get("/api", AbsintheHelpers.query_skeleton(query, "identities")) + + assert json_response(res, 200)["data"]["identities"] + |> Enum.map(fn identity -> Map.get(identity, "preferredUsername") end) + |> MapSet.new() == + MapSet.new([actor.preferred_username, "new_identity"]) + end end end diff --git a/test/mobilizon_web/resolvers/tag_resolver_test.exs b/test/mobilizon_web/resolvers/tag_resolver_test.exs new file mode 100644 index 000000000..fe5a1f081 --- /dev/null +++ b/test/mobilizon_web/resolvers/tag_resolver_test.exs @@ -0,0 +1,48 @@ +defmodule MobilizonWeb.Resolvers.TagResolverTest do + use MobilizonWeb.ConnCase + alias MobilizonWeb.AbsintheHelpers + import Mobilizon.Factory + + describe "Tag Resolver" do + test "list_tags/3 returns the list of tags", context do + tag1 = insert(:tag) + tag2 = insert(:tag) + tag3 = insert(:tag) + insert(:tag_relation, tag: tag1, link: tag2) + insert(:tag_relation, tag: tag3, link: tag1) + + query = """ + { + tags { + id, + slug, + title, + related { + id, + title, + slug + } + } + } + """ + + res = + context.conn + |> get("/api", AbsintheHelpers.query_skeleton(query, "tags")) + + tags = json_response(res, 200)["data"]["tags"] + assert tags |> length == 3 + + assert Enum.filter(tags, fn tag -> tag["slug"] == tag1.slug end) + |> hd + |> Map.get("related") + |> Enum.map(fn tag -> tag["slug"] end) + |> MapSet.new() == + [tag2, tag3] + |> Enum.map(fn + tag -> tag.slug + end) + |> MapSet.new() + end + end +end