Handle changing default actor unlogged

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-03-25 10:38:31 +01:00
parent 95516a4067
commit 7aadc447e1
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
2 changed files with 44 additions and 15 deletions

View file

@ -254,6 +254,8 @@ defmodule Mobilizon.GraphQL.Resolvers.User do
end end
end end
def change_default_actor(_parent, _args, _resolution), do: {:error, :unauthenticated}
@doc """ @doc """
Returns the list of events for all of this user's identities are going to Returns the list of events for all of this user's identities are going to
""" """

View file

@ -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_actor_params %{email: "test@test.tld", password: "testest", username: "test"}
@valid_single_actor_params %{preferred_username: "test2", keys: "yolo"} @valid_single_actor_params %{preferred_username: "test2", keys: "yolo"}
@ -835,7 +845,7 @@ defmodule Mobilizon.GraphQL.Resolvers.UserTest do
end end
describe "Resolver: change default actor for user" do 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 # Prepare user with two actors
user = insert(:user) user = insert(:user)
insert(:actor, user: 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 {:ok, %User{actors: actors}} = Users.get_user_with_actors(user.id)
assert length(actors) == 2 assert length(actors) == 2
mutation = """ res =
mutation { conn
changeDefaultActor(preferred_username: "#{actor2.preferred_username}") { |> AbsintheHelpers.graphql_query(
default_actor { query: @change_default_actor,
preferred_username 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 = res =
context.conn conn
|> auth_conn(user) |> 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"][ assert res["errors"] == nil
"preferred_username"
] == actor2.preferred_username assert res["data"]["changeDefaultActor"]["defaultActor"]["preferredUsername"] ==
actor2.preferred_username
end end
end end