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