2020-01-26 20:34:25 +01:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.ReportTest do
|
2020-01-26 21:36:50 +01:00
|
|
|
use Mobilizon.Web.ConnCase
|
2020-06-09 14:07:49 +02:00
|
|
|
use Mobilizon.Tests.Helpers
|
2019-09-22 16:26:23 +02:00
|
|
|
|
2019-07-23 13:49:22 +02:00
|
|
|
import Mobilizon.Factory
|
|
|
|
|
|
|
|
alias Mobilizon.Actors.Actor
|
2020-06-09 14:07:49 +02:00
|
|
|
alias Mobilizon.Config
|
2019-07-23 13:49:22 +02:00
|
|
|
alias Mobilizon.Events.Event
|
2019-09-22 16:26:23 +02:00
|
|
|
alias Mobilizon.Reports.{Note, Report}
|
|
|
|
alias Mobilizon.Users.User
|
|
|
|
|
2020-01-26 20:34:25 +01:00
|
|
|
alias Mobilizon.GraphQL.AbsintheHelpers
|
2019-07-23 13:49:22 +02:00
|
|
|
|
|
|
|
describe "Resolver: Report a content" do
|
2020-06-09 14:07:49 +02:00
|
|
|
@create_report_mutation """
|
2020-11-19 17:06:28 +01:00
|
|
|
mutation CreateReport($reportedId: ID!, $eventId: ID, $content: String) {
|
2020-06-09 14:07:49 +02:00
|
|
|
createReport(
|
|
|
|
reportedId: $reportedId,
|
|
|
|
eventId: $eventId,
|
|
|
|
content: $content
|
|
|
|
) {
|
|
|
|
content,
|
|
|
|
reporter {
|
|
|
|
id
|
|
|
|
},
|
|
|
|
event {
|
|
|
|
id
|
|
|
|
},
|
|
|
|
status
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2020-06-27 19:12:45 +02:00
|
|
|
setup do: clear_config([:anonymous, :reports])
|
2020-06-09 14:07:49 +02:00
|
|
|
|
|
|
|
setup %{conn: conn} do
|
|
|
|
Mobilizon.Config.clear_config_cache()
|
|
|
|
anonymous_actor_id = Config.anonymous_actor_id()
|
|
|
|
{:ok, conn: conn, anonymous_actor_id: anonymous_actor_id}
|
|
|
|
end
|
|
|
|
|
2019-07-23 13:49:22 +02:00
|
|
|
test "create_report/3 creates a report", %{conn: conn} do
|
|
|
|
%User{} = user_reporter = insert(:user)
|
|
|
|
%Actor{} = reporter = insert(:actor, user: user_reporter)
|
|
|
|
%Actor{} = reported = insert(:actor)
|
|
|
|
%Event{} = event = insert(:event, organizer_actor: reported)
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user_reporter)
|
2020-06-09 14:07:49 +02:00
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @create_report_mutation,
|
|
|
|
variables: %{
|
|
|
|
reportedId: reported.id,
|
|
|
|
eventId: event.id,
|
|
|
|
content: "This is an issue"
|
|
|
|
}
|
|
|
|
)
|
2019-07-23 13:49:22 +02:00
|
|
|
|
2020-06-09 14:07:49 +02:00
|
|
|
assert res["errors"] == nil
|
|
|
|
assert res["data"]["createReport"]["content"] == "This is an issue"
|
|
|
|
assert res["data"]["createReport"]["status"] == "OPEN"
|
|
|
|
assert res["data"]["createReport"]["event"]["id"] == to_string(event.id)
|
2019-09-09 09:31:08 +02:00
|
|
|
|
2020-06-09 14:07:49 +02:00
|
|
|
assert res["data"]["createReport"]["reporter"]["id"] ==
|
2019-09-09 09:31:08 +02:00
|
|
|
to_string(reporter.id)
|
2019-07-23 13:49:22 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
test "create_report/3 without being connected doesn't create any report", %{conn: conn} do
|
|
|
|
%Actor{} = reported = insert(:actor)
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
2020-06-09 14:07:49 +02:00
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @create_report_mutation,
|
|
|
|
variables: %{
|
|
|
|
reportedId: reported.id,
|
|
|
|
content: "This is an issue"
|
|
|
|
}
|
|
|
|
)
|
2019-07-23 13:49:22 +02:00
|
|
|
|
2020-06-09 14:07:49 +02:00
|
|
|
assert res["errors"] |> hd |> Map.get("message") ==
|
2019-07-23 13:49:22 +02:00
|
|
|
"You need to be logged-in to create reports"
|
|
|
|
end
|
2020-06-09 14:07:49 +02:00
|
|
|
|
|
|
|
test "create_report/3 anonymously creates a report if config has allowed", %{
|
|
|
|
conn: conn,
|
|
|
|
anonymous_actor_id: anonymous_actor_id
|
|
|
|
} do
|
|
|
|
%Actor{} = reported = insert(:actor)
|
|
|
|
Config.put([:anonymous, :reports, :allowed], true)
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @create_report_mutation,
|
|
|
|
variables: %{
|
|
|
|
reportedId: reported.id,
|
|
|
|
content: "This is an issue"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert is_nil(res["errors"])
|
|
|
|
assert res["data"]["createReport"]["content"] == "This is an issue"
|
|
|
|
assert res["data"]["createReport"]["status"] == "OPEN"
|
|
|
|
|
|
|
|
assert res["data"]["createReport"]["reporter"]["id"] ==
|
|
|
|
to_string(anonymous_actor_id)
|
|
|
|
end
|
2019-07-23 13:49:22 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "Resolver: update a report" do
|
2020-11-19 17:06:28 +01:00
|
|
|
@update_report_mutation """
|
|
|
|
mutation UpdateReport($reportId: ID!, $status: ReportStatus!) {
|
|
|
|
updateReportStatus(reportId: $reportId, status: $status) {
|
|
|
|
content,
|
|
|
|
reporter {
|
|
|
|
id
|
|
|
|
},
|
|
|
|
event {
|
|
|
|
id
|
|
|
|
},
|
|
|
|
status
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2019-07-23 13:49:22 +02:00
|
|
|
test "update_report/3 updates the report's status", %{conn: conn} do
|
|
|
|
%User{} = user_moderator = insert(:user, role: :moderator)
|
2020-11-19 17:06:28 +01:00
|
|
|
%Actor{} = _actor_moderator = insert(:actor, user: user_moderator)
|
2019-07-23 13:49:22 +02:00
|
|
|
%Report{} = report = insert(:report)
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user_moderator)
|
2020-11-19 17:06:28 +01:00
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @update_report_mutation,
|
|
|
|
variables: %{reportId: report.id, status: "RESOLVED"}
|
|
|
|
)
|
2019-07-23 13:49:22 +02:00
|
|
|
|
2020-11-19 17:06:28 +01:00
|
|
|
assert is_nil(res["errors"])
|
2019-07-23 13:49:22 +02:00
|
|
|
|
2020-11-19 17:06:28 +01:00
|
|
|
assert res["data"]["updateReportStatus"]["content"] ==
|
2019-07-23 13:49:22 +02:00
|
|
|
"This is problematic"
|
|
|
|
|
2020-11-19 17:06:28 +01:00
|
|
|
assert res["data"]["updateReportStatus"]["status"] == "RESOLVED"
|
2019-07-23 13:49:22 +02:00
|
|
|
|
2020-11-19 17:06:28 +01:00
|
|
|
assert res["data"]["updateReportStatus"]["reporter"]["id"] ==
|
2019-09-09 09:31:08 +02:00
|
|
|
to_string(report.reporter.id)
|
2019-07-23 13:49:22 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
test "create_report/3 without being connected doesn't create any report", %{conn: conn} do
|
|
|
|
%Report{} = report = insert(:report)
|
|
|
|
|
2020-11-19 17:06:28 +01:00
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @update_report_mutation,
|
|
|
|
variables: %{reportId: report.id, status: "RESOLVED"}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert res["errors"] |> hd |> Map.get("message") ==
|
|
|
|
"You need to be logged-in and a moderator to update a report"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "update_report/3 without being a moderator doesn't update any report", %{conn: conn} do
|
|
|
|
%User{} = user = insert(:user)
|
|
|
|
%Report{} = report = insert(:report)
|
2019-07-23 13:49:22 +02:00
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
2020-11-19 17:06:28 +01:00
|
|
|
|> auth_conn(user)
|
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @update_report_mutation,
|
|
|
|
variables: %{reportId: report.id, status: "RESOLVED"}
|
|
|
|
)
|
2019-07-23 13:49:22 +02:00
|
|
|
|
2020-11-19 17:06:28 +01:00
|
|
|
assert res["errors"] |> hd |> Map.get("message") ==
|
2019-07-23 13:49:22 +02:00
|
|
|
"You need to be logged-in and a moderator to update a report"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Resolver: Get list of reports" do
|
|
|
|
test "get an empty list of reports", %{conn: conn} do
|
|
|
|
%User{} = user_moderator = insert(:user, role: :moderator)
|
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
reports {
|
|
|
|
id,
|
|
|
|
reported {
|
|
|
|
preferredUsername
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "report"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["errors"] |> hd |> Map.get("message") ==
|
|
|
|
"You need to be logged-in and a moderator to list reports"
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user_moderator)
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "report"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["errors"] == nil
|
|
|
|
assert json_response(res, 200)["data"]["reports"] == []
|
|
|
|
end
|
|
|
|
|
|
|
|
test "get a list of reports", %{conn: conn} do
|
|
|
|
%User{} = user_moderator = insert(:user, role: :moderator)
|
|
|
|
|
2019-09-09 09:31:08 +02:00
|
|
|
# Report don't hold millisecond information so we need to wait a bit
|
|
|
|
# between each insert to keep order
|
|
|
|
%Report{id: report_1_id} = insert(:report, content: "My content 1")
|
|
|
|
Process.sleep(1000)
|
|
|
|
%Report{id: report_2_id} = insert(:report, content: "My content 2")
|
|
|
|
Process.sleep(1000)
|
|
|
|
%Report{id: report_3_id} = insert(:report, content: "My content 3")
|
|
|
|
%Report{} = insert(:report, status: :closed)
|
2019-07-23 13:49:22 +02:00
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
reports {
|
|
|
|
id,
|
|
|
|
reported {
|
|
|
|
preferredUsername
|
2019-09-09 09:31:08 +02:00
|
|
|
},
|
|
|
|
content,
|
|
|
|
updatedAt
|
2019-07-23 13:49:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user_moderator)
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "report"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["errors"] == nil
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["reports"]
|
|
|
|
|> Enum.map(fn report -> Map.get(report, "id") end) ==
|
2019-09-09 09:31:08 +02:00
|
|
|
Enum.map([report_3_id, report_2_id, report_1_id], &to_string/1)
|
2019-07-23 13:49:22 +02:00
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
reports(page: 2, limit: 2) {
|
|
|
|
id,
|
|
|
|
reported {
|
|
|
|
preferredUsername
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user_moderator)
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "report"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["errors"] == nil
|
|
|
|
assert json_response(res, 200)["data"]["reports"] |> length == 1
|
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
reports(page: 3, limit: 2) {
|
|
|
|
id,
|
|
|
|
reported {
|
|
|
|
preferredUsername
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user_moderator)
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "report"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["errors"] == nil
|
|
|
|
assert json_response(res, 200)["data"]["reports"] |> length == 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Resolver: View a report" do
|
|
|
|
test "view a report", %{conn: conn} do
|
|
|
|
%User{} = user_moderator = insert(:user, role: :moderator)
|
|
|
|
%Actor{} = insert(:actor, user: user_moderator)
|
|
|
|
%Actor{} = reporter = insert(:actor)
|
|
|
|
%Report{} = report = insert(:report, reporter: reporter)
|
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
report (id: "#{report.id}") {
|
|
|
|
id,
|
|
|
|
reported {
|
|
|
|
preferredUsername
|
|
|
|
},
|
|
|
|
reporter {
|
|
|
|
preferredUsername
|
|
|
|
},
|
|
|
|
event {
|
|
|
|
title
|
|
|
|
},
|
|
|
|
comments {
|
|
|
|
text
|
|
|
|
},
|
|
|
|
content
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "report"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["errors"] |> hd |> Map.get("message") ==
|
|
|
|
"You need to be logged-in and a moderator to view a report"
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user_moderator)
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "report"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["errors"] == nil
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["report"]["reported"]["preferredUsername"] ==
|
|
|
|
report.reported.preferred_username
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["report"]["reporter"]["preferredUsername"] ==
|
|
|
|
reporter.preferred_username
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["report"]["content"] == report.content
|
|
|
|
assert json_response(res, 200)["data"]["report"]["event"]["title"] == report.event.title
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["report"]["comments"] |> hd |> Map.get("text") ==
|
|
|
|
report.comments |> hd |> Map.get(:text)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Resolver: Add a note on a report" do
|
|
|
|
@report_note_content "I agree with this this report"
|
|
|
|
|
|
|
|
test "create_report_note/3 creates a report note", %{conn: conn} do
|
|
|
|
%User{} = user_moderator = insert(:user, role: :moderator)
|
2020-11-19 17:06:28 +01:00
|
|
|
%Actor{} = moderator = insert(:actor, user: user_moderator)
|
2019-07-23 13:49:22 +02:00
|
|
|
%Report{id: report_id} = insert(:report)
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
createReportNote(
|
|
|
|
report_id: #{report_id},
|
|
|
|
content: "#{@report_note_content}"
|
|
|
|
) {
|
|
|
|
content,
|
|
|
|
moderator {
|
|
|
|
preferred_username
|
|
|
|
},
|
|
|
|
report {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user_moderator)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["errors"] == nil
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["createReportNote"]["content"] ==
|
|
|
|
@report_note_content
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["createReportNote"]["moderator"][
|
|
|
|
"preferred_username"
|
|
|
|
] == moderator.preferred_username
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["createReportNote"]["report"]["id"] ==
|
|
|
|
to_string(report_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "delete_report_note deletes a report note", %{conn: conn} do
|
|
|
|
%User{} = user_moderator = insert(:user, role: :moderator)
|
2020-11-19 17:06:28 +01:00
|
|
|
%Actor{} = moderator = insert(:actor, user: user_moderator)
|
2019-07-23 13:49:22 +02:00
|
|
|
%Note{id: report_note_id} = insert(:report_note, moderator: moderator)
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
deleteReportNote(
|
|
|
|
note_id: #{report_note_id},
|
|
|
|
) {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user_moderator)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["errors"] == nil
|
2019-09-09 09:31:08 +02:00
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["deleteReportNote"]["id"] ==
|
|
|
|
to_string(report_note_id)
|
2019-07-23 13:49:22 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|