mobilizon.chapril.org-mobil.../test/mobilizon_web/resolvers/group_resolver_test.exs

253 lines
6.4 KiB
Elixir
Raw Normal View History

defmodule MobilizonWeb.Resolvers.GroupResolverTest do
use MobilizonWeb.ConnCase
alias MobilizonWeb.AbsintheHelpers
import Mobilizon.Factory
require Logger
@non_existent_username "nonexistent"
@new_group_params %{groupname: "new group"}
setup %{conn: conn} do
user = insert(:user)
actor = insert(:actor, user: user)
{:ok, conn: conn, actor: actor, user: user}
end
describe "Group Resolver" do
test "create_group/3 creates a group", %{conn: conn, user: user, actor: actor} do
mutation = """
mutation {
createGroup(
preferred_username: "#{@new_group_params.groupname}",
admin_actor_username: "#{actor.preferred_username}"
) {
preferred_username,
type
}
}
"""
res =
conn
|> auth_conn(user)
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert json_response(res, 200)["data"]["createGroup"]["preferred_username"] ==
@new_group_params.groupname
assert json_response(res, 200)["data"]["createGroup"]["type"] == "GROUP"
mutation = """
mutation {
createGroup(
preferred_username: "#{@new_group_params.groupname}",
admin_actor_username: "#{actor.preferred_username}",
) {
preferred_username,
type
}
}
"""
res =
conn
|> auth_conn(user)
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert hd(json_response(res, 200)["errors"])["message"] == "existing_group_name"
end
test "list_groups/3 returns all public or unlisted groups", context do
group = insert(:group, visibility: :unlisted)
insert(:group, visibility: :private)
query = """
{
groups {
preferredUsername,
}
}
"""
res =
context.conn
|> get("/api", AbsintheHelpers.query_skeleton(query, "groups"))
assert length(json_response(res, 200)["data"]["groups"]) == 1
assert hd(json_response(res, 200)["data"]["groups"])["preferredUsername"] ==
group.preferred_username
end
test "find_group/3 returns a group by it's username", context do
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 09:23:44 +01:00
test "delete_group/3 deletes a group", %{conn: conn, user: user, actor: actor} do
group = insert(:group)
2019-03-01 17:11:28 +01:00
insert(:member, parent: group, actor: actor, role: :administrator)
2019-01-25 09:23:44 +01: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
assert json_response(res, 200)["data"]["deleteGroup"]["id"] == group.id
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 09:42:31 +01:00
test "delete_group/3 should check user authentication", %{conn: conn, actor: actor} do
group = insert(:group)
2019-03-01 17:11:28 +01:00
insert(:member, parent: group, actor: actor, role: :member)
2019-02-01 09:42:31 +01: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 09:52:36 +01:00
test "delete_group/3 should check the actor is owned by the user", %{
conn: conn,
user: user,
actor: actor
} do
2019-02-01 09:42:31 +01:00
group = insert(:group)
2019-03-01 17:11:28 +01:00
insert(:member, parent: group, actor: actor, role: :member)
2019-02-01 09:42:31 +01: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 09:52:36 +01: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 09:42:31 +01: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 09:52:36 +01: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 09:42:31 +01:00
group = insert(:group)
2019-03-01 17:11:28 +01:00
insert(:member, parent: group, actor: actor, role: :member)
2019-02-01 09:42:31 +01: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
end
end