1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
6 use Pleroma.Web.ConnCase
12 alias Pleroma.Web.CommonAPI
15 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
20 setup do: oauth_access(["read:statuses"])
22 test "does NOT embed account/pleroma/relationship in statuses", %{
26 other_user = insert(:user)
28 {:ok, _} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
32 |> assign(:user, user)
33 |> get("/api/v1/timelines/home")
34 |> json_response_and_validate_schema(200)
36 assert Enum.all?(response, fn n ->
37 get_in(n, ["account", "pleroma", "relationship"]) == %{}
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"})
45 {:ok, unlisted_activity} = CommonAPI.post(user, %{status: ".", visibility: "unlisted"})
47 {:ok, private_activity} = CommonAPI.post(user, %{status: ".", visibility: "private"})
49 conn = get(conn, "/api/v1/timelines/home?exclude_visibilities[]=direct")
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
58 test "muted emotions", %{user: user, conn: conn} do
59 other_user = insert(:user)
60 {:ok, activity} = CommonAPI.post(user, %{status: "."})
62 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
63 User.mute(user, other_user)
67 |> assign(:user, user)
68 |> get("/api/v1/timelines/home")
69 |> json_response_and_validate_schema(200)
74 "emoji_reactions" => []
81 |> assign(:user, user)
82 |> get("/api/v1/timelines/home?with_muted=true")
83 |> json_response_and_validate_schema(200)
88 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]
96 @tag capture_log: true
97 test "the public timeline", %{conn: conn} do
100 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
102 _activity = insert(:note_activity, local: false)
104 conn = get(conn, "/api/v1/timelines/public?local=False")
106 assert length(json_response_and_validate_schema(conn, :ok)) == 2
108 conn = get(build_conn(), "/api/v1/timelines/public?local=True")
110 assert [%{"content" => "test"}] = json_response_and_validate_schema(conn, :ok)
112 conn = get(build_conn(), "/api/v1/timelines/public?local=1")
114 assert [%{"content" => "test"}] = json_response_and_validate_schema(conn, :ok)
116 # does not contain repeats
117 {:ok, _} = CommonAPI.repeat(activity.id, user)
119 conn = get(build_conn(), "/api/v1/timelines/public?local=true")
121 assert [_] = json_response_and_validate_schema(conn, :ok)
124 test "the public timeline includes only public statuses for an authenticated user" do
125 %{user: user, conn: conn} = oauth_access(["read:statuses"])
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"})
132 res_conn = get(conn, "/api/v1/timelines/public")
133 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
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)
142 conn = assign(conn, :user, blocker)
144 {:ok, %{id: activity_id} = activity} = CommonAPI.post(friend, %{status: "hey!"})
146 {:ok, reply_from_blockee} =
147 CommonAPI.post(blockee, %{status: "heya", in_reply_to_status_id: activity})
149 {:ok, _reply_from_friend} =
150 CommonAPI.post(friend, %{status: "status", in_reply_to_status_id: reply_from_blockee})
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})
157 get(conn, "/api/v1/timelines/public")
158 |> json_response_and_validate_schema(200)
160 assert length(response) == 2
161 [%{"id" => ^reply_from_me}, %{"id" => ^activity_id}] = response
164 test "doesn't return replies if follow is posting with users from blocked domain" do
165 %{conn: conn, user: blocker} = oauth_access(["read:statuses"])
166 friend = insert(:user)
167 blockee = insert(:user, ap_id: "https://example.com/users/blocked")
168 {:ok, blocker, friend} = User.follow(blocker, friend)
169 {:ok, blocker} = User.block_domain(blocker, "example.com")
171 conn = assign(conn, :user, blocker)
173 {:ok, %{id: activity_id} = activity} = CommonAPI.post(friend, %{status: "hey!"})
175 {:ok, reply_from_blockee} =
176 CommonAPI.post(blockee, %{status: "heya", in_reply_to_status_id: activity})
178 {:ok, _reply_from_friend} =
179 CommonAPI.post(friend, %{status: "status", in_reply_to_status_id: reply_from_blockee})
181 res_conn = get(conn, "/api/v1/timelines/public")
183 activities = json_response_and_validate_schema(res_conn, 200)
184 [%{"id" => ^activity_id}] = activities
187 test "can be filtered by instance", %{conn: conn} do
188 user = insert(:user, ap_id: "https://lain.com/users/lain")
189 insert(:note_activity, local: false)
190 insert(:note_activity, local: false)
192 {:ok, _} = CommonAPI.post(user, %{status: "test"})
194 conn = get(conn, "/api/v1/timelines/public?instance=lain.com")
196 assert length(json_response_and_validate_schema(conn, :ok)) == 1
199 test "muted emotions", %{conn: conn} do
201 token = insert(:oauth_token, user: user, scopes: ["read:statuses"])
205 |> assign(:user, user)
206 |> assign(:token, token)
208 other_user = insert(:user)
209 {:ok, activity} = CommonAPI.post(user, %{status: "."})
211 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
212 User.mute(user, other_user)
216 |> get("/api/v1/timelines/public")
217 |> json_response_and_validate_schema(200)
222 "emoji_reactions" => []
229 |> get("/api/v1/timelines/public?with_muted=true")
230 |> json_response_and_validate_schema(200)
235 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]
242 defp local_and_remote_activities do
243 insert(:note_activity)
244 insert(:note_activity, local: false)
248 describe "public with restrict unauthenticated timeline for local and federated timelines" do
249 setup do: local_and_remote_activities()
251 setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
253 setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
255 test "if user is unauthenticated", %{conn: conn} do
256 res_conn = get(conn, "/api/v1/timelines/public?local=true")
258 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
259 "error" => "authorization required for timeline view"
262 res_conn = get(conn, "/api/v1/timelines/public?local=false")
264 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
265 "error" => "authorization required for timeline view"
269 test "if user is authenticated" do
270 %{conn: conn} = oauth_access(["read:statuses"])
272 res_conn = get(conn, "/api/v1/timelines/public?local=true")
273 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
275 res_conn = get(conn, "/api/v1/timelines/public?local=false")
276 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
280 describe "public with restrict unauthenticated timeline for local" do
281 setup do: local_and_remote_activities()
283 setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
285 test "if user is unauthenticated", %{conn: conn} do
286 res_conn = get(conn, "/api/v1/timelines/public?local=true")
288 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
289 "error" => "authorization required for timeline view"
292 res_conn = get(conn, "/api/v1/timelines/public?local=false")
293 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
296 test "if user is authenticated", %{conn: _conn} do
297 %{conn: conn} = oauth_access(["read:statuses"])
299 res_conn = get(conn, "/api/v1/timelines/public?local=true")
300 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
302 res_conn = get(conn, "/api/v1/timelines/public?local=false")
303 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
307 describe "public with restrict unauthenticated timeline for remote" do
308 setup do: local_and_remote_activities()
310 setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
312 test "if user is unauthenticated", %{conn: conn} do
313 res_conn = get(conn, "/api/v1/timelines/public?local=true")
314 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
316 res_conn = get(conn, "/api/v1/timelines/public?local=false")
318 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
319 "error" => "authorization required for timeline view"
323 test "if user is authenticated", %{conn: _conn} do
324 %{conn: conn} = oauth_access(["read:statuses"])
326 res_conn = get(conn, "/api/v1/timelines/public?local=true")
327 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
329 res_conn = get(conn, "/api/v1/timelines/public?local=false")
330 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
335 test "direct timeline", %{conn: conn} do
336 user_one = insert(:user)
337 user_two = insert(:user)
339 {:ok, user_two, user_one} = User.follow(user_two, user_one)
342 CommonAPI.post(user_one, %{
343 status: "Hi @#{user_two.nickname}!",
347 {:ok, _follower_only} =
348 CommonAPI.post(user_one, %{
349 status: "Hi @#{user_two.nickname}!",
350 visibility: "private"
355 |> assign(:user, user_two)
356 |> assign(:token, insert(:oauth_token, user: user_two, scopes: ["read:statuses"]))
358 # Only direct should be visible here
359 res_conn = get(conn_user_two, "api/v1/timelines/direct")
361 assert [status] = json_response_and_validate_schema(res_conn, :ok)
363 assert %{"visibility" => "direct"} = status
364 assert status["url"] != direct.data["id"]
366 # User should be able to see their own direct message
369 |> assign(:user, user_one)
370 |> assign(:token, insert(:oauth_token, user: user_one, scopes: ["read:statuses"]))
371 |> get("api/v1/timelines/direct")
373 [status] = json_response_and_validate_schema(res_conn, :ok)
375 assert %{"visibility" => "direct"} = status
377 # Both should be visible here
378 res_conn = get(conn_user_two, "api/v1/timelines/home")
380 [_s1, _s2] = json_response_and_validate_schema(res_conn, :ok)
383 Enum.each(1..20, fn _ ->
385 CommonAPI.post(user_one, %{
386 status: "Hi @#{user_two.nickname}!",
391 res_conn = get(conn_user_two, "api/v1/timelines/direct")
393 statuses = json_response_and_validate_schema(res_conn, :ok)
394 assert length(statuses) == 20
396 max_id = List.last(statuses)["id"]
398 res_conn = get(conn_user_two, "api/v1/timelines/direct?max_id=#{max_id}")
400 assert [status] = json_response_and_validate_schema(res_conn, :ok)
402 assert status["url"] != direct.data["id"]
405 test "doesn't include DMs from blocked users" do
406 %{user: blocker, conn: conn} = oauth_access(["read:statuses"])
407 blocked = insert(:user)
408 other_user = insert(:user)
409 {:ok, _user_relationship} = User.block(blocker, blocked)
411 {:ok, _blocked_direct} =
412 CommonAPI.post(blocked, %{
413 status: "Hi @#{blocker.nickname}!",
418 CommonAPI.post(other_user, %{
419 status: "Hi @#{blocker.nickname}!",
423 res_conn = get(conn, "api/v1/timelines/direct")
425 [status] = json_response_and_validate_schema(res_conn, :ok)
426 assert status["id"] == direct.id
431 setup do: oauth_access(["read:lists"])
433 test "does not contain retoots", %{user: user, conn: conn} do
434 other_user = insert(:user)
435 {:ok, activity_one} = CommonAPI.post(user, %{status: "Marisa is cute."})
436 {:ok, activity_two} = CommonAPI.post(other_user, %{status: "Marisa is stupid."})
437 {:ok, _} = CommonAPI.repeat(activity_one.id, other_user)
439 {:ok, list} = Pleroma.List.create("name", user)
440 {:ok, list} = Pleroma.List.follow(list, other_user)
442 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
444 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
446 assert id == to_string(activity_two.id)
449 test "works with pagination", %{user: user, conn: conn} do
450 other_user = insert(:user)
451 {:ok, list} = Pleroma.List.create("name", user)
452 {:ok, list} = Pleroma.List.follow(list, other_user)
454 Enum.each(1..30, fn i ->
455 CommonAPI.post(other_user, %{status: "post number #{i}"})
459 get(conn, "/api/v1/timelines/list/#{list.id}?limit=1")
460 |> json_response_and_validate_schema(:ok)
462 assert length(res) == 1
467 get(conn, "/api/v1/timelines/list/#{list.id}?max_id=#{first["id"]}&limit=30")
468 |> json_response_and_validate_schema(:ok)
470 assert length(res) == 29
473 test "list timeline", %{user: user, conn: conn} do
474 other_user = insert(:user)
475 {:ok, _activity_one} = CommonAPI.post(user, %{status: "Marisa is cute."})
476 {:ok, activity_two} = CommonAPI.post(other_user, %{status: "Marisa is cute."})
477 {:ok, list} = Pleroma.List.create("name", user)
478 {:ok, list} = Pleroma.List.follow(list, other_user)
480 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
482 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
484 assert id == to_string(activity_two.id)
487 test "list timeline does not leak non-public statuses for unfollowed users", %{
491 other_user = insert(:user)
492 {:ok, activity_one} = CommonAPI.post(other_user, %{status: "Marisa is cute."})
494 {:ok, _activity_two} =
495 CommonAPI.post(other_user, %{
496 status: "Marisa is cute.",
497 visibility: "private"
500 {:ok, list} = Pleroma.List.create("name", user)
501 {:ok, list} = Pleroma.List.follow(list, other_user)
503 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
505 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
507 assert id == to_string(activity_one.id)
510 test "muted emotions", %{user: user, conn: conn} do
511 user2 = insert(:user)
512 user3 = insert(:user)
513 {:ok, activity} = CommonAPI.post(user2, %{status: "."})
515 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user3, "🎅")
516 User.mute(user, user3)
518 {:ok, list} = Pleroma.List.create("name", user)
519 {:ok, list} = Pleroma.List.follow(list, user2)
523 |> get("/api/v1/timelines/list/#{list.id}")
524 |> json_response_and_validate_schema(200)
529 "emoji_reactions" => []
536 |> get("/api/v1/timelines/list/#{list.id}?with_muted=true")
537 |> json_response_and_validate_schema(200)
542 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]
549 describe "hashtag" do
550 setup do: oauth_access(["n/a"])
552 @tag capture_log: true
553 test "hashtag timeline", %{conn: conn} do
554 following = insert(:user)
556 {:ok, activity} = CommonAPI.post(following, %{status: "test #2hu"})
558 nconn = get(conn, "/api/v1/timelines/tag/2hu")
560 assert [%{"id" => id}] = json_response_and_validate_schema(nconn, :ok)
562 assert id == to_string(activity.id)
564 # works for different capitalization too
565 nconn = get(conn, "/api/v1/timelines/tag/2HU")
567 assert [%{"id" => id}] = json_response_and_validate_schema(nconn, :ok)
569 assert id == to_string(activity.id)
572 test "multi-hashtag timeline", %{conn: conn} do
575 {:ok, activity_test} = CommonAPI.post(user, %{status: "#test"})
576 {:ok, activity_test1} = CommonAPI.post(user, %{status: "#test #test1"})
577 {:ok, activity_none} = CommonAPI.post(user, %{status: "#test #none"})
579 any_test = get(conn, "/api/v1/timelines/tag/test?any[]=test1")
581 [status_none, status_test1, status_test] = json_response_and_validate_schema(any_test, :ok)
583 assert to_string(activity_test.id) == status_test["id"]
584 assert to_string(activity_test1.id) == status_test1["id"]
585 assert to_string(activity_none.id) == status_none["id"]
587 restricted_test = get(conn, "/api/v1/timelines/tag/test?all[]=test1&none[]=none")
589 assert [status_test1] == json_response_and_validate_schema(restricted_test, :ok)
591 all_test = get(conn, "/api/v1/timelines/tag/test?all[]=none")
593 assert [status_none] == json_response_and_validate_schema(all_test, :ok)
596 test "muted emotions", %{conn: conn} do
598 token = insert(:oauth_token, user: user, scopes: ["read:statuses"])
602 |> assign(:user, user)
603 |> assign(:token, token)
605 other_user = insert(:user)
606 {:ok, activity} = CommonAPI.post(user, %{status: "test #2hu"})
608 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
609 User.mute(user, other_user)
613 |> get("/api/v1/timelines/tag/2hu")
614 |> json_response_and_validate_schema(200)
619 "emoji_reactions" => []
626 |> get("/api/v1/timelines/tag/2hu?with_muted=true")
627 |> json_response_and_validate_schema(200)
632 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]
639 describe "hashtag timeline handling of :restrict_unauthenticated setting" do
642 {:ok, activity1} = CommonAPI.post(user, %{status: "test #tag1"})
643 {:ok, _activity2} = CommonAPI.post(user, %{status: "test #tag1"})
646 |> Ecto.Changeset.change(%{local: false})
647 |> Pleroma.Repo.update()
649 base_uri = "/api/v1/timelines/tag/tag1"
650 error_response = %{"error" => "authorization required for timeline view"}
652 %{base_uri: base_uri, error_response: error_response}
655 defp ensure_authenticated_access(base_uri) do
656 %{conn: auth_conn} = oauth_access(["read:statuses"])
658 res_conn = get(auth_conn, "#{base_uri}?local=true")
659 assert length(json_response(res_conn, 200)) == 1
661 res_conn = get(auth_conn, "#{base_uri}?local=false")
662 assert length(json_response(res_conn, 200)) == 2
665 test "with default settings on private instances, returns 403 for unauthenticated users", %{
668 error_response: error_response
670 clear_config([:instance, :public], false)
671 clear_config([:restrict_unauthenticated, :timelines])
673 for local <- [true, false] do
674 res_conn = get(conn, "#{base_uri}?local=#{local}")
676 assert json_response(res_conn, :unauthorized) == error_response
679 ensure_authenticated_access(base_uri)
682 test "with `%{local: true, federated: true}`, returns 403 for unauthenticated users", %{
685 error_response: error_response
687 clear_config([:restrict_unauthenticated, :timelines, :local], true)
688 clear_config([:restrict_unauthenticated, :timelines, :federated], true)
690 for local <- [true, false] do
691 res_conn = get(conn, "#{base_uri}?local=#{local}")
693 assert json_response(res_conn, :unauthorized) == error_response
696 ensure_authenticated_access(base_uri)
699 test "with `%{local: false, federated: true}`, forbids unauthenticated access to federated timeline",
700 %{conn: conn, base_uri: base_uri, error_response: error_response} do
701 clear_config([:restrict_unauthenticated, :timelines, :local], false)
702 clear_config([:restrict_unauthenticated, :timelines, :federated], true)
704 res_conn = get(conn, "#{base_uri}?local=true")
705 assert length(json_response(res_conn, 200)) == 1
707 res_conn = get(conn, "#{base_uri}?local=false")
708 assert json_response(res_conn, :unauthorized) == error_response
710 ensure_authenticated_access(base_uri)
713 test "with `%{local: true, federated: false}`, forbids unauthenticated access to public timeline" <>
714 "(but not to local public activities which are delivered as part of federated timeline)",
715 %{conn: conn, base_uri: base_uri, error_response: error_response} do
716 clear_config([:restrict_unauthenticated, :timelines, :local], true)
717 clear_config([:restrict_unauthenticated, :timelines, :federated], false)
719 res_conn = get(conn, "#{base_uri}?local=true")
720 assert json_response(res_conn, :unauthorized) == error_response
722 # Note: local activities get delivered as part of federated timeline
723 res_conn = get(conn, "#{base_uri}?local=false")
724 assert length(json_response(res_conn, 200)) == 2
726 ensure_authenticated_access(base_uri)