2020-01-26 20:36:50 +00:00
|
|
|
defmodule Mobilizon.Web.Resolvers.GroupTest do
|
|
|
|
use Mobilizon.Web.ConnCase
|
2020-08-27 09:53:24 +00:00
|
|
|
use Oban.Testing, repo: Mobilizon.Storage.Repo
|
2020-01-26 19:34:25 +00:00
|
|
|
|
2018-12-03 10:58:57 +00:00
|
|
|
import Mobilizon.Factory
|
|
|
|
|
2020-01-26 19:34:25 +00:00
|
|
|
alias Mobilizon.GraphQL.AbsintheHelpers
|
|
|
|
|
2018-12-03 10:58:57 +00:00
|
|
|
@non_existent_username "nonexistent"
|
|
|
|
@new_group_params %{groupname: "new group"}
|
|
|
|
|
|
|
|
setup %{conn: conn} do
|
2019-01-25 12:59:58 +00:00
|
|
|
user = insert(:user)
|
|
|
|
actor = insert(:actor, user: user)
|
2018-12-03 10:58:57 +00:00
|
|
|
|
|
|
|
{:ok, conn: conn, actor: actor, user: user}
|
|
|
|
end
|
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
describe "create a group" do
|
2019-09-02 08:50:00 +00:00
|
|
|
test "create_group/3 should check the user owns the identity", %{conn: conn, user: user} do
|
|
|
|
another_actor = insert(:actor)
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
createGroup(
|
|
|
|
preferred_username: "#{@new_group_params.groupname}",
|
|
|
|
creator_actor_id: #{another_actor.id}
|
|
|
|
) {
|
|
|
|
preferred_username,
|
|
|
|
type
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
2019-10-25 15:43:37 +00:00
|
|
|
"Creator actor id is not owned by the current user"
|
2019-09-02 08:50:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "create_group/3 creates a group and check a group with this name does not already exist",
|
|
|
|
%{conn: conn, user: user, actor: actor} do
|
2018-12-03 10:58:57 +00:00
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
createGroup(
|
|
|
|
preferred_username: "#{@new_group_params.groupname}",
|
2019-09-02 08:50:00 +00:00
|
|
|
creator_actor_id: #{actor.id}
|
2018-12-03 10:58:57 +00:00
|
|
|
) {
|
|
|
|
preferred_username,
|
|
|
|
type
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
2018-12-03 11:08:18 +00:00
|
|
|
assert json_response(res, 200)["data"]["createGroup"]["preferred_username"] ==
|
|
|
|
@new_group_params.groupname
|
2018-12-03 10:58:57 +00:00
|
|
|
|
2018-12-03 11:08:18 +00:00
|
|
|
assert json_response(res, 200)["data"]["createGroup"]["type"] == "GROUP"
|
|
|
|
|
|
|
|
mutation = """
|
2018-12-03 10:58:57 +00:00
|
|
|
mutation {
|
|
|
|
createGroup(
|
|
|
|
preferred_username: "#{@new_group_params.groupname}",
|
2019-09-02 08:50:00 +00:00
|
|
|
creator_actor_id: #{actor.id},
|
2018-12-03 10:58:57 +00:00
|
|
|
) {
|
|
|
|
preferred_username,
|
|
|
|
type
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
2019-09-02 08:50:00 +00:00
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
|
|
|
"A group with this name already exists"
|
2018-12-03 10:58:57 +00:00
|
|
|
end
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|
2018-12-03 10:58:57 +00:00
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
describe "list groups" do
|
2020-08-27 09:53:24 +00:00
|
|
|
@list_groups_query """
|
|
|
|
{
|
2018-12-03 10:58:57 +00:00
|
|
|
groups {
|
2020-02-18 07:57:00 +00:00
|
|
|
elements {
|
|
|
|
preferredUsername,
|
|
|
|
},
|
|
|
|
total
|
2018-12-03 10:58:57 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-27 09:53:24 +00:00
|
|
|
"""
|
2018-12-03 10:58:57 +00:00
|
|
|
|
2020-08-27 09:53:24 +00:00
|
|
|
test "list_groups/3 doesn't returns all groups if not authenticated", %{conn: conn} do
|
|
|
|
insert(:group, visibility: :public)
|
|
|
|
insert(:group, visibility: :unlisted)
|
|
|
|
insert(:group, visibility: :private)
|
2019-04-25 17:05:05 +00:00
|
|
|
|
2020-08-27 09:53:24 +00:00
|
|
|
res = AbsintheHelpers.graphql_query(conn, query: @list_groups_query)
|
2018-12-03 10:58:57 +00:00
|
|
|
|
2020-08-27 09:53:24 +00:00
|
|
|
assert hd(res["errors"])["message"] == "You may not list groups unless moderator."
|
|
|
|
end
|
|
|
|
|
|
|
|
test "list_groups/3 doesn't return all groups if not a moderator", %{conn: conn} do
|
|
|
|
insert(:group, visibility: :public)
|
|
|
|
insert(:group, visibility: :unlisted)
|
|
|
|
insert(:group, visibility: :private)
|
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> AbsintheHelpers.graphql_query(query: @list_groups_query)
|
|
|
|
|
|
|
|
assert hd(res["errors"])["message"] == "You may not list groups unless moderator."
|
|
|
|
end
|
|
|
|
|
|
|
|
test "list_groups/3 returns all groups if a moderator", %{conn: conn} do
|
|
|
|
group_1 = insert(:group, visibility: :public)
|
|
|
|
group_2 = insert(:group, visibility: :unlisted)
|
|
|
|
group_3 = insert(:group, visibility: :private)
|
|
|
|
user = insert(:user, role: :moderator)
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> AbsintheHelpers.graphql_query(query: @list_groups_query)
|
|
|
|
|
|
|
|
assert res["data"]["groups"]["total"] == 3
|
|
|
|
|
|
|
|
assert res["data"]["groups"]["elements"]
|
|
|
|
|> Enum.map(& &1["preferredUsername"])
|
|
|
|
|> MapSet.new() ==
|
|
|
|
[group_1, group_2, group_3] |> Enum.map(& &1.preferred_username) |> MapSet.new()
|
2018-12-03 10:58:57 +00:00
|
|
|
end
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|
2018-12-03 10:58:57 +00:00
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
describe "find a group" do
|
|
|
|
@group_query """
|
|
|
|
query Group($preferredUsername: String!) {
|
|
|
|
group(preferredUsername: $preferredUsername) {
|
2018-12-03 10:58:57 +00:00
|
|
|
preferredUsername,
|
2020-02-18 07:57:00 +00:00
|
|
|
members {
|
|
|
|
total,
|
|
|
|
elements {
|
|
|
|
role,
|
|
|
|
actor {
|
|
|
|
preferredUsername
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-03 10:58:57 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-18 07:57:00 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
test "find_group/3 returns a group by its username", %{conn: conn, actor: actor, user: user} do
|
|
|
|
group = insert(:group)
|
|
|
|
insert(:member, parent: group, actor: actor, role: :administrator)
|
|
|
|
insert(:member, parent: group, role: :member)
|
2018-12-03 10:58:57 +00:00
|
|
|
|
|
|
|
res =
|
2020-02-18 07:57:00 +00:00
|
|
|
conn
|
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @group_query,
|
|
|
|
variables: %{
|
|
|
|
preferredUsername: group.preferred_username
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert res["errors"] == nil
|
2018-12-03 10:58:57 +00:00
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
assert res["data"]["group"]["preferredUsername"] ==
|
2018-12-03 10:58:57 +00:00
|
|
|
group.preferred_username
|
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
assert res["data"]["group"]["members"]["total"] == 2
|
2018-12-03 10:58:57 +00:00
|
|
|
|
|
|
|
res =
|
2020-02-18 07:57:00 +00:00
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @group_query,
|
|
|
|
variables: %{
|
|
|
|
preferredUsername: group.preferred_username,
|
|
|
|
actorId: actor.id
|
|
|
|
}
|
|
|
|
)
|
2018-12-03 10:58:57 +00:00
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
assert res["errors"] == nil
|
2018-12-03 10:58:57 +00:00
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
assert res["data"]["group"]["members"]["total"] == 2
|
|
|
|
assert hd(res["data"]["group"]["members"]["elements"])["role"] == "ADMINISTRATOR"
|
|
|
|
|
|
|
|
assert hd(res["data"]["group"]["members"]["elements"])["actor"]["preferredUsername"] ==
|
|
|
|
actor.preferred_username
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @group_query,
|
|
|
|
variables: %{preferredUsername: @non_existent_username}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert res["data"]["group"] == nil
|
|
|
|
|
|
|
|
assert hd(res["errors"])["message"] ==
|
2018-12-03 10:58:57 +00:00
|
|
|
"Group with name #{@non_existent_username} not found"
|
|
|
|
end
|
2019-01-25 08:23:44 +00:00
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
test "find_group doesn't list group members access if group is private", %{
|
|
|
|
conn: conn,
|
|
|
|
actor: actor
|
|
|
|
} do
|
|
|
|
group = insert(:group, visibility: :private)
|
|
|
|
insert(:member, parent: group, actor: actor, role: :administrator)
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @group_query,
|
|
|
|
variables: %{
|
|
|
|
preferredUsername: group.preferred_username
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert res["errors"] == nil
|
|
|
|
|
|
|
|
assert res["data"]["group"]["preferredUsername"] ==
|
|
|
|
group.preferred_username
|
|
|
|
|
|
|
|
assert res["data"]["group"]["members"] == %{"elements" => [], "total" => 1}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "delete a group" do
|
2020-08-27 09:53:24 +00:00
|
|
|
@delete_group_mutation """
|
|
|
|
mutation DeleteGroup($groupId: ID!) {
|
|
|
|
deleteGroup(
|
|
|
|
groupId: $groupId
|
|
|
|
) {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2019-01-25 08:23:44 +00:00
|
|
|
test "delete_group/3 deletes a group", %{conn: conn, user: user, actor: actor} do
|
|
|
|
group = insert(:group)
|
2019-03-01 16:11:28 +00:00
|
|
|
insert(:member, parent: group, actor: actor, role: :administrator)
|
2019-01-25 08:23:44 +00:00
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
2020-08-27 09:53:24 +00:00
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @delete_group_mutation,
|
|
|
|
variables: %{groupId: group.id}
|
|
|
|
)
|
2019-01-25 08:23:44 +00:00
|
|
|
|
2020-08-27 09:53:24 +00:00
|
|
|
assert res["errors"] == nil
|
|
|
|
assert res["data"]["deleteGroup"]["id"] == to_string(group.id)
|
|
|
|
|
|
|
|
assert_enqueued(
|
|
|
|
worker: Mobilizon.Service.Workers.Background,
|
|
|
|
args: %{
|
|
|
|
"actor_id" => group.id,
|
|
|
|
"author_id" => actor.id,
|
|
|
|
"op" => "delete_actor",
|
|
|
|
"reserve_username" => true,
|
|
|
|
"suspension" => false
|
|
|
|
}
|
|
|
|
)
|
2019-01-25 08:23:44 +00:00
|
|
|
|
2020-08-27 09:53:24 +00:00
|
|
|
# Can't be used right now, probably because we try to run a transaction in a Oban Job while using Ecto Sandbox
|
2019-01-25 08:23:44 +00:00
|
|
|
|
2020-08-27 09:53:24 +00:00
|
|
|
# assert %{success: 1, failure: 0} == Oban.drain_queue(queue: :background)
|
|
|
|
|
|
|
|
# res =
|
|
|
|
# conn
|
|
|
|
# |> auth_conn(user)
|
|
|
|
# |> AbsintheHelpers.graphql_query(
|
|
|
|
# query: @delete_group_mutation,
|
|
|
|
# variables: %{groupId: group.id}
|
|
|
|
# )
|
|
|
|
|
|
|
|
# assert res["data"] == "tt"
|
|
|
|
# assert hd(json_response(res, 200)["errors"])["message"] =~ "not found"
|
2019-01-25 08:23:44 +00:00
|
|
|
end
|
2019-02-01 08:42:31 +00:00
|
|
|
|
|
|
|
test "delete_group/3 should check user authentication", %{conn: conn, actor: actor} do
|
|
|
|
group = insert(:group)
|
2019-03-01 16:11:28 +00:00
|
|
|
insert(:member, parent: group, actor: actor, role: :member)
|
2019-02-01 08:42:31 +00:00
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
2020-08-27 09:53:24 +00:00
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @delete_group_mutation,
|
|
|
|
variables: %{groupId: group.id}
|
|
|
|
)
|
2019-02-01 08:42:31 +00:00
|
|
|
|
2020-08-27 09:53:24 +00:00
|
|
|
assert hd(res["errors"])["message"] =~ "logged-in"
|
2019-02-01 08:42:31 +00:00
|
|
|
end
|
|
|
|
|
2019-02-01 08:52:36 +00:00
|
|
|
test "delete_group/3 should check the actor is owned by the user", %{
|
|
|
|
conn: conn,
|
|
|
|
user: user,
|
|
|
|
actor: actor
|
|
|
|
} do
|
2019-02-01 08:42:31 +00:00
|
|
|
group = insert(:group)
|
2019-03-01 16:11:28 +00:00
|
|
|
insert(:member, parent: group, actor: actor, role: :member)
|
2019-02-01 08:42:31 +00:00
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
2020-08-27 09:53:24 +00:00
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @delete_group_mutation,
|
|
|
|
variables: %{groupId: group.id}
|
|
|
|
)
|
2019-02-01 08:42:31 +00:00
|
|
|
|
2020-08-27 09:53:24 +00:00
|
|
|
assert hd(res["errors"])["message"] ==
|
|
|
|
"Actor id is not an administrator of the selected group"
|
2019-02-01 08:42:31 +00:00
|
|
|
end
|
|
|
|
|
2019-02-01 08:52:36 +00:00
|
|
|
test "delete_group/3 should check the actor is a member of this group", %{
|
|
|
|
conn: conn,
|
2020-08-27 09:53:24 +00:00
|
|
|
user: user
|
2019-02-01 08:52:36 +00:00
|
|
|
} do
|
2019-02-01 08:42:31 +00:00
|
|
|
group = insert(:group)
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
2020-08-27 09:53:24 +00:00
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @delete_group_mutation,
|
|
|
|
variables: %{groupId: group.id}
|
|
|
|
)
|
2019-02-01 08:42:31 +00:00
|
|
|
|
2020-08-27 09:53:24 +00:00
|
|
|
assert hd(res["errors"])["message"] =~ "not a member"
|
2019-02-01 08:42:31 +00:00
|
|
|
end
|
|
|
|
|
2019-02-01 08:52:36 +00:00
|
|
|
test "delete_group/3 should check the actor is an administrator of this group", %{
|
|
|
|
conn: conn,
|
|
|
|
user: user,
|
|
|
|
actor: actor
|
|
|
|
} do
|
2019-02-01 08:42:31 +00:00
|
|
|
group = insert(:group)
|
2019-03-01 16:11:28 +00:00
|
|
|
insert(:member, parent: group, actor: actor, role: :member)
|
2019-02-01 08:42:31 +00:00
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
2020-08-27 09:53:24 +00:00
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @delete_group_mutation,
|
|
|
|
variables: %{groupId: group.id}
|
|
|
|
)
|
2019-02-01 08:42:31 +00:00
|
|
|
|
2020-08-27 09:53:24 +00:00
|
|
|
assert hd(res["errors"])["message"] =~ "not an administrator"
|
2019-02-01 08:42:31 +00:00
|
|
|
end
|
2018-12-03 10:58:57 +00:00
|
|
|
end
|
|
|
|
end
|