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} =
115 CommonAPI.post(user, %{"status" => ".", "visibility" => "unlisted"})
117 {:ok, private_activity} =
118 CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
120 conn = get(conn, "/api/v1/timelines/home", %{"exclude_visibilities" => ["direct"]})
122 assert status_ids = json_response(conn, :ok) |> Enum.map(& &1["id"])
123 assert public_activity.id in status_ids
124 assert unlisted_activity.id in status_ids
125 assert private_activity.id in status_ids
126 refute direct_activity.id in status_ids
131 @tag capture_log: true
132 test "the public timeline", %{conn: conn} do
133 following = insert(:user)
135 {:ok, _activity} = CommonAPI.post(following, %{"status" => "test"})
137 _activity = insert(:note_activity, local: false)
139 conn = get(conn, "/api/v1/timelines/public", %{"local" => "False"})
141 assert length(json_response(conn, :ok)) == 2
143 conn = get(build_conn(), "/api/v1/timelines/public", %{"local" => "True"})
145 assert [%{"content" => "test"}] = json_response(conn, :ok)
147 conn = get(build_conn(), "/api/v1/timelines/public", %{"local" => "1"})
149 assert [%{"content" => "test"}] = json_response(conn, :ok)
152 test "the public timeline includes only public statuses for an authenticated user" do
153 %{user: user, conn: conn} = oauth_access(["read:statuses"])
155 {:ok, _activity} = CommonAPI.post(user, %{"status" => "test"})
156 {:ok, _activity} = CommonAPI.post(user, %{"status" => "test", "visibility" => "private"})
157 {:ok, _activity} = CommonAPI.post(user, %{"status" => "test", "visibility" => "unlisted"})
158 {:ok, _activity} = CommonAPI.post(user, %{"status" => "test", "visibility" => "direct"})
160 res_conn = get(conn, "/api/v1/timelines/public")
161 assert length(json_response(res_conn, 200)) == 1
165 defp local_and_remote_activities do
166 insert(:note_activity)
167 insert(:note_activity, local: false)
171 describe "public with restrict unauthenticated timeline for local and federated timelines" do
172 setup do: local_and_remote_activities()
174 setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
176 setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
178 test "if user is unauthenticated", %{conn: conn} do
179 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "true"})
181 assert json_response(res_conn, :unauthorized) == %{
182 "error" => "authorization required for timeline view"
185 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "false"})
187 assert json_response(res_conn, :unauthorized) == %{
188 "error" => "authorization required for timeline view"
192 test "if user is authenticated" do
193 %{conn: conn} = oauth_access(["read:statuses"])
195 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "true"})
196 assert length(json_response(res_conn, 200)) == 1
198 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "false"})
199 assert length(json_response(res_conn, 200)) == 2
203 describe "public with restrict unauthenticated timeline for local" do
204 setup do: local_and_remote_activities()
206 setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
208 test "if user is unauthenticated", %{conn: conn} do
209 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "true"})
211 assert json_response(res_conn, :unauthorized) == %{
212 "error" => "authorization required for timeline view"
215 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "false"})
216 assert length(json_response(res_conn, 200)) == 2
219 test "if user is authenticated", %{conn: _conn} do
220 %{conn: conn} = oauth_access(["read:statuses"])
222 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "true"})
223 assert length(json_response(res_conn, 200)) == 1
225 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "false"})
226 assert length(json_response(res_conn, 200)) == 2
230 describe "public with restrict unauthenticated timeline for remote" do
231 setup do: local_and_remote_activities()
233 setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
235 test "if user is unauthenticated", %{conn: conn} do
236 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "true"})
237 assert length(json_response(res_conn, 200)) == 1
239 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "false"})
241 assert json_response(res_conn, :unauthorized) == %{
242 "error" => "authorization required for timeline view"
246 test "if user is authenticated", %{conn: _conn} do
247 %{conn: conn} = oauth_access(["read:statuses"])
249 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "true"})
250 assert length(json_response(res_conn, 200)) == 1
252 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "false"})
253 assert length(json_response(res_conn, 200)) == 2
258 test "direct timeline", %{conn: conn} do
259 user_one = insert(:user)
260 user_two = insert(:user)
262 {:ok, user_two} = User.follow(user_two, user_one)
265 CommonAPI.post(user_one, %{
266 "status" => "Hi @#{user_two.nickname}!",
267 "visibility" => "direct"
270 {:ok, _follower_only} =
271 CommonAPI.post(user_one, %{
272 "status" => "Hi @#{user_two.nickname}!",
273 "visibility" => "private"
278 |> assign(:user, user_two)
279 |> assign(:token, insert(:oauth_token, user: user_two, scopes: ["read:statuses"]))
281 # Only direct should be visible here
282 res_conn = get(conn_user_two, "api/v1/timelines/direct")
284 [status] = json_response(res_conn, :ok)
286 assert %{"visibility" => "direct"} = status
287 assert status["url"] != direct.data["id"]
289 # User should be able to see their own direct message
292 |> assign(:user, user_one)
293 |> assign(:token, insert(:oauth_token, user: user_one, scopes: ["read:statuses"]))
294 |> get("api/v1/timelines/direct")
296 [status] = json_response(res_conn, :ok)
298 assert %{"visibility" => "direct"} = status
300 # Both should be visible here
301 res_conn = get(conn_user_two, "api/v1/timelines/home")
303 [_s1, _s2] = json_response(res_conn, :ok)
306 Enum.each(1..20, fn _ ->
308 CommonAPI.post(user_one, %{
309 "status" => "Hi @#{user_two.nickname}!",
310 "visibility" => "direct"
314 res_conn = get(conn_user_two, "api/v1/timelines/direct")
316 statuses = json_response(res_conn, :ok)
317 assert length(statuses) == 20
320 get(conn_user_two, "api/v1/timelines/direct", %{max_id: List.last(statuses)["id"]})
322 [status] = json_response(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}!",
336 "visibility" => "direct"
340 CommonAPI.post(other_user, %{
341 "status" => "Hi @#{blocker.nickname}!",
342 "visibility" => "direct"
345 res_conn = get(conn, "api/v1/timelines/direct")
347 [status] = json_response(res_conn, :ok)
348 assert status["id"] == direct.id
353 setup do: oauth_access(["read:lists"])
355 test "list timeline", %{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 cute."})
359 {:ok, list} = Pleroma.List.create("name", user)
360 {:ok, list} = Pleroma.List.follow(list, other_user)
362 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
364 assert [%{"id" => id}] = json_response(conn, :ok)
366 assert id == to_string(activity_two.id)
369 test "list timeline does not leak non-public statuses for unfollowed users", %{
373 other_user = insert(:user)
374 {:ok, activity_one} = CommonAPI.post(other_user, %{"status" => "Marisa is cute."})
376 {:ok, _activity_two} =
377 CommonAPI.post(other_user, %{
378 "status" => "Marisa is cute.",
379 "visibility" => "private"
382 {:ok, list} = Pleroma.List.create("name", user)
383 {:ok, list} = Pleroma.List.follow(list, other_user)
385 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
387 assert [%{"id" => id}] = json_response(conn, :ok)
389 assert id == to_string(activity_one.id)
393 describe "hashtag" do
394 setup do: oauth_access(["n/a"])
396 @tag capture_log: true
397 test "hashtag timeline", %{conn: conn} do
398 following = insert(:user)
400 {:ok, activity} = CommonAPI.post(following, %{"status" => "test #2hu"})
402 nconn = get(conn, "/api/v1/timelines/tag/2hu")
404 assert [%{"id" => id}] = json_response(nconn, :ok)
406 assert id == to_string(activity.id)
408 # works for different capitalization too
409 nconn = get(conn, "/api/v1/timelines/tag/2HU")
411 assert [%{"id" => id}] = json_response(nconn, :ok)
413 assert id == to_string(activity.id)
416 test "multi-hashtag timeline", %{conn: conn} do
419 {:ok, activity_test} = CommonAPI.post(user, %{"status" => "#test"})
420 {:ok, activity_test1} = CommonAPI.post(user, %{"status" => "#test #test1"})
421 {:ok, activity_none} = CommonAPI.post(user, %{"status" => "#test #none"})
423 any_test = get(conn, "/api/v1/timelines/tag/test", %{"any" => ["test1"]})
425 [status_none, status_test1, status_test] = json_response(any_test, :ok)
427 assert to_string(activity_test.id) == status_test["id"]
428 assert to_string(activity_test1.id) == status_test1["id"]
429 assert to_string(activity_none.id) == status_none["id"]
432 get(conn, "/api/v1/timelines/tag/test", %{"all" => ["test1"], "none" => ["none"]})
434 assert [status_test1] == json_response(restricted_test, :ok)
436 all_test = get(conn, "/api/v1/timelines/tag/test", %{"all" => ["none"]})
438 assert [status_none] == json_response(all_test, :ok)