Merge remote-tracking branch 'upstream/develop' into restrict-origin
[akkoma] / test / web / mastodon_api / controllers / timeline_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
6 use Pleroma.Web.ConnCase
7
8 import Pleroma.Factory
9 import Tesla.Mock
10
11 alias Pleroma.Config
12 alias Pleroma.User
13 alias Pleroma.Web.CommonAPI
14
15 setup do
16 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
17 :ok
18 end
19
20 describe "home" do
21 setup do: oauth_access(["read:statuses"])
22
23 test "does NOT embed account/pleroma/relationship in statuses", %{
24 user: user,
25 conn: conn
26 } do
27 other_user = insert(:user)
28
29 {:ok, _} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
30
31 response =
32 conn
33 |> assign(:user, user)
34 |> get("/api/v1/timelines/home")
35 |> json_response_and_validate_schema(200)
36
37 assert Enum.all?(response, fn n ->
38 get_in(n, ["account", "pleroma", "relationship"]) == %{}
39 end)
40 end
41
42 test "the home timeline when the direct messages are excluded", %{user: user, conn: conn} do
43 {:ok, public_activity} = CommonAPI.post(user, %{status: ".", visibility: "public"})
44 {:ok, direct_activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
45
46 {:ok, unlisted_activity} = CommonAPI.post(user, %{status: ".", visibility: "unlisted"})
47
48 {:ok, private_activity} = CommonAPI.post(user, %{status: ".", visibility: "private"})
49
50 conn = get(conn, "/api/v1/timelines/home?exclude_visibilities[]=direct")
51
52 assert status_ids = json_response_and_validate_schema(conn, :ok) |> Enum.map(& &1["id"])
53 assert public_activity.id in status_ids
54 assert unlisted_activity.id in status_ids
55 assert private_activity.id in status_ids
56 refute direct_activity.id in status_ids
57 end
58 end
59
60 describe "public" do
61 @tag capture_log: true
62 test "the public timeline", %{conn: conn} do
63 user = insert(:user)
64
65 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
66
67 _activity = insert(:note_activity, local: false)
68
69 conn = get(conn, "/api/v1/timelines/public?local=False")
70
71 assert length(json_response_and_validate_schema(conn, :ok)) == 2
72
73 conn = get(build_conn(), "/api/v1/timelines/public?local=True")
74
75 assert [%{"content" => "test"}] = json_response_and_validate_schema(conn, :ok)
76
77 conn = get(build_conn(), "/api/v1/timelines/public?local=1")
78
79 assert [%{"content" => "test"}] = json_response_and_validate_schema(conn, :ok)
80
81 # does not contain repeats
82 {:ok, _} = CommonAPI.repeat(activity.id, user)
83
84 conn = get(build_conn(), "/api/v1/timelines/public?local=true")
85
86 assert [_] = json_response_and_validate_schema(conn, :ok)
87 end
88
89 test "the public timeline includes only public statuses for an authenticated user" do
90 %{user: user, conn: conn} = oauth_access(["read:statuses"])
91
92 {:ok, _activity} = CommonAPI.post(user, %{status: "test"})
93 {:ok, _activity} = CommonAPI.post(user, %{status: "test", visibility: "private"})
94 {:ok, _activity} = CommonAPI.post(user, %{status: "test", visibility: "unlisted"})
95 {:ok, _activity} = CommonAPI.post(user, %{status: "test", visibility: "direct"})
96
97 res_conn = get(conn, "/api/v1/timelines/public")
98 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
99 end
100
101 test "doesn't return replies if follower is posting with blocked user" do
102 %{conn: conn, user: blocker} = oauth_access(["read:statuses"])
103 [blockee, friend] = insert_list(2, :user)
104 {:ok, blocker} = User.follow(blocker, friend)
105 {:ok, _} = User.block(blocker, blockee)
106
107 conn = assign(conn, :user, blocker)
108
109 {:ok, %{id: activity_id} = activity} = CommonAPI.post(friend, %{status: "hey!"})
110
111 {:ok, reply_from_blockee} =
112 CommonAPI.post(blockee, %{status: "heya", in_reply_to_status_id: activity})
113
114 {:ok, _reply_from_friend} =
115 CommonAPI.post(friend, %{status: "status", in_reply_to_status_id: reply_from_blockee})
116
117 # Still shows replies from yourself
118 {:ok, %{id: reply_from_me}} =
119 CommonAPI.post(blocker, %{status: "status", in_reply_to_status_id: reply_from_blockee})
120
121 response =
122 get(conn, "/api/v1/timelines/public")
123 |> json_response_and_validate_schema(200)
124
125 assert length(response) == 2
126 [%{"id" => ^reply_from_me}, %{"id" => ^activity_id}] = response
127 end
128
129 test "doesn't return replies if follow is posting with users from blocked domain" do
130 %{conn: conn, user: blocker} = oauth_access(["read:statuses"])
131 friend = insert(:user)
132 blockee = insert(:user, ap_id: "https://example.com/users/blocked")
133 {:ok, blocker} = User.follow(blocker, friend)
134 {:ok, blocker} = User.block_domain(blocker, "example.com")
135
136 conn = assign(conn, :user, blocker)
137
138 {:ok, %{id: activity_id} = activity} = CommonAPI.post(friend, %{status: "hey!"})
139
140 {:ok, reply_from_blockee} =
141 CommonAPI.post(blockee, %{status: "heya", in_reply_to_status_id: activity})
142
143 {:ok, _reply_from_friend} =
144 CommonAPI.post(friend, %{status: "status", in_reply_to_status_id: reply_from_blockee})
145
146 res_conn = get(conn, "/api/v1/timelines/public")
147
148 activities = json_response_and_validate_schema(res_conn, 200)
149 [%{"id" => ^activity_id}] = activities
150 end
151
152 test "can be filtered by instance", %{conn: conn} do
153 user = insert(:user, ap_id: "https://lain.com/users/lain")
154 insert(:note_activity, local: false)
155 insert(:note_activity, local: false)
156
157 {:ok, _} = CommonAPI.post(user, %{status: "test"})
158
159 conn = get(conn, "/api/v1/timelines/public?instance=lain.com")
160
161 assert length(json_response_and_validate_schema(conn, :ok)) == 1
162 end
163 end
164
165 defp local_and_remote_activities do
166 insert(:note_activity)
167 insert(:note_activity, local: false)
168 :ok
169 end
170
171 describe "public with restrict unauthenticated timeline for local and federated timelines" do
172 setup do: local_and_remote_activities()
173
174 setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
175
176 setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
177
178 test "if user is unauthenticated", %{conn: conn} do
179 res_conn = get(conn, "/api/v1/timelines/public?local=true")
180
181 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
182 "error" => "authorization required for timeline view"
183 }
184
185 res_conn = get(conn, "/api/v1/timelines/public?local=false")
186
187 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
188 "error" => "authorization required for timeline view"
189 }
190 end
191
192 test "if user is authenticated" do
193 %{conn: conn} = oauth_access(["read:statuses"])
194
195 res_conn = get(conn, "/api/v1/timelines/public?local=true")
196 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
197
198 res_conn = get(conn, "/api/v1/timelines/public?local=false")
199 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
200 end
201 end
202
203 describe "public with restrict unauthenticated timeline for local" do
204 setup do: local_and_remote_activities()
205
206 setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
207
208 test "if user is unauthenticated", %{conn: conn} do
209 res_conn = get(conn, "/api/v1/timelines/public?local=true")
210
211 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
212 "error" => "authorization required for timeline view"
213 }
214
215 res_conn = get(conn, "/api/v1/timelines/public?local=false")
216 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
217 end
218
219 test "if user is authenticated", %{conn: _conn} do
220 %{conn: conn} = oauth_access(["read:statuses"])
221
222 res_conn = get(conn, "/api/v1/timelines/public?local=true")
223 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
224
225 res_conn = get(conn, "/api/v1/timelines/public?local=false")
226 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
227 end
228 end
229
230 describe "public with restrict unauthenticated timeline for remote" do
231 setup do: local_and_remote_activities()
232
233 setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
234
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_and_validate_schema(res_conn, 200)) == 1
238
239 res_conn = get(conn, "/api/v1/timelines/public?local=false")
240
241 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
242 "error" => "authorization required for timeline view"
243 }
244 end
245
246 test "if user is authenticated", %{conn: _conn} do
247 %{conn: conn} = oauth_access(["read:statuses"])
248
249 res_conn = get(conn, "/api/v1/timelines/public?local=true")
250 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
251
252 res_conn = get(conn, "/api/v1/timelines/public?local=false")
253 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
254 end
255 end
256
257 describe "direct" do
258 test "direct timeline", %{conn: conn} do
259 user_one = insert(:user)
260 user_two = insert(:user)
261
262 {:ok, user_two} = User.follow(user_two, user_one)
263
264 {:ok, direct} =
265 CommonAPI.post(user_one, %{
266 status: "Hi @#{user_two.nickname}!",
267 visibility: "direct"
268 })
269
270 {:ok, _follower_only} =
271 CommonAPI.post(user_one, %{
272 status: "Hi @#{user_two.nickname}!",
273 visibility: "private"
274 })
275
276 conn_user_two =
277 conn
278 |> assign(:user, user_two)
279 |> assign(:token, insert(:oauth_token, user: user_two, scopes: ["read:statuses"]))
280
281 # Only direct should be visible here
282 res_conn = get(conn_user_two, "api/v1/timelines/direct")
283
284 assert [status] = json_response_and_validate_schema(res_conn, :ok)
285
286 assert %{"visibility" => "direct"} = status
287 assert status["url"] != direct.data["id"]
288
289 # User should be able to see their own direct message
290 res_conn =
291 build_conn()
292 |> assign(:user, user_one)
293 |> assign(:token, insert(:oauth_token, user: user_one, scopes: ["read:statuses"]))
294 |> get("api/v1/timelines/direct")
295
296 [status] = json_response_and_validate_schema(res_conn, :ok)
297
298 assert %{"visibility" => "direct"} = status
299
300 # Both should be visible here
301 res_conn = get(conn_user_two, "api/v1/timelines/home")
302
303 [_s1, _s2] = json_response_and_validate_schema(res_conn, :ok)
304
305 # Test pagination
306 Enum.each(1..20, fn _ ->
307 {:ok, _} =
308 CommonAPI.post(user_one, %{
309 status: "Hi @#{user_two.nickname}!",
310 visibility: "direct"
311 })
312 end)
313
314 res_conn = get(conn_user_two, "api/v1/timelines/direct")
315
316 statuses = json_response_and_validate_schema(res_conn, :ok)
317 assert length(statuses) == 20
318
319 max_id = List.last(statuses)["id"]
320
321 res_conn = get(conn_user_two, "api/v1/timelines/direct?max_id=#{max_id}")
322
323 assert [status] = json_response_and_validate_schema(res_conn, :ok)
324
325 assert status["url"] != direct.data["id"]
326 end
327
328 test "doesn't include DMs from blocked users" do
329 %{user: blocker, conn: conn} = oauth_access(["read:statuses"])
330 blocked = insert(:user)
331 other_user = insert(:user)
332 {:ok, _user_relationship} = User.block(blocker, blocked)
333
334 {:ok, _blocked_direct} =
335 CommonAPI.post(blocked, %{
336 status: "Hi @#{blocker.nickname}!",
337 visibility: "direct"
338 })
339
340 {:ok, direct} =
341 CommonAPI.post(other_user, %{
342 status: "Hi @#{blocker.nickname}!",
343 visibility: "direct"
344 })
345
346 res_conn = get(conn, "api/v1/timelines/direct")
347
348 [status] = json_response_and_validate_schema(res_conn, :ok)
349 assert status["id"] == direct.id
350 end
351 end
352
353 describe "list" do
354 setup do: oauth_access(["read:lists"])
355
356 test "does not contain retoots", %{user: user, conn: conn} do
357 other_user = insert(:user)
358 {:ok, activity_one} = CommonAPI.post(user, %{status: "Marisa is cute."})
359 {:ok, activity_two} = CommonAPI.post(other_user, %{status: "Marisa is stupid."})
360 {:ok, _} = CommonAPI.repeat(activity_one.id, other_user)
361
362 {:ok, list} = Pleroma.List.create("name", user)
363 {:ok, list} = Pleroma.List.follow(list, other_user)
364
365 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
366
367 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
368
369 assert id == to_string(activity_two.id)
370 end
371
372 test "works with pagination", %{user: user, conn: conn} do
373 other_user = insert(:user)
374 {:ok, list} = Pleroma.List.create("name", user)
375 {:ok, list} = Pleroma.List.follow(list, other_user)
376
377 Enum.each(1..30, fn i ->
378 CommonAPI.post(other_user, %{status: "post number #{i}"})
379 end)
380
381 res =
382 get(conn, "/api/v1/timelines/list/#{list.id}?limit=1")
383 |> json_response_and_validate_schema(:ok)
384
385 assert length(res) == 1
386
387 [first] = res
388
389 res =
390 get(conn, "/api/v1/timelines/list/#{list.id}?max_id=#{first["id"]}&limit=30")
391 |> json_response_and_validate_schema(:ok)
392
393 assert length(res) == 29
394 end
395
396 test "list timeline", %{user: user, conn: conn} do
397 other_user = insert(:user)
398 {:ok, _activity_one} = CommonAPI.post(user, %{status: "Marisa is cute."})
399 {:ok, activity_two} = CommonAPI.post(other_user, %{status: "Marisa is cute."})
400 {:ok, list} = Pleroma.List.create("name", user)
401 {:ok, list} = Pleroma.List.follow(list, other_user)
402
403 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
404
405 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
406
407 assert id == to_string(activity_two.id)
408 end
409
410 test "list timeline does not leak non-public statuses for unfollowed users", %{
411 user: user,
412 conn: conn
413 } do
414 other_user = insert(:user)
415 {:ok, activity_one} = CommonAPI.post(other_user, %{status: "Marisa is cute."})
416
417 {:ok, _activity_two} =
418 CommonAPI.post(other_user, %{
419 status: "Marisa is cute.",
420 visibility: "private"
421 })
422
423 {:ok, list} = Pleroma.List.create("name", user)
424 {:ok, list} = Pleroma.List.follow(list, other_user)
425
426 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
427
428 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
429
430 assert id == to_string(activity_one.id)
431 end
432 end
433
434 describe "hashtag" do
435 setup do: oauth_access(["n/a"])
436
437 @tag capture_log: true
438 test "hashtag timeline", %{conn: conn} do
439 following = insert(:user)
440
441 {:ok, activity} = CommonAPI.post(following, %{status: "test #2hu"})
442
443 nconn = get(conn, "/api/v1/timelines/tag/2hu")
444
445 assert [%{"id" => id}] = json_response_and_validate_schema(nconn, :ok)
446
447 assert id == to_string(activity.id)
448
449 # works for different capitalization too
450 nconn = get(conn, "/api/v1/timelines/tag/2HU")
451
452 assert [%{"id" => id}] = json_response_and_validate_schema(nconn, :ok)
453
454 assert id == to_string(activity.id)
455 end
456
457 test "multi-hashtag timeline", %{conn: conn} do
458 user = insert(:user)
459
460 {:ok, activity_test} = CommonAPI.post(user, %{status: "#test"})
461 {:ok, activity_test1} = CommonAPI.post(user, %{status: "#test #test1"})
462 {:ok, activity_none} = CommonAPI.post(user, %{status: "#test #none"})
463
464 any_test = get(conn, "/api/v1/timelines/tag/test?any[]=test1")
465
466 [status_none, status_test1, status_test] = json_response_and_validate_schema(any_test, :ok)
467
468 assert to_string(activity_test.id) == status_test["id"]
469 assert to_string(activity_test1.id) == status_test1["id"]
470 assert to_string(activity_none.id) == status_none["id"]
471
472 restricted_test = get(conn, "/api/v1/timelines/tag/test?all[]=test1&none[]=none")
473
474 assert [status_test1] == json_response_and_validate_schema(restricted_test, :ok)
475
476 all_test = get(conn, "/api/v1/timelines/tag/test?all[]=none")
477
478 assert [status_none] == json_response_and_validate_schema(all_test, :ok)
479 end
480 end
481
482 describe "hashtag timeline handling of :restrict_unauthenticated setting" do
483 setup do
484 user = insert(:user)
485 {:ok, activity1} = CommonAPI.post(user, %{status: "test #tag1"})
486 {:ok, _activity2} = CommonAPI.post(user, %{status: "test #tag1"})
487
488 activity1
489 |> Ecto.Changeset.change(%{local: false})
490 |> Pleroma.Repo.update()
491
492 base_uri = "/api/v1/timelines/tag/tag1"
493 error_response = %{"error" => "authorization required for timeline view"}
494
495 %{base_uri: base_uri, error_response: error_response}
496 end
497
498 defp ensure_authenticated_access(base_uri) do
499 %{conn: auth_conn} = oauth_access(["read:statuses"])
500
501 res_conn = get(auth_conn, "#{base_uri}?local=true")
502 assert length(json_response(res_conn, 200)) == 1
503
504 res_conn = get(auth_conn, "#{base_uri}?local=false")
505 assert length(json_response(res_conn, 200)) == 2
506 end
507
508 test "with default settings on private instances, returns 403 for unauthenticated users", %{
509 conn: conn,
510 base_uri: base_uri,
511 error_response: error_response
512 } do
513 clear_config([:instance, :public], false)
514 clear_config([:restrict_unauthenticated, :timelines])
515
516 for local <- [true, false] do
517 res_conn = get(conn, "#{base_uri}?local=#{local}")
518
519 assert json_response(res_conn, :unauthorized) == error_response
520 end
521
522 ensure_authenticated_access(base_uri)
523 end
524
525 test "with `%{local: true, federated: true}`, returns 403 for unauthenticated users", %{
526 conn: conn,
527 base_uri: base_uri,
528 error_response: error_response
529 } do
530 clear_config([:restrict_unauthenticated, :timelines, :local], true)
531 clear_config([:restrict_unauthenticated, :timelines, :federated], true)
532
533 for local <- [true, false] do
534 res_conn = get(conn, "#{base_uri}?local=#{local}")
535
536 assert json_response(res_conn, :unauthorized) == error_response
537 end
538
539 ensure_authenticated_access(base_uri)
540 end
541
542 test "with `%{local: false, federated: true}`, forbids unauthenticated access to federated timeline",
543 %{conn: conn, base_uri: base_uri, error_response: error_response} do
544 clear_config([:restrict_unauthenticated, :timelines, :local], false)
545 clear_config([:restrict_unauthenticated, :timelines, :federated], true)
546
547 res_conn = get(conn, "#{base_uri}?local=true")
548 assert length(json_response(res_conn, 200)) == 1
549
550 res_conn = get(conn, "#{base_uri}?local=false")
551 assert json_response(res_conn, :unauthorized) == error_response
552
553 ensure_authenticated_access(base_uri)
554 end
555
556 test "with `%{local: true, federated: false}`, forbids unauthenticated access to public timeline" <>
557 "(but not to local public activities which are delivered as part of federated timeline)",
558 %{conn: conn, base_uri: base_uri, error_response: error_response} do
559 clear_config([:restrict_unauthenticated, :timelines, :local], true)
560 clear_config([:restrict_unauthenticated, :timelines, :federated], false)
561
562 res_conn = get(conn, "#{base_uri}?local=true")
563 assert json_response(res_conn, :unauthorized) == error_response
564
565 # Note: local activities get delivered as part of federated timeline
566 res_conn = get(conn, "#{base_uri}?local=false")
567 assert length(json_response(res_conn, 200)) == 2
568
569 ensure_authenticated_access(base_uri)
570 end
571 end
572 end