Add support for activity expiration to common and Masto API
[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 assert activity.data["listMessage"] == list.ap_id
143 end
144
145 test "it returns error when status is empty and no attachments" do
146 user = insert(:user)
147
148 assert {:error, "Cannot post an empty status without attachments"} =
149 CommonAPI.post(user, %{"status" => ""})
150 end
151
152 test "it returns error when character limit is exceeded" do
153 limit = Pleroma.Config.get([:instance, :limit])
154 Pleroma.Config.put([:instance, :limit], 5)
155
156 user = insert(:user)
157
158 assert {:error, "The status is over the character limit"} =
159 CommonAPI.post(user, %{"status" => "foobar"})
160
161 Pleroma.Config.put([:instance, :limit], limit)
162 end
163
164 test "it can handle activities that expire" do
165 user = insert(:user)
166
167 expires_at =
168 NaiveDateTime.utc_now()
169 |> NaiveDateTime.truncate(:second)
170 |> NaiveDateTime.add(1_000_000, :second)
171
172 expires_at_iso8601 = expires_at |> NaiveDateTime.to_iso8601()
173
174 assert {:ok, activity} =
175 CommonAPI.post(user, %{"status" => "chai", "expires_at" => expires_at_iso8601})
176
177 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
178 assert expiration.scheduled_at == expires_at
179 end
180 end
181
182 describe "reactions" do
183 test "repeating a status" do
184 user = insert(:user)
185 other_user = insert(:user)
186
187 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
188
189 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
190 end
191
192 test "favoriting a status" do
193 user = insert(:user)
194 other_user = insert(:user)
195
196 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
197
198 {:ok, %Activity{}, _} = CommonAPI.favorite(activity.id, user)
199 end
200
201 test "retweeting a status twice returns an error" do
202 user = insert(:user)
203 other_user = insert(:user)
204
205 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
206 {:ok, %Activity{}, _object} = CommonAPI.repeat(activity.id, user)
207 {:error, _} = CommonAPI.repeat(activity.id, user)
208 end
209
210 test "favoriting a status twice returns an error" do
211 user = insert(:user)
212 other_user = insert(:user)
213
214 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
215 {:ok, %Activity{}, _object} = CommonAPI.favorite(activity.id, user)
216 {:error, _} = CommonAPI.favorite(activity.id, user)
217 end
218 end
219
220 describe "pinned statuses" do
221 setup do
222 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
223
224 user = insert(:user)
225 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
226
227 [user: user, activity: activity]
228 end
229
230 test "pin status", %{user: user, activity: activity} do
231 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
232
233 id = activity.id
234 user = refresh_record(user)
235
236 assert %User{info: %{pinned_activities: [^id]}} = user
237 end
238
239 test "unlisted statuses can be pinned", %{user: user} do
240 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!", "visibility" => "unlisted"})
241 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
242 end
243
244 test "only self-authored can be pinned", %{activity: activity} do
245 user = insert(:user)
246
247 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
248 end
249
250 test "max pinned statuses", %{user: user, activity: activity_one} do
251 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
252
253 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
254
255 user = refresh_record(user)
256
257 assert {:error, "You have already pinned the maximum number of statuses"} =
258 CommonAPI.pin(activity_two.id, user)
259 end
260
261 test "unpin status", %{user: user, activity: activity} do
262 {:ok, activity} = CommonAPI.pin(activity.id, user)
263
264 user = refresh_record(user)
265
266 assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user)
267
268 user = refresh_record(user)
269
270 assert %User{info: %{pinned_activities: []}} = user
271 end
272
273 test "should unpin when deleting a status", %{user: user, activity: activity} do
274 {:ok, activity} = CommonAPI.pin(activity.id, user)
275
276 user = refresh_record(user)
277
278 assert {:ok, _} = CommonAPI.delete(activity.id, user)
279
280 user = refresh_record(user)
281
282 assert %User{info: %{pinned_activities: []}} = user
283 end
284 end
285
286 describe "mute tests" do
287 setup do
288 user = insert(:user)
289
290 activity = insert(:note_activity)
291
292 [user: user, activity: activity]
293 end
294
295 test "add mute", %{user: user, activity: activity} do
296 {:ok, _} = CommonAPI.add_mute(user, activity)
297 assert CommonAPI.thread_muted?(user, activity)
298 end
299
300 test "remove mute", %{user: user, activity: activity} do
301 CommonAPI.add_mute(user, activity)
302 {:ok, _} = CommonAPI.remove_mute(user, activity)
303 refute CommonAPI.thread_muted?(user, activity)
304 end
305
306 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
307 CommonAPI.add_mute(user, activity)
308 {:error, _} = CommonAPI.add_mute(user, activity)
309 end
310 end
311
312 describe "reports" do
313 test "creates a report" do
314 reporter = insert(:user)
315 target_user = insert(:user)
316
317 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
318
319 reporter_ap_id = reporter.ap_id
320 target_ap_id = target_user.ap_id
321 activity_ap_id = activity.data["id"]
322 comment = "foobar"
323
324 report_data = %{
325 "account_id" => target_user.id,
326 "comment" => comment,
327 "status_ids" => [activity.id]
328 }
329
330 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
331
332 assert %Activity{
333 actor: ^reporter_ap_id,
334 data: %{
335 "type" => "Flag",
336 "content" => ^comment,
337 "object" => [^target_ap_id, ^activity_ap_id],
338 "state" => "open"
339 }
340 } = flag_activity
341 end
342
343 test "updates report state" do
344 [reporter, target_user] = insert_pair(:user)
345 activity = insert(:note_activity, user: target_user)
346
347 {:ok, %Activity{id: report_id}} =
348 CommonAPI.report(reporter, %{
349 "account_id" => target_user.id,
350 "comment" => "I feel offended",
351 "status_ids" => [activity.id]
352 })
353
354 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
355
356 assert report.data["state"] == "resolved"
357 end
358
359 test "does not update report state when state is unsupported" do
360 [reporter, target_user] = insert_pair(:user)
361 activity = insert(:note_activity, user: target_user)
362
363 {:ok, %Activity{id: report_id}} =
364 CommonAPI.report(reporter, %{
365 "account_id" => target_user.id,
366 "comment" => "I feel offended",
367 "status_ids" => [activity.id]
368 })
369
370 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
371 end
372 end
373
374 describe "reblog muting" do
375 setup do
376 muter = insert(:user)
377
378 muted = insert(:user)
379
380 [muter: muter, muted: muted]
381 end
382
383 test "add a reblog mute", %{muter: muter, muted: muted} do
384 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
385
386 assert User.showing_reblogs?(muter, muted) == false
387 end
388
389 test "remove a reblog mute", %{muter: muter, muted: muted} do
390 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
391 {:ok, muter} = CommonAPI.show_reblogs(muter, muted)
392
393 assert User.showing_reblogs?(muter, muted) == true
394 end
395 end
396
397 describe "unfollow/2" do
398 test "also unsubscribes a user" do
399 [follower, followed] = insert_pair(:user)
400 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
401 {:ok, followed} = User.subscribe(follower, followed)
402
403 assert User.subscribed_to?(follower, followed)
404
405 {:ok, follower} = CommonAPI.unfollow(follower, followed)
406
407 refute User.subscribed_to?(follower, followed)
408 end
409 end
410
411 describe "accept_follow_request/2" do
412 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
413 user = insert(:user, info: %{locked: true})
414 follower = insert(:user)
415 follower_two = insert(:user)
416
417 {:ok, follow_activity} = ActivityPub.follow(follower, user)
418 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
419 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
420
421 assert follow_activity.data["state"] == "pending"
422 assert follow_activity_two.data["state"] == "pending"
423 assert follow_activity_three.data["state"] == "pending"
424
425 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
426
427 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
428 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
429 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
430 end
431
432 test "after rejection, it sets all existing pending follow request states to 'reject'" do
433 user = insert(:user, info: %{locked: true})
434 follower = insert(:user)
435 follower_two = insert(:user)
436
437 {:ok, follow_activity} = ActivityPub.follow(follower, user)
438 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
439 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
440
441 assert follow_activity.data["state"] == "pending"
442 assert follow_activity_two.data["state"] == "pending"
443 assert follow_activity_three.data["state"] == "pending"
444
445 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
446
447 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
448 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
449 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
450 end
451 end
452
453 describe "vote/3" do
454 test "does not allow to vote twice" do
455 user = insert(:user)
456 other_user = insert(:user)
457
458 {:ok, activity} =
459 CommonAPI.post(user, %{
460 "status" => "Am I cute?",
461 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
462 })
463
464 object = Object.normalize(activity)
465
466 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
467
468 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
469 end
470 end
471 end