[#1973] Fixed accounts rendering in GET /api/v1/pleroma/chats with truish :restrict_u...
[akkoma] / test / web / admin_api / views / report_view_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
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, force: true}),
28 AdminAPI.AccountView.render("show.json", %{user: user})
29 ),
30 account:
31 Map.merge(
32 MastodonAPI.AccountView.render("show.json", %{user: other_user, force: true}),
33 AdminAPI.AccountView.render("show.json", %{user: other_user})
34 ),
35 statuses: [],
36 notes: [],
37 state: "open",
38 id: activity.id
39 }
40
41 result =
42 ReportView.render("show.json", Report.extract_report_info(activity))
43 |> Map.delete(:created_at)
44
45 assert result == expected
46 end
47
48 test "includes reported statuses" do
49 user = insert(:user)
50 other_user = insert(:user)
51 {:ok, activity} = CommonAPI.post(other_user, %{status: "toot"})
52
53 {:ok, report_activity} =
54 CommonAPI.report(user, %{account_id: other_user.id, status_ids: [activity.id]})
55
56 other_user = Pleroma.User.get_by_id(other_user.id)
57
58 expected = %{
59 content: nil,
60 actor:
61 Map.merge(
62 MastodonAPI.AccountView.render("show.json", %{user: user, force: true}),
63 AdminAPI.AccountView.render("show.json", %{user: user})
64 ),
65 account:
66 Map.merge(
67 MastodonAPI.AccountView.render("show.json", %{user: other_user, force: true}),
68 AdminAPI.AccountView.render("show.json", %{user: other_user})
69 ),
70 statuses: [StatusView.render("show.json", %{activity: activity})],
71 state: "open",
72 notes: [],
73 id: report_activity.id
74 }
75
76 result =
77 ReportView.render("show.json", Report.extract_report_info(report_activity))
78 |> Map.delete(:created_at)
79
80 assert result == expected
81 end
82
83 test "renders report's state" do
84 user = insert(:user)
85 other_user = insert(:user)
86
87 {:ok, activity} = CommonAPI.report(user, %{account_id: other_user.id})
88 {:ok, activity} = CommonAPI.update_report_state(activity.id, "closed")
89
90 assert %{state: "closed"} =
91 ReportView.render("show.json", Report.extract_report_info(activity))
92 end
93
94 test "renders report description" do
95 user = insert(:user)
96 other_user = insert(:user)
97
98 {:ok, activity} =
99 CommonAPI.report(user, %{
100 account_id: other_user.id,
101 comment: "posts are too good for this instance"
102 })
103
104 assert %{content: "posts are too good for this instance"} =
105 ReportView.render("show.json", Report.extract_report_info(activity))
106 end
107
108 test "sanitizes report description" do
109 user = insert(:user)
110 other_user = insert(:user)
111
112 {:ok, activity} =
113 CommonAPI.report(user, %{
114 account_id: other_user.id,
115 comment: ""
116 })
117
118 data = Map.put(activity.data, "content", "<script> alert('hecked :D:D:D:D:D:D:D') </script>")
119 activity = Map.put(activity, :data, data)
120
121 refute "<script> alert('hecked :D:D:D:D:D:D:D') </script>" ==
122 ReportView.render("show.json", Report.extract_report_info(activity))[:content]
123 end
124
125 test "doesn't error out when the user doesn't exists" do
126 user = insert(:user)
127 other_user = insert(:user)
128
129 {:ok, activity} =
130 CommonAPI.report(user, %{
131 account_id: other_user.id,
132 comment: ""
133 })
134
135 Pleroma.User.delete(other_user)
136 Pleroma.User.invalidate_cache(other_user)
137
138 assert %{} = ReportView.render("show.json", Report.extract_report_info(activity))
139 end
140 end