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")
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=true"
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(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(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(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(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(conn, :ok)) == 2
141 conn = get(build_conn(), "/api/v1/timelines/public", %{"local" => "True"})
143 assert [%{"content" => "test"}] = json_response(conn, :ok)
145 conn = get(build_conn(), "/api/v1/timelines/public", %{"local" => "1"})
147 assert [%{"content" => "test"}] = json_response(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(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(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(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(res_conn, 200)) == 1
196 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "false"})
197 assert length(json_response(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(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(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(res_conn, 200)) == 1
223 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "false"})
224 assert length(json_response(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(res_conn, 200)) == 1
237 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "false"})
239 assert json_response(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(res_conn, 200)) == 1
250 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "false"})
251 assert length(json_response(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 [status] = json_response(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(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(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(res_conn, :ok)
315 assert length(statuses) == 20
318 get(conn_user_two, "api/v1/timelines/direct", %{max_id: List.last(statuses)["id"]})
320 [status] = json_response(res_conn, :ok)
322 assert status["url"] != direct.data["id"]
325 test "doesn't include DMs from blocked users" do
326 %{user: blocker, conn: conn} = oauth_access(["read:statuses"])
327 blocked = insert(:user)
328 other_user = insert(:user)
329 {:ok, _user_relationship} = User.block(blocker, blocked)
331 {:ok, _blocked_direct} =
332 CommonAPI.post(blocked, %{
333 status: "Hi @#{blocker.nickname}!",
338 CommonAPI.post(other_user, %{
339 status: "Hi @#{blocker.nickname}!",
343 res_conn = get(conn, "api/v1/timelines/direct")
345 [status] = json_response(res_conn, :ok)
346 assert status["id"] == direct.id
351 setup do: oauth_access(["read:lists"])
353 test "list timeline", %{user: user, conn: conn} do
354 other_user = insert(:user)
355 {:ok, _activity_one} = CommonAPI.post(user, %{status: "Marisa is cute."})
356 {:ok, activity_two} = CommonAPI.post(other_user, %{status: "Marisa is cute."})
357 {:ok, list} = Pleroma.List.create("name", user)
358 {:ok, list} = Pleroma.List.follow(list, other_user)
360 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
362 assert [%{"id" => id}] = json_response(conn, :ok)
364 assert id == to_string(activity_two.id)
367 test "list timeline does not leak non-public statuses for unfollowed users", %{
371 other_user = insert(:user)
372 {:ok, activity_one} = CommonAPI.post(other_user, %{status: "Marisa is cute."})
374 {:ok, _activity_two} =
375 CommonAPI.post(other_user, %{
376 status: "Marisa is cute.",
377 visibility: "private"
380 {:ok, list} = Pleroma.List.create("name", user)
381 {:ok, list} = Pleroma.List.follow(list, other_user)
383 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
385 assert [%{"id" => id}] = json_response(conn, :ok)
387 assert id == to_string(activity_one.id)
391 describe "hashtag" do
392 setup do: oauth_access(["n/a"])
394 @tag capture_log: true
395 test "hashtag timeline", %{conn: conn} do
396 following = insert(:user)
398 {:ok, activity} = CommonAPI.post(following, %{status: "test #2hu"})
400 nconn = get(conn, "/api/v1/timelines/tag/2hu")
402 assert [%{"id" => id}] = json_response(nconn, :ok)
404 assert id == to_string(activity.id)
406 # works for different capitalization too
407 nconn = get(conn, "/api/v1/timelines/tag/2HU")
409 assert [%{"id" => id}] = json_response(nconn, :ok)
411 assert id == to_string(activity.id)
414 test "multi-hashtag timeline", %{conn: conn} do
417 {:ok, activity_test} = CommonAPI.post(user, %{status: "#test"})
418 {:ok, activity_test1} = CommonAPI.post(user, %{status: "#test #test1"})
419 {:ok, activity_none} = CommonAPI.post(user, %{status: "#test #none"})
421 any_test = get(conn, "/api/v1/timelines/tag/test", %{"any" => ["test1"]})
423 [status_none, status_test1, status_test] = json_response(any_test, :ok)
425 assert to_string(activity_test.id) == status_test["id"]
426 assert to_string(activity_test1.id) == status_test1["id"]
427 assert to_string(activity_none.id) == status_none["id"]
430 get(conn, "/api/v1/timelines/tag/test", %{"all" => ["test1"], "none" => ["none"]})
432 assert [status_test1] == json_response(restricted_test, :ok)
434 all_test = get(conn, "/api/v1/timelines/tag/test", %{"all" => ["none"]})
436 assert [status_none] == json_response(all_test, :ok)