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