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