From 7aadc447e16fdd626a85c25cbcd18358359f3bb0 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 25 Mar 2021 10:38:31 +0100 Subject: [PATCH] Handle changing default actor unlogged Signed-off-by: Thomas Citharel --- lib/graphql/resolvers/user.ex | 2 + test/graphql/resolvers/user_test.exs | 57 ++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/lib/graphql/resolvers/user.ex b/lib/graphql/resolvers/user.ex index de5bc7160..a07da0074 100644 --- a/lib/graphql/resolvers/user.ex +++ b/lib/graphql/resolvers/user.ex @@ -254,6 +254,8 @@ defmodule Mobilizon.GraphQL.Resolvers.User do end end + def change_default_actor(_parent, _args, _resolution), do: {:error, :unauthenticated} + @doc """ Returns the list of events for all of this user's identities are going to """ diff --git a/test/graphql/resolvers/user_test.exs b/test/graphql/resolvers/user_test.exs index b0e4fc5c2..e701553c0 100644 --- a/test/graphql/resolvers/user_test.exs +++ b/test/graphql/resolvers/user_test.exs @@ -121,6 +121,16 @@ defmodule Mobilizon.GraphQL.Resolvers.UserTest do } """ + @change_default_actor """ + mutation ChangeDefaultActor($preferredUsername: String!) { + changeDefaultActor(preferredUsername: $preferredUsername) { + defaultActor { + preferredUsername + } + } + } + """ + @valid_actor_params %{email: "test@test.tld", password: "testest", username: "test"} @valid_single_actor_params %{preferred_username: "test2", keys: "yolo"} @@ -835,7 +845,7 @@ defmodule Mobilizon.GraphQL.Resolvers.UserTest do end describe "Resolver: change default actor for user" do - test "test change_default_actor/3 with valid actor", context do + test "test change_default_actor/3 without being logged-in", %{conn: conn} do # Prepare user with two actors user = insert(:user) insert(:actor, user: user) @@ -848,24 +858,41 @@ defmodule Mobilizon.GraphQL.Resolvers.UserTest do assert {:ok, %User{actors: actors}} = Users.get_user_with_actors(user.id) assert length(actors) == 2 - mutation = """ - mutation { - changeDefaultActor(preferred_username: "#{actor2.preferred_username}") { - default_actor { - preferred_username - } - } - } - """ + res = + conn + |> AbsintheHelpers.graphql_query( + query: @change_default_actor, + variables: %{preferredUsername: actor2.preferred_username} + ) + + assert hd(res["errors"])["message"] == "You need to be logged in" + end + + test "test change_default_actor/3 with valid actor", %{conn: conn} do + # Prepare user with two actors + user = insert(:user) + insert(:actor, user: user) + + assert {:ok, %User{actors: _actors}} = Users.get_user_with_actors(user.id) + + actor_params = @valid_single_actor_params |> Map.put(:user_id, user.id) + assert {:ok, %Actor{} = actor2} = Actors.create_actor(actor_params) + + assert {:ok, %User{actors: actors}} = Users.get_user_with_actors(user.id) + assert length(actors) == 2 res = - context.conn + conn |> auth_conn(user) - |> post("/api", AbsintheHelpers.mutation_skeleton(mutation)) + |> AbsintheHelpers.graphql_query( + query: @change_default_actor, + variables: %{preferredUsername: actor2.preferred_username} + ) - assert json_response(res, 200)["data"]["changeDefaultActor"]["default_actor"][ - "preferred_username" - ] == actor2.preferred_username + assert res["errors"] == nil + + assert res["data"]["changeDefaultActor"]["defaultActor"]["preferredUsername"] == + actor2.preferred_username end end