2020-01-26 19:34:25 +00:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.PersonTest do
|
2019-12-03 10:29:51 +00:00
|
|
|
use Oban.Testing, repo: Mobilizon.Storage.Repo
|
2020-01-26 20:36:50 +00:00
|
|
|
use Mobilizon.Web.ConnCase
|
2018-11-06 09:30:27 +00:00
|
|
|
|
2020-01-22 23:55:07 +00:00
|
|
|
import Mobilizon.Factory
|
|
|
|
|
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Service.Workers
|
|
|
|
|
2020-01-26 19:34:25 +00:00
|
|
|
alias Mobilizon.GraphQL.AbsintheHelpers
|
2020-01-22 23:55:07 +00:00
|
|
|
|
2020-01-28 18:18:33 +00:00
|
|
|
alias Mobilizon.Web.Endpoint
|
|
|
|
|
2018-11-06 09:30:27 +00:00
|
|
|
@non_existent_username "nonexistent"
|
|
|
|
|
2022-05-06 17:30:52 +00:00
|
|
|
@get_person_query """
|
|
|
|
query Person($id: ID!) {
|
|
|
|
person(id: $id) {
|
|
|
|
preferredUsername,
|
2020-10-02 07:52:47 +00:00
|
|
|
}
|
2022-05-06 17:30:52 +00:00
|
|
|
}
|
|
|
|
"""
|
2018-11-06 09:30:27 +00:00
|
|
|
|
2022-05-06 17:30:52 +00:00
|
|
|
@fetch_person_query """
|
|
|
|
query FetchPerson($preferredUsername: String!) {
|
|
|
|
fetchPerson(preferredUsername: $preferredUsername) {
|
|
|
|
preferredUsername,
|
2018-11-06 09:30:27 +00:00
|
|
|
}
|
2022-05-06 17:30:52 +00:00
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
@fetch_identities_query """
|
|
|
|
{
|
|
|
|
identities {
|
|
|
|
avatar {
|
|
|
|
url
|
|
|
|
},
|
|
|
|
preferredUsername,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
describe "Getting a person" do
|
2020-10-02 07:52:47 +00:00
|
|
|
test "get_person/3 returns a person by its username", %{conn: conn} do
|
|
|
|
user = insert(:user)
|
|
|
|
actor = insert(:actor, user: user)
|
2018-11-06 09:30:27 +00:00
|
|
|
|
|
|
|
res =
|
2020-06-11 17:13:21 +00:00
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
2020-10-02 07:52:47 +00:00
|
|
|
|> AbsintheHelpers.graphql_query(query: @get_person_query, variables: %{id: actor.id})
|
2018-11-06 09:30:27 +00:00
|
|
|
|
2020-10-02 07:52:47 +00:00
|
|
|
assert is_nil(res["errors"])
|
2018-11-06 09:30:27 +00:00
|
|
|
|
2020-10-02 07:52:47 +00:00
|
|
|
assert res["data"]["person"]["preferredUsername"] ==
|
|
|
|
actor.preferred_username
|
2018-11-06 09:30:27 +00:00
|
|
|
|
|
|
|
res =
|
2020-06-11 17:13:21 +00:00
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
2020-10-02 07:52:47 +00:00
|
|
|
|> AbsintheHelpers.graphql_query(query: @get_person_query, variables: %{id: "6895567"})
|
2018-11-06 09:30:27 +00:00
|
|
|
|
2020-10-02 07:52:47 +00:00
|
|
|
assert res["data"]["person"] == nil
|
2018-11-06 09:30:27 +00:00
|
|
|
|
2020-10-02 07:52:47 +00:00
|
|
|
assert hd(res["errors"])["message"] ==
|
2019-10-04 16:28:25 +00:00
|
|
|
"Person with ID 6895567 not found"
|
|
|
|
end
|
|
|
|
|
2019-10-15 19:18:03 +00:00
|
|
|
test "find_person/3 returns a person by its username", context do
|
2019-10-04 16:28:25 +00:00
|
|
|
user = insert(:user)
|
|
|
|
actor = insert(:actor, user: user)
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
2020-10-02 07:52:47 +00:00
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @fetch_person_query,
|
|
|
|
variables: %{preferredUsername: actor.preferred_username}
|
|
|
|
)
|
2019-10-04 16:28:25 +00:00
|
|
|
|
2020-10-02 07:52:47 +00:00
|
|
|
assert hd(res["errors"])["message"] == "You need to be logged in"
|
|
|
|
assert hd(res["errors"])["status_code"] == 401
|
2019-10-04 16:28:25 +00:00
|
|
|
|
2020-10-02 07:52:47 +00:00
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @fetch_person_query,
|
|
|
|
variables: %{preferredUsername: actor.preferred_username}
|
|
|
|
)
|
2019-10-04 16:28:25 +00:00
|
|
|
|
2020-10-02 07:52:47 +00:00
|
|
|
assert res["data"]["fetchPerson"]["preferredUsername"] ==
|
|
|
|
actor.preferred_username
|
2019-10-04 16:28:25 +00:00
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
2020-10-02 07:52:47 +00:00
|
|
|
|> auth_conn(user)
|
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @fetch_person_query,
|
|
|
|
variables: %{preferredUsername: @non_existent_username}
|
|
|
|
)
|
2019-10-04 16:28:25 +00:00
|
|
|
|
2020-10-02 07:52:47 +00:00
|
|
|
assert res["data"]["fetchPerson"] == nil
|
2019-10-04 16:28:25 +00:00
|
|
|
|
2020-10-02 07:52:47 +00:00
|
|
|
assert hd(res["errors"])["message"] ==
|
2019-10-04 16:28:25 +00:00
|
|
|
"Person with username #{@non_existent_username} not found"
|
2018-11-06 09:30:27 +00:00
|
|
|
end
|
|
|
|
|
2018-11-23 14:03:53 +00:00
|
|
|
test "get_current_person/3 returns the current logged-in actor", context do
|
2019-01-25 12:59:58 +00:00
|
|
|
user = insert(:user)
|
|
|
|
actor = insert(:actor, user: user)
|
2018-11-06 09:30:27 +00:00
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
2018-11-23 14:03:53 +00:00
|
|
|
loggedPerson {
|
2019-05-22 12:12:11 +00:00
|
|
|
avatar {
|
|
|
|
url
|
|
|
|
},
|
2018-11-06 09:30:27 +00:00
|
|
|
preferredUsername,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
2018-11-23 14:03:53 +00:00
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "logged_person"))
|
2018-11-06 09:30:27 +00:00
|
|
|
|
2018-11-23 14:03:53 +00:00
|
|
|
assert json_response(res, 200)["data"]["loggedPerson"] == nil
|
2018-11-06 09:30:27 +00:00
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
2020-10-02 07:52:47 +00:00
|
|
|
"You need to be logged in"
|
2018-11-06 09:30:27 +00:00
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
2018-11-29 16:43:22 +00:00
|
|
|
|> auth_conn(user)
|
2018-11-23 14:03:53 +00:00
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "logged_person"))
|
2018-11-06 09:30:27 +00:00
|
|
|
|
2018-11-23 14:03:53 +00:00
|
|
|
assert json_response(res, 200)["data"]["loggedPerson"]["preferredUsername"] ==
|
2018-11-06 09:30:27 +00:00
|
|
|
actor.preferred_username
|
2019-05-22 12:12:11 +00:00
|
|
|
|
2020-01-28 18:18:33 +00:00
|
|
|
assert json_response(res, 200)["data"]["loggedPerson"]["avatar"]["url"] =~ Endpoint.url()
|
2018-11-06 09:30:27 +00:00
|
|
|
end
|
2022-05-06 17:30:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "create_person/3" do
|
|
|
|
@create_person_mutation """
|
|
|
|
mutation CreatePerson(
|
|
|
|
$preferredUsername: String!
|
|
|
|
$name: String!
|
|
|
|
$summary: String
|
|
|
|
$avatar: MediaInput
|
|
|
|
$banner: MediaInput
|
|
|
|
) {
|
|
|
|
createPerson(
|
|
|
|
preferredUsername: $preferredUsername
|
|
|
|
name: $name
|
|
|
|
summary: $summary
|
|
|
|
avatar: $avatar
|
|
|
|
banner: $banner
|
|
|
|
) {
|
|
|
|
id
|
|
|
|
preferredUsername
|
|
|
|
avatar {
|
|
|
|
id,
|
|
|
|
url
|
|
|
|
},
|
|
|
|
banner {
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
2019-02-22 17:07:20 +00:00
|
|
|
|
2022-05-06 17:30:52 +00:00
|
|
|
test "creates a new identity", %{conn: conn} do
|
2019-02-22 17:07:20 +00:00
|
|
|
user = insert(:user)
|
|
|
|
actor = insert(:actor, user: user)
|
|
|
|
|
|
|
|
res =
|
2022-05-06 17:30:52 +00:00
|
|
|
conn
|
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @create_person_mutation,
|
|
|
|
variables: %{
|
|
|
|
preferredUsername: "new_identity",
|
|
|
|
name: "secret person",
|
|
|
|
summary: "no-one will know who I am"
|
|
|
|
}
|
|
|
|
)
|
2019-02-22 17:07:20 +00:00
|
|
|
|
2022-05-06 17:30:52 +00:00
|
|
|
assert res["data"]["createPerson"] == nil
|
2019-02-22 17:07:20 +00:00
|
|
|
|
2022-05-06 17:30:52 +00:00
|
|
|
assert hd(res["errors"])["message"] ==
|
2020-10-02 07:52:47 +00:00
|
|
|
"You need to be logged in"
|
2019-02-22 17:07:20 +00:00
|
|
|
|
|
|
|
res =
|
2022-05-06 17:30:52 +00:00
|
|
|
conn
|
2019-02-22 17:07:20 +00:00
|
|
|
|> auth_conn(user)
|
2022-05-06 17:30:52 +00:00
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @create_person_mutation,
|
|
|
|
variables: %{
|
|
|
|
preferredUsername: "new_identity",
|
|
|
|
name: "secret person",
|
|
|
|
summary: "no-one will know who I am"
|
|
|
|
}
|
|
|
|
)
|
2019-02-22 17:07:20 +00:00
|
|
|
|
2022-05-06 17:30:52 +00:00
|
|
|
assert res["data"]["createPerson"]["preferredUsername"] ==
|
2019-02-22 17:07:20 +00:00
|
|
|
"new_identity"
|
|
|
|
|
|
|
|
res =
|
2022-05-06 17:30:52 +00:00
|
|
|
conn
|
|
|
|
|> AbsintheHelpers.graphql_query(query: @fetch_identities_query)
|
2019-02-22 17:07:20 +00:00
|
|
|
|
2022-05-06 17:30:52 +00:00
|
|
|
assert res["data"]["identities"] == nil
|
2019-02-22 17:07:20 +00:00
|
|
|
|
2022-05-06 17:30:52 +00:00
|
|
|
assert hd(res["errors"])["message"] ==
|
2020-10-02 07:52:47 +00:00
|
|
|
"You need to be logged in"
|
2019-02-22 17:07:20 +00:00
|
|
|
|
|
|
|
res =
|
2022-05-06 17:30:52 +00:00
|
|
|
conn
|
2019-02-22 17:07:20 +00:00
|
|
|
|> auth_conn(user)
|
2022-05-06 17:30:52 +00:00
|
|
|
|> AbsintheHelpers.graphql_query(query: @fetch_identities_query)
|
2019-02-22 17:07:20 +00:00
|
|
|
|
2022-05-06 17:30:52 +00:00
|
|
|
assert res["data"]["identities"]
|
2019-02-22 17:07:20 +00:00
|
|
|
|> Enum.map(fn identity -> Map.get(identity, "preferredUsername") end)
|
|
|
|
|> MapSet.new() ==
|
|
|
|
MapSet.new([actor.preferred_username, "new_identity"])
|
|
|
|
end
|
2019-03-21 19:23:42 +00:00
|
|
|
|
2022-05-06 17:30:52 +00:00
|
|
|
test "with an avatar and an banner creates a new identity", %{conn: conn} do
|
2019-05-22 12:12:11 +00:00
|
|
|
user = insert(:user)
|
|
|
|
insert(:actor, user: user)
|
2019-03-21 19:23:42 +00:00
|
|
|
|
2022-05-06 17:30:52 +00:00
|
|
|
variables = %{
|
|
|
|
preferredUsername: "new_identity",
|
|
|
|
name: "secret person",
|
|
|
|
summary: "no-one will know who I am",
|
|
|
|
banner: %{
|
|
|
|
media: %{
|
|
|
|
file: "landscape.jpg",
|
|
|
|
name: "irish landscape",
|
|
|
|
alt: "The beautiful atlantic way"
|
2019-03-21 19:23:42 +00:00
|
|
|
}
|
2022-05-06 17:30:52 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-22 12:12:11 +00:00
|
|
|
|
|
|
|
map = %{
|
2022-05-06 17:30:52 +00:00
|
|
|
"query" => @create_person_mutation,
|
|
|
|
"variables" => variables,
|
2019-05-22 12:12:11 +00:00
|
|
|
"landscape.jpg" => %Plug.Upload{
|
|
|
|
path: "test/fixtures/picture.png",
|
|
|
|
filename: "landscape.jpg"
|
2019-03-21 19:23:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 12:12:11 +00:00
|
|
|
res =
|
2022-05-06 17:30:52 +00:00
|
|
|
conn
|
2019-05-22 12:12:11 +00:00
|
|
|
|> put_req_header("content-type", "multipart/form-data")
|
2022-05-06 17:30:52 +00:00
|
|
|
|> post(
|
|
|
|
"/api",
|
|
|
|
map
|
|
|
|
)
|
|
|
|
|> json_response(200)
|
2019-03-21 19:23:42 +00:00
|
|
|
|
2022-05-06 17:30:52 +00:00
|
|
|
assert res["data"]["createPerson"] == nil
|
2019-03-21 19:23:42 +00:00
|
|
|
|
2022-05-06 17:30:52 +00:00
|
|
|
assert hd(res["errors"])["message"] ==
|
2020-10-02 07:52:47 +00:00
|
|
|
"You need to be logged in"
|
2019-03-21 19:23:42 +00:00
|
|
|
|
2019-05-22 12:12:11 +00:00
|
|
|
res =
|
2022-05-06 17:30:52 +00:00
|
|
|
conn
|
2019-05-22 12:12:11 +00:00
|
|
|
|> auth_conn(user)
|
|
|
|
|> put_req_header("content-type", "multipart/form-data")
|
2022-05-06 17:30:52 +00:00
|
|
|
|> post(
|
|
|
|
"/api",
|
|
|
|
map
|
|
|
|
)
|
|
|
|
|> json_response(200)
|
2019-03-21 19:23:42 +00:00
|
|
|
|
2022-05-06 17:30:52 +00:00
|
|
|
assert res["data"]["createPerson"]["preferredUsername"] ==
|
2019-05-22 12:12:11 +00:00
|
|
|
"new_identity"
|
|
|
|
|
2022-05-06 17:30:52 +00:00
|
|
|
assert res["data"]["createPerson"]["banner"]["id"]
|
2019-05-22 12:12:11 +00:00
|
|
|
|
2022-05-06 17:30:52 +00:00
|
|
|
assert res["data"]["createPerson"]["banner"]["name"] ==
|
2019-05-22 12:12:11 +00:00
|
|
|
"The beautiful atlantic way"
|
|
|
|
|
2022-05-06 17:30:52 +00:00
|
|
|
assert res["data"]["createPerson"]["banner"]["url"] =~
|
2020-01-28 18:18:33 +00:00
|
|
|
Endpoint.url() <> "/media/"
|
2019-05-22 12:12:11 +00:00
|
|
|
end
|
|
|
|
|
2022-05-06 17:30:52 +00:00
|
|
|
test "with an username that is not acceptable", %{conn: conn} do
|
|
|
|
user = insert(:user)
|
|
|
|
_actor = insert(:actor, user: user)
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @create_person_mutation,
|
|
|
|
variables: %{
|
|
|
|
preferredUsername: "me@no",
|
|
|
|
name: "wrong person"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert hd(res["errors"])["message"] ==
|
|
|
|
["Username must only contain alphanumeric lowercased characters and underscores."]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "update_person/3" do
|
|
|
|
test "updates an existing identity", context do
|
2019-06-17 15:15:27 +00:00
|
|
|
user = insert(:user)
|
2019-10-04 16:28:25 +00:00
|
|
|
%Actor{id: person_id} = insert(:actor, user: user, preferred_username: "riri")
|
2019-06-17 15:15:27 +00:00
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
updatePerson(
|
2019-10-04 16:28:25 +00:00
|
|
|
id: "#{person_id}",
|
2019-06-17 15:15:27 +00:00
|
|
|
name: "riri updated",
|
|
|
|
summary: "summary updated",
|
|
|
|
banner: {
|
2020-11-26 10:41:13 +00:00
|
|
|
media: {
|
2019-06-17 15:15:27 +00:00
|
|
|
file: "landscape.jpg",
|
|
|
|
name: "irish landscape",
|
|
|
|
alt: "The beautiful atlantic way"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
id,
|
|
|
|
preferredUsername,
|
|
|
|
name,
|
|
|
|
summary,
|
|
|
|
avatar {
|
|
|
|
id,
|
|
|
|
url
|
|
|
|
},
|
|
|
|
banner {
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
map = %{
|
|
|
|
"query" => mutation,
|
|
|
|
"landscape.jpg" => %Plug.Upload{
|
|
|
|
path: "test/fixtures/picture.png",
|
|
|
|
filename: "landscape.jpg"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> put_req_header("content-type", "multipart/form-data")
|
|
|
|
|> post("/api", map)
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["updatePerson"] == nil
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
2020-10-02 07:52:47 +00:00
|
|
|
"You need to be logged in"
|
2019-06-17 15:15:27 +00:00
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> put_req_header("content-type", "multipart/form-data")
|
|
|
|
|> post("/api", map)
|
|
|
|
|
|
|
|
res_person = json_response(res, 200)["data"]["updatePerson"]
|
|
|
|
|
|
|
|
assert res_person["preferredUsername"] == "riri"
|
|
|
|
assert res_person["name"] == "riri updated"
|
|
|
|
assert res_person["summary"] == "summary updated"
|
|
|
|
|
|
|
|
assert res_person["banner"]["id"]
|
|
|
|
assert res_person["banner"]["name"] == "The beautiful atlantic way"
|
2020-01-28 18:18:33 +00:00
|
|
|
assert res_person["banner"]["url"] =~ Endpoint.url() <> "/media/"
|
2019-06-17 15:15:27 +00:00
|
|
|
end
|
|
|
|
|
2022-05-06 17:30:52 +00:00
|
|
|
test "should fail to update a not owned identity", context do
|
2019-06-17 15:15:27 +00:00
|
|
|
user1 = insert(:user)
|
|
|
|
user2 = insert(:user)
|
2019-10-04 16:28:25 +00:00
|
|
|
%Actor{id: person_id} = insert(:actor, user: user2, preferred_username: "riri")
|
2019-06-17 15:15:27 +00:00
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
updatePerson(
|
2019-10-04 16:28:25 +00:00
|
|
|
id: "#{person_id}",
|
2019-06-17 15:15:27 +00:00
|
|
|
name: "riri updated",
|
|
|
|
) {
|
|
|
|
id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> auth_conn(user1)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["updatePerson"] == nil
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
2020-09-29 07:53:48 +00:00
|
|
|
"Profile is not owned by authenticated user"
|
2019-06-17 15:15:27 +00:00
|
|
|
end
|
|
|
|
|
2022-05-06 17:30:52 +00:00
|
|
|
test "should fail to update a not existing identity", context do
|
2019-06-17 15:15:27 +00:00
|
|
|
user = insert(:user)
|
|
|
|
insert(:actor, user: user, preferred_username: "riri")
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
updatePerson(
|
2019-10-04 16:28:25 +00:00
|
|
|
id: "48918",
|
2019-06-17 15:15:27 +00:00
|
|
|
name: "riri updated",
|
|
|
|
) {
|
|
|
|
id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["updatePerson"] == nil
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
2020-09-29 07:53:48 +00:00
|
|
|
"Profile not found"
|
2019-06-17 15:15:27 +00:00
|
|
|
end
|
2022-05-06 17:30:52 +00:00
|
|
|
end
|
2019-06-17 15:15:27 +00:00
|
|
|
|
2022-05-06 17:30:52 +00:00
|
|
|
describe "delete_person/3" do
|
|
|
|
test "should fail to update a not owned identity", context do
|
2019-06-17 15:15:27 +00:00
|
|
|
user1 = insert(:user)
|
|
|
|
user2 = insert(:user)
|
2019-10-04 16:28:25 +00:00
|
|
|
%Actor{id: person_id} = insert(:actor, user: user2, preferred_username: "riri")
|
2019-06-17 15:15:27 +00:00
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
2019-10-04 16:28:25 +00:00
|
|
|
deletePerson(id: "#{person_id}") {
|
2019-06-17 15:15:27 +00:00
|
|
|
id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> auth_conn(user1)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
2019-08-26 13:44:02 +00:00
|
|
|
assert json_response(res, 200)["data"]["deletePerson"] == nil
|
2019-06-17 15:15:27 +00:00
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
2020-09-29 07:53:48 +00:00
|
|
|
"Profile is not owned by authenticated user"
|
2019-06-17 15:15:27 +00:00
|
|
|
end
|
|
|
|
|
2022-05-06 17:30:52 +00:00
|
|
|
test "should fail to delete a not existing identity", context do
|
2019-06-17 15:15:27 +00:00
|
|
|
user = insert(:user)
|
|
|
|
insert(:actor, user: user, preferred_username: "riri")
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
2019-10-04 16:28:25 +00:00
|
|
|
deletePerson(id: "9798665") {
|
2019-06-17 15:15:27 +00:00
|
|
|
id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
2019-08-26 13:44:02 +00:00
|
|
|
assert json_response(res, 200)["data"]["deletePerson"] == nil
|
2019-06-17 15:15:27 +00:00
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
2020-09-29 07:53:48 +00:00
|
|
|
"Profile not found"
|
2019-06-17 15:15:27 +00:00
|
|
|
end
|
|
|
|
|
2022-05-06 17:30:52 +00:00
|
|
|
test "should fail to delete the last user identity", context do
|
2019-06-17 15:15:27 +00:00
|
|
|
user = insert(:user)
|
2019-10-04 16:28:25 +00:00
|
|
|
%Actor{id: person_id} = insert(:actor, user: user, preferred_username: "riri")
|
2019-06-17 15:15:27 +00:00
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
2019-10-04 16:28:25 +00:00
|
|
|
deletePerson(id: "#{person_id}") {
|
2019-06-17 15:15:27 +00:00
|
|
|
id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
2019-08-26 13:44:02 +00:00
|
|
|
assert json_response(res, 200)["data"]["deletePerson"] == nil
|
2019-06-17 15:15:27 +00:00
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
|
|
|
"Cannot remove the last identity of a user"
|
|
|
|
end
|
|
|
|
|
2022-05-06 17:30:52 +00:00
|
|
|
test "should fail to delete an identity that is the last admin of a group",
|
2019-08-26 13:44:02 +00:00
|
|
|
context do
|
|
|
|
group = insert(:group)
|
|
|
|
classic_user = insert(:user)
|
|
|
|
classic_actor = insert(:actor, user: classic_user, preferred_username: "classic_user")
|
|
|
|
|
|
|
|
admin_user = insert(:user)
|
|
|
|
admin_actor = insert(:actor, user: admin_user, preferred_username: "last_admin")
|
|
|
|
insert(:actor, user: admin_user)
|
|
|
|
|
|
|
|
insert(:member, %{actor: admin_actor, role: :creator, parent: group})
|
|
|
|
insert(:member, %{actor: classic_actor, role: :member, parent: group})
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
2019-10-04 16:28:25 +00:00
|
|
|
deletePerson(id: "#{admin_actor.id}") {
|
2019-08-26 13:44:02 +00:00
|
|
|
id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> auth_conn(admin_user)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["deletePerson"] == nil
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
|
|
|
"Cannot remove the last administrator of a group"
|
|
|
|
end
|
|
|
|
|
2022-05-06 17:30:52 +00:00
|
|
|
test "should delete an actor identity", context do
|
2019-06-17 15:15:27 +00:00
|
|
|
user = insert(:user)
|
2019-10-04 16:28:25 +00:00
|
|
|
%Actor{id: person_id} = insert(:actor, user: user, preferred_username: "riri")
|
2019-06-17 15:15:27 +00:00
|
|
|
insert(:actor, user: user, preferred_username: "fifi")
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
2019-10-04 16:28:25 +00:00
|
|
|
deletePerson(id: "#{person_id}") {
|
2019-06-17 15:15:27 +00:00
|
|
|
id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["errors"] == nil
|
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
assert_enqueued(
|
2020-01-22 23:55:07 +00:00
|
|
|
worker: Workers.Background,
|
2020-08-27 09:53:24 +00:00
|
|
|
args: %{
|
|
|
|
"actor_id" => person_id,
|
|
|
|
"op" => "delete_actor",
|
|
|
|
"author_id" => nil,
|
|
|
|
"suspension" => false,
|
|
|
|
"reserve_username" => true
|
|
|
|
}
|
2019-12-03 10:29:51 +00:00
|
|
|
)
|
|
|
|
|
2022-03-23 10:38:25 +00:00
|
|
|
assert %{success: 1, snoozed: 0, failure: 0, discard: 0} ==
|
|
|
|
Oban.drain_queue(queue: :background)
|
2019-12-03 10:29:51 +00:00
|
|
|
|
2019-06-17 15:15:27 +00:00
|
|
|
query = """
|
|
|
|
{
|
2019-10-04 16:28:25 +00:00
|
|
|
person(id: "#{person_id}") {
|
2019-06-17 15:15:27 +00:00
|
|
|
id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "person"))
|
|
|
|
|
2019-10-04 16:28:25 +00:00
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
|
|
|
"Person with ID #{person_id} not found"
|
2019-06-17 15:15:27 +00:00
|
|
|
end
|
2019-09-26 14:38:58 +00:00
|
|
|
end
|
2019-06-17 15:15:27 +00:00
|
|
|
|
2019-09-26 14:38:58 +00:00
|
|
|
describe "get_current_person/3" do
|
2019-05-22 12:12:11 +00:00
|
|
|
test "get_current_person/3 can return the events the person is going to", context do
|
|
|
|
user = insert(:user)
|
|
|
|
actor = insert(:actor, user: user)
|
2019-03-21 19:23:42 +00:00
|
|
|
|
2019-05-22 12:12:11 +00:00
|
|
|
query = """
|
|
|
|
{
|
|
|
|
loggedPerson {
|
2019-09-26 14:38:58 +00:00
|
|
|
participations {
|
2020-06-11 17:13:21 +00:00
|
|
|
elements {
|
|
|
|
event {
|
|
|
|
uuid,
|
|
|
|
title
|
|
|
|
}
|
2019-09-26 14:38:58 +00:00
|
|
|
}
|
2019-05-22 12:12:11 +00:00
|
|
|
}
|
2019-03-21 19:23:42 +00:00
|
|
|
}
|
2019-05-22 12:12:11 +00:00
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "logged_person"))
|
|
|
|
|
2020-06-11 17:13:21 +00:00
|
|
|
assert json_response(res, 200)["data"]["loggedPerson"]["participations"]["elements"] == []
|
2019-05-22 12:12:11 +00:00
|
|
|
|
|
|
|
event = insert(:event, %{organizer_actor: actor})
|
|
|
|
insert(:participant, %{actor: actor, event: event})
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "logged_person"))
|
|
|
|
|
2020-06-11 17:13:21 +00:00
|
|
|
assert json_response(res, 200)["data"]["loggedPerson"]["participations"]["elements"] == [
|
2019-09-26 14:38:58 +00:00
|
|
|
%{"event" => %{"title" => event.title, "uuid" => event.uuid}}
|
2019-05-22 12:12:11 +00:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2020-06-11 17:13:21 +00:00
|
|
|
@person_participations """
|
|
|
|
query PersonParticipations($actorId: ID!) {
|
|
|
|
person(id: $actorId) {
|
|
|
|
participations {
|
|
|
|
total,
|
|
|
|
elements {
|
2019-09-26 14:38:58 +00:00
|
|
|
event {
|
|
|
|
uuid,
|
|
|
|
title
|
|
|
|
}
|
2019-05-22 12:12:11 +00:00
|
|
|
}
|
2020-06-11 17:13:21 +00:00
|
|
|
}
|
2019-03-21 19:23:42 +00:00
|
|
|
}
|
2020-06-11 17:13:21 +00:00
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
test "find_person/3 can return the events an identity is going to if it's the same actor",
|
|
|
|
context do
|
|
|
|
user = insert(:user)
|
|
|
|
actor = insert(:actor, user: user)
|
|
|
|
insert(:actor, user: user)
|
|
|
|
actor_from_other_user = insert(:actor)
|
2019-05-22 12:12:11 +00:00
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> auth_conn(user)
|
2020-06-11 17:13:21 +00:00
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @person_participations,
|
|
|
|
variables: %{actorId: actor.id}
|
|
|
|
)
|
2019-05-22 12:12:11 +00:00
|
|
|
|
2020-06-11 17:13:21 +00:00
|
|
|
assert res["data"]["person"]["participations"]["elements"] == []
|
2019-03-21 19:23:42 +00:00
|
|
|
|
2019-05-22 12:12:11 +00:00
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> auth_conn(user)
|
2020-06-11 17:13:21 +00:00
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @person_participations,
|
|
|
|
variables: %{actorId: actor_from_other_user.id}
|
|
|
|
)
|
2019-03-21 19:23:42 +00:00
|
|
|
|
2020-06-11 17:13:21 +00:00
|
|
|
assert res["data"]["person"]["participations"]["elements"] == nil
|
2019-03-21 19:23:42 +00:00
|
|
|
|
2020-06-11 17:13:21 +00:00
|
|
|
assert hd(res["errors"])["message"] ==
|
2020-09-29 07:53:48 +00:00
|
|
|
"Profile is not owned by authenticated user"
|
2019-05-22 12:12:11 +00:00
|
|
|
end
|
2019-09-26 14:38:58 +00:00
|
|
|
|
|
|
|
test "find_person/3 can return the participation for an identity on a specific event",
|
|
|
|
context do
|
|
|
|
user = insert(:user)
|
|
|
|
actor = insert(:actor, user: user)
|
|
|
|
event = insert(:event, organizer_actor: actor)
|
|
|
|
insert(:participant, event: event, actor: actor)
|
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
2019-10-04 16:28:25 +00:00
|
|
|
person(id: "#{actor.id}") {
|
2019-09-26 14:38:58 +00:00
|
|
|
participations(eventId: "#{event.id}") {
|
2020-06-11 17:13:21 +00:00
|
|
|
elements {
|
|
|
|
event {
|
|
|
|
uuid,
|
|
|
|
title
|
|
|
|
}
|
2019-09-26 14:38:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "person"))
|
|
|
|
|
2020-06-11 17:13:21 +00:00
|
|
|
assert json_response(res, 200)["data"]["person"]["participations"]["elements"] == [
|
2019-09-26 14:38:58 +00:00
|
|
|
%{
|
|
|
|
"event" => %{
|
|
|
|
"uuid" => event.uuid,
|
|
|
|
"title" => event.title
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
end
|
2019-03-21 19:23:42 +00:00
|
|
|
end
|
2020-06-11 17:13:21 +00:00
|
|
|
|
|
|
|
describe "suspend_profile/3" do
|
|
|
|
@suspend_profile_mutation """
|
|
|
|
mutation SuspendProfile($id: ID!) {
|
|
|
|
suspendProfile(id: $id) {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
@person_query """
|
|
|
|
query Person($id: ID!) {
|
|
|
|
person(id: $id) {
|
|
|
|
id,
|
|
|
|
suspended
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
@moderation_logs_query """
|
|
|
|
{
|
|
|
|
actionLogs {
|
2021-04-27 15:53:11 +00:00
|
|
|
total
|
|
|
|
elements {
|
|
|
|
action,
|
|
|
|
actor {
|
2020-06-11 17:13:21 +00:00
|
|
|
id,
|
|
|
|
preferredUsername
|
2021-04-27 15:53:11 +00:00
|
|
|
},
|
|
|
|
object {
|
|
|
|
...on Person {
|
|
|
|
id,
|
|
|
|
preferredUsername
|
|
|
|
}
|
2020-06-11 17:13:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
test "suspends a remote profile", %{conn: conn} do
|
|
|
|
modo = insert(:user, role: :moderator)
|
|
|
|
%Actor{id: modo_actor_id} = insert(:actor, user: modo)
|
|
|
|
%Actor{id: remote_profile_id} = insert(:actor, domain: "mobilizon.org", user: nil)
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(modo)
|
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @suspend_profile_mutation,
|
|
|
|
variables: %{id: remote_profile_id}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert is_nil(res["errors"])
|
|
|
|
assert res["data"]["suspendProfile"]["id"] == to_string(remote_profile_id)
|
|
|
|
|
2022-03-23 10:38:25 +00:00
|
|
|
assert %{success: 1, snoozed: 0, failure: 0, discard: 0} ==
|
|
|
|
Oban.drain_queue(queue: :background)
|
2020-06-11 17:13:21 +00:00
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(modo)
|
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @person_query,
|
|
|
|
variables: %{id: remote_profile_id}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert res["data"]["person"]["suspended"] == true
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(modo)
|
|
|
|
|> AbsintheHelpers.graphql_query(query: @moderation_logs_query)
|
|
|
|
|
2021-04-27 15:53:11 +00:00
|
|
|
actionlog = hd(res["data"]["actionLogs"]["elements"])
|
2020-06-11 17:13:21 +00:00
|
|
|
refute is_nil(actionlog)
|
|
|
|
assert actionlog["action"] == "ACTOR_SUSPENSION"
|
|
|
|
assert actionlog["actor"]["id"] == to_string(modo_actor_id)
|
|
|
|
assert actionlog["object"]["id"] == to_string(remote_profile_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "doesn't suspend if profile is local", %{conn: conn} do
|
|
|
|
modo = insert(:user, role: :moderator)
|
|
|
|
%Actor{} = insert(:actor, user: modo)
|
|
|
|
%Actor{id: profile_id} = insert(:actor)
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(modo)
|
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @suspend_profile_mutation,
|
|
|
|
variables: %{id: profile_id}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert hd(res["errors"])["message"] == "No remote profile found with this ID"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "doesn't suspend if user is not at least moderator", %{conn: conn} do
|
|
|
|
fake_modo = insert(:user)
|
|
|
|
%Actor{} = insert(:actor, user: fake_modo)
|
|
|
|
%Actor{id: remote_profile_id} = insert(:actor, domain: "mobilizon.org", user: nil)
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(fake_modo)
|
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @suspend_profile_mutation,
|
|
|
|
variables: %{id: remote_profile_id}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert hd(res["errors"])["message"] ==
|
|
|
|
"Only moderators and administrators can suspend a profile"
|
|
|
|
end
|
|
|
|
end
|
2018-11-06 09:30:27 +00:00
|
|
|
end
|