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