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