2020-01-26 20:36:50 +00:00
|
|
|
defmodule Mobilizon.Web.Resolvers.GroupTest do
|
|
|
|
use Mobilizon.Web.ConnCase
|
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
|
|
|
|
|
|
|
|
describe "Group Resolver" 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
|
|
|
|
|
2019-04-25 17:05:05 +00:00
|
|
|
test "list_groups/3 returns all public or unlisted groups", context do
|
|
|
|
group = insert(:group, visibility: :unlisted)
|
|
|
|
insert(:group, visibility: :private)
|
2018-12-03 11:08:18 +00:00
|
|
|
|
2018-12-03 10:58:57 +00:00
|
|
|
query = """
|
|
|
|
{
|
|
|
|
groups {
|
|
|
|
preferredUsername,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
2019-04-25 17:05:05 +00:00
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "groups"))
|
|
|
|
|
|
|
|
assert length(json_response(res, 200)["data"]["groups"]) == 1
|
2018-12-03 10:58:57 +00:00
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["data"]["groups"])["preferredUsername"] ==
|
|
|
|
group.preferred_username
|
|
|
|
end
|
|
|
|
|
2019-10-15 19:18:03 +00:00
|
|
|
test "find_group/3 returns a group by its username", context do
|
2018-12-03 10:58:57 +00:00
|
|
|
group = insert(:group)
|
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
group(preferredUsername: "#{group.preferred_username}") {
|
|
|
|
preferredUsername,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "group"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["group"]["preferredUsername"] ==
|
|
|
|
group.preferred_username
|
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
group(preferredUsername: "#{@non_existent_username}") {
|
|
|
|
preferredUsername,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "group"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["group"] == nil
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
|
|
|
"Group with name #{@non_existent_username} not found"
|
|
|
|
end
|
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
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
deleteGroup(
|
|
|
|
actor_id: #{actor.id},
|
|
|
|
group_id: #{group.id}
|
|
|
|
) {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["errors"] == nil
|
2019-09-09 07:31:08 +00:00
|
|
|
assert json_response(res, 200)["data"]["deleteGroup"]["id"] == to_string(group.id)
|
2019-01-25 08:23:44 +00:00
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] =~ "not found"
|
|
|
|
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
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
deleteGroup(
|
|
|
|
actor_id: #{actor.id},
|
|
|
|
group_id: #{group.id}
|
|
|
|
) {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] =~ "logged-in"
|
|
|
|
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
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
deleteGroup(
|
|
|
|
actor_id: 159,
|
|
|
|
group_id: #{group.id}
|
|
|
|
) {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] =~ "not owned"
|
|
|
|
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,
|
|
|
|
user: user,
|
|
|
|
actor: actor
|
|
|
|
} do
|
2019-02-01 08:42:31 +00:00
|
|
|
group = insert(:group)
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
deleteGroup(
|
|
|
|
actor_id: #{actor.id},
|
|
|
|
group_id: #{group.id}
|
|
|
|
) {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] =~ "not a member"
|
|
|
|
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
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
deleteGroup(
|
|
|
|
actor_id: #{actor.id},
|
|
|
|
group_id: #{group.id}
|
|
|
|
) {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] =~ "not an administrator"
|
|
|
|
end
|
2018-12-03 10:58:57 +00:00
|
|
|
end
|
|
|
|
end
|