9637c2b90f456da8542a526bb33285b727802482
[anni] / test / pleroma / web / admin_api / views / report_view_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.AdminAPI.ReportViewTest do
6   use Pleroma.DataCase, async: true
7
8   import Pleroma.Factory
9
10   alias Pleroma.Web.AdminAPI
11   alias Pleroma.Web.AdminAPI.Report
12   alias Pleroma.Web.AdminAPI.ReportView
13   alias Pleroma.Web.CommonAPI
14   alias Pleroma.Web.MastodonAPI
15   alias Pleroma.Web.MastodonAPI.StatusView
16
17   test "renders a report" do
18     user = insert(:user)
19     other_user = insert(:user)
20
21     {:ok, activity} = CommonAPI.report(user, %{account_id: other_user.id})
22
23     expected = %{
24       content: nil,
25       actor:
26         Map.merge(
27           MastodonAPI.AccountView.render("show.json", %{user: user, skip_visibility_check: true}),
28           AdminAPI.AccountView.render("show.json", %{user: user})
29         ),
30       account:
31         Map.merge(
32           MastodonAPI.AccountView.render("show.json", %{
33             user: other_user,
34             skip_visibility_check: true
35           }),
36           AdminAPI.AccountView.render("show.json", %{user: other_user})
37         ),
38       statuses: [],
39       notes: [],
40       state: "open",
41       id: activity.id
42     }
43
44     result =
45       ReportView.render("show.json", Report.extract_report_info(activity))
46       |> Map.delete(:created_at)
47
48     assert result == expected
49   end
50
51   test "includes reported statuses" do
52     user = insert(:user)
53     other_user = insert(:user)
54     {:ok, activity} = CommonAPI.post(other_user, %{status: "toot"})
55
56     {:ok, report_activity} =
57       CommonAPI.report(user, %{account_id: other_user.id, status_ids: [activity.id]})
58
59     other_user = Pleroma.User.get_by_id(other_user.id)
60
61     expected = %{
62       content: nil,
63       actor:
64         Map.merge(
65           MastodonAPI.AccountView.render("show.json", %{user: user, skip_visibility_check: true}),
66           AdminAPI.AccountView.render("show.json", %{user: user})
67         ),
68       account:
69         Map.merge(
70           MastodonAPI.AccountView.render("show.json", %{
71             user: other_user,
72             skip_visibility_check: true
73           }),
74           AdminAPI.AccountView.render("show.json", %{user: other_user})
75         ),
76       statuses: [StatusView.render("show.json", %{activity: activity})],
77       state: "open",
78       notes: [],
79       id: report_activity.id
80     }
81
82     result =
83       ReportView.render("show.json", Report.extract_report_info(report_activity))
84       |> Map.delete(:created_at)
85
86     assert result == expected
87   end
88
89   test "renders report's state" do
90     user = insert(:user)
91     other_user = insert(:user)
92
93     {:ok, activity} = CommonAPI.report(user, %{account_id: other_user.id})
94     {:ok, activity} = CommonAPI.update_report_state(activity.id, "closed")
95
96     assert %{state: "closed"} =
97              ReportView.render("show.json", Report.extract_report_info(activity))
98   end
99
100   test "renders report description" do
101     user = insert(:user)
102     other_user = insert(:user)
103
104     {:ok, activity} =
105       CommonAPI.report(user, %{
106         account_id: other_user.id,
107         comment: "posts are too good for this instance"
108       })
109
110     assert %{content: "posts are too good for this instance"} =
111              ReportView.render("show.json", Report.extract_report_info(activity))
112   end
113
114   test "sanitizes report description" do
115     user = insert(:user)
116     other_user = insert(:user)
117
118     {:ok, activity} =
119       CommonAPI.report(user, %{
120         account_id: other_user.id,
121         comment: ""
122       })
123
124     data = Map.put(activity.data, "content", "<script> alert('hecked :D:D:D:D:D:D:D') </script>")
125     activity = Map.put(activity, :data, data)
126
127     refute "<script> alert('hecked :D:D:D:D:D:D:D') </script>" ==
128              ReportView.render("show.json", Report.extract_report_info(activity))[:content]
129   end
130
131   test "doesn't error out when the user doesn't exists" do
132     user = insert(:user)
133     other_user = insert(:user)
134
135     {:ok, activity} =
136       CommonAPI.report(user, %{
137         account_id: other_user.id,
138         comment: ""
139       })
140
141     Pleroma.User.delete(other_user)
142     Pleroma.User.invalidate_cache(other_user)
143
144     assert %{} = ReportView.render("show.json", Report.extract_report_info(activity))
145   end
146
147   test "reports are ordered newest first" do
148     user = insert(:user)
149     other_user = insert(:user)
150
151     {:ok, report1} =
152       CommonAPI.report(user, %{
153         account_id: other_user.id,
154         comment: "first report"
155       })
156
157     {:ok, report2} =
158       CommonAPI.report(user, %{
159         account_id: other_user.id,
160         comment: "second report"
161       })
162
163     %{reports: rendered} =
164       ReportView.render("index.json",
165         reports: Pleroma.Web.ActivityPub.Utils.get_reports(%{}, 1, 50)
166       )
167
168     assert report2.id == rendered |> Enum.at(0) |> Map.get(:id)
169     assert report1.id == rendered |> Enum.at(1) |> Map.get(:id)
170   end
171 end