summaryrefslogtreecommitdiff
path: root/test/pleroma/web/admin_api/controllers/report_controller_test.exs
blob: c141cf69de10b642b64176c07a1fd4c4cc07b87c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.AdminAPI.ReportControllerTest do
  use Pleroma.Web.ConnCase, async: false

  import Pleroma.Factory

  alias Pleroma.Activity
  alias Pleroma.ModerationLog
  alias Pleroma.Repo
  alias Pleroma.ReportNote
  alias Pleroma.Web.CommonAPI

  setup do
    admin = insert(:user, is_admin: true)
    token = insert(:oauth_admin_token, user: admin)

    conn =
      build_conn()
      |> assign(:user, admin)
      |> assign(:token, token)

    {:ok, %{admin: admin, token: token, conn: conn}}
  end

  describe "GET /api/pleroma/admin/reports/:id" do
    setup do
      clear_config([:instance, :admin_privileges], [:reports_manage_reports])
    end

    test "returns 403 if not privileged with :reports_manage_reports", %{conn: conn} do
      clear_config([:instance, :admin_privileges], [])

      conn =
        conn
        |> get("/api/pleroma/admin/reports/report_id")

      assert json_response(conn, :forbidden)
    end

    test "returns report by its id", %{conn: conn} do
      [reporter, target_user] = insert_pair(:user)
      activity = insert(:note_activity, user: target_user)

      {:ok, %{id: report_id}} =
        CommonAPI.report(reporter, %{
          account_id: target_user.id,
          comment: "I feel offended",
          status_ids: [activity.id]
        })

      conn
      |> put_req_header("content-type", "application/json")
      |> post("/api/pleroma/admin/reports/#{report_id}/notes", %{
        content: "this is an admin note"
      })

      response =
        conn
        |> get("/api/pleroma/admin/reports/#{report_id}")
        |> json_response_and_validate_schema(:ok)

      assert response["id"] == report_id

      [notes] = response["notes"]
      assert notes["content"] == "this is an admin note"
    end

    test "renders reported content even if the status is deleted", %{conn: conn} do
      [reporter, target_user] = insert_pair(:user)
      activity = insert(:note_activity, user: target_user)
      activity = Activity.normalize(activity)

      {:ok, %{id: report_id}} =
        CommonAPI.report(reporter, %{
          account_id: target_user.id,
          comment: "I feel offended",
          status_ids: [activity.id]
        })

      CommonAPI.delete(activity.id, target_user)

      response =
        conn
        |> get("/api/pleroma/admin/reports/#{report_id}")
        |> json_response_and_validate_schema(:ok)

      assert response["id"] == report_id

      assert [status] = response["statuses"]
      assert activity.object.data["id"] == status["uri"]
      assert activity.object.data["content"] == status["content"]
    end

    test "returns 404 when report id is invalid", %{conn: conn} do
      conn = get(conn, "/api/pleroma/admin/reports/test")

      assert json_response_and_validate_schema(conn, :not_found) == %{"error" => "Not found"}
    end
  end

  describe "PATCH /api/pleroma/admin/reports" do
    setup do
      clear_config([:instance, :admin_privileges], [:reports_manage_reports])

      [reporter, target_user] = insert_pair(:user)
      activity = insert(:note_activity, user: target_user)

      {:ok, %{id: report_id}} =
        CommonAPI.report(reporter, %{
          account_id: target_user.id,
          comment: "I feel offended",
          status_ids: [activity.id]
        })

      {:ok, %{id: second_report_id}} =
        CommonAPI.report(reporter, %{
          account_id: target_user.id,
          comment: "I feel very offended",
          status_ids: [activity.id]
        })

      %{
        id: report_id,
        second_report_id: second_report_id
      }
    end

    test "returns 403 if not privileged with :reports_manage_reports", %{
      conn: conn,
      id: id,
      admin: admin
    } do
      clear_config([:instance, :admin_privileges], [])

      conn =
        conn
        |> assign(:token, insert(:oauth_token, user: admin, scopes: ["admin:write:reports"]))
        |> put_req_header("content-type", "application/json")
        |> patch("/api/pleroma/admin/reports", %{
          "reports" => [%{"state" => "resolved", "id" => id}]
        })

      assert json_response(conn, :forbidden)
    end

    test "requires admin:write:reports scope", %{conn: conn, id: id, admin: admin} do
      read_token = insert(:oauth_token, user: admin, scopes: ["admin:read"])
      write_token = insert(:oauth_token, user: admin, scopes: ["admin:write:reports"])

      response =
        conn
        |> assign(:token, read_token)
        |> put_req_header("content-type", "application/json")
        |> patch("/api/pleroma/admin/reports", %{
          "reports" => [%{"state" => "resolved", "id" => id}]
        })
        |> json_response_and_validate_schema(403)

      assert response == %{
               "error" => "Insufficient permissions: admin:write:reports."
             }

      conn
      |> assign(:token, write_token)
      |> put_req_header("content-type", "application/json")
      |> patch("/api/pleroma/admin/reports", %{
        "reports" => [%{"state" => "resolved", "id" => id}]
      })
      |> json_response_and_validate_schema(:no_content)
    end

    test "mark report as resolved", %{conn: conn, id: id, admin: admin} do
      conn
      |> put_req_header("content-type", "application/json")
      |> patch("/api/pleroma/admin/reports", %{
        "reports" => [
          %{"state" => "resolved", "id" => id}
        ]
      })
      |> json_response_and_validate_schema(:no_content)

      activity = Activity.get_by_id_with_user_actor(id)
      assert activity.data["state"] == "resolved"

      log_entry = Repo.one(ModerationLog)

      assert ModerationLog.get_log_entry_message(log_entry) ==
               "@#{admin.nickname} updated report ##{id} (on user @#{activity.user_actor.nickname}) with 'resolved' state"
    end

    test "closes report", %{conn: conn, id: id, admin: admin} do
      conn
      |> put_req_header("content-type", "application/json")
      |> patch("/api/pleroma/admin/reports", %{
        "reports" => [
          %{"state" => "closed", "id" => id}
        ]
      })
      |> json_response_and_validate_schema(:no_content)

      activity = Activity.get_by_id_with_user_actor(id)
      assert activity.data["state"] == "closed"

      log_entry = Repo.one(ModerationLog)

      assert ModerationLog.get_log_entry_message(log_entry) ==
               "@#{admin.nickname} updated report ##{id} (on user @#{activity.user_actor.nickname}) with 'closed' state"
    end

    test "returns 400 when state is unknown", %{conn: conn, id: id} do
      conn =
        conn
        |> put_req_header("content-type", "application/json")
        |> patch("/api/pleroma/admin/reports", %{
          "reports" => [
            %{"state" => "test", "id" => id}
          ]
        })

      assert "Unsupported state" =
               hd(json_response_and_validate_schema(conn, :bad_request))["error"]
    end

    test "returns 404 when report is not exist", %{conn: conn} do
      conn =
        conn
        |> put_req_header("content-type", "application/json")
        |> patch("/api/pleroma/admin/reports", %{
          "reports" => [
            %{"state" => "closed", "id" => "test"}
          ]
        })

      assert hd(json_response_and_validate_schema(conn, :bad_request))["error"] == "not_found"
    end

    test "updates state of multiple reports", %{
      conn: conn,
      id: id,
      admin: admin,
      second_report_id: second_report_id
    } do
      conn
      |> put_req_header("content-type", "application/json")
      |> patch("/api/pleroma/admin/reports", %{
        "reports" => [
          %{"state" => "resolved", "id" => id},
          %{"state" => "closed", "id" => second_report_id}
        ]
      })
      |> json_response_and_validate_schema(:no_content)

      activity = Activity.get_by_id_with_user_actor(id)
      second_activity = Activity.get_by_id_with_user_actor(second_report_id)
      assert activity.data["state"] == "resolved"
      assert second_activity.data["state"] == "closed"

      [first_log_entry, second_log_entry] = Repo.all(ModerationLog)

      assert ModerationLog.get_log_entry_message(first_log_entry) ==
               "@#{admin.nickname} updated report ##{id} (on user @#{activity.user_actor.nickname}) with 'resolved' state"

      assert ModerationLog.get_log_entry_message(second_log_entry) ==
               "@#{admin.nickname} updated report ##{second_report_id} (on user @#{second_activity.user_actor.nickname}) with 'closed' state"
    end
  end

  describe "GET /api/pleroma/admin/reports" do
    setup do
      clear_config([:instance, :admin_privileges], [:reports_manage_reports])
    end

    test "returns 403 if not privileged with :reports_manage_reports", %{conn: conn} do
      clear_config([:instance, :admin_privileges], [])

      conn =
        conn
        |> get(report_path(conn, :index))

      assert json_response(conn, :forbidden)
    end

    test "returns empty response when no reports created", %{conn: conn} do
      response =
        conn
        |> get(report_path(conn, :index))
        |> json_response_and_validate_schema(:ok)

      assert Enum.empty?(response["reports"])
      assert response["total"] == 0
    end

    test "returns reports", %{conn: conn} do
      [reporter, target_user] = insert_pair(:user)
      activity = insert(:note_activity, user: target_user)

      {:ok, %{id: report_id}} =
        CommonAPI.report(reporter, %{
          account_id: target_user.id,
          comment: "I feel offended",
          status_ids: [activity.id]
        })

      response =
        conn
        |> get(report_path(conn, :index))
        |> json_response_and_validate_schema(:ok)

      [report] = response["reports"]

      assert length(response["reports"]) == 1
      assert report["id"] == report_id

      assert response["total"] == 1
    end

    test "returns reports with specified state", %{conn: conn} do
      [reporter, target_user] = insert_pair(:user)
      activity = insert(:note_activity, user: target_user)

      {:ok, %{id: first_report_id}} =
        CommonAPI.report(reporter, %{
          account_id: target_user.id,
          comment: "I feel offended",
          status_ids: [activity.id]
        })

      {:ok, %{id: second_report_id}} =
        CommonAPI.report(reporter, %{
          account_id: target_user.id,
          comment: "I don't like this user"
        })

      CommonAPI.update_report_state(second_report_id, "closed")

      response =
        conn
        |> get(report_path(conn, :index, %{state: "open"}))
        |> json_response_and_validate_schema(:ok)

      assert [open_report] = response["reports"]

      assert length(response["reports"]) == 1
      assert open_report["id"] == first_report_id

      assert response["total"] == 1

      response =
        conn
        |> get(report_path(conn, :index, %{state: "closed"}))
        |> json_response_and_validate_schema(:ok)

      assert [closed_report] = response["reports"]

      assert length(response["reports"]) == 1
      assert closed_report["id"] == second_report_id

      assert response["total"] == 1

      assert %{"total" => 0, "reports" => []} ==
               conn
               |> get(report_path(conn, :index, %{state: "resolved"}))
               |> json_response_and_validate_schema(:ok)
    end

    test "renders content correctly", %{conn: conn} do
      [reporter, target_user] = insert_pair(:user)
      note = insert(:note, user: target_user, data: %{"content" => "mew 1"})
      note2 = insert(:note, user: target_user, data: %{"content" => "mew 2"})
      activity = insert(:note_activity, user: target_user, note: note)
      activity2 = insert(:note_activity, user: target_user, note: note2)

      {:ok, _report} =
        CommonAPI.report(reporter, %{
          account_id: target_user.id,
          comment: "I feel offended",
          status_ids: [activity.id, activity2.id]
        })

      CommonAPI.delete(activity.id, target_user)
      CommonAPI.delete(activity2.id, target_user)

      response =
        conn
        |> get(report_path(conn, :index))
        |> json_response_and_validate_schema(:ok)

      assert [open_report] = response["reports"]
      assert %{"statuses" => [s1, s2]} = open_report
      assert "mew 1" in [s1["content"], s2["content"]]
      assert "mew 2" in [s1["content"], s2["content"]]
    end

    test "returns 403 when requested by a non-admin" do
      user = insert(:user)
      token = insert(:oauth_token, user: user)

      conn =
        build_conn()
        |> assign(:user, user)
        |> assign(:token, token)
        |> get("/api/pleroma/admin/reports")

      assert json_response(conn, :forbidden) ==
               %{"error" => "User is not a staff member."}
    end

    test "returns 403 when requested by anonymous" do
      conn = get(build_conn(), "/api/pleroma/admin/reports")

      assert json_response(conn, :forbidden) == %{
               "error" => "Invalid credentials."
             }
    end
  end

  describe "POST /api/pleroma/admin/reports/:id/notes" do
    setup %{conn: conn, admin: admin} do
      clear_config([:instance, :admin_privileges], [:reports_manage_reports])

      [reporter, target_user] = insert_pair(:user)
      activity = insert(:note_activity, user: target_user)

      {:ok, %{id: report_id}} =
        CommonAPI.report(reporter, %{
          account_id: target_user.id,
          comment: "I feel offended",
          status_ids: [activity.id]
        })

      conn
      |> put_req_header("content-type", "application/json")
      |> post("/api/pleroma/admin/reports/#{report_id}/notes", %{
        content: "this is disgusting!"
      })

      conn
      |> put_req_header("content-type", "application/json")
      |> post("/api/pleroma/admin/reports/#{report_id}/notes", %{
        content: "this is disgusting2!"
      })

      %{
        admin_id: admin.id,
        report_id: report_id
      }
    end

    test "returns 403 if not privileged with :reports_manage_reports", %{
      conn: conn,
      report_id: report_id
    } do
      clear_config([:instance, :admin_privileges], [])

      post_conn =
        conn
        |> put_req_header("content-type", "application/json")
        |> post("/api/pleroma/admin/reports/#{report_id}/notes", %{
          content: "this is disgusting2!"
        })

      delete_conn = delete(conn, "/api/pleroma/admin/reports/#{report_id}/notes/note.id")

      assert json_response(post_conn, :forbidden)
      assert json_response(delete_conn, :forbidden)
    end

    test "it creates report note", %{admin_id: admin_id, report_id: report_id} do
      assert [note, _] = Repo.all(ReportNote)

      assert %{
               activity_id: ^report_id,
               content: "this is disgusting!",
               user_id: ^admin_id
             } = note
    end

    test "it returns reports with notes", %{conn: conn, admin: admin} do
      conn = get(conn, "/api/pleroma/admin/reports")

      response = json_response_and_validate_schema(conn, 200)
      notes = hd(response["reports"])["notes"]
      [note, _] = notes

      assert note["user"]["nickname"] == admin.nickname
      # We use '=~' because the order of the notes isn't guaranteed
      assert note["content"] =~ "this is disgusting"
      assert note["created_at"]
      assert response["total"] == 1
    end

    test "it deletes the note", %{conn: conn, report_id: report_id} do
      assert ReportNote |> Repo.all() |> length() == 2
      assert [note, _] = Repo.all(ReportNote)

      delete(conn, "/api/pleroma/admin/reports/#{report_id}/notes/#{note.id}")

      assert ReportNote |> Repo.all() |> length() == 1
    end
  end
end