a39a00e0221ff2b3e8265540da28ab53980ec4a1
[akkoma] / test / moderation_log_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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
10
11 import Pleroma.Factory
12
13 describe "user moderation" do
14 setup do
15 admin = insert(:user, info: %{is_admin: true})
16 moderator = insert(:user, info: %{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 user @#{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 report = %Activity{
186 id: "9m9I1F4p8ftrTP6QTI",
187 data: %{
188 "type" => "Flag",
189 "state" => "resolved"
190 }
191 }
192
193 {:ok, _} =
194 ModerationLog.insert_log(%{
195 actor: moderator,
196 action: "report_update",
197 subject: report
198 })
199
200 log = Repo.one(ModerationLog)
201
202 assert log.data["message"] ==
203 "@#{moderator.nickname} updated report ##{report.id} with 'resolved' state"
204 end
205
206 test "logging report response", %{moderator: moderator} do
207 report = %Activity{
208 id: "9m9I1F4p8ftrTP6QTI",
209 data: %{
210 "type" => "Note"
211 }
212 }
213
214 {:ok, _} =
215 ModerationLog.insert_log(%{
216 actor: moderator,
217 action: "report_response",
218 subject: report,
219 text: "look at this"
220 })
221
222 log = Repo.one(ModerationLog)
223
224 assert log.data["message"] ==
225 "@#{moderator.nickname} responded with 'look at this' to report ##{report.id}"
226 end
227
228 test "logging status sensitivity update", %{moderator: moderator} do
229 note = insert(:note_activity)
230
231 {:ok, _} =
232 ModerationLog.insert_log(%{
233 actor: moderator,
234 action: "status_update",
235 subject: note,
236 sensitive: "true",
237 visibility: nil
238 })
239
240 log = Repo.one(ModerationLog)
241
242 assert log.data["message"] ==
243 "@#{moderator.nickname} updated status ##{note.id}, set sensitive: 'true'"
244 end
245
246 test "logging status visibility update", %{moderator: moderator} do
247 note = insert(:note_activity)
248
249 {:ok, _} =
250 ModerationLog.insert_log(%{
251 actor: moderator,
252 action: "status_update",
253 subject: note,
254 sensitive: nil,
255 visibility: "private"
256 })
257
258 log = Repo.one(ModerationLog)
259
260 assert log.data["message"] ==
261 "@#{moderator.nickname} updated status ##{note.id}, set visibility: 'private'"
262 end
263
264 test "logging status sensitivity & visibility update", %{moderator: moderator} do
265 note = insert(:note_activity)
266
267 {:ok, _} =
268 ModerationLog.insert_log(%{
269 actor: moderator,
270 action: "status_update",
271 subject: note,
272 sensitive: "true",
273 visibility: "private"
274 })
275
276 log = Repo.one(ModerationLog)
277
278 assert log.data["message"] ==
279 "@#{moderator.nickname} updated status ##{note.id}, set sensitive: 'true', visibility: 'private'"
280 end
281
282 test "logging status deletion", %{moderator: moderator} do
283 note = insert(:note_activity)
284
285 {:ok, _} =
286 ModerationLog.insert_log(%{
287 actor: moderator,
288 action: "status_delete",
289 subject_id: note.id
290 })
291
292 log = Repo.one(ModerationLog)
293
294 assert log.data["message"] == "@#{moderator.nickname} deleted status ##{note.id}"
295 end
296 end
297 end