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 # This one should not show up in the TL
55 {:ok, _activity} = CommonAPI.post_chat_message(third_user, user, ":gun:")
57 ret_conn = get(conn, uri)
59 assert Enum.empty?(json_response(ret_conn, :ok))
61 {:ok, _user} = User.follow(user, following)
63 ret_conn = get(conn, uri)
68 "content" => "repeated post",
71 "relationship" => %{"following" => false, "followed_by" => false}
75 "account" => %{"pleroma" => %{"relationship" => %{"following" => true}}}
81 "pleroma" => %{"relationship" => %{"following" => true}}
84 ] = json_response(ret_conn, :ok)
86 {:ok, _user} = User.follow(third_user, user)
88 ret_conn = get(conn, uri)
93 "content" => "repeated post",
97 "relationship" => %{"following" => false, "followed_by" => true}
101 "account" => %{"pleroma" => %{"relationship" => %{"following" => true}}}
106 "acct" => "followed",
107 "pleroma" => %{"relationship" => %{"following" => true}}
110 ] = json_response(ret_conn, :ok)
113 test "the home timeline when the direct messages are excluded", %{user: user, conn: conn} do
114 {:ok, public_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "public"})
115 {:ok, direct_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
117 {:ok, unlisted_activity} =
118 CommonAPI.post(user, %{"status" => ".", "visibility" => "unlisted"})
120 {:ok, private_activity} =
121 CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
123 conn = get(conn, "/api/v1/timelines/home", %{"exclude_visibilities" => ["direct"]})
125 assert status_ids = json_response(conn, :ok) |> Enum.map(& &1["id"])
126 assert public_activity.id in status_ids
127 assert unlisted_activity.id in status_ids
128 assert private_activity.id in status_ids
129 refute direct_activity.id in status_ids
134 @tag capture_log: true
135 test "the public timeline", %{conn: conn} do
136 following = insert(:user)
138 {:ok, _activity} = CommonAPI.post(following, %{"status" => "test"})
140 _activity = insert(:note_activity, local: false)
142 conn = get(conn, "/api/v1/timelines/public", %{"local" => "False"})
144 assert length(json_response(conn, :ok)) == 2
146 conn = get(build_conn(), "/api/v1/timelines/public", %{"local" => "True"})
148 assert [%{"content" => "test"}] = json_response(conn, :ok)
150 conn = get(build_conn(), "/api/v1/timelines/public", %{"local" => "1"})
152 assert [%{"content" => "test"}] = json_response(conn, :ok)
155 test "the public timeline includes only public statuses for an authenticated user" do
156 %{user: user, conn: conn} = oauth_access(["read:statuses"])
158 {:ok, _activity} = CommonAPI.post(user, %{"status" => "test"})
159 {:ok, _activity} = CommonAPI.post(user, %{"status" => "test", "visibility" => "private"})
160 {:ok, _activity} = CommonAPI.post(user, %{"status" => "test", "visibility" => "unlisted"})
161 {:ok, _activity} = CommonAPI.post(user, %{"status" => "test", "visibility" => "direct"})
163 res_conn = get(conn, "/api/v1/timelines/public")
164 assert length(json_response(res_conn, 200)) == 1
168 defp local_and_remote_activities do
169 insert(:note_activity)
170 insert(:note_activity, local: false)
174 describe "public with restrict unauthenticated timeline for local and federated timelines" do
175 setup do: local_and_remote_activities()
177 setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
179 setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
181 test "if user is unauthenticated", %{conn: conn} do
182 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "true"})
184 assert json_response(res_conn, :unauthorized) == %{
185 "error" => "authorization required for timeline view"
188 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "false"})
190 assert json_response(res_conn, :unauthorized) == %{
191 "error" => "authorization required for timeline view"
195 test "if user is authenticated" do
196 %{conn: conn} = oauth_access(["read:statuses"])
198 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "true"})
199 assert length(json_response(res_conn, 200)) == 1
201 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "false"})
202 assert length(json_response(res_conn, 200)) == 2
206 describe "public with restrict unauthenticated timeline for local" do
207 setup do: local_and_remote_activities()
209 setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
211 test "if user is unauthenticated", %{conn: conn} do
212 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "true"})
214 assert json_response(res_conn, :unauthorized) == %{
215 "error" => "authorization required for timeline view"
218 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "false"})
219 assert length(json_response(res_conn, 200)) == 2
222 test "if user is authenticated", %{conn: _conn} do
223 %{conn: conn} = oauth_access(["read:statuses"])
225 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "true"})
226 assert length(json_response(res_conn, 200)) == 1
228 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "false"})
229 assert length(json_response(res_conn, 200)) == 2
233 describe "public with restrict unauthenticated timeline for remote" do
234 setup do: local_and_remote_activities()
236 setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
238 test "if user is unauthenticated", %{conn: conn} do
239 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "true"})
240 assert length(json_response(res_conn, 200)) == 1
242 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "false"})
244 assert json_response(res_conn, :unauthorized) == %{
245 "error" => "authorization required for timeline view"
249 test "if user is authenticated", %{conn: _conn} do
250 %{conn: conn} = oauth_access(["read:statuses"])
252 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "true"})
253 assert length(json_response(res_conn, 200)) == 1
255 res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "false"})
256 assert length(json_response(res_conn, 200)) == 2
261 test "direct timeline", %{conn: conn} do
262 user_one = insert(:user)
263 user_two = insert(:user)
265 {:ok, user_two} = User.follow(user_two, user_one)
268 CommonAPI.post(user_one, %{
269 "status" => "Hi @#{user_two.nickname}!",
270 "visibility" => "direct"
273 {:ok, _follower_only} =
274 CommonAPI.post(user_one, %{
275 "status" => "Hi @#{user_two.nickname}!",
276 "visibility" => "private"
281 |> assign(:user, user_two)
282 |> assign(:token, insert(:oauth_token, user: user_two, scopes: ["read:statuses"]))
284 # Only direct should be visible here
285 res_conn = get(conn_user_two, "api/v1/timelines/direct")
287 [status] = json_response(res_conn, :ok)
289 assert %{"visibility" => "direct"} = status
290 assert status["url"] != direct.data["id"]
292 # User should be able to see their own direct message
295 |> assign(:user, user_one)
296 |> assign(:token, insert(:oauth_token, user: user_one, scopes: ["read:statuses"]))
297 |> get("api/v1/timelines/direct")
299 [status] = json_response(res_conn, :ok)
301 assert %{"visibility" => "direct"} = status
303 # Both should be visible here
304 res_conn = get(conn_user_two, "api/v1/timelines/home")
306 [_s1, _s2] = json_response(res_conn, :ok)
309 Enum.each(1..20, fn _ ->
311 CommonAPI.post(user_one, %{
312 "status" => "Hi @#{user_two.nickname}!",
313 "visibility" => "direct"
317 res_conn = get(conn_user_two, "api/v1/timelines/direct")
319 statuses = json_response(res_conn, :ok)
320 assert length(statuses) == 20
323 get(conn_user_two, "api/v1/timelines/direct", %{max_id: List.last(statuses)["id"]})
325 [status] = json_response(res_conn, :ok)
327 assert status["url"] != direct.data["id"]
330 test "doesn't include DMs from blocked users" do
331 %{user: blocker, conn: conn} = oauth_access(["read:statuses"])
332 blocked = insert(:user)
333 other_user = insert(:user)
334 {:ok, _user_relationship} = User.block(blocker, blocked)
336 {:ok, _blocked_direct} =
337 CommonAPI.post(blocked, %{
338 "status" => "Hi @#{blocker.nickname}!",
339 "visibility" => "direct"
343 CommonAPI.post(other_user, %{
344 "status" => "Hi @#{blocker.nickname}!",
345 "visibility" => "direct"
348 res_conn = get(conn, "api/v1/timelines/direct")
350 [status] = json_response(res_conn, :ok)
351 assert status["id"] == direct.id
356 setup do: oauth_access(["read:lists"])
358 test "list timeline", %{user: user, conn: conn} do
359 other_user = insert(:user)
360 {:ok, _activity_one} = CommonAPI.post(user, %{"status" => "Marisa is cute."})
361 {:ok, activity_two} = CommonAPI.post(other_user, %{"status" => "Marisa is cute."})
362 {:ok, list} = Pleroma.List.create("name", user)
363 {:ok, list} = Pleroma.List.follow(list, other_user)
365 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
367 assert [%{"id" => id}] = json_response(conn, :ok)
369 assert id == to_string(activity_two.id)
372 test "list timeline does not leak non-public statuses for unfollowed users", %{
376 other_user = insert(:user)
377 {:ok, activity_one} = CommonAPI.post(other_user, %{"status" => "Marisa is cute."})
379 {:ok, _activity_two} =
380 CommonAPI.post(other_user, %{
381 "status" => "Marisa is cute.",
382 "visibility" => "private"
385 {:ok, list} = Pleroma.List.create("name", user)
386 {:ok, list} = Pleroma.List.follow(list, other_user)
388 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
390 assert [%{"id" => id}] = json_response(conn, :ok)
392 assert id == to_string(activity_one.id)
396 describe "hashtag" do
397 setup do: oauth_access(["n/a"])
399 @tag capture_log: true
400 test "hashtag timeline", %{conn: conn} do
401 following = insert(:user)
403 {:ok, activity} = CommonAPI.post(following, %{"status" => "test #2hu"})
405 nconn = get(conn, "/api/v1/timelines/tag/2hu")
407 assert [%{"id" => id}] = json_response(nconn, :ok)
409 assert id == to_string(activity.id)
411 # works for different capitalization too
412 nconn = get(conn, "/api/v1/timelines/tag/2HU")
414 assert [%{"id" => id}] = json_response(nconn, :ok)
416 assert id == to_string(activity.id)
419 test "multi-hashtag timeline", %{conn: conn} do
422 {:ok, activity_test} = CommonAPI.post(user, %{"status" => "#test"})
423 {:ok, activity_test1} = CommonAPI.post(user, %{"status" => "#test #test1"})
424 {:ok, activity_none} = CommonAPI.post(user, %{"status" => "#test #none"})
426 any_test = get(conn, "/api/v1/timelines/tag/test", %{"any" => ["test1"]})
428 [status_none, status_test1, status_test] = json_response(any_test, :ok)
430 assert to_string(activity_test.id) == status_test["id"]
431 assert to_string(activity_test1.id) == status_test1["id"]
432 assert to_string(activity_none.id) == status_none["id"]
435 get(conn, "/api/v1/timelines/tag/test", %{"all" => ["test1"], "none" => ["none"]})
437 assert [status_test1] == json_response(restricted_test, :ok)
439 all_test = get(conn, "/api/v1/timelines/tag/test", %{"all" => ["none"]})
441 assert [status_none] == json_response(all_test, :ok)