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 render account/pleroma/relationship if this is disabled by default", %{
27 clear_config([:extensions, :output_relationships_in_statuses_by_default], false)
29 other_user = insert(:user)
31 {:ok, _} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
35 |> assign(:user, user)
36 |> get("/api/v1/timelines/home")
37 |> json_response_and_validate_schema(200)
39 assert Enum.all?(response, fn n ->
40 get_in(n, ["account", "pleroma", "relationship"]) == %{}
44 test "the home timeline", %{user: user, conn: conn} do
45 uri = "/api/v1/timelines/home?with_relationships=1"
47 following = insert(:user, nickname: "followed")
48 third_user = insert(:user, nickname: "repeated")
50 {:ok, _activity} = CommonAPI.post(following, %{status: "post"})
51 {:ok, activity} = CommonAPI.post(third_user, %{status: "repeated post"})
52 {:ok, _, _} = CommonAPI.repeat(activity.id, following)
54 ret_conn = get(conn, uri)
56 assert Enum.empty?(json_response_and_validate_schema(ret_conn, :ok))
58 {:ok, _user} = User.follow(user, following)
60 ret_conn = get(conn, uri)
65 "content" => "repeated post",
68 "relationship" => %{"following" => false, "followed_by" => false}
72 "account" => %{"pleroma" => %{"relationship" => %{"following" => true}}}
78 "pleroma" => %{"relationship" => %{"following" => true}}
81 ] = json_response_and_validate_schema(ret_conn, :ok)
83 {:ok, _user} = User.follow(third_user, user)
85 ret_conn = get(conn, uri)
90 "content" => "repeated post",
94 "relationship" => %{"following" => false, "followed_by" => true}
98 "account" => %{"pleroma" => %{"relationship" => %{"following" => true}}}
103 "acct" => "followed",
104 "pleroma" => %{"relationship" => %{"following" => true}}
107 ] = json_response_and_validate_schema(ret_conn, :ok)
110 test "the home timeline when the direct messages are excluded", %{user: user, conn: conn} do
111 {:ok, public_activity} = CommonAPI.post(user, %{status: ".", visibility: "public"})
112 {:ok, direct_activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
114 {:ok, unlisted_activity} = CommonAPI.post(user, %{status: ".", visibility: "unlisted"})
116 {:ok, private_activity} = CommonAPI.post(user, %{status: ".", visibility: "private"})
118 conn = get(conn, "/api/v1/timelines/home?exclude_visibilities[]=direct")
120 assert status_ids = json_response_and_validate_schema(conn, :ok) |> Enum.map(& &1["id"])
121 assert public_activity.id in status_ids
122 assert unlisted_activity.id in status_ids
123 assert private_activity.id in status_ids
124 refute direct_activity.id in status_ids
129 @tag capture_log: true
130 test "the public timeline", %{conn: conn} do
131 following = insert(:user)
133 {:ok, _activity} = CommonAPI.post(following, %{status: "test"})
135 _activity = insert(:note_activity, local: false)
137 conn = get(conn, "/api/v1/timelines/public?local=False")
139 assert length(json_response_and_validate_schema(conn, :ok)) == 2
141 conn = get(build_conn(), "/api/v1/timelines/public?local=True")
143 assert [%{"content" => "test"}] = json_response_and_validate_schema(conn, :ok)
145 conn = get(build_conn(), "/api/v1/timelines/public?local=1")
147 assert [%{"content" => "test"}] = json_response_and_validate_schema(conn, :ok)
150 test "the public timeline includes only public statuses for an authenticated user" do
151 %{user: user, conn: conn} = oauth_access(["read:statuses"])
153 {:ok, _activity} = CommonAPI.post(user, %{status: "test"})
154 {:ok, _activity} = CommonAPI.post(user, %{status: "test", visibility: "private"})
155 {:ok, _activity} = CommonAPI.post(user, %{status: "test", visibility: "unlisted"})
156 {:ok, _activity} = CommonAPI.post(user, %{status: "test", visibility: "direct"})
158 res_conn = get(conn, "/api/v1/timelines/public")
159 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
163 defp local_and_remote_activities do
164 insert(:note_activity)
165 insert(:note_activity, local: false)
169 describe "public with restrict unauthenticated timeline for local and federated timelines" do
170 setup do: local_and_remote_activities()
172 setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
174 setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
176 test "if user is unauthenticated", %{conn: conn} do
177 res_conn = get(conn, "/api/v1/timelines/public?local=true")
179 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
180 "error" => "authorization required for timeline view"
183 res_conn = get(conn, "/api/v1/timelines/public?local=false")
185 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
186 "error" => "authorization required for timeline view"
190 test "if user is authenticated" do
191 %{conn: conn} = oauth_access(["read:statuses"])
193 res_conn = get(conn, "/api/v1/timelines/public?local=true")
194 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
196 res_conn = get(conn, "/api/v1/timelines/public?local=false")
197 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
201 describe "public with restrict unauthenticated timeline for local" do
202 setup do: local_and_remote_activities()
204 setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
206 test "if user is unauthenticated", %{conn: conn} do
207 res_conn = get(conn, "/api/v1/timelines/public?local=true")
209 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
210 "error" => "authorization required for timeline view"
213 res_conn = get(conn, "/api/v1/timelines/public?local=false")
214 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
217 test "if user is authenticated", %{conn: _conn} do
218 %{conn: conn} = oauth_access(["read:statuses"])
220 res_conn = get(conn, "/api/v1/timelines/public?local=true")
221 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
223 res_conn = get(conn, "/api/v1/timelines/public?local=false")
224 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
228 describe "public with restrict unauthenticated timeline for remote" do
229 setup do: local_and_remote_activities()
231 setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
233 test "if user is unauthenticated", %{conn: conn} do
234 res_conn = get(conn, "/api/v1/timelines/public?local=true")
235 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
237 res_conn = get(conn, "/api/v1/timelines/public?local=false")
239 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
240 "error" => "authorization required for timeline view"
244 test "if user is authenticated", %{conn: _conn} do
245 %{conn: conn} = oauth_access(["read:statuses"])
247 res_conn = get(conn, "/api/v1/timelines/public?local=true")
248 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
250 res_conn = get(conn, "/api/v1/timelines/public?local=false")
251 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
256 test "direct timeline", %{conn: conn} do
257 user_one = insert(:user)
258 user_two = insert(:user)
260 {:ok, user_two} = User.follow(user_two, user_one)
263 CommonAPI.post(user_one, %{
264 status: "Hi @#{user_two.nickname}!",
268 {:ok, _follower_only} =
269 CommonAPI.post(user_one, %{
270 status: "Hi @#{user_two.nickname}!",
271 visibility: "private"
276 |> assign(:user, user_two)
277 |> assign(:token, insert(:oauth_token, user: user_two, scopes: ["read:statuses"]))
279 # Only direct should be visible here
280 res_conn = get(conn_user_two, "api/v1/timelines/direct")
282 assert [status] = json_response_and_validate_schema(res_conn, :ok)
284 assert %{"visibility" => "direct"} = status
285 assert status["url"] != direct.data["id"]
287 # User should be able to see their own direct message
290 |> assign(:user, user_one)
291 |> assign(:token, insert(:oauth_token, user: user_one, scopes: ["read:statuses"]))
292 |> get("api/v1/timelines/direct")
294 [status] = json_response_and_validate_schema(res_conn, :ok)
296 assert %{"visibility" => "direct"} = status
298 # Both should be visible here
299 res_conn = get(conn_user_two, "api/v1/timelines/home")
301 [_s1, _s2] = json_response_and_validate_schema(res_conn, :ok)
304 Enum.each(1..20, fn _ ->
306 CommonAPI.post(user_one, %{
307 status: "Hi @#{user_two.nickname}!",
312 res_conn = get(conn_user_two, "api/v1/timelines/direct")
314 statuses = json_response_and_validate_schema(res_conn, :ok)
315 assert length(statuses) == 20
317 max_id = List.last(statuses)["id"]
319 res_conn = get(conn_user_two, "api/v1/timelines/direct?max_id=#{max_id}")
321 assert [status] = json_response_and_validate_schema(res_conn, :ok)
323 assert status["url"] != direct.data["id"]
326 test "doesn't include DMs from blocked users" do
327 %{user: blocker, conn: conn} = oauth_access(["read:statuses"])
328 blocked = insert(:user)
329 other_user = insert(:user)
330 {:ok, _user_relationship} = User.block(blocker, blocked)
332 {:ok, _blocked_direct} =
333 CommonAPI.post(blocked, %{
334 status: "Hi @#{blocker.nickname}!",
339 CommonAPI.post(other_user, %{
340 status: "Hi @#{blocker.nickname}!",
344 res_conn = get(conn, "api/v1/timelines/direct")
346 [status] = json_response_and_validate_schema(res_conn, :ok)
347 assert status["id"] == direct.id
352 setup do: oauth_access(["read:lists"])
354 test "list timeline", %{user: user, conn: conn} do
355 other_user = insert(:user)
356 {:ok, _activity_one} = CommonAPI.post(user, %{status: "Marisa is cute."})
357 {:ok, activity_two} = CommonAPI.post(other_user, %{status: "Marisa is cute."})
358 {:ok, list} = Pleroma.List.create("name", user)
359 {:ok, list} = Pleroma.List.follow(list, other_user)
361 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
363 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
365 assert id == to_string(activity_two.id)
368 test "list timeline does not leak non-public statuses for unfollowed users", %{
372 other_user = insert(:user)
373 {:ok, activity_one} = CommonAPI.post(other_user, %{status: "Marisa is cute."})
375 {:ok, _activity_two} =
376 CommonAPI.post(other_user, %{
377 status: "Marisa is cute.",
378 visibility: "private"
381 {:ok, list} = Pleroma.List.create("name", user)
382 {:ok, list} = Pleroma.List.follow(list, other_user)
384 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
386 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
388 assert id == to_string(activity_one.id)
392 describe "hashtag" do
393 setup do: oauth_access(["n/a"])
395 @tag capture_log: true
396 test "hashtag timeline", %{conn: conn} do
397 following = insert(:user)
399 {:ok, activity} = CommonAPI.post(following, %{status: "test #2hu"})
401 nconn = get(conn, "/api/v1/timelines/tag/2hu")
403 assert [%{"id" => id}] = json_response_and_validate_schema(nconn, :ok)
405 assert id == to_string(activity.id)
407 # works for different capitalization too
408 nconn = get(conn, "/api/v1/timelines/tag/2HU")
410 assert [%{"id" => id}] = json_response_and_validate_schema(nconn, :ok)
412 assert id == to_string(activity.id)
415 test "multi-hashtag timeline", %{conn: conn} do
418 {:ok, activity_test} = CommonAPI.post(user, %{status: "#test"})
419 {:ok, activity_test1} = CommonAPI.post(user, %{status: "#test #test1"})
420 {:ok, activity_none} = CommonAPI.post(user, %{status: "#test #none"})
422 any_test = get(conn, "/api/v1/timelines/tag/test?any[]=test1")
424 [status_none, status_test1, status_test] = json_response_and_validate_schema(any_test, :ok)
426 assert to_string(activity_test.id) == status_test["id"]
427 assert to_string(activity_test1.id) == status_test1["id"]
428 assert to_string(activity_none.id) == status_none["id"]
430 restricted_test = get(conn, "/api/v1/timelines/tag/test?all[]=test1&none[]=none")
432 assert [status_test1] == json_response_and_validate_schema(restricted_test, :ok)
434 all_test = get(conn, "/api/v1/timelines/tag/test?all[]=none")
436 assert [status_none] == json_response_and_validate_schema(all_test, :ok)