Add link verification in profile fields (#405)
[akkoma] / test / pleroma / moderation_log_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.ModerationLogTest do
6 alias Pleroma.Activity
7 alias Pleroma.ModerationLog
8
9 use Pleroma.DataCase, async: true
10
11 import Pleroma.Factory
12
13 describe "user moderation" do
14 setup do
15 admin = insert(:user, is_admin: true)
16 moderator = insert(:user, is_moderator: true)
17 subject1 = insert(:user)
18 subject2 = insert(:user)
19
20 [admin: admin, moderator: moderator, subject1: subject1, subject2: subject2]
21 end
22
23 test "logging user deletion by moderator", %{moderator: moderator, subject1: subject1} do
24 {:ok, _} =
25 ModerationLog.insert_log(%{
26 actor: moderator,
27 subject: [subject1],
28 action: "delete"
29 })
30
31 log = Repo.one(ModerationLog)
32
33 assert log.data["message"] == "@#{moderator.nickname} deleted users: @#{subject1.nickname}"
34 end
35
36 test "logging user creation by moderator", %{
37 moderator: moderator,
38 subject1: subject1,
39 subject2: subject2
40 } do
41 {:ok, _} =
42 ModerationLog.insert_log(%{
43 actor: moderator,
44 subjects: [subject1, subject2],
45 action: "create"
46 })
47
48 log = Repo.one(ModerationLog)
49
50 assert log.data["message"] ==
51 "@#{moderator.nickname} created users: @#{subject1.nickname}, @#{subject2.nickname}"
52 end
53
54 test "logging user follow by admin", %{admin: admin, subject1: subject1, subject2: subject2} do
55 {:ok, _} =
56 ModerationLog.insert_log(%{
57 actor: admin,
58 followed: subject1,
59 follower: subject2,
60 action: "follow"
61 })
62
63 log = Repo.one(ModerationLog)
64
65 assert log.data["message"] ==
66 "@#{admin.nickname} made @#{subject2.nickname} follow @#{subject1.nickname}"
67 end
68
69 test "logging user unfollow by admin", %{admin: admin, subject1: subject1, subject2: subject2} do
70 {:ok, _} =
71 ModerationLog.insert_log(%{
72 actor: admin,
73 followed: subject1,
74 follower: subject2,
75 action: "unfollow"
76 })
77
78 log = Repo.one(ModerationLog)
79
80 assert log.data["message"] ==
81 "@#{admin.nickname} made @#{subject2.nickname} unfollow @#{subject1.nickname}"
82 end
83
84 test "logging user tagged by admin", %{admin: admin, subject1: subject1, subject2: subject2} do
85 {:ok, _} =
86 ModerationLog.insert_log(%{
87 actor: admin,
88 nicknames: [subject1.nickname, subject2.nickname],
89 tags: ["foo", "bar"],
90 action: "tag"
91 })
92
93 log = Repo.one(ModerationLog)
94
95 users =
96 [subject1.nickname, subject2.nickname]
97 |> Enum.map(&"@#{&1}")
98 |> Enum.join(", ")
99
100 tags = ["foo", "bar"] |> Enum.join(", ")
101
102 assert log.data["message"] == "@#{admin.nickname} added tags: #{tags} to users: #{users}"
103 end
104
105 test "logging user untagged by admin", %{admin: admin, subject1: subject1, subject2: subject2} do
106 {:ok, _} =
107 ModerationLog.insert_log(%{
108 actor: admin,
109 nicknames: [subject1.nickname, subject2.nickname],
110 tags: ["foo", "bar"],
111 action: "untag"
112 })
113
114 log = Repo.one(ModerationLog)
115
116 users =
117 [subject1.nickname, subject2.nickname]
118 |> Enum.map(&"@#{&1}")
119 |> Enum.join(", ")
120
121 tags = ["foo", "bar"] |> Enum.join(", ")
122
123 assert log.data["message"] ==
124 "@#{admin.nickname} removed tags: #{tags} from users: #{users}"
125 end
126
127 test "logging user grant by moderator", %{moderator: moderator, subject1: subject1} do
128 {:ok, _} =
129 ModerationLog.insert_log(%{
130 actor: moderator,
131 subject: [subject1],
132 action: "grant",
133 permission: "moderator"
134 })
135
136 log = Repo.one(ModerationLog)
137
138 assert log.data["message"] == "@#{moderator.nickname} made @#{subject1.nickname} moderator"
139 end
140
141 test "logging user revoke by moderator", %{moderator: moderator, subject1: subject1} do
142 {:ok, _} =
143 ModerationLog.insert_log(%{
144 actor: moderator,
145 subject: [subject1],
146 action: "revoke",
147 permission: "moderator"
148 })
149
150 log = Repo.one(ModerationLog)
151
152 assert log.data["message"] ==
153 "@#{moderator.nickname} revoked moderator role from @#{subject1.nickname}"
154 end
155
156 test "logging relay follow", %{moderator: moderator} do
157 {:ok, _} =
158 ModerationLog.insert_log(%{
159 actor: moderator,
160 action: "relay_follow",
161 target: "https://example.org/relay"
162 })
163
164 log = Repo.one(ModerationLog)
165
166 assert log.data["message"] ==
167 "@#{moderator.nickname} followed relay: https://example.org/relay"
168 end
169
170 test "logging relay unfollow", %{moderator: moderator} do
171 {:ok, _} =
172 ModerationLog.insert_log(%{
173 actor: moderator,
174 action: "relay_unfollow",
175 target: "https://example.org/relay"
176 })
177
178 log = Repo.one(ModerationLog)
179
180 assert log.data["message"] ==
181 "@#{moderator.nickname} unfollowed relay: https://example.org/relay"
182 end
183
184 test "logging report update", %{moderator: moderator} do
185 user = insert(:user)
186
187 report = %Activity{
188 id: "9m9I1F4p8ftrTP6QTI",
189 data: %{
190 "type" => "Flag",
191 "state" => "resolved",
192 "actor" => user.ap_id
193 }
194 }
195
196 {:ok, _} =
197 ModerationLog.insert_log(%{
198 actor: moderator,
199 action: "report_update",
200 subject: report,
201 subject_actor: user
202 })
203
204 log = Repo.one(ModerationLog)
205
206 assert log.data["message"] ==
207 "@#{moderator.nickname} updated report ##{report.id} (on user @#{user.nickname}) with 'resolved' state"
208 end
209
210 test "logging report response", %{moderator: moderator} do
211 user = insert(:user)
212
213 report = %Activity{
214 id: "9m9I1F4p8ftrTP6QTI",
215 data: %{
216 "type" => "Note",
217 "actor" => user.ap_id
218 }
219 }
220
221 attrs = %{
222 actor: moderator,
223 action: "report_note",
224 subject: report,
225 text: "look at this"
226 }
227
228 {:ok, log1} = ModerationLog.insert_log(attrs)
229 log = Repo.get(ModerationLog, log1.id)
230
231 assert log.data["message"] ==
232 "@#{moderator.nickname} added note 'look at this' to report ##{report.id}"
233
234 {:ok, log2} = ModerationLog.insert_log(Map.merge(attrs, %{subject_actor: user}))
235
236 log = Repo.get(ModerationLog, log2.id)
237
238 assert log.data["message"] ==
239 "@#{moderator.nickname} added note 'look at this' to report ##{report.id} on user @#{user.nickname}"
240 end
241
242 test "logging status sensitivity update", %{moderator: moderator} do
243 note = insert(:note_activity)
244
245 {:ok, _} =
246 ModerationLog.insert_log(%{
247 actor: moderator,
248 action: "status_update",
249 subject: note,
250 sensitive: "true",
251 visibility: nil
252 })
253
254 log = Repo.one(ModerationLog)
255
256 assert log.data["message"] ==
257 "@#{moderator.nickname} updated status ##{note.id}, set sensitive: 'true'"
258 end
259
260 test "logging status visibility update", %{moderator: moderator} do
261 note = insert(:note_activity)
262
263 {:ok, _} =
264 ModerationLog.insert_log(%{
265 actor: moderator,
266 action: "status_update",
267 subject: note,
268 sensitive: nil,
269 visibility: "private"
270 })
271
272 log = Repo.one(ModerationLog)
273
274 assert log.data["message"] ==
275 "@#{moderator.nickname} updated status ##{note.id}, set visibility: 'private'"
276 end
277
278 test "logging status sensitivity & visibility update", %{moderator: moderator} do
279 note = insert(:note_activity)
280
281 {:ok, _} =
282 ModerationLog.insert_log(%{
283 actor: moderator,
284 action: "status_update",
285 subject: note,
286 sensitive: "true",
287 visibility: "private"
288 })
289
290 log = Repo.one(ModerationLog)
291
292 assert log.data["message"] ==
293 "@#{moderator.nickname} updated status ##{note.id}, set sensitive: 'true', visibility: 'private'"
294 end
295
296 test "logging status deletion", %{moderator: moderator} do
297 note = insert(:note_activity)
298
299 {:ok, _} =
300 ModerationLog.insert_log(%{
301 actor: moderator,
302 action: "status_delete",
303 subject_id: note.id
304 })
305
306 log = Repo.one(ModerationLog)
307
308 assert log.data["message"] == "@#{moderator.nickname} deleted status ##{note.id}"
309 end
310 end
311 end