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
152 defp local_and_remote_activities do
153 insert(:note_activity)
154 insert(:note_activity, local: false)
158 describe "public with restrict unauthenticated timeline for local and federated timelines" do
159 setup do: local_and_remote_activities()
161 setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
163 setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
165 test "if user is unauthenticated", %{conn: conn} do
166 res_conn = get(conn, "/api/v1/timelines/public?local=true")
168 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
169 "error" => "authorization required for timeline view"
172 res_conn = get(conn, "/api/v1/timelines/public?local=false")
174 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
175 "error" => "authorization required for timeline view"
179 test "if user is authenticated" do
180 %{conn: conn} = oauth_access(["read:statuses"])
182 res_conn = get(conn, "/api/v1/timelines/public?local=true")
183 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
185 res_conn = get(conn, "/api/v1/timelines/public?local=false")
186 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
190 describe "public with restrict unauthenticated timeline for local" do
191 setup do: local_and_remote_activities()
193 setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
195 test "if user is unauthenticated", %{conn: conn} do
196 res_conn = get(conn, "/api/v1/timelines/public?local=true")
198 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
199 "error" => "authorization required for timeline view"
202 res_conn = get(conn, "/api/v1/timelines/public?local=false")
203 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
206 test "if user is authenticated", %{conn: _conn} do
207 %{conn: conn} = oauth_access(["read:statuses"])
209 res_conn = get(conn, "/api/v1/timelines/public?local=true")
210 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
212 res_conn = get(conn, "/api/v1/timelines/public?local=false")
213 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
217 describe "public with restrict unauthenticated timeline for remote" do
218 setup do: local_and_remote_activities()
220 setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
222 test "if user is unauthenticated", %{conn: conn} do
223 res_conn = get(conn, "/api/v1/timelines/public?local=true")
224 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
226 res_conn = get(conn, "/api/v1/timelines/public?local=false")
228 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
229 "error" => "authorization required for timeline view"
233 test "if user is authenticated", %{conn: _conn} do
234 %{conn: conn} = oauth_access(["read:statuses"])
236 res_conn = get(conn, "/api/v1/timelines/public?local=true")
237 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
239 res_conn = get(conn, "/api/v1/timelines/public?local=false")
240 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
245 test "direct timeline", %{conn: conn} do
246 user_one = insert(:user)
247 user_two = insert(:user)
249 {:ok, user_two} = User.follow(user_two, user_one)
252 CommonAPI.post(user_one, %{
253 status: "Hi @#{user_two.nickname}!",
257 {:ok, _follower_only} =
258 CommonAPI.post(user_one, %{
259 status: "Hi @#{user_two.nickname}!",
260 visibility: "private"
265 |> assign(:user, user_two)
266 |> assign(:token, insert(:oauth_token, user: user_two, scopes: ["read:statuses"]))
268 # Only direct should be visible here
269 res_conn = get(conn_user_two, "api/v1/timelines/direct")
271 assert [status] = json_response_and_validate_schema(res_conn, :ok)
273 assert %{"visibility" => "direct"} = status
274 assert status["url"] != direct.data["id"]
276 # User should be able to see their own direct message
279 |> assign(:user, user_one)
280 |> assign(:token, insert(:oauth_token, user: user_one, scopes: ["read:statuses"]))
281 |> get("api/v1/timelines/direct")
283 [status] = json_response_and_validate_schema(res_conn, :ok)
285 assert %{"visibility" => "direct"} = status
287 # Both should be visible here
288 res_conn = get(conn_user_two, "api/v1/timelines/home")
290 [_s1, _s2] = json_response_and_validate_schema(res_conn, :ok)
293 Enum.each(1..20, fn _ ->
295 CommonAPI.post(user_one, %{
296 status: "Hi @#{user_two.nickname}!",
301 res_conn = get(conn_user_two, "api/v1/timelines/direct")
303 statuses = json_response_and_validate_schema(res_conn, :ok)
304 assert length(statuses) == 20
306 max_id = List.last(statuses)["id"]
308 res_conn = get(conn_user_two, "api/v1/timelines/direct?max_id=#{max_id}")
310 assert [status] = json_response_and_validate_schema(res_conn, :ok)
312 assert status["url"] != direct.data["id"]
315 test "doesn't include DMs from blocked users" do
316 %{user: blocker, conn: conn} = oauth_access(["read:statuses"])
317 blocked = insert(:user)
318 other_user = insert(:user)
319 {:ok, _user_relationship} = User.block(blocker, blocked)
321 {:ok, _blocked_direct} =
322 CommonAPI.post(blocked, %{
323 status: "Hi @#{blocker.nickname}!",
328 CommonAPI.post(other_user, %{
329 status: "Hi @#{blocker.nickname}!",
333 res_conn = get(conn, "api/v1/timelines/direct")
335 [status] = json_response_and_validate_schema(res_conn, :ok)
336 assert status["id"] == direct.id
341 setup do: oauth_access(["read:lists"])
343 test "does not contain retoots", %{user: user, conn: conn} do
344 other_user = insert(:user)
345 {:ok, activity_one} = CommonAPI.post(user, %{status: "Marisa is cute."})
346 {:ok, activity_two} = CommonAPI.post(other_user, %{status: "Marisa is stupid."})
347 {:ok, _} = CommonAPI.repeat(activity_one.id, other_user)
349 {:ok, list} = Pleroma.List.create("name", user)
350 {:ok, list} = Pleroma.List.follow(list, other_user)
352 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
354 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
356 assert id == to_string(activity_two.id)
359 test "works with pagination", %{user: user, conn: conn} do
360 other_user = insert(:user)
361 {:ok, list} = Pleroma.List.create("name", user)
362 {:ok, list} = Pleroma.List.follow(list, other_user)
364 Enum.each(1..30, fn i ->
365 CommonAPI.post(other_user, %{status: "post number #{i}"})
369 get(conn, "/api/v1/timelines/list/#{list.id}?limit=1")
370 |> json_response_and_validate_schema(:ok)
372 assert length(res) == 1
377 get(conn, "/api/v1/timelines/list/#{list.id}?max_id=#{first["id"]}&limit=30")
378 |> json_response_and_validate_schema(:ok)
380 assert length(res) == 29
383 test "list timeline", %{user: user, conn: conn} do
384 other_user = insert(:user)
385 {:ok, _activity_one} = CommonAPI.post(user, %{status: "Marisa is cute."})
386 {:ok, activity_two} = CommonAPI.post(other_user, %{status: "Marisa is cute."})
387 {:ok, list} = Pleroma.List.create("name", user)
388 {:ok, list} = Pleroma.List.follow(list, other_user)
390 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
392 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
394 assert id == to_string(activity_two.id)
397 test "list timeline does not leak non-public statuses for unfollowed users", %{
401 other_user = insert(:user)
402 {:ok, activity_one} = CommonAPI.post(other_user, %{status: "Marisa is cute."})
404 {:ok, _activity_two} =
405 CommonAPI.post(other_user, %{
406 status: "Marisa is cute.",
407 visibility: "private"
410 {:ok, list} = Pleroma.List.create("name", user)
411 {:ok, list} = Pleroma.List.follow(list, other_user)
413 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
415 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
417 assert id == to_string(activity_one.id)
421 describe "hashtag" do
422 setup do: oauth_access(["n/a"])
424 @tag capture_log: true
425 test "hashtag timeline", %{conn: conn} do
426 following = insert(:user)
428 {:ok, activity} = CommonAPI.post(following, %{status: "test #2hu"})
430 nconn = get(conn, "/api/v1/timelines/tag/2hu")
432 assert [%{"id" => id}] = json_response_and_validate_schema(nconn, :ok)
434 assert id == to_string(activity.id)
436 # works for different capitalization too
437 nconn = get(conn, "/api/v1/timelines/tag/2HU")
439 assert [%{"id" => id}] = json_response_and_validate_schema(nconn, :ok)
441 assert id == to_string(activity.id)
444 test "multi-hashtag timeline", %{conn: conn} do
447 {:ok, activity_test} = CommonAPI.post(user, %{status: "#test"})
448 {:ok, activity_test1} = CommonAPI.post(user, %{status: "#test #test1"})
449 {:ok, activity_none} = CommonAPI.post(user, %{status: "#test #none"})
451 any_test = get(conn, "/api/v1/timelines/tag/test?any[]=test1")
453 [status_none, status_test1, status_test] = json_response_and_validate_schema(any_test, :ok)
455 assert to_string(activity_test.id) == status_test["id"]
456 assert to_string(activity_test1.id) == status_test1["id"]
457 assert to_string(activity_none.id) == status_none["id"]
459 restricted_test = get(conn, "/api/v1/timelines/tag/test?all[]=test1&none[]=none")
461 assert [status_test1] == json_response_and_validate_schema(restricted_test, :ok)
463 all_test = get(conn, "/api/v1/timelines/tag/test?all[]=none")
465 assert [status_none] == json_response_and_validate_schema(all_test, :ok)
469 describe "hashtag timeline handling of :restrict_unauthenticated setting" do
472 {:ok, activity1} = CommonAPI.post(user, %{status: "test #tag1"})
473 {:ok, _activity2} = CommonAPI.post(user, %{status: "test #tag1"})
476 |> Ecto.Changeset.change(%{local: false})
477 |> Pleroma.Repo.update()
479 base_uri = "/api/v1/timelines/tag/tag1"
480 error_response = %{"error" => "authorization required for timeline view"}
482 %{base_uri: base_uri, error_response: error_response}
485 defp ensure_authenticated_access(base_uri) do
486 %{conn: auth_conn} = oauth_access(["read:statuses"])
488 res_conn = get(auth_conn, "#{base_uri}?local=true")
489 assert length(json_response(res_conn, 200)) == 1
491 res_conn = get(auth_conn, "#{base_uri}?local=false")
492 assert length(json_response(res_conn, 200)) == 2
495 test "with default settings on private instances, returns 403 for unauthenticated users", %{
498 error_response: error_response
500 clear_config([:instance, :public], false)
501 clear_config([:restrict_unauthenticated, :timelines])
503 for local <- [true, false] do
504 res_conn = get(conn, "#{base_uri}?local=#{local}")
506 assert json_response(res_conn, :unauthorized) == error_response
509 ensure_authenticated_access(base_uri)
512 test "with `%{local: true, federated: true}`, returns 403 for unauthenticated users", %{
515 error_response: error_response
517 clear_config([:restrict_unauthenticated, :timelines, :local], true)
518 clear_config([:restrict_unauthenticated, :timelines, :federated], true)
520 for local <- [true, false] do
521 res_conn = get(conn, "#{base_uri}?local=#{local}")
523 assert json_response(res_conn, :unauthorized) == error_response
526 ensure_authenticated_access(base_uri)
529 test "with `%{local: false, federated: true}`, forbids unauthenticated access to federated timeline",
530 %{conn: conn, base_uri: base_uri, error_response: error_response} do
531 clear_config([:restrict_unauthenticated, :timelines, :local], false)
532 clear_config([:restrict_unauthenticated, :timelines, :federated], true)
534 res_conn = get(conn, "#{base_uri}?local=true")
535 assert length(json_response(res_conn, 200)) == 1
537 res_conn = get(conn, "#{base_uri}?local=false")
538 assert json_response(res_conn, :unauthorized) == error_response
540 ensure_authenticated_access(base_uri)
543 test "with `%{local: true, federated: false}`, forbids unauthenticated access to public timeline" <>
544 "(but not to local public activities which are delivered as part of federated timeline)",
545 %{conn: conn, base_uri: base_uri, error_response: error_response} do
546 clear_config([:restrict_unauthenticated, :timelines, :local], true)
547 clear_config([:restrict_unauthenticated, :timelines, :federated], false)
549 res_conn = get(conn, "#{base_uri}?local=true")
550 assert json_response(res_conn, :unauthorized) == error_response
552 # Note: local activities get delivered as part of federated timeline
553 res_conn = get(conn, "#{base_uri}?local=false")
554 assert length(json_response(res_conn, 200)) == 2
556 ensure_authenticated_access(base_uri)