Merge branch 'fix/fedsocket-useragent' into 'develop'
[akkoma] / test / web / mastodon_api / controllers / timeline_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
6 use Pleroma.Web.ConnCase
7
8 import Pleroma.Factory
9 import Tesla.Mock
10
11 alias Pleroma.Config
12 alias Pleroma.User
13 alias Pleroma.Web.CommonAPI
14
15 setup do
16 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
17 :ok
18 end
19
20 describe "home" do
21 setup do: oauth_access(["read:statuses"])
22
23 test "does NOT embed account/pleroma/relationship in statuses", %{
24 user: user,
25 conn: conn
26 } do
27 other_user = insert(:user)
28
29 {:ok, _} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
30
31 response =
32 conn
33 |> assign(:user, user)
34 |> get("/api/v1/timelines/home")
35 |> json_response_and_validate_schema(200)
36
37 assert Enum.all?(response, fn n ->
38 get_in(n, ["account", "pleroma", "relationship"]) == %{}
39 end)
40 end
41
42 test "the home timeline when the direct messages are excluded", %{user: user, conn: conn} do
43 {:ok, public_activity} = CommonAPI.post(user, %{status: ".", visibility: "public"})
44 {:ok, direct_activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
45
46 {:ok, unlisted_activity} = CommonAPI.post(user, %{status: ".", visibility: "unlisted"})
47
48 {:ok, private_activity} = CommonAPI.post(user, %{status: ".", visibility: "private"})
49
50 conn = get(conn, "/api/v1/timelines/home?exclude_visibilities[]=direct")
51
52 assert status_ids = json_response_and_validate_schema(conn, :ok) |> Enum.map(& &1["id"])
53 assert public_activity.id in status_ids
54 assert unlisted_activity.id in status_ids
55 assert private_activity.id in status_ids
56 refute direct_activity.id in status_ids
57 end
58 end
59
60 describe "public" do
61 @tag capture_log: true
62 test "the public timeline", %{conn: conn} do
63 user = insert(:user)
64
65 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
66
67 _activity = insert(:note_activity, local: false)
68
69 conn = get(conn, "/api/v1/timelines/public?local=False")
70
71 assert length(json_response_and_validate_schema(conn, :ok)) == 2
72
73 conn = get(build_conn(), "/api/v1/timelines/public?local=True")
74
75 assert [%{"content" => "test"}] = json_response_and_validate_schema(conn, :ok)
76
77 conn = get(build_conn(), "/api/v1/timelines/public?local=1")
78
79 assert [%{"content" => "test"}] = json_response_and_validate_schema(conn, :ok)
80
81 # does not contain repeats
82 {:ok, _} = CommonAPI.repeat(activity.id, user)
83
84 conn = get(build_conn(), "/api/v1/timelines/public?local=true")
85
86 assert [_] = json_response_and_validate_schema(conn, :ok)
87 end
88
89 test "the public timeline includes only public statuses for an authenticated user" do
90 %{user: user, conn: conn} = oauth_access(["read:statuses"])
91
92 {:ok, _activity} = CommonAPI.post(user, %{status: "test"})
93 {:ok, _activity} = CommonAPI.post(user, %{status: "test", visibility: "private"})
94 {:ok, _activity} = CommonAPI.post(user, %{status: "test", visibility: "unlisted"})
95 {:ok, _activity} = CommonAPI.post(user, %{status: "test", visibility: "direct"})
96
97 res_conn = get(conn, "/api/v1/timelines/public")
98 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
99 end
100
101 test "doesn't return replies if follower is posting with blocked user" do
102 %{conn: conn, user: blocker} = oauth_access(["read:statuses"])
103 [blockee, friend] = insert_list(2, :user)
104 {:ok, blocker} = User.follow(blocker, friend)
105 {:ok, _} = User.block(blocker, blockee)
106
107 conn = assign(conn, :user, blocker)
108
109 {:ok, %{id: activity_id} = activity} = CommonAPI.post(friend, %{status: "hey!"})
110
111 {:ok, reply_from_blockee} =
112 CommonAPI.post(blockee, %{status: "heya", in_reply_to_status_id: activity})
113
114 {:ok, _reply_from_friend} =
115 CommonAPI.post(friend, %{status: "status", in_reply_to_status_id: reply_from_blockee})
116
117 # Still shows replies from yourself
118 {:ok, %{id: reply_from_me}} =
119 CommonAPI.post(blocker, %{status: "status", in_reply_to_status_id: reply_from_blockee})
120
121 response =
122 get(conn, "/api/v1/timelines/public")
123 |> json_response_and_validate_schema(200)
124
125 assert length(response) == 2
126 [%{"id" => ^reply_from_me}, %{"id" => ^activity_id}] = response
127 end
128
129 test "doesn't return replies if follow is posting with users from blocked domain" do
130 %{conn: conn, user: blocker} = oauth_access(["read:statuses"])
131 friend = insert(:user)
132 blockee = insert(:user, ap_id: "https://example.com/users/blocked")
133 {:ok, blocker} = User.follow(blocker, friend)
134 {:ok, blocker} = User.block_domain(blocker, "example.com")
135
136 conn = assign(conn, :user, blocker)
137
138 {:ok, %{id: activity_id} = activity} = CommonAPI.post(friend, %{status: "hey!"})
139
140 {:ok, reply_from_blockee} =
141 CommonAPI.post(blockee, %{status: "heya", in_reply_to_status_id: activity})
142
143 {:ok, _reply_from_friend} =
144 CommonAPI.post(friend, %{status: "status", in_reply_to_status_id: reply_from_blockee})
145
146 res_conn = get(conn, "/api/v1/timelines/public")
147
148 activities = json_response_and_validate_schema(res_conn, 200)
149 [%{"id" => ^activity_id}] = activities
150 end
151 end
152
153 defp local_and_remote_activities do
154 insert(:note_activity)
155 insert(:note_activity, local: false)
156 :ok
157 end
158
159 describe "public with restrict unauthenticated timeline for local and federated timelines" do
160 setup do: local_and_remote_activities()
161
162 setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
163
164 setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
165
166 test "if user is unauthenticated", %{conn: conn} do
167 res_conn = get(conn, "/api/v1/timelines/public?local=true")
168
169 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
170 "error" => "authorization required for timeline view"
171 }
172
173 res_conn = get(conn, "/api/v1/timelines/public?local=false")
174
175 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
176 "error" => "authorization required for timeline view"
177 }
178 end
179
180 test "if user is authenticated" do
181 %{conn: conn} = oauth_access(["read:statuses"])
182
183 res_conn = get(conn, "/api/v1/timelines/public?local=true")
184 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
185
186 res_conn = get(conn, "/api/v1/timelines/public?local=false")
187 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
188 end
189 end
190
191 describe "public with restrict unauthenticated timeline for local" do
192 setup do: local_and_remote_activities()
193
194 setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
195
196 test "if user is unauthenticated", %{conn: conn} do
197 res_conn = get(conn, "/api/v1/timelines/public?local=true")
198
199 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
200 "error" => "authorization required for timeline view"
201 }
202
203 res_conn = get(conn, "/api/v1/timelines/public?local=false")
204 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
205 end
206
207 test "if user is authenticated", %{conn: _conn} do
208 %{conn: conn} = oauth_access(["read:statuses"])
209
210 res_conn = get(conn, "/api/v1/timelines/public?local=true")
211 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
212
213 res_conn = get(conn, "/api/v1/timelines/public?local=false")
214 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
215 end
216 end
217
218 describe "public with restrict unauthenticated timeline for remote" do
219 setup do: local_and_remote_activities()
220
221 setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
222
223 test "if user is unauthenticated", %{conn: conn} do
224 res_conn = get(conn, "/api/v1/timelines/public?local=true")
225 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
226
227 res_conn = get(conn, "/api/v1/timelines/public?local=false")
228
229 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
230 "error" => "authorization required for timeline view"
231 }
232 end
233
234 test "if user is authenticated", %{conn: _conn} do
235 %{conn: conn} = oauth_access(["read:statuses"])
236
237 res_conn = get(conn, "/api/v1/timelines/public?local=true")
238 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
239
240 res_conn = get(conn, "/api/v1/timelines/public?local=false")
241 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
242 end
243 end
244
245 describe "direct" do
246 test "direct timeline", %{conn: conn} do
247 user_one = insert(:user)
248 user_two = insert(:user)
249
250 {:ok, user_two} = User.follow(user_two, user_one)
251
252 {:ok, direct} =
253 CommonAPI.post(user_one, %{
254 status: "Hi @#{user_two.nickname}!",
255 visibility: "direct"
256 })
257
258 {:ok, _follower_only} =
259 CommonAPI.post(user_one, %{
260 status: "Hi @#{user_two.nickname}!",
261 visibility: "private"
262 })
263
264 conn_user_two =
265 conn
266 |> assign(:user, user_two)
267 |> assign(:token, insert(:oauth_token, user: user_two, scopes: ["read:statuses"]))
268
269 # Only direct should be visible here
270 res_conn = get(conn_user_two, "api/v1/timelines/direct")
271
272 assert [status] = json_response_and_validate_schema(res_conn, :ok)
273
274 assert %{"visibility" => "direct"} = status
275 assert status["url"] != direct.data["id"]
276
277 # User should be able to see their own direct message
278 res_conn =
279 build_conn()
280 |> assign(:user, user_one)
281 |> assign(:token, insert(:oauth_token, user: user_one, scopes: ["read:statuses"]))
282 |> get("api/v1/timelines/direct")
283
284 [status] = json_response_and_validate_schema(res_conn, :ok)
285
286 assert %{"visibility" => "direct"} = status
287
288 # Both should be visible here
289 res_conn = get(conn_user_two, "api/v1/timelines/home")
290
291 [_s1, _s2] = json_response_and_validate_schema(res_conn, :ok)
292
293 # Test pagination
294 Enum.each(1..20, fn _ ->
295 {:ok, _} =
296 CommonAPI.post(user_one, %{
297 status: "Hi @#{user_two.nickname}!",
298 visibility: "direct"
299 })
300 end)
301
302 res_conn = get(conn_user_two, "api/v1/timelines/direct")
303
304 statuses = json_response_and_validate_schema(res_conn, :ok)
305 assert length(statuses) == 20
306
307 max_id = List.last(statuses)["id"]
308
309 res_conn = get(conn_user_two, "api/v1/timelines/direct?max_id=#{max_id}")
310
311 assert [status] = json_response_and_validate_schema(res_conn, :ok)
312
313 assert status["url"] != direct.data["id"]
314 end
315
316 test "doesn't include DMs from blocked users" do
317 %{user: blocker, conn: conn} = oauth_access(["read:statuses"])
318 blocked = insert(:user)
319 other_user = insert(:user)
320 {:ok, _user_relationship} = User.block(blocker, blocked)
321
322 {:ok, _blocked_direct} =
323 CommonAPI.post(blocked, %{
324 status: "Hi @#{blocker.nickname}!",
325 visibility: "direct"
326 })
327
328 {:ok, direct} =
329 CommonAPI.post(other_user, %{
330 status: "Hi @#{blocker.nickname}!",
331 visibility: "direct"
332 })
333
334 res_conn = get(conn, "api/v1/timelines/direct")
335
336 [status] = json_response_and_validate_schema(res_conn, :ok)
337 assert status["id"] == direct.id
338 end
339 end
340
341 describe "list" do
342 setup do: oauth_access(["read:lists"])
343
344 test "does not contain retoots", %{user: user, conn: conn} do
345 other_user = insert(:user)
346 {:ok, activity_one} = CommonAPI.post(user, %{status: "Marisa is cute."})
347 {:ok, activity_two} = CommonAPI.post(other_user, %{status: "Marisa is stupid."})
348 {:ok, _} = CommonAPI.repeat(activity_one.id, other_user)
349
350 {:ok, list} = Pleroma.List.create("name", user)
351 {:ok, list} = Pleroma.List.follow(list, other_user)
352
353 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
354
355 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
356
357 assert id == to_string(activity_two.id)
358 end
359
360 test "works with pagination", %{user: user, conn: conn} do
361 other_user = insert(:user)
362 {:ok, list} = Pleroma.List.create("name", user)
363 {:ok, list} = Pleroma.List.follow(list, other_user)
364
365 Enum.each(1..30, fn i ->
366 CommonAPI.post(other_user, %{status: "post number #{i}"})
367 end)
368
369 res =
370 get(conn, "/api/v1/timelines/list/#{list.id}?limit=1")
371 |> json_response_and_validate_schema(:ok)
372
373 assert length(res) == 1
374
375 [first] = res
376
377 res =
378 get(conn, "/api/v1/timelines/list/#{list.id}?max_id=#{first["id"]}&limit=30")
379 |> json_response_and_validate_schema(:ok)
380
381 assert length(res) == 29
382 end
383
384 test "list timeline", %{user: user, conn: conn} do
385 other_user = insert(:user)
386 {:ok, _activity_one} = CommonAPI.post(user, %{status: "Marisa is cute."})
387 {:ok, activity_two} = CommonAPI.post(other_user, %{status: "Marisa is cute."})
388 {:ok, list} = Pleroma.List.create("name", user)
389 {:ok, list} = Pleroma.List.follow(list, other_user)
390
391 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
392
393 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
394
395 assert id == to_string(activity_two.id)
396 end
397
398 test "list timeline does not leak non-public statuses for unfollowed users", %{
399 user: user,
400 conn: conn
401 } do
402 other_user = insert(:user)
403 {:ok, activity_one} = CommonAPI.post(other_user, %{status: "Marisa is cute."})
404
405 {:ok, _activity_two} =
406 CommonAPI.post(other_user, %{
407 status: "Marisa is cute.",
408 visibility: "private"
409 })
410
411 {:ok, list} = Pleroma.List.create("name", user)
412 {:ok, list} = Pleroma.List.follow(list, other_user)
413
414 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
415
416 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
417
418 assert id == to_string(activity_one.id)
419 end
420 end
421
422 describe "hashtag" do
423 setup do: oauth_access(["n/a"])
424
425 @tag capture_log: true
426 test "hashtag timeline", %{conn: conn} do
427 following = insert(:user)
428
429 {:ok, activity} = CommonAPI.post(following, %{status: "test #2hu"})
430
431 nconn = get(conn, "/api/v1/timelines/tag/2hu")
432
433 assert [%{"id" => id}] = json_response_and_validate_schema(nconn, :ok)
434
435 assert id == to_string(activity.id)
436
437 # works for different capitalization too
438 nconn = get(conn, "/api/v1/timelines/tag/2HU")
439
440 assert [%{"id" => id}] = json_response_and_validate_schema(nconn, :ok)
441
442 assert id == to_string(activity.id)
443 end
444
445 test "multi-hashtag timeline", %{conn: conn} do
446 user = insert(:user)
447
448 {:ok, activity_test} = CommonAPI.post(user, %{status: "#test"})
449 {:ok, activity_test1} = CommonAPI.post(user, %{status: "#test #test1"})
450 {:ok, activity_none} = CommonAPI.post(user, %{status: "#test #none"})
451
452 any_test = get(conn, "/api/v1/timelines/tag/test?any[]=test1")
453
454 [status_none, status_test1, status_test] = json_response_and_validate_schema(any_test, :ok)
455
456 assert to_string(activity_test.id) == status_test["id"]
457 assert to_string(activity_test1.id) == status_test1["id"]
458 assert to_string(activity_none.id) == status_none["id"]
459
460 restricted_test = get(conn, "/api/v1/timelines/tag/test?all[]=test1&none[]=none")
461
462 assert [status_test1] == json_response_and_validate_schema(restricted_test, :ok)
463
464 all_test = get(conn, "/api/v1/timelines/tag/test?all[]=none")
465
466 assert [status_none] == json_response_and_validate_schema(all_test, :ok)
467 end
468 end
469
470 describe "hashtag timeline handling of :restrict_unauthenticated setting" do
471 setup do
472 user = insert(:user)
473 {:ok, activity1} = CommonAPI.post(user, %{status: "test #tag1"})
474 {:ok, _activity2} = CommonAPI.post(user, %{status: "test #tag1"})
475
476 activity1
477 |> Ecto.Changeset.change(%{local: false})
478 |> Pleroma.Repo.update()
479
480 base_uri = "/api/v1/timelines/tag/tag1"
481 error_response = %{"error" => "authorization required for timeline view"}
482
483 %{base_uri: base_uri, error_response: error_response}
484 end
485
486 defp ensure_authenticated_access(base_uri) do
487 %{conn: auth_conn} = oauth_access(["read:statuses"])
488
489 res_conn = get(auth_conn, "#{base_uri}?local=true")
490 assert length(json_response(res_conn, 200)) == 1
491
492 res_conn = get(auth_conn, "#{base_uri}?local=false")
493 assert length(json_response(res_conn, 200)) == 2
494 end
495
496 test "with default settings on private instances, returns 403 for unauthenticated users", %{
497 conn: conn,
498 base_uri: base_uri,
499 error_response: error_response
500 } do
501 clear_config([:instance, :public], false)
502 clear_config([:restrict_unauthenticated, :timelines])
503
504 for local <- [true, false] do
505 res_conn = get(conn, "#{base_uri}?local=#{local}")
506
507 assert json_response(res_conn, :unauthorized) == error_response
508 end
509
510 ensure_authenticated_access(base_uri)
511 end
512
513 test "with `%{local: true, federated: true}`, returns 403 for unauthenticated users", %{
514 conn: conn,
515 base_uri: base_uri,
516 error_response: error_response
517 } do
518 clear_config([:restrict_unauthenticated, :timelines, :local], true)
519 clear_config([:restrict_unauthenticated, :timelines, :federated], true)
520
521 for local <- [true, false] do
522 res_conn = get(conn, "#{base_uri}?local=#{local}")
523
524 assert json_response(res_conn, :unauthorized) == error_response
525 end
526
527 ensure_authenticated_access(base_uri)
528 end
529
530 test "with `%{local: false, federated: true}`, forbids unauthenticated access to federated timeline",
531 %{conn: conn, base_uri: base_uri, error_response: error_response} do
532 clear_config([:restrict_unauthenticated, :timelines, :local], false)
533 clear_config([:restrict_unauthenticated, :timelines, :federated], true)
534
535 res_conn = get(conn, "#{base_uri}?local=true")
536 assert length(json_response(res_conn, 200)) == 1
537
538 res_conn = get(conn, "#{base_uri}?local=false")
539 assert json_response(res_conn, :unauthorized) == error_response
540
541 ensure_authenticated_access(base_uri)
542 end
543
544 test "with `%{local: true, federated: false}`, forbids unauthenticated access to public timeline" <>
545 "(but not to local public activities which are delivered as part of federated timeline)",
546 %{conn: conn, base_uri: base_uri, error_response: error_response} do
547 clear_config([:restrict_unauthenticated, :timelines, :local], true)
548 clear_config([:restrict_unauthenticated, :timelines, :federated], false)
549
550 res_conn = get(conn, "#{base_uri}?local=true")
551 assert json_response(res_conn, :unauthorized) == error_response
552
553 # Note: local activities get delivered as part of federated timeline
554 res_conn = get(conn, "#{base_uri}?local=false")
555 assert length(json_response(res_conn, 200)) == 2
556
557 ensure_authenticated_access(base_uri)
558 end
559 end
560 end