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 res_conn = get(conn, "/api/v1/timelines/public")
118 [%{"id" => ^activity_id}] = json_response_and_validate_schema(res_conn, 200)
121 test "doesn't return replies if follow is posting with users from blocked domain" do
122 %{conn: conn, user: blocker} = oauth_access(["read:statuses"])
123 friend = insert(:user)
124 blockee = insert(:user, ap_id: "https://example.com/users/blocked")
125 {:ok, blocker} = User.follow(blocker, friend)
126 {:ok, blocker} = User.block_domain(blocker, "example.com")
128 conn = assign(conn, :user, blocker)
130 {:ok, %{id: activity_id} = activity} = CommonAPI.post(friend, %{status: "hey!"})
132 {:ok, reply_from_blockee} =
133 CommonAPI.post(blockee, %{status: "heya", in_reply_to_status_id: activity})
135 {:ok, _reply_from_friend} =
136 CommonAPI.post(friend, %{status: "status", in_reply_to_status_id: reply_from_blockee})
138 res_conn = get(conn, "/api/v1/timelines/public")
140 activities = json_response_and_validate_schema(res_conn, 200)
141 [%{"id" => ^activity_id}] = activities
145 defp local_and_remote_activities do
146 insert(:note_activity)
147 insert(:note_activity, local: false)
151 describe "public with restrict unauthenticated timeline for local and federated timelines" do
152 setup do: local_and_remote_activities()
154 setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
156 setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
158 test "if user is unauthenticated", %{conn: conn} do
159 res_conn = get(conn, "/api/v1/timelines/public?local=true")
161 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
162 "error" => "authorization required for timeline view"
165 res_conn = get(conn, "/api/v1/timelines/public?local=false")
167 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
168 "error" => "authorization required for timeline view"
172 test "if user is authenticated" do
173 %{conn: conn} = oauth_access(["read:statuses"])
175 res_conn = get(conn, "/api/v1/timelines/public?local=true")
176 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
178 res_conn = get(conn, "/api/v1/timelines/public?local=false")
179 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
183 describe "public with restrict unauthenticated timeline for local" do
184 setup do: local_and_remote_activities()
186 setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
188 test "if user is unauthenticated", %{conn: conn} do
189 res_conn = get(conn, "/api/v1/timelines/public?local=true")
191 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
192 "error" => "authorization required for timeline view"
195 res_conn = get(conn, "/api/v1/timelines/public?local=false")
196 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
199 test "if user is authenticated", %{conn: _conn} do
200 %{conn: conn} = oauth_access(["read:statuses"])
202 res_conn = get(conn, "/api/v1/timelines/public?local=true")
203 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
205 res_conn = get(conn, "/api/v1/timelines/public?local=false")
206 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
210 describe "public with restrict unauthenticated timeline for remote" do
211 setup do: local_and_remote_activities()
213 setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
215 test "if user is unauthenticated", %{conn: conn} do
216 res_conn = get(conn, "/api/v1/timelines/public?local=true")
217 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
219 res_conn = get(conn, "/api/v1/timelines/public?local=false")
221 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
222 "error" => "authorization required for timeline view"
226 test "if user is authenticated", %{conn: _conn} do
227 %{conn: conn} = oauth_access(["read:statuses"])
229 res_conn = get(conn, "/api/v1/timelines/public?local=true")
230 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
232 res_conn = get(conn, "/api/v1/timelines/public?local=false")
233 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
238 test "direct timeline", %{conn: conn} do
239 user_one = insert(:user)
240 user_two = insert(:user)
242 {:ok, user_two} = User.follow(user_two, user_one)
245 CommonAPI.post(user_one, %{
246 status: "Hi @#{user_two.nickname}!",
250 {:ok, _follower_only} =
251 CommonAPI.post(user_one, %{
252 status: "Hi @#{user_two.nickname}!",
253 visibility: "private"
258 |> assign(:user, user_two)
259 |> assign(:token, insert(:oauth_token, user: user_two, scopes: ["read:statuses"]))
261 # Only direct should be visible here
262 res_conn = get(conn_user_two, "api/v1/timelines/direct")
264 assert [status] = json_response_and_validate_schema(res_conn, :ok)
266 assert %{"visibility" => "direct"} = status
267 assert status["url"] != direct.data["id"]
269 # User should be able to see their own direct message
272 |> assign(:user, user_one)
273 |> assign(:token, insert(:oauth_token, user: user_one, scopes: ["read:statuses"]))
274 |> get("api/v1/timelines/direct")
276 [status] = json_response_and_validate_schema(res_conn, :ok)
278 assert %{"visibility" => "direct"} = status
280 # Both should be visible here
281 res_conn = get(conn_user_two, "api/v1/timelines/home")
283 [_s1, _s2] = json_response_and_validate_schema(res_conn, :ok)
286 Enum.each(1..20, fn _ ->
288 CommonAPI.post(user_one, %{
289 status: "Hi @#{user_two.nickname}!",
294 res_conn = get(conn_user_two, "api/v1/timelines/direct")
296 statuses = json_response_and_validate_schema(res_conn, :ok)
297 assert length(statuses) == 20
299 max_id = List.last(statuses)["id"]
301 res_conn = get(conn_user_two, "api/v1/timelines/direct?max_id=#{max_id}")
303 assert [status] = json_response_and_validate_schema(res_conn, :ok)
305 assert status["url"] != direct.data["id"]
308 test "doesn't include DMs from blocked users" do
309 %{user: blocker, conn: conn} = oauth_access(["read:statuses"])
310 blocked = insert(:user)
311 other_user = insert(:user)
312 {:ok, _user_relationship} = User.block(blocker, blocked)
314 {:ok, _blocked_direct} =
315 CommonAPI.post(blocked, %{
316 status: "Hi @#{blocker.nickname}!",
321 CommonAPI.post(other_user, %{
322 status: "Hi @#{blocker.nickname}!",
326 res_conn = get(conn, "api/v1/timelines/direct")
328 [status] = json_response_and_validate_schema(res_conn, :ok)
329 assert status["id"] == direct.id
334 setup do: oauth_access(["read:lists"])
336 test "list timeline", %{user: user, conn: conn} do
337 other_user = insert(:user)
338 {:ok, _activity_one} = CommonAPI.post(user, %{status: "Marisa is cute."})
339 {:ok, activity_two} = CommonAPI.post(other_user, %{status: "Marisa is cute."})
340 {:ok, list} = Pleroma.List.create("name", user)
341 {:ok, list} = Pleroma.List.follow(list, other_user)
343 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
345 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
347 assert id == to_string(activity_two.id)
350 test "list timeline does not leak non-public statuses for unfollowed users", %{
354 other_user = insert(:user)
355 {:ok, activity_one} = CommonAPI.post(other_user, %{status: "Marisa is cute."})
357 {:ok, _activity_two} =
358 CommonAPI.post(other_user, %{
359 status: "Marisa is cute.",
360 visibility: "private"
363 {:ok, list} = Pleroma.List.create("name", user)
364 {:ok, list} = Pleroma.List.follow(list, other_user)
366 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
368 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
370 assert id == to_string(activity_one.id)
374 describe "hashtag" do
375 setup do: oauth_access(["n/a"])
377 @tag capture_log: true
378 test "hashtag timeline", %{conn: conn} do
379 following = insert(:user)
381 {:ok, activity} = CommonAPI.post(following, %{status: "test #2hu"})
383 nconn = get(conn, "/api/v1/timelines/tag/2hu")
385 assert [%{"id" => id}] = json_response_and_validate_schema(nconn, :ok)
387 assert id == to_string(activity.id)
389 # works for different capitalization too
390 nconn = get(conn, "/api/v1/timelines/tag/2HU")
392 assert [%{"id" => id}] = json_response_and_validate_schema(nconn, :ok)
394 assert id == to_string(activity.id)
397 test "multi-hashtag timeline", %{conn: conn} do
400 {:ok, activity_test} = CommonAPI.post(user, %{status: "#test"})
401 {:ok, activity_test1} = CommonAPI.post(user, %{status: "#test #test1"})
402 {:ok, activity_none} = CommonAPI.post(user, %{status: "#test #none"})
404 any_test = get(conn, "/api/v1/timelines/tag/test?any[]=test1")
406 [status_none, status_test1, status_test] = json_response_and_validate_schema(any_test, :ok)
408 assert to_string(activity_test.id) == status_test["id"]
409 assert to_string(activity_test1.id) == status_test1["id"]
410 assert to_string(activity_none.id) == status_none["id"]
412 restricted_test = get(conn, "/api/v1/timelines/tag/test?all[]=test1&none[]=none")
414 assert [status_test1] == json_response_and_validate_schema(restricted_test, :ok)
416 all_test = get(conn, "/api/v1/timelines/tag/test?all[]=none")
418 assert [status_none] == json_response_and_validate_schema(all_test, :ok)
422 describe "hashtag timeline handling of :restrict_unauthenticated setting" do
425 {:ok, activity1} = CommonAPI.post(user, %{status: "test #tag1"})
426 {:ok, _activity2} = CommonAPI.post(user, %{status: "test #tag1"})
429 |> Ecto.Changeset.change(%{local: false})
430 |> Pleroma.Repo.update()
432 base_uri = "/api/v1/timelines/tag/tag1"
433 error_response = %{"error" => "authorization required for timeline view"}
435 %{base_uri: base_uri, error_response: error_response}
438 defp ensure_authenticated_access(base_uri) do
439 %{conn: auth_conn} = oauth_access(["read:statuses"])
441 res_conn = get(auth_conn, "#{base_uri}?local=true")
442 assert length(json_response(res_conn, 200)) == 1
444 res_conn = get(auth_conn, "#{base_uri}?local=false")
445 assert length(json_response(res_conn, 200)) == 2
448 test "with `%{local: true, federated: true}`, returns 403 for unauthenticated users", %{
451 error_response: error_response
453 clear_config([:restrict_unauthenticated, :timelines, :local], true)
454 clear_config([:restrict_unauthenticated, :timelines, :federated], true)
456 for local <- [true, false] do
457 res_conn = get(conn, "#{base_uri}?local=#{local}")
459 assert json_response(res_conn, :unauthorized) == error_response
462 ensure_authenticated_access(base_uri)
465 test "with `%{local: false, federated: true}`, forbids unauthenticated access to federated timeline",
466 %{conn: conn, base_uri: base_uri, error_response: error_response} do
467 clear_config([:restrict_unauthenticated, :timelines, :local], false)
468 clear_config([:restrict_unauthenticated, :timelines, :federated], true)
470 res_conn = get(conn, "#{base_uri}?local=true")
471 assert length(json_response(res_conn, 200)) == 1
473 res_conn = get(conn, "#{base_uri}?local=false")
474 assert json_response(res_conn, :unauthorized) == error_response
476 ensure_authenticated_access(base_uri)
479 test "with `%{local: true, federated: false}`, forbids unauthenticated access to public timeline" <>
480 "(but not to local public activities which are delivered as part of federated timeline)",
481 %{conn: conn, base_uri: base_uri, error_response: error_response} do
482 clear_config([:restrict_unauthenticated, :timelines, :local], true)
483 clear_config([:restrict_unauthenticated, :timelines, :federated], false)
485 res_conn = get(conn, "#{base_uri}?local=true")
486 assert json_response(res_conn, :unauthorized) == error_response
488 # Note: local activities get delivered as part of federated timeline
489 res_conn = get(conn, "#{base_uri}?local=false")
490 assert length(json_response(res_conn, 200)) == 2
492 ensure_authenticated_access(base_uri)