Merge remote-tracking branch 'upstream/develop' into block-behavior
[akkoma] / test / pleroma / 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.User
12 alias Pleroma.Web.CommonAPI
13
14 setup do
15 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
16 :ok
17 end
18
19 describe "home" do
20 setup do: oauth_access(["read:statuses"])
21
22 test "does NOT embed account/pleroma/relationship in statuses", %{
23 user: user,
24 conn: conn
25 } do
26 other_user = insert(:user)
27
28 {:ok, _} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
29
30 response =
31 conn
32 |> assign(:user, user)
33 |> get("/api/v1/timelines/home")
34 |> json_response_and_validate_schema(200)
35
36 assert Enum.all?(response, fn n ->
37 get_in(n, ["account", "pleroma", "relationship"]) == %{}
38 end)
39 end
40
41 test "the home timeline when the direct messages are excluded", %{user: user, conn: conn} do
42 {:ok, public_activity} = CommonAPI.post(user, %{status: ".", visibility: "public"})
43 {:ok, direct_activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
44
45 {:ok, unlisted_activity} = CommonAPI.post(user, %{status: ".", visibility: "unlisted"})
46
47 {:ok, private_activity} = CommonAPI.post(user, %{status: ".", visibility: "private"})
48
49 conn = get(conn, "/api/v1/timelines/home?exclude_visibilities[]=direct")
50
51 assert status_ids = json_response_and_validate_schema(conn, :ok) |> Enum.map(& &1["id"])
52 assert public_activity.id in status_ids
53 assert unlisted_activity.id in status_ids
54 assert private_activity.id in status_ids
55 refute direct_activity.id in status_ids
56 end
57
58 test "muted emotions", %{user: user, conn: conn} do
59 other_user = insert(:user)
60 {:ok, activity} = CommonAPI.post(user, %{status: "."})
61
62 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
63 User.mute(user, other_user)
64
65 result =
66 conn
67 |> assign(:user, user)
68 |> get("/api/v1/timelines/home")
69 |> json_response_and_validate_schema(200)
70
71 assert [
72 %{
73 "pleroma" => %{
74 "emoji_reactions" => []
75 }
76 }
77 ] = result
78
79 result =
80 conn
81 |> assign(:user, user)
82 |> get("/api/v1/timelines/home?with_muted=true")
83 |> json_response_and_validate_schema(200)
84
85 assert [
86 %{
87 "pleroma" => %{
88 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]
89 }
90 }
91 ] = result
92 end
93 end
94
95 describe "public" do
96 @tag capture_log: true
97 test "the public timeline", %{conn: conn} do
98 user = insert(:user)
99
100 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
101
102 _activity = insert(:note_activity, local: false)
103
104 conn = get(conn, "/api/v1/timelines/public?local=False")
105
106 assert length(json_response_and_validate_schema(conn, :ok)) == 2
107
108 conn = get(build_conn(), "/api/v1/timelines/public?local=True")
109
110 assert [%{"content" => "test"}] = json_response_and_validate_schema(conn, :ok)
111
112 conn = get(build_conn(), "/api/v1/timelines/public?local=1")
113
114 assert [%{"content" => "test"}] = json_response_and_validate_schema(conn, :ok)
115
116 # does not contain repeats
117 {:ok, _} = CommonAPI.repeat(activity.id, user)
118
119 conn = get(build_conn(), "/api/v1/timelines/public?local=true")
120
121 assert [_] = json_response_and_validate_schema(conn, :ok)
122 end
123
124 test "the public timeline includes only public statuses for an authenticated user" do
125 %{user: user, conn: conn} = oauth_access(["read:statuses"])
126
127 {:ok, _activity} = CommonAPI.post(user, %{status: "test"})
128 {:ok, _activity} = CommonAPI.post(user, %{status: "test", visibility: "private"})
129 {:ok, _activity} = CommonAPI.post(user, %{status: "test", visibility: "unlisted"})
130 {:ok, _activity} = CommonAPI.post(user, %{status: "test", visibility: "direct"})
131
132 res_conn = get(conn, "/api/v1/timelines/public")
133 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
134 end
135
136 test "doesn't return replies if follower is posting with blocked user" do
137 %{conn: conn, user: blocker} = oauth_access(["read:statuses"])
138 [blockee, friend] = insert_list(2, :user)
139 {:ok, blocker, friend} = User.follow(blocker, friend)
140 {:ok, _} = User.block(blocker, blockee)
141
142 conn = assign(conn, :user, blocker)
143
144 {:ok, %{id: activity_id} = activity} = CommonAPI.post(friend, %{status: "hey!"})
145
146 {:ok, reply_from_blockee} =
147 CommonAPI.post(blockee, %{status: "heya", in_reply_to_status_id: activity})
148
149 {:ok, _reply_from_friend} =
150 CommonAPI.post(friend, %{status: "status", in_reply_to_status_id: reply_from_blockee})
151
152 # Still shows replies from yourself
153 {:ok, %{id: reply_from_me}} =
154 CommonAPI.post(blocker, %{status: "status", in_reply_to_status_id: reply_from_blockee})
155
156 response =
157 get(conn, "/api/v1/timelines/public")
158 |> json_response_and_validate_schema(200)
159
160 assert length(response) == 2
161 [%{"id" => ^reply_from_me}, %{"id" => ^activity_id}] = response
162 end
163
164 test "doesn't return posts from users who blocked you when :blockers_visible is disabled" do
165 clear_config([:activitypub, :blockers_visible], false)
166
167 %{conn: conn, user: blockee} = oauth_access(["read:statuses"])
168 blocker = insert(:user)
169 {:ok, _} = User.block(blocker, blockee)
170
171 conn = assign(conn, :user, blockee)
172
173 {:ok, _} = CommonAPI.post(blocker, %{status: "hey!"})
174
175 response =
176 get(conn, "/api/v1/timelines/public")
177 |> json_response_and_validate_schema(200)
178
179 assert length(response) == 0
180 end
181
182 test "doesn't return replies if follow is posting with users from blocked domain" do
183 %{conn: conn, user: blocker} = oauth_access(["read:statuses"])
184 friend = insert(:user)
185 blockee = insert(:user, ap_id: "https://example.com/users/blocked")
186 {:ok, blocker, friend} = User.follow(blocker, friend)
187 {:ok, blocker} = User.block_domain(blocker, "example.com")
188
189 conn = assign(conn, :user, blocker)
190
191 {:ok, %{id: activity_id} = activity} = CommonAPI.post(friend, %{status: "hey!"})
192
193 {:ok, reply_from_blockee} =
194 CommonAPI.post(blockee, %{status: "heya", in_reply_to_status_id: activity})
195
196 {:ok, _reply_from_friend} =
197 CommonAPI.post(friend, %{status: "status", in_reply_to_status_id: reply_from_blockee})
198
199 res_conn = get(conn, "/api/v1/timelines/public")
200
201 activities = json_response_and_validate_schema(res_conn, 200)
202 [%{"id" => ^activity_id}] = activities
203 end
204
205 test "can be filtered by instance", %{conn: conn} do
206 user = insert(:user, ap_id: "https://lain.com/users/lain")
207 insert(:note_activity, local: false)
208 insert(:note_activity, local: false)
209
210 {:ok, _} = CommonAPI.post(user, %{status: "test"})
211
212 conn = get(conn, "/api/v1/timelines/public?instance=lain.com")
213
214 assert length(json_response_and_validate_schema(conn, :ok)) == 1
215 end
216
217 test "muted emotions", %{conn: conn} do
218 user = insert(:user)
219 token = insert(:oauth_token, user: user, scopes: ["read:statuses"])
220
221 conn =
222 conn
223 |> assign(:user, user)
224 |> assign(:token, token)
225
226 other_user = insert(:user)
227 {:ok, activity} = CommonAPI.post(user, %{status: "."})
228
229 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
230 User.mute(user, other_user)
231
232 result =
233 conn
234 |> get("/api/v1/timelines/public")
235 |> json_response_and_validate_schema(200)
236
237 assert [
238 %{
239 "pleroma" => %{
240 "emoji_reactions" => []
241 }
242 }
243 ] = result
244
245 result =
246 conn
247 |> get("/api/v1/timelines/public?with_muted=true")
248 |> json_response_and_validate_schema(200)
249
250 assert [
251 %{
252 "pleroma" => %{
253 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]
254 }
255 }
256 ] = result
257 end
258 end
259
260 defp local_and_remote_activities do
261 insert(:note_activity)
262 insert(:note_activity, local: false)
263 :ok
264 end
265
266 describe "public with restrict unauthenticated timeline for local and federated timelines" do
267 setup do: local_and_remote_activities()
268
269 setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
270
271 setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
272
273 test "if user is unauthenticated", %{conn: conn} do
274 res_conn = get(conn, "/api/v1/timelines/public?local=true")
275
276 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
277 "error" => "authorization required for timeline view"
278 }
279
280 res_conn = get(conn, "/api/v1/timelines/public?local=false")
281
282 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
283 "error" => "authorization required for timeline view"
284 }
285 end
286
287 test "if user is authenticated" do
288 %{conn: conn} = oauth_access(["read:statuses"])
289
290 res_conn = get(conn, "/api/v1/timelines/public?local=true")
291 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
292
293 res_conn = get(conn, "/api/v1/timelines/public?local=false")
294 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
295 end
296 end
297
298 describe "public with restrict unauthenticated timeline for local" do
299 setup do: local_and_remote_activities()
300
301 setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
302
303 test "if user is unauthenticated", %{conn: conn} do
304 res_conn = get(conn, "/api/v1/timelines/public?local=true")
305
306 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
307 "error" => "authorization required for timeline view"
308 }
309
310 res_conn = get(conn, "/api/v1/timelines/public?local=false")
311 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
312 end
313
314 test "if user is authenticated", %{conn: _conn} do
315 %{conn: conn} = oauth_access(["read:statuses"])
316
317 res_conn = get(conn, "/api/v1/timelines/public?local=true")
318 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
319
320 res_conn = get(conn, "/api/v1/timelines/public?local=false")
321 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
322 end
323 end
324
325 describe "public with restrict unauthenticated timeline for remote" do
326 setup do: local_and_remote_activities()
327
328 setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
329
330 test "if user is unauthenticated", %{conn: conn} do
331 res_conn = get(conn, "/api/v1/timelines/public?local=true")
332 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
333
334 res_conn = get(conn, "/api/v1/timelines/public?local=false")
335
336 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
337 "error" => "authorization required for timeline view"
338 }
339 end
340
341 test "if user is authenticated", %{conn: _conn} do
342 %{conn: conn} = oauth_access(["read:statuses"])
343
344 res_conn = get(conn, "/api/v1/timelines/public?local=true")
345 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
346
347 res_conn = get(conn, "/api/v1/timelines/public?local=false")
348 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
349 end
350 end
351
352 describe "direct" do
353 test "direct timeline", %{conn: conn} do
354 user_one = insert(:user)
355 user_two = insert(:user)
356
357 {:ok, user_two, user_one} = User.follow(user_two, user_one)
358
359 {:ok, direct} =
360 CommonAPI.post(user_one, %{
361 status: "Hi @#{user_two.nickname}!",
362 visibility: "direct"
363 })
364
365 {:ok, _follower_only} =
366 CommonAPI.post(user_one, %{
367 status: "Hi @#{user_two.nickname}!",
368 visibility: "private"
369 })
370
371 conn_user_two =
372 conn
373 |> assign(:user, user_two)
374 |> assign(:token, insert(:oauth_token, user: user_two, scopes: ["read:statuses"]))
375
376 # Only direct should be visible here
377 res_conn = get(conn_user_two, "api/v1/timelines/direct")
378
379 assert [status] = json_response_and_validate_schema(res_conn, :ok)
380
381 assert %{"visibility" => "direct"} = status
382 assert status["url"] != direct.data["id"]
383
384 # User should be able to see their own direct message
385 res_conn =
386 build_conn()
387 |> assign(:user, user_one)
388 |> assign(:token, insert(:oauth_token, user: user_one, scopes: ["read:statuses"]))
389 |> get("api/v1/timelines/direct")
390
391 [status] = json_response_and_validate_schema(res_conn, :ok)
392
393 assert %{"visibility" => "direct"} = status
394
395 # Both should be visible here
396 res_conn = get(conn_user_two, "api/v1/timelines/home")
397
398 [_s1, _s2] = json_response_and_validate_schema(res_conn, :ok)
399
400 # Test pagination
401 Enum.each(1..20, fn _ ->
402 {:ok, _} =
403 CommonAPI.post(user_one, %{
404 status: "Hi @#{user_two.nickname}!",
405 visibility: "direct"
406 })
407 end)
408
409 res_conn = get(conn_user_two, "api/v1/timelines/direct")
410
411 statuses = json_response_and_validate_schema(res_conn, :ok)
412 assert length(statuses) == 20
413
414 max_id = List.last(statuses)["id"]
415
416 res_conn = get(conn_user_two, "api/v1/timelines/direct?max_id=#{max_id}")
417
418 assert [status] = json_response_and_validate_schema(res_conn, :ok)
419
420 assert status["url"] != direct.data["id"]
421 end
422
423 test "doesn't include DMs from blocked users" do
424 %{user: blocker, conn: conn} = oauth_access(["read:statuses"])
425 blocked = insert(:user)
426 other_user = insert(:user)
427 {:ok, _user_relationship} = User.block(blocker, blocked)
428
429 {:ok, _blocked_direct} =
430 CommonAPI.post(blocked, %{
431 status: "Hi @#{blocker.nickname}!",
432 visibility: "direct"
433 })
434
435 {:ok, direct} =
436 CommonAPI.post(other_user, %{
437 status: "Hi @#{blocker.nickname}!",
438 visibility: "direct"
439 })
440
441 res_conn = get(conn, "api/v1/timelines/direct")
442
443 [status] = json_response_and_validate_schema(res_conn, :ok)
444 assert status["id"] == direct.id
445 end
446 end
447
448 describe "list" do
449 setup do: oauth_access(["read:lists"])
450
451 test "does not contain retoots", %{user: user, conn: conn} do
452 other_user = insert(:user)
453 {:ok, activity_one} = CommonAPI.post(user, %{status: "Marisa is cute."})
454 {:ok, activity_two} = CommonAPI.post(other_user, %{status: "Marisa is stupid."})
455 {:ok, _} = CommonAPI.repeat(activity_one.id, other_user)
456
457 {:ok, list} = Pleroma.List.create("name", user)
458 {:ok, list} = Pleroma.List.follow(list, other_user)
459
460 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
461
462 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
463
464 assert id == to_string(activity_two.id)
465 end
466
467 test "works with pagination", %{user: user, conn: conn} do
468 other_user = insert(:user)
469 {:ok, list} = Pleroma.List.create("name", user)
470 {:ok, list} = Pleroma.List.follow(list, other_user)
471
472 Enum.each(1..30, fn i ->
473 CommonAPI.post(other_user, %{status: "post number #{i}"})
474 end)
475
476 res =
477 get(conn, "/api/v1/timelines/list/#{list.id}?limit=1")
478 |> json_response_and_validate_schema(:ok)
479
480 assert length(res) == 1
481
482 [first] = res
483
484 res =
485 get(conn, "/api/v1/timelines/list/#{list.id}?max_id=#{first["id"]}&limit=30")
486 |> json_response_and_validate_schema(:ok)
487
488 assert length(res) == 29
489 end
490
491 test "list timeline", %{user: user, conn: conn} do
492 other_user = insert(:user)
493 {:ok, _activity_one} = CommonAPI.post(user, %{status: "Marisa is cute."})
494 {:ok, activity_two} = CommonAPI.post(other_user, %{status: "Marisa is cute."})
495 {:ok, list} = Pleroma.List.create("name", user)
496 {:ok, list} = Pleroma.List.follow(list, other_user)
497
498 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
499
500 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
501
502 assert id == to_string(activity_two.id)
503 end
504
505 test "list timeline does not leak non-public statuses for unfollowed users", %{
506 user: user,
507 conn: conn
508 } do
509 other_user = insert(:user)
510 {:ok, activity_one} = CommonAPI.post(other_user, %{status: "Marisa is cute."})
511
512 {:ok, _activity_two} =
513 CommonAPI.post(other_user, %{
514 status: "Marisa is cute.",
515 visibility: "private"
516 })
517
518 {:ok, list} = Pleroma.List.create("name", user)
519 {:ok, list} = Pleroma.List.follow(list, other_user)
520
521 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
522
523 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
524
525 assert id == to_string(activity_one.id)
526 end
527
528 test "muted emotions", %{user: user, conn: conn} do
529 user2 = insert(:user)
530 user3 = insert(:user)
531 {:ok, activity} = CommonAPI.post(user2, %{status: "."})
532
533 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user3, "🎅")
534 User.mute(user, user3)
535
536 {:ok, list} = Pleroma.List.create("name", user)
537 {:ok, list} = Pleroma.List.follow(list, user2)
538
539 result =
540 conn
541 |> get("/api/v1/timelines/list/#{list.id}")
542 |> json_response_and_validate_schema(200)
543
544 assert [
545 %{
546 "pleroma" => %{
547 "emoji_reactions" => []
548 }
549 }
550 ] = result
551
552 result =
553 conn
554 |> get("/api/v1/timelines/list/#{list.id}?with_muted=true")
555 |> json_response_and_validate_schema(200)
556
557 assert [
558 %{
559 "pleroma" => %{
560 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]
561 }
562 }
563 ] = result
564 end
565 end
566
567 describe "hashtag" do
568 setup do: oauth_access(["n/a"])
569
570 @tag capture_log: true
571 test "hashtag timeline", %{conn: conn} do
572 following = insert(:user)
573
574 {:ok, activity} = CommonAPI.post(following, %{status: "test #2hu"})
575
576 nconn = get(conn, "/api/v1/timelines/tag/2hu")
577
578 assert [%{"id" => id}] = json_response_and_validate_schema(nconn, :ok)
579
580 assert id == to_string(activity.id)
581
582 # works for different capitalization too
583 nconn = get(conn, "/api/v1/timelines/tag/2HU")
584
585 assert [%{"id" => id}] = json_response_and_validate_schema(nconn, :ok)
586
587 assert id == to_string(activity.id)
588 end
589
590 test "multi-hashtag timeline", %{conn: conn} do
591 user = insert(:user)
592
593 {:ok, activity_test} = CommonAPI.post(user, %{status: "#test"})
594 {:ok, activity_test1} = CommonAPI.post(user, %{status: "#test #test1"})
595 {:ok, activity_none} = CommonAPI.post(user, %{status: "#test #none"})
596
597 any_test = get(conn, "/api/v1/timelines/tag/test?any[]=test1")
598
599 [status_none, status_test1, status_test] = json_response_and_validate_schema(any_test, :ok)
600
601 assert to_string(activity_test.id) == status_test["id"]
602 assert to_string(activity_test1.id) == status_test1["id"]
603 assert to_string(activity_none.id) == status_none["id"]
604
605 restricted_test = get(conn, "/api/v1/timelines/tag/test?all[]=test1&none[]=none")
606
607 assert [status_test1] == json_response_and_validate_schema(restricted_test, :ok)
608
609 all_test = get(conn, "/api/v1/timelines/tag/test?all[]=none")
610
611 assert [status_none] == json_response_and_validate_schema(all_test, :ok)
612 end
613
614 test "muted emotions", %{conn: conn} do
615 user = insert(:user)
616 token = insert(:oauth_token, user: user, scopes: ["read:statuses"])
617
618 conn =
619 conn
620 |> assign(:user, user)
621 |> assign(:token, token)
622
623 other_user = insert(:user)
624 {:ok, activity} = CommonAPI.post(user, %{status: "test #2hu"})
625
626 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
627 User.mute(user, other_user)
628
629 result =
630 conn
631 |> get("/api/v1/timelines/tag/2hu")
632 |> json_response_and_validate_schema(200)
633
634 assert [
635 %{
636 "pleroma" => %{
637 "emoji_reactions" => []
638 }
639 }
640 ] = result
641
642 result =
643 conn
644 |> get("/api/v1/timelines/tag/2hu?with_muted=true")
645 |> json_response_and_validate_schema(200)
646
647 assert [
648 %{
649 "pleroma" => %{
650 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]
651 }
652 }
653 ] = result
654 end
655 end
656
657 describe "hashtag timeline handling of :restrict_unauthenticated setting" do
658 setup do
659 user = insert(:user)
660 {:ok, activity1} = CommonAPI.post(user, %{status: "test #tag1"})
661 {:ok, _activity2} = CommonAPI.post(user, %{status: "test #tag1"})
662
663 activity1
664 |> Ecto.Changeset.change(%{local: false})
665 |> Pleroma.Repo.update()
666
667 base_uri = "/api/v1/timelines/tag/tag1"
668 error_response = %{"error" => "authorization required for timeline view"}
669
670 %{base_uri: base_uri, error_response: error_response}
671 end
672
673 defp ensure_authenticated_access(base_uri) do
674 %{conn: auth_conn} = oauth_access(["read:statuses"])
675
676 res_conn = get(auth_conn, "#{base_uri}?local=true")
677 assert length(json_response(res_conn, 200)) == 1
678
679 res_conn = get(auth_conn, "#{base_uri}?local=false")
680 assert length(json_response(res_conn, 200)) == 2
681 end
682
683 test "with default settings on private instances, returns 403 for unauthenticated users", %{
684 conn: conn,
685 base_uri: base_uri,
686 error_response: error_response
687 } do
688 clear_config([:instance, :public], false)
689 clear_config([:restrict_unauthenticated, :timelines])
690
691 for local <- [true, false] do
692 res_conn = get(conn, "#{base_uri}?local=#{local}")
693
694 assert json_response(res_conn, :unauthorized) == error_response
695 end
696
697 ensure_authenticated_access(base_uri)
698 end
699
700 test "with `%{local: true, federated: true}`, returns 403 for unauthenticated users", %{
701 conn: conn,
702 base_uri: base_uri,
703 error_response: error_response
704 } do
705 clear_config([:restrict_unauthenticated, :timelines, :local], true)
706 clear_config([:restrict_unauthenticated, :timelines, :federated], true)
707
708 for local <- [true, false] do
709 res_conn = get(conn, "#{base_uri}?local=#{local}")
710
711 assert json_response(res_conn, :unauthorized) == error_response
712 end
713
714 ensure_authenticated_access(base_uri)
715 end
716
717 test "with `%{local: false, federated: true}`, forbids unauthenticated access to federated timeline",
718 %{conn: conn, base_uri: base_uri, error_response: error_response} do
719 clear_config([:restrict_unauthenticated, :timelines, :local], false)
720 clear_config([:restrict_unauthenticated, :timelines, :federated], true)
721
722 res_conn = get(conn, "#{base_uri}?local=true")
723 assert length(json_response(res_conn, 200)) == 1
724
725 res_conn = get(conn, "#{base_uri}?local=false")
726 assert json_response(res_conn, :unauthorized) == error_response
727
728 ensure_authenticated_access(base_uri)
729 end
730
731 test "with `%{local: true, federated: false}`, forbids unauthenticated access to public timeline" <>
732 "(but not to local public activities which are delivered as part of federated timeline)",
733 %{conn: conn, base_uri: base_uri, error_response: error_response} do
734 clear_config([:restrict_unauthenticated, :timelines, :local], true)
735 clear_config([:restrict_unauthenticated, :timelines, :federated], false)
736
737 res_conn = get(conn, "#{base_uri}?local=true")
738 assert json_response(res_conn, :unauthorized) == error_response
739
740 # Note: local activities get delivered as part of federated timeline
741 res_conn = get(conn, "#{base_uri}?local=false")
742 assert length(json_response(res_conn, 200)) == 2
743
744 ensure_authenticated_access(base_uri)
745 end
746 end
747 end