Merge branch 'develop' into feature/addressable-lists
[akkoma] / test / web / common_api / common_api_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.Web.CommonAPITest do
6 use Pleroma.DataCase
7 alias Pleroma.Activity
8 alias Pleroma.Object
9 alias Pleroma.User
10 alias Pleroma.Web.ActivityPub.ActivityPub
11 alias Pleroma.Web.CommonAPI
12
13 import Pleroma.Factory
14
15 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
16 har = insert(:user)
17 jafnhar = insert(:user)
18 tridi = insert(:user)
19 option = Pleroma.Config.get([:instance, :safe_dm_mentions])
20 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
21
22 {:ok, activity} =
23 CommonAPI.post(har, %{
24 "status" => "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
25 "visibility" => "direct"
26 })
27
28 refute tridi.ap_id in activity.recipients
29 assert jafnhar.ap_id in activity.recipients
30 Pleroma.Config.put([:instance, :safe_dm_mentions], option)
31 end
32
33 test "it de-duplicates tags" do
34 user = insert(:user)
35 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu #2HU"})
36
37 object = Object.normalize(activity)
38
39 assert object.data["tag"] == ["2hu"]
40 end
41
42 test "it adds emoji in the object" do
43 user = insert(:user)
44 {:ok, activity} = CommonAPI.post(user, %{"status" => ":firefox:"})
45
46 assert Object.normalize(activity).data["emoji"]["firefox"]
47 end
48
49 test "it adds emoji when updating profiles" do
50 user = insert(:user, %{name: ":firefox:"})
51
52 CommonAPI.update(user)
53 user = User.get_cached_by_ap_id(user.ap_id)
54 [firefox] = user.info.source_data["tag"]
55
56 assert firefox["name"] == ":firefox:"
57 end
58
59 describe "posting" do
60 test "it supports explicit addressing" do
61 user = insert(:user)
62 user_two = insert(:user)
63 user_three = insert(:user)
64 user_four = insert(:user)
65
66 {:ok, activity} =
67 CommonAPI.post(user, %{
68 "status" =>
69 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
70 "to" => [user_two.nickname, user_four.nickname, "nonexistent"]
71 })
72
73 assert user.ap_id in activity.recipients
74 assert user_two.ap_id in activity.recipients
75 assert user_four.ap_id in activity.recipients
76 refute user_three.ap_id in activity.recipients
77 end
78
79 test "it filters out obviously bad tags when accepting a post as HTML" do
80 user = insert(:user)
81
82 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
83
84 {:ok, activity} =
85 CommonAPI.post(user, %{
86 "status" => post,
87 "content_type" => "text/html"
88 })
89
90 object = Object.normalize(activity)
91
92 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
93 end
94
95 test "it filters out obviously bad tags when accepting a post as Markdown" do
96 user = insert(:user)
97
98 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
99
100 {:ok, activity} =
101 CommonAPI.post(user, %{
102 "status" => post,
103 "content_type" => "text/markdown"
104 })
105
106 object = Object.normalize(activity)
107
108 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
109 end
110
111 test "it does not allow replies to direct messages that are not direct messages themselves" do
112 user = insert(:user)
113
114 {:ok, activity} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
115
116 assert {:ok, _} =
117 CommonAPI.post(user, %{
118 "status" => "suya..",
119 "visibility" => "direct",
120 "in_reply_to_status_id" => activity.id
121 })
122
123 Enum.each(["public", "private", "unlisted"], fn visibility ->
124 assert {:error, "The message visibility must be direct"} =
125 CommonAPI.post(user, %{
126 "status" => "suya..",
127 "visibility" => visibility,
128 "in_reply_to_status_id" => activity.id
129 })
130 end)
131 end
132
133 test "it allows to address a list" do
134 user = insert(:user)
135 {:ok, list} = Pleroma.List.create("foo", user)
136
137 {:ok, activity} =
138 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
139
140 assert activity.data["bcc"] == [list.ap_id]
141 assert activity.recipients == [list.ap_id, user.ap_id]
142 end
143 end
144
145 describe "reactions" do
146 test "repeating a status" do
147 user = insert(:user)
148 other_user = insert(:user)
149
150 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
151
152 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
153 end
154
155 test "favoriting a status" do
156 user = insert(:user)
157 other_user = insert(:user)
158
159 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
160
161 {:ok, %Activity{}, _} = CommonAPI.favorite(activity.id, user)
162 end
163
164 test "retweeting a status twice returns an error" do
165 user = insert(:user)
166 other_user = insert(:user)
167
168 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
169 {:ok, %Activity{}, _object} = CommonAPI.repeat(activity.id, user)
170 {:error, _} = CommonAPI.repeat(activity.id, user)
171 end
172
173 test "favoriting a status twice returns an error" do
174 user = insert(:user)
175 other_user = insert(:user)
176
177 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
178 {:ok, %Activity{}, _object} = CommonAPI.favorite(activity.id, user)
179 {:error, _} = CommonAPI.favorite(activity.id, user)
180 end
181 end
182
183 describe "pinned statuses" do
184 setup do
185 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
186
187 user = insert(:user)
188 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
189
190 [user: user, activity: activity]
191 end
192
193 test "pin status", %{user: user, activity: activity} do
194 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
195
196 id = activity.id
197 user = refresh_record(user)
198
199 assert %User{info: %{pinned_activities: [^id]}} = user
200 end
201
202 test "unlisted statuses can be pinned", %{user: user} do
203 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!", "visibility" => "unlisted"})
204 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
205 end
206
207 test "only self-authored can be pinned", %{activity: activity} do
208 user = insert(:user)
209
210 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
211 end
212
213 test "max pinned statuses", %{user: user, activity: activity_one} do
214 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
215
216 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
217
218 user = refresh_record(user)
219
220 assert {:error, "You have already pinned the maximum number of statuses"} =
221 CommonAPI.pin(activity_two.id, user)
222 end
223
224 test "unpin status", %{user: user, activity: activity} do
225 {:ok, activity} = CommonAPI.pin(activity.id, user)
226
227 user = refresh_record(user)
228
229 assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user)
230
231 user = refresh_record(user)
232
233 assert %User{info: %{pinned_activities: []}} = user
234 end
235
236 test "should unpin when deleting a status", %{user: user, activity: activity} do
237 {:ok, activity} = CommonAPI.pin(activity.id, user)
238
239 user = refresh_record(user)
240
241 assert {:ok, _} = CommonAPI.delete(activity.id, user)
242
243 user = refresh_record(user)
244
245 assert %User{info: %{pinned_activities: []}} = user
246 end
247 end
248
249 describe "mute tests" do
250 setup do
251 user = insert(:user)
252
253 activity = insert(:note_activity)
254
255 [user: user, activity: activity]
256 end
257
258 test "add mute", %{user: user, activity: activity} do
259 {:ok, _} = CommonAPI.add_mute(user, activity)
260 assert CommonAPI.thread_muted?(user, activity)
261 end
262
263 test "remove mute", %{user: user, activity: activity} do
264 CommonAPI.add_mute(user, activity)
265 {:ok, _} = CommonAPI.remove_mute(user, activity)
266 refute CommonAPI.thread_muted?(user, activity)
267 end
268
269 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
270 CommonAPI.add_mute(user, activity)
271 {:error, _} = CommonAPI.add_mute(user, activity)
272 end
273 end
274
275 describe "reports" do
276 test "creates a report" do
277 reporter = insert(:user)
278 target_user = insert(:user)
279
280 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
281
282 reporter_ap_id = reporter.ap_id
283 target_ap_id = target_user.ap_id
284 activity_ap_id = activity.data["id"]
285 comment = "foobar"
286
287 report_data = %{
288 "account_id" => target_user.id,
289 "comment" => comment,
290 "status_ids" => [activity.id]
291 }
292
293 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
294
295 assert %Activity{
296 actor: ^reporter_ap_id,
297 data: %{
298 "type" => "Flag",
299 "content" => ^comment,
300 "object" => [^target_ap_id, ^activity_ap_id],
301 "state" => "open"
302 }
303 } = flag_activity
304 end
305
306 test "updates report state" do
307 [reporter, target_user] = insert_pair(:user)
308 activity = insert(:note_activity, user: target_user)
309
310 {:ok, %Activity{id: report_id}} =
311 CommonAPI.report(reporter, %{
312 "account_id" => target_user.id,
313 "comment" => "I feel offended",
314 "status_ids" => [activity.id]
315 })
316
317 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
318
319 assert report.data["state"] == "resolved"
320 end
321
322 test "does not update report state when state is unsupported" do
323 [reporter, target_user] = insert_pair(:user)
324 activity = insert(:note_activity, user: target_user)
325
326 {:ok, %Activity{id: report_id}} =
327 CommonAPI.report(reporter, %{
328 "account_id" => target_user.id,
329 "comment" => "I feel offended",
330 "status_ids" => [activity.id]
331 })
332
333 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
334 end
335 end
336
337 describe "reblog muting" do
338 setup do
339 muter = insert(:user)
340
341 muted = insert(:user)
342
343 [muter: muter, muted: muted]
344 end
345
346 test "add a reblog mute", %{muter: muter, muted: muted} do
347 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
348
349 assert User.showing_reblogs?(muter, muted) == false
350 end
351
352 test "remove a reblog mute", %{muter: muter, muted: muted} do
353 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
354 {:ok, muter} = CommonAPI.show_reblogs(muter, muted)
355
356 assert User.showing_reblogs?(muter, muted) == true
357 end
358 end
359
360 describe "accept_follow_request/2" do
361 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
362 user = insert(:user, info: %{locked: true})
363 follower = insert(:user)
364 follower_two = insert(:user)
365
366 {:ok, follow_activity} = ActivityPub.follow(follower, user)
367 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
368 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
369
370 assert follow_activity.data["state"] == "pending"
371 assert follow_activity_two.data["state"] == "pending"
372 assert follow_activity_three.data["state"] == "pending"
373
374 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
375
376 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
377 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
378 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
379 end
380
381 test "after rejection, it sets all existing pending follow request states to 'reject'" do
382 user = insert(:user, info: %{locked: true})
383 follower = insert(:user)
384 follower_two = insert(:user)
385
386 {:ok, follow_activity} = ActivityPub.follow(follower, user)
387 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
388 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
389
390 assert follow_activity.data["state"] == "pending"
391 assert follow_activity_two.data["state"] == "pending"
392 assert follow_activity_three.data["state"] == "pending"
393
394 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
395
396 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
397 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
398 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
399 end
400 end
401 end