1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
13 alias Pleroma.Web.CommonAPI
16 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
21 setup do: oauth_access(["read:statuses"])
23 test "does NOT embed account/pleroma/relationship in statuses", %{
27 other_user = insert(:user)
29 {:ok, _} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
33 |> assign(:user, user)
34 |> get("/api/v1/timelines/home")
35 |> json_response_and_validate_schema(200)
37 assert Enum.all?(response, fn n ->
38 get_in(n, ["account", "pleroma", "relationship"]) == %{}
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"})
46 {:ok, unlisted_activity} = CommonAPI.post(user, %{status: ".", visibility: "unlisted"})
48 {:ok, private_activity} = CommonAPI.post(user, %{status: ".", visibility: "private"})
50 conn = get(conn, "/api/v1/timelines/home?exclude_visibilities[]=direct")
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
61 @tag capture_log: true
62 test "the public timeline", %{conn: conn} do
65 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
67 _activity = insert(:note_activity, local: false)
69 conn = get(conn, "/api/v1/timelines/public?local=False")
71 assert length(json_response_and_validate_schema(conn, :ok)) == 2
73 conn = get(build_conn(), "/api/v1/timelines/public?local=True")
75 assert [%{"content" => "test"}] = json_response_and_validate_schema(conn, :ok)
77 conn = get(build_conn(), "/api/v1/timelines/public?local=1")
79 assert [%{"content" => "test"}] = json_response_and_validate_schema(conn, :ok)
81 # does not contain repeats
82 {:ok, _} = CommonAPI.repeat(activity.id, user)
84 conn = get(build_conn(), "/api/v1/timelines/public?local=true")
86 assert [_] = json_response_and_validate_schema(conn, :ok)
89 test "the public timeline includes only public statuses for an authenticated user" do
90 %{user: user, conn: conn} = oauth_access(["read:statuses"])
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"})
97 res_conn = get(conn, "/api/v1/timelines/public")
98 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
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)
107 conn = assign(conn, :user, blocker)
109 {:ok, %{id: activity_id} = activity} = CommonAPI.post(friend, %{status: "hey!"})
111 {:ok, reply_from_blockee} =
112 CommonAPI.post(blockee, %{status: "heya", in_reply_to_status_id: activity})
114 {:ok, _reply_from_friend} =
115 CommonAPI.post(friend, %{status: "status", in_reply_to_status_id: reply_from_blockee})
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})
122 get(conn, "/api/v1/timelines/public")
123 |> json_response_and_validate_schema(200)
125 assert length(response) == 2
126 [%{"id" => ^reply_from_me}, %{"id" => ^activity_id}] = response
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")
136 conn = assign(conn, :user, blocker)
138 {:ok, %{id: activity_id} = activity} = CommonAPI.post(friend, %{status: "hey!"})
140 {:ok, reply_from_blockee} =
141 CommonAPI.post(blockee, %{status: "heya", in_reply_to_status_id: activity})
143 {:ok, _reply_from_friend} =
144 CommonAPI.post(friend, %{status: "status", in_reply_to_status_id: reply_from_blockee})
146 res_conn = get(conn, "/api/v1/timelines/public")
148 activities = json_response_and_validate_schema(res_conn, 200)
149 [%{"id" => ^activity_id}] = activities
153 defp local_and_remote_activities do
154 insert(:note_activity)
155 insert(:note_activity, local: false)
159 describe "public with restrict unauthenticated timeline for local and federated timelines" do
160 setup do: local_and_remote_activities()
162 setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
164 setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
166 test "if user is unauthenticated", %{conn: conn} do
167 res_conn = get(conn, "/api/v1/timelines/public?local=true")
169 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
170 "error" => "authorization required for timeline view"
173 res_conn = get(conn, "/api/v1/timelines/public?local=false")
175 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
176 "error" => "authorization required for timeline view"
180 test "if user is authenticated" do
181 %{conn: conn} = oauth_access(["read:statuses"])
183 res_conn = get(conn, "/api/v1/timelines/public?local=true")
184 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
186 res_conn = get(conn, "/api/v1/timelines/public?local=false")
187 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
191 describe "public with restrict unauthenticated timeline for local" do
192 setup do: local_and_remote_activities()
194 setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
196 test "if user is unauthenticated", %{conn: conn} do
197 res_conn = get(conn, "/api/v1/timelines/public?local=true")
199 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
200 "error" => "authorization required for timeline view"
203 res_conn = get(conn, "/api/v1/timelines/public?local=false")
204 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
207 test "if user is authenticated", %{conn: _conn} do
208 %{conn: conn} = oauth_access(["read:statuses"])
210 res_conn = get(conn, "/api/v1/timelines/public?local=true")
211 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
213 res_conn = get(conn, "/api/v1/timelines/public?local=false")
214 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
218 describe "public with restrict unauthenticated timeline for remote" do
219 setup do: local_and_remote_activities()
221 setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
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
227 res_conn = get(conn, "/api/v1/timelines/public?local=false")
229 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
230 "error" => "authorization required for timeline view"
234 test "if user is authenticated", %{conn: _conn} do
235 %{conn: conn} = oauth_access(["read:statuses"])
237 res_conn = get(conn, "/api/v1/timelines/public?local=true")
238 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
240 res_conn = get(conn, "/api/v1/timelines/public?local=false")
241 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
246 test "direct timeline", %{conn: conn} do
247 user_one = insert(:user)
248 user_two = insert(:user)
250 {:ok, user_two} = User.follow(user_two, user_one)
253 CommonAPI.post(user_one, %{
254 status: "Hi @#{user_two.nickname}!",
258 {:ok, _follower_only} =
259 CommonAPI.post(user_one, %{
260 status: "Hi @#{user_two.nickname}!",
261 visibility: "private"
266 |> assign(:user, user_two)
267 |> assign(:token, insert(:oauth_token, user: user_two, scopes: ["read:statuses"]))
269 # Only direct should be visible here
270 res_conn = get(conn_user_two, "api/v1/timelines/direct")
272 assert [status] = json_response_and_validate_schema(res_conn, :ok)
274 assert %{"visibility" => "direct"} = status
275 assert status["url"] != direct.data["id"]
277 # User should be able to see their own direct message
280 |> assign(:user, user_one)
281 |> assign(:token, insert(:oauth_token, user: user_one, scopes: ["read:statuses"]))
282 |> get("api/v1/timelines/direct")
284 [status] = json_response_and_validate_schema(res_conn, :ok)
286 assert %{"visibility" => "direct"} = status
288 # Both should be visible here
289 res_conn = get(conn_user_two, "api/v1/timelines/home")
291 [_s1, _s2] = json_response_and_validate_schema(res_conn, :ok)
294 Enum.each(1..20, fn _ ->
296 CommonAPI.post(user_one, %{
297 status: "Hi @#{user_two.nickname}!",
302 res_conn = get(conn_user_two, "api/v1/timelines/direct")
304 statuses = json_response_and_validate_schema(res_conn, :ok)
305 assert length(statuses) == 20
307 max_id = List.last(statuses)["id"]
309 res_conn = get(conn_user_two, "api/v1/timelines/direct?max_id=#{max_id}")
311 assert [status] = json_response_and_validate_schema(res_conn, :ok)
313 assert status["url"] != direct.data["id"]
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)
322 {:ok, _blocked_direct} =
323 CommonAPI.post(blocked, %{
324 status: "Hi @#{blocker.nickname}!",
329 CommonAPI.post(other_user, %{
330 status: "Hi @#{blocker.nickname}!",
334 res_conn = get(conn, "api/v1/timelines/direct")
336 [status] = json_response_and_validate_schema(res_conn, :ok)
337 assert status["id"] == direct.id
342 setup do: oauth_access(["read:lists"])
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)
350 {:ok, list} = Pleroma.List.create("name", user)
351 {:ok, list} = Pleroma.List.follow(list, other_user)
353 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
355 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
357 assert id == to_string(activity_two.id)
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)
365 Enum.each(1..30, fn i ->
366 CommonAPI.post(other_user, %{status: "post number #{i}"})
370 get(conn, "/api/v1/timelines/list/#{list.id}?limit=1")
371 |> json_response_and_validate_schema(:ok)
373 assert length(res) == 1
378 get(conn, "/api/v1/timelines/list/#{list.id}?max_id=#{first["id"]}&limit=30")
379 |> json_response_and_validate_schema(:ok)
381 assert length(res) == 29
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)
391 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
393 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
395 assert id == to_string(activity_two.id)
398 test "list timeline does not leak non-public statuses for unfollowed users", %{
402 other_user = insert(:user)
403 {:ok, activity_one} = CommonAPI.post(other_user, %{status: "Marisa is cute."})
405 {:ok, _activity_two} =
406 CommonAPI.post(other_user, %{
407 status: "Marisa is cute.",
408 visibility: "private"
411 {:ok, list} = Pleroma.List.create("name", user)
412 {:ok, list} = Pleroma.List.follow(list, other_user)
414 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
416 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
418 assert id == to_string(activity_one.id)
422 describe "hashtag" do
423 setup do: oauth_access(["n/a"])
425 @tag capture_log: true
426 test "hashtag timeline", %{conn: conn} do
427 following = insert(:user)
429 {:ok, activity} = CommonAPI.post(following, %{status: "test #2hu"})
431 nconn = get(conn, "/api/v1/timelines/tag/2hu")
433 assert [%{"id" => id}] = json_response_and_validate_schema(nconn, :ok)
435 assert id == to_string(activity.id)
437 # works for different capitalization too
438 nconn = get(conn, "/api/v1/timelines/tag/2HU")
440 assert [%{"id" => id}] = json_response_and_validate_schema(nconn, :ok)
442 assert id == to_string(activity.id)
445 test "multi-hashtag timeline", %{conn: conn} do
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"})
452 any_test = get(conn, "/api/v1/timelines/tag/test?any[]=test1")
454 [status_none, status_test1, status_test] = json_response_and_validate_schema(any_test, :ok)
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"]
460 restricted_test = get(conn, "/api/v1/timelines/tag/test?all[]=test1&none[]=none")
462 assert [status_test1] == json_response_and_validate_schema(restricted_test, :ok)
464 all_test = get(conn, "/api/v1/timelines/tag/test?all[]=none")
466 assert [status_none] == json_response_and_validate_schema(all_test, :ok)
470 describe "hashtag timeline handling of :restrict_unauthenticated setting" do
473 {:ok, activity1} = CommonAPI.post(user, %{status: "test #tag1"})
474 {:ok, _activity2} = CommonAPI.post(user, %{status: "test #tag1"})
477 |> Ecto.Changeset.change(%{local: false})
478 |> Pleroma.Repo.update()
480 base_uri = "/api/v1/timelines/tag/tag1"
481 error_response = %{"error" => "authorization required for timeline view"}
483 %{base_uri: base_uri, error_response: error_response}
486 defp ensure_authenticated_access(base_uri) do
487 %{conn: auth_conn} = oauth_access(["read:statuses"])
489 res_conn = get(auth_conn, "#{base_uri}?local=true")
490 assert length(json_response(res_conn, 200)) == 1
492 res_conn = get(auth_conn, "#{base_uri}?local=false")
493 assert length(json_response(res_conn, 200)) == 2
496 test "with default settings on private instances, returns 403 for unauthenticated users", %{
499 error_response: error_response
501 clear_config([:instance, :public], false)
502 clear_config([:restrict_unauthenticated, :timelines])
504 for local <- [true, false] do
505 res_conn = get(conn, "#{base_uri}?local=#{local}")
507 assert json_response(res_conn, :unauthorized) == error_response
510 ensure_authenticated_access(base_uri)
513 test "with `%{local: true, federated: true}`, returns 403 for unauthenticated users", %{
516 error_response: error_response
518 clear_config([:restrict_unauthenticated, :timelines, :local], true)
519 clear_config([:restrict_unauthenticated, :timelines, :federated], true)
521 for local <- [true, false] do
522 res_conn = get(conn, "#{base_uri}?local=#{local}")
524 assert json_response(res_conn, :unauthorized) == error_response
527 ensure_authenticated_access(base_uri)
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)
535 res_conn = get(conn, "#{base_uri}?local=true")
536 assert length(json_response(res_conn, 200)) == 1
538 res_conn = get(conn, "#{base_uri}?local=false")
539 assert json_response(res_conn, :unauthorized) == error_response
541 ensure_authenticated_access(base_uri)
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)
550 res_conn = get(conn, "#{base_uri}?local=true")
551 assert json_response(res_conn, :unauthorized) == error_response
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
557 ensure_authenticated_access(base_uri)