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
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
60 @tag capture_log: true
61 test "the public timeline", %{conn: conn} do
64 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
66 _activity = insert(:note_activity, local: false)
68 conn = get(conn, "/api/v1/timelines/public?local=False")
70 assert length(json_response_and_validate_schema(conn, :ok)) == 2
72 conn = get(build_conn(), "/api/v1/timelines/public?local=True")
74 assert [%{"content" => "test"}] = json_response_and_validate_schema(conn, :ok)
76 conn = get(build_conn(), "/api/v1/timelines/public?local=1")
78 assert [%{"content" => "test"}] = json_response_and_validate_schema(conn, :ok)
80 # does not contain repeats
81 {:ok, _} = CommonAPI.repeat(activity.id, user)
83 conn = get(build_conn(), "/api/v1/timelines/public?local=true")
85 assert [_] = json_response_and_validate_schema(conn, :ok)
88 test "the public timeline includes only public statuses for an authenticated user" do
89 %{user: user, conn: conn} = oauth_access(["read:statuses"])
91 {:ok, _activity} = CommonAPI.post(user, %{status: "test"})
92 {:ok, _activity} = CommonAPI.post(user, %{status: "test", visibility: "private"})
93 {:ok, _activity} = CommonAPI.post(user, %{status: "test", visibility: "unlisted"})
94 {:ok, _activity} = CommonAPI.post(user, %{status: "test", visibility: "direct"})
96 res_conn = get(conn, "/api/v1/timelines/public")
97 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
100 test "doesn't return replies if follower is posting with blocked user" do
101 %{conn: conn, user: blocker} = oauth_access(["read:statuses"])
102 [blockee, friend] = insert_list(2, :user)
103 {:ok, blocker} = User.follow(blocker, friend)
104 {:ok, _} = User.block(blocker, blockee)
106 conn = assign(conn, :user, blocker)
108 {:ok, %{id: activity_id} = activity} = CommonAPI.post(friend, %{status: "hey!"})
110 {:ok, reply_from_blockee} =
111 CommonAPI.post(blockee, %{status: "heya", in_reply_to_status_id: activity})
113 {:ok, _reply_from_friend} =
114 CommonAPI.post(friend, %{status: "status", in_reply_to_status_id: reply_from_blockee})
116 # Still shows replies from yourself
117 {:ok, %{id: reply_from_me}} =
118 CommonAPI.post(blocker, %{status: "status", in_reply_to_status_id: reply_from_blockee})
121 get(conn, "/api/v1/timelines/public")
122 |> json_response_and_validate_schema(200)
124 assert length(response) == 2
125 [%{"id" => ^reply_from_me}, %{"id" => ^activity_id}] = response
128 test "doesn't return replies if follow is posting with users from blocked domain" do
129 %{conn: conn, user: blocker} = oauth_access(["read:statuses"])
130 friend = insert(:user)
131 blockee = insert(:user, ap_id: "https://example.com/users/blocked")
132 {:ok, blocker} = User.follow(blocker, friend)
133 {:ok, blocker} = User.block_domain(blocker, "example.com")
135 conn = assign(conn, :user, blocker)
137 {:ok, %{id: activity_id} = activity} = CommonAPI.post(friend, %{status: "hey!"})
139 {:ok, reply_from_blockee} =
140 CommonAPI.post(blockee, %{status: "heya", in_reply_to_status_id: activity})
142 {:ok, _reply_from_friend} =
143 CommonAPI.post(friend, %{status: "status", in_reply_to_status_id: reply_from_blockee})
145 res_conn = get(conn, "/api/v1/timelines/public")
147 activities = json_response_and_validate_schema(res_conn, 200)
148 [%{"id" => ^activity_id}] = activities
151 test "can be filtered by instance", %{conn: conn} do
152 user = insert(:user, ap_id: "https://lain.com/users/lain")
153 insert(:note_activity, local: false)
154 insert(:note_activity, local: false)
156 {:ok, _} = CommonAPI.post(user, %{status: "test"})
158 conn = get(conn, "/api/v1/timelines/public?instance=lain.com")
160 assert length(json_response_and_validate_schema(conn, :ok)) == 1
164 defp local_and_remote_activities do
165 insert(:note_activity)
166 insert(:note_activity, local: false)
170 describe "public with restrict unauthenticated timeline for local and federated timelines" do
171 setup do: local_and_remote_activities()
173 setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
175 setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
177 test "if user is unauthenticated", %{conn: conn} do
178 res_conn = get(conn, "/api/v1/timelines/public?local=true")
180 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
181 "error" => "authorization required for timeline view"
184 res_conn = get(conn, "/api/v1/timelines/public?local=false")
186 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
187 "error" => "authorization required for timeline view"
191 test "if user is authenticated" do
192 %{conn: conn} = oauth_access(["read:statuses"])
194 res_conn = get(conn, "/api/v1/timelines/public?local=true")
195 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
197 res_conn = get(conn, "/api/v1/timelines/public?local=false")
198 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
202 describe "public with restrict unauthenticated timeline for local" do
203 setup do: local_and_remote_activities()
205 setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
207 test "if user is unauthenticated", %{conn: conn} do
208 res_conn = get(conn, "/api/v1/timelines/public?local=true")
210 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
211 "error" => "authorization required for timeline view"
214 res_conn = get(conn, "/api/v1/timelines/public?local=false")
215 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
218 test "if user is authenticated", %{conn: _conn} do
219 %{conn: conn} = oauth_access(["read:statuses"])
221 res_conn = get(conn, "/api/v1/timelines/public?local=true")
222 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
224 res_conn = get(conn, "/api/v1/timelines/public?local=false")
225 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
229 describe "public with restrict unauthenticated timeline for remote" do
230 setup do: local_and_remote_activities()
232 setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
234 test "if user is unauthenticated", %{conn: conn} do
235 res_conn = get(conn, "/api/v1/timelines/public?local=true")
236 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
238 res_conn = get(conn, "/api/v1/timelines/public?local=false")
240 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
241 "error" => "authorization required for timeline view"
245 test "if user is authenticated", %{conn: _conn} do
246 %{conn: conn} = oauth_access(["read:statuses"])
248 res_conn = get(conn, "/api/v1/timelines/public?local=true")
249 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
251 res_conn = get(conn, "/api/v1/timelines/public?local=false")
252 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
257 test "direct timeline", %{conn: conn} do
258 user_one = insert(:user)
259 user_two = insert(:user)
261 {:ok, user_two} = User.follow(user_two, user_one)
264 CommonAPI.post(user_one, %{
265 status: "Hi @#{user_two.nickname}!",
269 {:ok, _follower_only} =
270 CommonAPI.post(user_one, %{
271 status: "Hi @#{user_two.nickname}!",
272 visibility: "private"
277 |> assign(:user, user_two)
278 |> assign(:token, insert(:oauth_token, user: user_two, scopes: ["read:statuses"]))
280 # Only direct should be visible here
281 res_conn = get(conn_user_two, "api/v1/timelines/direct")
283 assert [status] = json_response_and_validate_schema(res_conn, :ok)
285 assert %{"visibility" => "direct"} = status
286 assert status["url"] != direct.data["id"]
288 # User should be able to see their own direct message
291 |> assign(:user, user_one)
292 |> assign(:token, insert(:oauth_token, user: user_one, scopes: ["read:statuses"]))
293 |> get("api/v1/timelines/direct")
295 [status] = json_response_and_validate_schema(res_conn, :ok)
297 assert %{"visibility" => "direct"} = status
299 # Both should be visible here
300 res_conn = get(conn_user_two, "api/v1/timelines/home")
302 [_s1, _s2] = json_response_and_validate_schema(res_conn, :ok)
305 Enum.each(1..20, fn _ ->
307 CommonAPI.post(user_one, %{
308 status: "Hi @#{user_two.nickname}!",
313 res_conn = get(conn_user_two, "api/v1/timelines/direct")
315 statuses = json_response_and_validate_schema(res_conn, :ok)
316 assert length(statuses) == 20
318 max_id = List.last(statuses)["id"]
320 res_conn = get(conn_user_two, "api/v1/timelines/direct?max_id=#{max_id}")
322 assert [status] = json_response_and_validate_schema(res_conn, :ok)
324 assert status["url"] != direct.data["id"]
327 test "doesn't include DMs from blocked users" do
328 %{user: blocker, conn: conn} = oauth_access(["read:statuses"])
329 blocked = insert(:user)
330 other_user = insert(:user)
331 {:ok, _user_relationship} = User.block(blocker, blocked)
333 {:ok, _blocked_direct} =
334 CommonAPI.post(blocked, %{
335 status: "Hi @#{blocker.nickname}!",
340 CommonAPI.post(other_user, %{
341 status: "Hi @#{blocker.nickname}!",
345 res_conn = get(conn, "api/v1/timelines/direct")
347 [status] = json_response_and_validate_schema(res_conn, :ok)
348 assert status["id"] == direct.id
353 setup do: oauth_access(["read:lists"])
355 test "does not contain retoots", %{user: user, conn: conn} do
356 other_user = insert(:user)
357 {:ok, activity_one} = CommonAPI.post(user, %{status: "Marisa is cute."})
358 {:ok, activity_two} = CommonAPI.post(other_user, %{status: "Marisa is stupid."})
359 {:ok, _} = CommonAPI.repeat(activity_one.id, other_user)
361 {:ok, list} = Pleroma.List.create("name", user)
362 {:ok, list} = Pleroma.List.follow(list, other_user)
364 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
366 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
368 assert id == to_string(activity_two.id)
371 test "works with pagination", %{user: user, conn: conn} do
372 other_user = insert(:user)
373 {:ok, list} = Pleroma.List.create("name", user)
374 {:ok, list} = Pleroma.List.follow(list, other_user)
376 Enum.each(1..30, fn i ->
377 CommonAPI.post(other_user, %{status: "post number #{i}"})
381 get(conn, "/api/v1/timelines/list/#{list.id}?limit=1")
382 |> json_response_and_validate_schema(:ok)
384 assert length(res) == 1
389 get(conn, "/api/v1/timelines/list/#{list.id}?max_id=#{first["id"]}&limit=30")
390 |> json_response_and_validate_schema(:ok)
392 assert length(res) == 29
395 test "list timeline", %{user: user, conn: conn} do
396 other_user = insert(:user)
397 {:ok, _activity_one} = CommonAPI.post(user, %{status: "Marisa is cute."})
398 {:ok, activity_two} = CommonAPI.post(other_user, %{status: "Marisa is cute."})
399 {:ok, list} = Pleroma.List.create("name", user)
400 {:ok, list} = Pleroma.List.follow(list, other_user)
402 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
404 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
406 assert id == to_string(activity_two.id)
409 test "list timeline does not leak non-public statuses for unfollowed users", %{
413 other_user = insert(:user)
414 {:ok, activity_one} = CommonAPI.post(other_user, %{status: "Marisa is cute."})
416 {:ok, _activity_two} =
417 CommonAPI.post(other_user, %{
418 status: "Marisa is cute.",
419 visibility: "private"
422 {:ok, list} = Pleroma.List.create("name", user)
423 {:ok, list} = Pleroma.List.follow(list, other_user)
425 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
427 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
429 assert id == to_string(activity_one.id)
433 describe "hashtag" do
434 setup do: oauth_access(["n/a"])
436 @tag capture_log: true
437 test "hashtag timeline", %{conn: conn} do
438 following = insert(:user)
440 {:ok, activity} = CommonAPI.post(following, %{status: "test #2hu"})
442 nconn = get(conn, "/api/v1/timelines/tag/2hu")
444 assert [%{"id" => id}] = json_response_and_validate_schema(nconn, :ok)
446 assert id == to_string(activity.id)
448 # works for different capitalization too
449 nconn = get(conn, "/api/v1/timelines/tag/2HU")
451 assert [%{"id" => id}] = json_response_and_validate_schema(nconn, :ok)
453 assert id == to_string(activity.id)
456 test "multi-hashtag timeline", %{conn: conn} do
459 {:ok, activity_test} = CommonAPI.post(user, %{status: "#test"})
460 {:ok, activity_test1} = CommonAPI.post(user, %{status: "#test #test1"})
461 {:ok, activity_none} = CommonAPI.post(user, %{status: "#test #none"})
463 any_test = get(conn, "/api/v1/timelines/tag/test?any[]=test1")
465 [status_none, status_test1, status_test] = json_response_and_validate_schema(any_test, :ok)
467 assert to_string(activity_test.id) == status_test["id"]
468 assert to_string(activity_test1.id) == status_test1["id"]
469 assert to_string(activity_none.id) == status_none["id"]
471 restricted_test = get(conn, "/api/v1/timelines/tag/test?all[]=test1&none[]=none")
473 assert [status_test1] == json_response_and_validate_schema(restricted_test, :ok)
475 all_test = get(conn, "/api/v1/timelines/tag/test?all[]=none")
477 assert [status_none] == json_response_and_validate_schema(all_test, :ok)
481 describe "hashtag timeline handling of :restrict_unauthenticated setting" do
484 {:ok, activity1} = CommonAPI.post(user, %{status: "test #tag1"})
485 {:ok, _activity2} = CommonAPI.post(user, %{status: "test #tag1"})
488 |> Ecto.Changeset.change(%{local: false})
489 |> Pleroma.Repo.update()
491 base_uri = "/api/v1/timelines/tag/tag1"
492 error_response = %{"error" => "authorization required for timeline view"}
494 %{base_uri: base_uri, error_response: error_response}
497 defp ensure_authenticated_access(base_uri) do
498 %{conn: auth_conn} = oauth_access(["read:statuses"])
500 res_conn = get(auth_conn, "#{base_uri}?local=true")
501 assert length(json_response(res_conn, 200)) == 1
503 res_conn = get(auth_conn, "#{base_uri}?local=false")
504 assert length(json_response(res_conn, 200)) == 2
507 test "with default settings on private instances, returns 403 for unauthenticated users", %{
510 error_response: error_response
512 clear_config([:instance, :public], false)
513 clear_config([:restrict_unauthenticated, :timelines])
515 for local <- [true, false] do
516 res_conn = get(conn, "#{base_uri}?local=#{local}")
518 assert json_response(res_conn, :unauthorized) == error_response
521 ensure_authenticated_access(base_uri)
524 test "with `%{local: true, federated: true}`, returns 403 for unauthenticated users", %{
527 error_response: error_response
529 clear_config([:restrict_unauthenticated, :timelines, :local], true)
530 clear_config([:restrict_unauthenticated, :timelines, :federated], true)
532 for local <- [true, false] do
533 res_conn = get(conn, "#{base_uri}?local=#{local}")
535 assert json_response(res_conn, :unauthorized) == error_response
538 ensure_authenticated_access(base_uri)
541 test "with `%{local: false, federated: true}`, forbids unauthenticated access to federated timeline",
542 %{conn: conn, base_uri: base_uri, error_response: error_response} do
543 clear_config([:restrict_unauthenticated, :timelines, :local], false)
544 clear_config([:restrict_unauthenticated, :timelines, :federated], true)
546 res_conn = get(conn, "#{base_uri}?local=true")
547 assert length(json_response(res_conn, 200)) == 1
549 res_conn = get(conn, "#{base_uri}?local=false")
550 assert json_response(res_conn, :unauthorized) == error_response
552 ensure_authenticated_access(base_uri)
555 test "with `%{local: true, federated: false}`, forbids unauthenticated access to public timeline" <>
556 "(but not to local public activities which are delivered as part of federated timeline)",
557 %{conn: conn, base_uri: base_uri, error_response: error_response} do
558 clear_config([:restrict_unauthenticated, :timelines, :local], true)
559 clear_config([:restrict_unauthenticated, :timelines, :federated], false)
561 res_conn = get(conn, "#{base_uri}?local=true")
562 assert json_response(res_conn, :unauthorized) == error_response
564 # Note: local activities get delivered as part of federated timeline
565 res_conn = get(conn, "#{base_uri}?local=false")
566 assert length(json_response(res_conn, 200)) == 2
568 ensure_authenticated_access(base_uri)