AccountController: Fix muting / unmuting reblogs.
[akkoma] / test / web / mastodon_api / controllers / account_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.AccountControllerTest do
6 use Pleroma.Web.ConnCase
7
8 alias Pleroma.Config
9 alias Pleroma.Repo
10 alias Pleroma.User
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.ActivityPub.InternalFetchActor
13 alias Pleroma.Web.CommonAPI
14 alias Pleroma.Web.OAuth.Token
15
16 import Pleroma.Factory
17
18 describe "account fetching" do
19 setup do: clear_config([:instance, :limit_to_local_content])
20
21 test "works by id" do
22 %User{id: user_id} = insert(:user)
23
24 assert %{"id" => ^user_id} =
25 build_conn()
26 |> get("/api/v1/accounts/#{user_id}")
27 |> json_response_and_validate_schema(200)
28
29 assert %{"error" => "Can't find user"} =
30 build_conn()
31 |> get("/api/v1/accounts/-1")
32 |> json_response_and_validate_schema(404)
33 end
34
35 test "works by nickname" do
36 user = insert(:user)
37
38 assert %{"id" => user_id} =
39 build_conn()
40 |> get("/api/v1/accounts/#{user.nickname}")
41 |> json_response_and_validate_schema(200)
42 end
43
44 test "works by nickname for remote users" do
45 Config.put([:instance, :limit_to_local_content], false)
46
47 user = insert(:user, nickname: "user@example.com", local: false)
48
49 assert %{"id" => user_id} =
50 build_conn()
51 |> get("/api/v1/accounts/#{user.nickname}")
52 |> json_response_and_validate_schema(200)
53 end
54
55 test "respects limit_to_local_content == :all for remote user nicknames" do
56 Config.put([:instance, :limit_to_local_content], :all)
57
58 user = insert(:user, nickname: "user@example.com", local: false)
59
60 assert build_conn()
61 |> get("/api/v1/accounts/#{user.nickname}")
62 |> json_response_and_validate_schema(404)
63 end
64
65 test "respects limit_to_local_content == :unauthenticated for remote user nicknames" do
66 Config.put([:instance, :limit_to_local_content], :unauthenticated)
67
68 user = insert(:user, nickname: "user@example.com", local: false)
69 reading_user = insert(:user)
70
71 conn =
72 build_conn()
73 |> get("/api/v1/accounts/#{user.nickname}")
74
75 assert json_response_and_validate_schema(conn, 404)
76
77 conn =
78 build_conn()
79 |> assign(:user, reading_user)
80 |> assign(:token, insert(:oauth_token, user: reading_user, scopes: ["read:accounts"]))
81 |> get("/api/v1/accounts/#{user.nickname}")
82
83 assert %{"id" => id} = json_response_and_validate_schema(conn, 200)
84 assert id == user.id
85 end
86
87 test "accounts fetches correct account for nicknames beginning with numbers", %{conn: conn} do
88 # Need to set an old-style integer ID to reproduce the problem
89 # (these are no longer assigned to new accounts but were preserved
90 # for existing accounts during the migration to flakeIDs)
91 user_one = insert(:user, %{id: 1212})
92 user_two = insert(:user, %{nickname: "#{user_one.id}garbage"})
93
94 acc_one =
95 conn
96 |> get("/api/v1/accounts/#{user_one.id}")
97 |> json_response_and_validate_schema(:ok)
98
99 acc_two =
100 conn
101 |> get("/api/v1/accounts/#{user_two.nickname}")
102 |> json_response_and_validate_schema(:ok)
103
104 acc_three =
105 conn
106 |> get("/api/v1/accounts/#{user_two.id}")
107 |> json_response_and_validate_schema(:ok)
108
109 refute acc_one == acc_two
110 assert acc_two == acc_three
111 end
112
113 test "returns 404 when user is invisible", %{conn: conn} do
114 user = insert(:user, %{invisible: true})
115
116 assert %{"error" => "Can't find user"} =
117 conn
118 |> get("/api/v1/accounts/#{user.nickname}")
119 |> json_response_and_validate_schema(404)
120 end
121
122 test "returns 404 for internal.fetch actor", %{conn: conn} do
123 %User{nickname: "internal.fetch"} = InternalFetchActor.get_actor()
124
125 assert %{"error" => "Can't find user"} =
126 conn
127 |> get("/api/v1/accounts/internal.fetch")
128 |> json_response_and_validate_schema(404)
129 end
130
131 test "returns 404 for deactivated user", %{conn: conn} do
132 user = insert(:user, deactivated: true)
133
134 assert %{"error" => "Can't find user"} =
135 conn
136 |> get("/api/v1/accounts/#{user.id}")
137 |> json_response_and_validate_schema(:not_found)
138 end
139 end
140
141 defp local_and_remote_users do
142 local = insert(:user)
143 remote = insert(:user, local: false)
144 {:ok, local: local, remote: remote}
145 end
146
147 describe "user fetching with restrict unauthenticated profiles for local and remote" do
148 setup do: local_and_remote_users()
149
150 setup do: clear_config([:restrict_unauthenticated, :profiles, :local], true)
151
152 setup do: clear_config([:restrict_unauthenticated, :profiles, :remote], true)
153
154 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
155 assert %{"error" => "This API requires an authenticated user"} ==
156 conn
157 |> get("/api/v1/accounts/#{local.id}")
158 |> json_response_and_validate_schema(:unauthorized)
159
160 assert %{"error" => "This API requires an authenticated user"} ==
161 conn
162 |> get("/api/v1/accounts/#{remote.id}")
163 |> json_response_and_validate_schema(:unauthorized)
164 end
165
166 test "if user is authenticated", %{local: local, remote: remote} do
167 %{conn: conn} = oauth_access(["read"])
168
169 res_conn = get(conn, "/api/v1/accounts/#{local.id}")
170 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
171
172 res_conn = get(conn, "/api/v1/accounts/#{remote.id}")
173 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
174 end
175 end
176
177 describe "user fetching with restrict unauthenticated profiles for local" do
178 setup do: local_and_remote_users()
179
180 setup do: clear_config([:restrict_unauthenticated, :profiles, :local], true)
181
182 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
183 res_conn = get(conn, "/api/v1/accounts/#{local.id}")
184
185 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
186 "error" => "This API requires an authenticated user"
187 }
188
189 res_conn = get(conn, "/api/v1/accounts/#{remote.id}")
190 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
191 end
192
193 test "if user is authenticated", %{local: local, remote: remote} do
194 %{conn: conn} = oauth_access(["read"])
195
196 res_conn = get(conn, "/api/v1/accounts/#{local.id}")
197 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
198
199 res_conn = get(conn, "/api/v1/accounts/#{remote.id}")
200 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
201 end
202 end
203
204 describe "user fetching with restrict unauthenticated profiles for remote" do
205 setup do: local_and_remote_users()
206
207 setup do: clear_config([:restrict_unauthenticated, :profiles, :remote], true)
208
209 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
210 res_conn = get(conn, "/api/v1/accounts/#{local.id}")
211 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
212
213 res_conn = get(conn, "/api/v1/accounts/#{remote.id}")
214
215 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
216 "error" => "This API requires an authenticated user"
217 }
218 end
219
220 test "if user is authenticated", %{local: local, remote: remote} do
221 %{conn: conn} = oauth_access(["read"])
222
223 res_conn = get(conn, "/api/v1/accounts/#{local.id}")
224 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
225
226 res_conn = get(conn, "/api/v1/accounts/#{remote.id}")
227 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
228 end
229 end
230
231 describe "user timelines" do
232 setup do: oauth_access(["read:statuses"])
233
234 test "works with announces that are just addressed to public", %{conn: conn} do
235 user = insert(:user, ap_id: "https://honktest/u/test", local: false)
236 other_user = insert(:user)
237
238 {:ok, post} = CommonAPI.post(other_user, %{status: "bonkeronk"})
239
240 {:ok, announce, _} =
241 %{
242 "@context" => "https://www.w3.org/ns/activitystreams",
243 "actor" => "https://honktest/u/test",
244 "id" => "https://honktest/u/test/bonk/1793M7B9MQ48847vdx",
245 "object" => post.data["object"],
246 "published" => "2019-06-25T19:33:58Z",
247 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
248 "type" => "Announce"
249 }
250 |> ActivityPub.persist(local: false)
251
252 assert resp =
253 conn
254 |> get("/api/v1/accounts/#{user.id}/statuses")
255 |> json_response_and_validate_schema(200)
256
257 assert [%{"id" => id}] = resp
258 assert id == announce.id
259 end
260
261 test "deactivated user", %{conn: conn} do
262 user = insert(:user, deactivated: true)
263
264 assert %{"error" => "Can't find user"} ==
265 conn
266 |> get("/api/v1/accounts/#{user.id}/statuses")
267 |> json_response_and_validate_schema(:not_found)
268 end
269
270 test "returns 404 when user is invisible", %{conn: conn} do
271 user = insert(:user, %{invisible: true})
272
273 assert %{"error" => "Can't find user"} =
274 conn
275 |> get("/api/v1/accounts/#{user.id}")
276 |> json_response_and_validate_schema(404)
277 end
278
279 test "respects blocks", %{user: user_one, conn: conn} do
280 user_two = insert(:user)
281 user_three = insert(:user)
282
283 User.block(user_one, user_two)
284
285 {:ok, activity} = CommonAPI.post(user_two, %{status: "User one sux0rz"})
286 {:ok, repeat} = CommonAPI.repeat(activity.id, user_three)
287
288 assert resp =
289 conn
290 |> get("/api/v1/accounts/#{user_two.id}/statuses")
291 |> json_response_and_validate_schema(200)
292
293 assert [%{"id" => id}] = resp
294 assert id == activity.id
295
296 # Even a blocked user will deliver the full user timeline, there would be
297 # no point in looking at a blocked users timeline otherwise
298 assert resp =
299 conn
300 |> get("/api/v1/accounts/#{user_two.id}/statuses")
301 |> json_response_and_validate_schema(200)
302
303 assert [%{"id" => id}] = resp
304 assert id == activity.id
305
306 # Third user's timeline includes the repeat when viewed by unauthenticated user
307 resp =
308 build_conn()
309 |> get("/api/v1/accounts/#{user_three.id}/statuses")
310 |> json_response_and_validate_schema(200)
311
312 assert [%{"id" => id}] = resp
313 assert id == repeat.id
314
315 # When viewing a third user's timeline, the blocked users' statuses will NOT be shown
316 resp = get(conn, "/api/v1/accounts/#{user_three.id}/statuses")
317
318 assert [] == json_response_and_validate_schema(resp, 200)
319 end
320
321 test "gets users statuses", %{conn: conn} do
322 user_one = insert(:user)
323 user_two = insert(:user)
324 user_three = insert(:user)
325
326 {:ok, _user_three} = User.follow(user_three, user_one)
327
328 {:ok, activity} = CommonAPI.post(user_one, %{status: "HI!!!"})
329
330 {:ok, direct_activity} =
331 CommonAPI.post(user_one, %{
332 status: "Hi, @#{user_two.nickname}.",
333 visibility: "direct"
334 })
335
336 {:ok, private_activity} =
337 CommonAPI.post(user_one, %{status: "private", visibility: "private"})
338
339 # TODO!!!
340 resp =
341 conn
342 |> get("/api/v1/accounts/#{user_one.id}/statuses")
343 |> json_response_and_validate_schema(200)
344
345 assert [%{"id" => id}] = resp
346 assert id == to_string(activity.id)
347
348 resp =
349 conn
350 |> assign(:user, user_two)
351 |> assign(:token, insert(:oauth_token, user: user_two, scopes: ["read:statuses"]))
352 |> get("/api/v1/accounts/#{user_one.id}/statuses")
353 |> json_response_and_validate_schema(200)
354
355 assert [%{"id" => id_one}, %{"id" => id_two}] = resp
356 assert id_one == to_string(direct_activity.id)
357 assert id_two == to_string(activity.id)
358
359 resp =
360 conn
361 |> assign(:user, user_three)
362 |> assign(:token, insert(:oauth_token, user: user_three, scopes: ["read:statuses"]))
363 |> get("/api/v1/accounts/#{user_one.id}/statuses")
364 |> json_response_and_validate_schema(200)
365
366 assert [%{"id" => id_one}, %{"id" => id_two}] = resp
367 assert id_one == to_string(private_activity.id)
368 assert id_two == to_string(activity.id)
369 end
370
371 test "unimplemented pinned statuses feature", %{conn: conn} do
372 note = insert(:note_activity)
373 user = User.get_cached_by_ap_id(note.data["actor"])
374
375 conn = get(conn, "/api/v1/accounts/#{user.id}/statuses?pinned=true")
376
377 assert json_response_and_validate_schema(conn, 200) == []
378 end
379
380 test "gets an users media, excludes reblogs", %{conn: conn} do
381 note = insert(:note_activity)
382 user = User.get_cached_by_ap_id(note.data["actor"])
383 other_user = insert(:user)
384
385 file = %Plug.Upload{
386 content_type: "image/jpg",
387 path: Path.absname("test/fixtures/image.jpg"),
388 filename: "an_image.jpg"
389 }
390
391 {:ok, %{id: media_id}} = ActivityPub.upload(file, actor: user.ap_id)
392
393 {:ok, %{id: image_post_id}} = CommonAPI.post(user, %{status: "cofe", media_ids: [media_id]})
394
395 {:ok, %{id: media_id}} = ActivityPub.upload(file, actor: other_user.ap_id)
396
397 {:ok, %{id: other_image_post_id}} =
398 CommonAPI.post(other_user, %{status: "cofe2", media_ids: [media_id]})
399
400 {:ok, _announce} = CommonAPI.repeat(other_image_post_id, user)
401
402 conn = get(conn, "/api/v1/accounts/#{user.id}/statuses?only_media=true")
403
404 assert [%{"id" => ^image_post_id}] = json_response_and_validate_schema(conn, 200)
405
406 conn = get(build_conn(), "/api/v1/accounts/#{user.id}/statuses?only_media=1")
407
408 assert [%{"id" => ^image_post_id}] = json_response_and_validate_schema(conn, 200)
409 end
410
411 test "gets a user's statuses without reblogs", %{user: user, conn: conn} do
412 {:ok, %{id: post_id}} = CommonAPI.post(user, %{status: "HI!!!"})
413 {:ok, _} = CommonAPI.repeat(post_id, user)
414
415 conn = get(conn, "/api/v1/accounts/#{user.id}/statuses?exclude_reblogs=true")
416 assert [%{"id" => ^post_id}] = json_response_and_validate_schema(conn, 200)
417
418 conn = get(conn, "/api/v1/accounts/#{user.id}/statuses?exclude_reblogs=1")
419 assert [%{"id" => ^post_id}] = json_response_and_validate_schema(conn, 200)
420 end
421
422 test "filters user's statuses by a hashtag", %{user: user, conn: conn} do
423 {:ok, %{id: post_id}} = CommonAPI.post(user, %{status: "#hashtag"})
424 {:ok, _post} = CommonAPI.post(user, %{status: "hashtag"})
425
426 conn = get(conn, "/api/v1/accounts/#{user.id}/statuses?tagged=hashtag")
427 assert [%{"id" => ^post_id}] = json_response_and_validate_schema(conn, 200)
428 end
429
430 test "the user views their own timelines and excludes direct messages", %{
431 user: user,
432 conn: conn
433 } do
434 {:ok, %{id: public_activity_id}} =
435 CommonAPI.post(user, %{status: ".", visibility: "public"})
436
437 {:ok, _direct_activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
438
439 conn = get(conn, "/api/v1/accounts/#{user.id}/statuses?exclude_visibilities[]=direct")
440 assert [%{"id" => ^public_activity_id}] = json_response_and_validate_schema(conn, 200)
441 end
442 end
443
444 defp local_and_remote_activities(%{local: local, remote: remote}) do
445 insert(:note_activity, user: local)
446 insert(:note_activity, user: remote, local: false)
447
448 :ok
449 end
450
451 describe "statuses with restrict unauthenticated profiles for local and remote" do
452 setup do: local_and_remote_users()
453 setup :local_and_remote_activities
454
455 setup do: clear_config([:restrict_unauthenticated, :profiles, :local], true)
456
457 setup do: clear_config([:restrict_unauthenticated, :profiles, :remote], true)
458
459 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
460 assert %{"error" => "This API requires an authenticated user"} ==
461 conn
462 |> get("/api/v1/accounts/#{local.id}/statuses")
463 |> json_response_and_validate_schema(:unauthorized)
464
465 assert %{"error" => "This API requires an authenticated user"} ==
466 conn
467 |> get("/api/v1/accounts/#{remote.id}/statuses")
468 |> json_response_and_validate_schema(:unauthorized)
469 end
470
471 test "if user is authenticated", %{local: local, remote: remote} do
472 %{conn: conn} = oauth_access(["read"])
473
474 res_conn = get(conn, "/api/v1/accounts/#{local.id}/statuses")
475 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
476
477 res_conn = get(conn, "/api/v1/accounts/#{remote.id}/statuses")
478 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
479 end
480 end
481
482 describe "statuses with restrict unauthenticated profiles for local" do
483 setup do: local_and_remote_users()
484 setup :local_and_remote_activities
485
486 setup do: clear_config([:restrict_unauthenticated, :profiles, :local], true)
487
488 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
489 assert %{"error" => "This API requires an authenticated user"} ==
490 conn
491 |> get("/api/v1/accounts/#{local.id}/statuses")
492 |> json_response_and_validate_schema(:unauthorized)
493
494 res_conn = get(conn, "/api/v1/accounts/#{remote.id}/statuses")
495 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
496 end
497
498 test "if user is authenticated", %{local: local, remote: remote} do
499 %{conn: conn} = oauth_access(["read"])
500
501 res_conn = get(conn, "/api/v1/accounts/#{local.id}/statuses")
502 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
503
504 res_conn = get(conn, "/api/v1/accounts/#{remote.id}/statuses")
505 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
506 end
507 end
508
509 describe "statuses with restrict unauthenticated profiles for remote" do
510 setup do: local_and_remote_users()
511 setup :local_and_remote_activities
512
513 setup do: clear_config([:restrict_unauthenticated, :profiles, :remote], true)
514
515 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
516 res_conn = get(conn, "/api/v1/accounts/#{local.id}/statuses")
517 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
518
519 assert %{"error" => "This API requires an authenticated user"} ==
520 conn
521 |> get("/api/v1/accounts/#{remote.id}/statuses")
522 |> json_response_and_validate_schema(:unauthorized)
523 end
524
525 test "if user is authenticated", %{local: local, remote: remote} do
526 %{conn: conn} = oauth_access(["read"])
527
528 res_conn = get(conn, "/api/v1/accounts/#{local.id}/statuses")
529 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
530
531 res_conn = get(conn, "/api/v1/accounts/#{remote.id}/statuses")
532 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
533 end
534 end
535
536 describe "followers" do
537 setup do: oauth_access(["read:accounts"])
538
539 test "getting followers", %{user: user, conn: conn} do
540 other_user = insert(:user)
541 {:ok, %{id: user_id}} = User.follow(user, other_user)
542
543 conn = get(conn, "/api/v1/accounts/#{other_user.id}/followers")
544
545 assert [%{"id" => ^user_id}] = json_response_and_validate_schema(conn, 200)
546 end
547
548 test "getting followers, hide_followers", %{user: user, conn: conn} do
549 other_user = insert(:user, hide_followers: true)
550 {:ok, _user} = User.follow(user, other_user)
551
552 conn = get(conn, "/api/v1/accounts/#{other_user.id}/followers")
553
554 assert [] == json_response_and_validate_schema(conn, 200)
555 end
556
557 test "getting followers, hide_followers, same user requesting" do
558 user = insert(:user)
559 other_user = insert(:user, hide_followers: true)
560 {:ok, _user} = User.follow(user, other_user)
561
562 conn =
563 build_conn()
564 |> assign(:user, other_user)
565 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:accounts"]))
566 |> get("/api/v1/accounts/#{other_user.id}/followers")
567
568 refute [] == json_response_and_validate_schema(conn, 200)
569 end
570
571 test "getting followers, pagination", %{user: user, conn: conn} do
572 {:ok, %User{id: follower1_id}} = :user |> insert() |> User.follow(user)
573 {:ok, %User{id: follower2_id}} = :user |> insert() |> User.follow(user)
574 {:ok, %User{id: follower3_id}} = :user |> insert() |> User.follow(user)
575
576 assert [%{"id" => ^follower3_id}, %{"id" => ^follower2_id}] =
577 conn
578 |> get("/api/v1/accounts/#{user.id}/followers?since_id=#{follower1_id}")
579 |> json_response_and_validate_schema(200)
580
581 assert [%{"id" => ^follower2_id}, %{"id" => ^follower1_id}] =
582 conn
583 |> get("/api/v1/accounts/#{user.id}/followers?max_id=#{follower3_id}")
584 |> json_response_and_validate_schema(200)
585
586 res_conn = get(conn, "/api/v1/accounts/#{user.id}/followers?limit=1&max_id=#{follower3_id}")
587
588 assert [%{"id" => ^follower2_id}] = json_response_and_validate_schema(res_conn, 200)
589
590 assert [link_header] = get_resp_header(res_conn, "link")
591 assert link_header =~ ~r/min_id=#{follower2_id}/
592 assert link_header =~ ~r/max_id=#{follower2_id}/
593 end
594 end
595
596 describe "following" do
597 setup do: oauth_access(["read:accounts"])
598
599 test "getting following", %{user: user, conn: conn} do
600 other_user = insert(:user)
601 {:ok, user} = User.follow(user, other_user)
602
603 conn = get(conn, "/api/v1/accounts/#{user.id}/following")
604
605 assert [%{"id" => id}] = json_response_and_validate_schema(conn, 200)
606 assert id == to_string(other_user.id)
607 end
608
609 test "getting following, hide_follows, other user requesting" do
610 user = insert(:user, hide_follows: true)
611 other_user = insert(:user)
612 {:ok, user} = User.follow(user, other_user)
613
614 conn =
615 build_conn()
616 |> assign(:user, other_user)
617 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:accounts"]))
618 |> get("/api/v1/accounts/#{user.id}/following")
619
620 assert [] == json_response_and_validate_schema(conn, 200)
621 end
622
623 test "getting following, hide_follows, same user requesting" do
624 user = insert(:user, hide_follows: true)
625 other_user = insert(:user)
626 {:ok, user} = User.follow(user, other_user)
627
628 conn =
629 build_conn()
630 |> assign(:user, user)
631 |> assign(:token, insert(:oauth_token, user: user, scopes: ["read:accounts"]))
632 |> get("/api/v1/accounts/#{user.id}/following")
633
634 refute [] == json_response_and_validate_schema(conn, 200)
635 end
636
637 test "getting following, pagination", %{user: user, conn: conn} do
638 following1 = insert(:user)
639 following2 = insert(:user)
640 following3 = insert(:user)
641 {:ok, _} = User.follow(user, following1)
642 {:ok, _} = User.follow(user, following2)
643 {:ok, _} = User.follow(user, following3)
644
645 res_conn = get(conn, "/api/v1/accounts/#{user.id}/following?since_id=#{following1.id}")
646
647 assert [%{"id" => id3}, %{"id" => id2}] = json_response_and_validate_schema(res_conn, 200)
648 assert id3 == following3.id
649 assert id2 == following2.id
650
651 res_conn = get(conn, "/api/v1/accounts/#{user.id}/following?max_id=#{following3.id}")
652
653 assert [%{"id" => id2}, %{"id" => id1}] = json_response_and_validate_schema(res_conn, 200)
654 assert id2 == following2.id
655 assert id1 == following1.id
656
657 res_conn =
658 get(conn, "/api/v1/accounts/#{user.id}/following?limit=1&max_id=#{following3.id}")
659
660 assert [%{"id" => id2}] = json_response_and_validate_schema(res_conn, 200)
661 assert id2 == following2.id
662
663 assert [link_header] = get_resp_header(res_conn, "link")
664 assert link_header =~ ~r/min_id=#{following2.id}/
665 assert link_header =~ ~r/max_id=#{following2.id}/
666 end
667 end
668
669 describe "follow/unfollow" do
670 setup do: oauth_access(["follow"])
671
672 test "following / unfollowing a user", %{conn: conn} do
673 %{id: other_user_id, nickname: other_user_nickname} = insert(:user)
674
675 assert %{"id" => _id, "following" => true} =
676 conn
677 |> post("/api/v1/accounts/#{other_user_id}/follow")
678 |> json_response_and_validate_schema(200)
679
680 assert %{"id" => _id, "following" => false} =
681 conn
682 |> post("/api/v1/accounts/#{other_user_id}/unfollow")
683 |> json_response_and_validate_schema(200)
684
685 assert %{"id" => ^other_user_id} =
686 conn
687 |> put_req_header("content-type", "application/json")
688 |> post("/api/v1/follows", %{"uri" => other_user_nickname})
689 |> json_response_and_validate_schema(200)
690 end
691
692 test "cancelling follow request", %{conn: conn} do
693 %{id: other_user_id} = insert(:user, %{locked: true})
694
695 assert %{"id" => ^other_user_id, "following" => false, "requested" => true} =
696 conn
697 |> post("/api/v1/accounts/#{other_user_id}/follow")
698 |> json_response_and_validate_schema(:ok)
699
700 assert %{"id" => ^other_user_id, "following" => false, "requested" => false} =
701 conn
702 |> post("/api/v1/accounts/#{other_user_id}/unfollow")
703 |> json_response_and_validate_schema(:ok)
704 end
705
706 test "following without reblogs" do
707 %{conn: conn} = oauth_access(["follow", "read:statuses"])
708 followed = insert(:user)
709 other_user = insert(:user)
710
711 ret_conn =
712 conn
713 |> put_req_header("content-type", "application/json")
714 |> post("/api/v1/accounts/#{followed.id}/follow", %{reblogs: false})
715
716 assert %{"showing_reblogs" => false} = json_response_and_validate_schema(ret_conn, 200)
717
718 {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
719 {:ok, %{id: reblog_id}} = CommonAPI.repeat(activity.id, followed)
720
721 assert [] ==
722 conn
723 |> get("/api/v1/timelines/home")
724 |> json_response(200)
725
726 assert %{"showing_reblogs" => true} =
727 conn
728 |> put_req_header("content-type", "application/json")
729 |> post("/api/v1/accounts/#{followed.id}/follow", %{reblogs: true})
730 |> json_response_and_validate_schema(200)
731
732 assert [%{"id" => ^reblog_id}] =
733 conn
734 |> get("/api/v1/timelines/home")
735 |> json_response(200)
736 end
737
738 test "following with reblogs" do
739 %{conn: conn} = oauth_access(["follow", "read:statuses"])
740 followed = insert(:user)
741 other_user = insert(:user)
742
743 ret_conn = post(conn, "/api/v1/accounts/#{followed.id}/follow")
744
745 assert %{"showing_reblogs" => true} = json_response_and_validate_schema(ret_conn, 200)
746
747 {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
748 {:ok, %{id: reblog_id}} = CommonAPI.repeat(activity.id, followed)
749
750 assert [%{"id" => ^reblog_id}] =
751 conn
752 |> get("/api/v1/timelines/home")
753 |> json_response(200)
754
755 assert %{"showing_reblogs" => false} =
756 conn
757 |> put_req_header("content-type", "application/json")
758 |> post("/api/v1/accounts/#{followed.id}/follow", %{reblogs: false})
759 |> json_response_and_validate_schema(200)
760
761 assert [] ==
762 conn
763 |> get("/api/v1/timelines/home")
764 |> json_response(200)
765 end
766
767 test "following / unfollowing errors", %{user: user, conn: conn} do
768 # self follow
769 conn_res = post(conn, "/api/v1/accounts/#{user.id}/follow")
770
771 assert %{"error" => "Can not follow yourself"} =
772 json_response_and_validate_schema(conn_res, 400)
773
774 # self unfollow
775 user = User.get_cached_by_id(user.id)
776 conn_res = post(conn, "/api/v1/accounts/#{user.id}/unfollow")
777
778 assert %{"error" => "Can not unfollow yourself"} =
779 json_response_and_validate_schema(conn_res, 400)
780
781 # self follow via uri
782 user = User.get_cached_by_id(user.id)
783
784 assert %{"error" => "Can not follow yourself"} =
785 conn
786 |> put_req_header("content-type", "multipart/form-data")
787 |> post("/api/v1/follows", %{"uri" => user.nickname})
788 |> json_response_and_validate_schema(400)
789
790 # follow non existing user
791 conn_res = post(conn, "/api/v1/accounts/doesntexist/follow")
792 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn_res, 404)
793
794 # follow non existing user via uri
795 conn_res =
796 conn
797 |> put_req_header("content-type", "multipart/form-data")
798 |> post("/api/v1/follows", %{"uri" => "doesntexist"})
799
800 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn_res, 404)
801
802 # unfollow non existing user
803 conn_res = post(conn, "/api/v1/accounts/doesntexist/unfollow")
804 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn_res, 404)
805 end
806 end
807
808 describe "mute/unmute" do
809 setup do: oauth_access(["write:mutes"])
810
811 test "with notifications", %{conn: conn} do
812 other_user = insert(:user)
813
814 assert %{"id" => _id, "muting" => true, "muting_notifications" => true} =
815 conn
816 |> post("/api/v1/accounts/#{other_user.id}/mute")
817 |> json_response_and_validate_schema(200)
818
819 conn = post(conn, "/api/v1/accounts/#{other_user.id}/unmute")
820
821 assert %{"id" => _id, "muting" => false, "muting_notifications" => false} =
822 json_response_and_validate_schema(conn, 200)
823 end
824
825 test "without notifications", %{conn: conn} do
826 other_user = insert(:user)
827
828 ret_conn =
829 conn
830 |> put_req_header("content-type", "multipart/form-data")
831 |> post("/api/v1/accounts/#{other_user.id}/mute", %{"notifications" => "false"})
832
833 assert %{"id" => _id, "muting" => true, "muting_notifications" => false} =
834 json_response_and_validate_schema(ret_conn, 200)
835
836 conn = post(conn, "/api/v1/accounts/#{other_user.id}/unmute")
837
838 assert %{"id" => _id, "muting" => false, "muting_notifications" => false} =
839 json_response_and_validate_schema(conn, 200)
840 end
841 end
842
843 describe "pinned statuses" do
844 setup do
845 user = insert(:user)
846 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!"})
847 %{conn: conn} = oauth_access(["read:statuses"], user: user)
848
849 [conn: conn, user: user, activity: activity]
850 end
851
852 test "returns pinned statuses", %{conn: conn, user: user, activity: %{id: activity_id}} do
853 {:ok, _} = CommonAPI.pin(activity_id, user)
854
855 assert [%{"id" => ^activity_id, "pinned" => true}] =
856 conn
857 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
858 |> json_response_and_validate_schema(200)
859 end
860 end
861
862 test "blocking / unblocking a user" do
863 %{conn: conn} = oauth_access(["follow"])
864 other_user = insert(:user)
865
866 ret_conn = post(conn, "/api/v1/accounts/#{other_user.id}/block")
867
868 assert %{"id" => _id, "blocking" => true} = json_response_and_validate_schema(ret_conn, 200)
869
870 conn = post(conn, "/api/v1/accounts/#{other_user.id}/unblock")
871
872 assert %{"id" => _id, "blocking" => false} = json_response_and_validate_schema(conn, 200)
873 end
874
875 describe "create account by app" do
876 setup do
877 valid_params = %{
878 username: "lain",
879 email: "lain@example.org",
880 password: "PlzDontHackLain",
881 agreement: true
882 }
883
884 [valid_params: valid_params]
885 end
886
887 setup do: clear_config([:instance, :account_activation_required])
888
889 test "Account registration via Application", %{conn: conn} do
890 conn =
891 conn
892 |> put_req_header("content-type", "application/json")
893 |> post("/api/v1/apps", %{
894 client_name: "client_name",
895 redirect_uris: "urn:ietf:wg:oauth:2.0:oob",
896 scopes: "read, write, follow"
897 })
898
899 assert %{
900 "client_id" => client_id,
901 "client_secret" => client_secret,
902 "id" => _,
903 "name" => "client_name",
904 "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob",
905 "vapid_key" => _,
906 "website" => nil
907 } = json_response_and_validate_schema(conn, 200)
908
909 conn =
910 post(conn, "/oauth/token", %{
911 grant_type: "client_credentials",
912 client_id: client_id,
913 client_secret: client_secret
914 })
915
916 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
917 json_response(conn, 200)
918
919 assert token
920 token_from_db = Repo.get_by(Token, token: token)
921 assert token_from_db
922 assert refresh
923 assert scope == "read write follow"
924
925 conn =
926 build_conn()
927 |> put_req_header("content-type", "multipart/form-data")
928 |> put_req_header("authorization", "Bearer " <> token)
929 |> post("/api/v1/accounts", %{
930 username: "lain",
931 email: "lain@example.org",
932 password: "PlzDontHackLain",
933 bio: "Test Bio",
934 agreement: true
935 })
936
937 %{
938 "access_token" => token,
939 "created_at" => _created_at,
940 "scope" => _scope,
941 "token_type" => "Bearer"
942 } = json_response_and_validate_schema(conn, 200)
943
944 token_from_db = Repo.get_by(Token, token: token)
945 assert token_from_db
946 token_from_db = Repo.preload(token_from_db, :user)
947 assert token_from_db.user
948
949 assert token_from_db.user.confirmation_pending
950 end
951
952 test "returns error when user already registred", %{conn: conn, valid_params: valid_params} do
953 _user = insert(:user, email: "lain@example.org")
954 app_token = insert(:oauth_token, user: nil)
955
956 res =
957 conn
958 |> put_req_header("authorization", "Bearer " <> app_token.token)
959 |> put_req_header("content-type", "application/json")
960 |> post("/api/v1/accounts", valid_params)
961
962 assert json_response_and_validate_schema(res, 400) == %{
963 "error" => "{\"email\":[\"has already been taken\"]}"
964 }
965 end
966
967 test "returns bad_request if missing required params", %{
968 conn: conn,
969 valid_params: valid_params
970 } do
971 app_token = insert(:oauth_token, user: nil)
972
973 conn =
974 conn
975 |> put_req_header("authorization", "Bearer " <> app_token.token)
976 |> put_req_header("content-type", "application/json")
977
978 res = post(conn, "/api/v1/accounts", valid_params)
979 assert json_response_and_validate_schema(res, 200)
980
981 [{127, 0, 0, 1}, {127, 0, 0, 2}, {127, 0, 0, 3}, {127, 0, 0, 4}]
982 |> Stream.zip(Map.delete(valid_params, :email))
983 |> Enum.each(fn {ip, {attr, _}} ->
984 res =
985 conn
986 |> Map.put(:remote_ip, ip)
987 |> post("/api/v1/accounts", Map.delete(valid_params, attr))
988 |> json_response_and_validate_schema(400)
989
990 assert res == %{
991 "error" => "Missing field: #{attr}.",
992 "errors" => [
993 %{
994 "message" => "Missing field: #{attr}",
995 "source" => %{"pointer" => "/#{attr}"},
996 "title" => "Invalid value"
997 }
998 ]
999 }
1000 end)
1001 end
1002
1003 setup do: clear_config([:instance, :account_activation_required])
1004
1005 test "returns bad_request if missing email params when :account_activation_required is enabled",
1006 %{conn: conn, valid_params: valid_params} do
1007 Pleroma.Config.put([:instance, :account_activation_required], true)
1008
1009 app_token = insert(:oauth_token, user: nil)
1010
1011 conn =
1012 conn
1013 |> put_req_header("authorization", "Bearer " <> app_token.token)
1014 |> put_req_header("content-type", "application/json")
1015
1016 res =
1017 conn
1018 |> Map.put(:remote_ip, {127, 0, 0, 5})
1019 |> post("/api/v1/accounts", Map.delete(valid_params, :email))
1020
1021 assert json_response_and_validate_schema(res, 400) ==
1022 %{"error" => "Missing parameter: email"}
1023
1024 res =
1025 conn
1026 |> Map.put(:remote_ip, {127, 0, 0, 6})
1027 |> post("/api/v1/accounts", Map.put(valid_params, :email, ""))
1028
1029 assert json_response_and_validate_schema(res, 400) == %{
1030 "error" => "{\"email\":[\"can't be blank\"]}"
1031 }
1032 end
1033
1034 test "allow registration without an email", %{conn: conn, valid_params: valid_params} do
1035 app_token = insert(:oauth_token, user: nil)
1036 conn = put_req_header(conn, "authorization", "Bearer " <> app_token.token)
1037
1038 res =
1039 conn
1040 |> put_req_header("content-type", "application/json")
1041 |> Map.put(:remote_ip, {127, 0, 0, 7})
1042 |> post("/api/v1/accounts", Map.delete(valid_params, :email))
1043
1044 assert json_response_and_validate_schema(res, 200)
1045 end
1046
1047 test "allow registration with an empty email", %{conn: conn, valid_params: valid_params} do
1048 app_token = insert(:oauth_token, user: nil)
1049 conn = put_req_header(conn, "authorization", "Bearer " <> app_token.token)
1050
1051 res =
1052 conn
1053 |> put_req_header("content-type", "application/json")
1054 |> Map.put(:remote_ip, {127, 0, 0, 8})
1055 |> post("/api/v1/accounts", Map.put(valid_params, :email, ""))
1056
1057 assert json_response_and_validate_schema(res, 200)
1058 end
1059
1060 test "returns forbidden if token is invalid", %{conn: conn, valid_params: valid_params} do
1061 res =
1062 conn
1063 |> put_req_header("authorization", "Bearer " <> "invalid-token")
1064 |> put_req_header("content-type", "multipart/form-data")
1065 |> post("/api/v1/accounts", valid_params)
1066
1067 assert json_response_and_validate_schema(res, 403) == %{"error" => "Invalid credentials"}
1068 end
1069
1070 test "registration from trusted app" do
1071 clear_config([Pleroma.Captcha, :enabled], true)
1072 app = insert(:oauth_app, trusted: true, scopes: ["read", "write", "follow", "push"])
1073
1074 conn =
1075 build_conn()
1076 |> post("/oauth/token", %{
1077 "grant_type" => "client_credentials",
1078 "client_id" => app.client_id,
1079 "client_secret" => app.client_secret
1080 })
1081
1082 assert %{"access_token" => token, "token_type" => "Bearer"} = json_response(conn, 200)
1083
1084 response =
1085 build_conn()
1086 |> Plug.Conn.put_req_header("authorization", "Bearer " <> token)
1087 |> put_req_header("content-type", "multipart/form-data")
1088 |> post("/api/v1/accounts", %{
1089 nickname: "nickanme",
1090 agreement: true,
1091 email: "email@example.com",
1092 fullname: "Lain",
1093 username: "Lain",
1094 password: "some_password",
1095 confirm: "some_password"
1096 })
1097 |> json_response_and_validate_schema(200)
1098
1099 assert %{
1100 "access_token" => access_token,
1101 "created_at" => _,
1102 "scope" => ["read", "write", "follow", "push"],
1103 "token_type" => "Bearer"
1104 } = response
1105
1106 response =
1107 build_conn()
1108 |> Plug.Conn.put_req_header("authorization", "Bearer " <> access_token)
1109 |> get("/api/v1/accounts/verify_credentials")
1110 |> json_response_and_validate_schema(200)
1111
1112 assert %{
1113 "acct" => "Lain",
1114 "bot" => false,
1115 "display_name" => "Lain",
1116 "follow_requests_count" => 0,
1117 "followers_count" => 0,
1118 "following_count" => 0,
1119 "locked" => false,
1120 "note" => "",
1121 "source" => %{
1122 "fields" => [],
1123 "note" => "",
1124 "pleroma" => %{
1125 "actor_type" => "Person",
1126 "discoverable" => false,
1127 "no_rich_text" => false,
1128 "show_role" => true
1129 },
1130 "privacy" => "public",
1131 "sensitive" => false
1132 },
1133 "statuses_count" => 0,
1134 "username" => "Lain"
1135 } = response
1136 end
1137 end
1138
1139 describe "create account by app / rate limit" do
1140 setup do: clear_config([:rate_limit, :app_account_creation], {10_000, 2})
1141
1142 test "respects rate limit setting", %{conn: conn} do
1143 app_token = insert(:oauth_token, user: nil)
1144
1145 conn =
1146 conn
1147 |> put_req_header("authorization", "Bearer " <> app_token.token)
1148 |> Map.put(:remote_ip, {15, 15, 15, 15})
1149 |> put_req_header("content-type", "multipart/form-data")
1150
1151 for i <- 1..2 do
1152 conn =
1153 conn
1154 |> post("/api/v1/accounts", %{
1155 username: "#{i}lain",
1156 email: "#{i}lain@example.org",
1157 password: "PlzDontHackLain",
1158 agreement: true
1159 })
1160
1161 %{
1162 "access_token" => token,
1163 "created_at" => _created_at,
1164 "scope" => _scope,
1165 "token_type" => "Bearer"
1166 } = json_response_and_validate_schema(conn, 200)
1167
1168 token_from_db = Repo.get_by(Token, token: token)
1169 assert token_from_db
1170 token_from_db = Repo.preload(token_from_db, :user)
1171 assert token_from_db.user
1172
1173 assert token_from_db.user.confirmation_pending
1174 end
1175
1176 conn =
1177 post(conn, "/api/v1/accounts", %{
1178 username: "6lain",
1179 email: "6lain@example.org",
1180 password: "PlzDontHackLain",
1181 agreement: true
1182 })
1183
1184 assert json_response_and_validate_schema(conn, :too_many_requests) == %{
1185 "error" => "Throttled"
1186 }
1187 end
1188 end
1189
1190 describe "create account with enabled captcha" do
1191 setup %{conn: conn} do
1192 app_token = insert(:oauth_token, user: nil)
1193
1194 conn =
1195 conn
1196 |> put_req_header("authorization", "Bearer " <> app_token.token)
1197 |> put_req_header("content-type", "multipart/form-data")
1198
1199 [conn: conn]
1200 end
1201
1202 setup do: clear_config([Pleroma.Captcha, :enabled], true)
1203
1204 test "creates an account and returns 200 if captcha is valid", %{conn: conn} do
1205 %{token: token, answer_data: answer_data} = Pleroma.Captcha.new()
1206
1207 params = %{
1208 username: "lain",
1209 email: "lain@example.org",
1210 password: "PlzDontHackLain",
1211 agreement: true,
1212 captcha_solution: Pleroma.Captcha.Mock.solution(),
1213 captcha_token: token,
1214 captcha_answer_data: answer_data
1215 }
1216
1217 assert %{
1218 "access_token" => access_token,
1219 "created_at" => _,
1220 "scope" => ["read"],
1221 "token_type" => "Bearer"
1222 } =
1223 conn
1224 |> post("/api/v1/accounts", params)
1225 |> json_response_and_validate_schema(:ok)
1226
1227 assert Token |> Repo.get_by(token: access_token) |> Repo.preload(:user) |> Map.get(:user)
1228
1229 Cachex.del(:used_captcha_cache, token)
1230 end
1231
1232 test "returns 400 if any captcha field is not provided", %{conn: conn} do
1233 captcha_fields = [:captcha_solution, :captcha_token, :captcha_answer_data]
1234
1235 valid_params = %{
1236 username: "lain",
1237 email: "lain@example.org",
1238 password: "PlzDontHackLain",
1239 agreement: true,
1240 captcha_solution: "xx",
1241 captcha_token: "xx",
1242 captcha_answer_data: "xx"
1243 }
1244
1245 for field <- captcha_fields do
1246 expected = %{
1247 "error" => "{\"captcha\":[\"Invalid CAPTCHA (Missing parameter: #{field})\"]}"
1248 }
1249
1250 assert expected ==
1251 conn
1252 |> post("/api/v1/accounts", Map.delete(valid_params, field))
1253 |> json_response_and_validate_schema(:bad_request)
1254 end
1255 end
1256
1257 test "returns an error if captcha is invalid", %{conn: conn} do
1258 params = %{
1259 username: "lain",
1260 email: "lain@example.org",
1261 password: "PlzDontHackLain",
1262 agreement: true,
1263 captcha_solution: "cofe",
1264 captcha_token: "cofe",
1265 captcha_answer_data: "cofe"
1266 }
1267
1268 assert %{"error" => "{\"captcha\":[\"Invalid answer data\"]}"} ==
1269 conn
1270 |> post("/api/v1/accounts", params)
1271 |> json_response_and_validate_schema(:bad_request)
1272 end
1273 end
1274
1275 describe "GET /api/v1/accounts/:id/lists - account_lists" do
1276 test "returns lists to which the account belongs" do
1277 %{user: user, conn: conn} = oauth_access(["read:lists"])
1278 other_user = insert(:user)
1279 assert {:ok, %Pleroma.List{id: list_id} = list} = Pleroma.List.create("Test List", user)
1280 {:ok, %{following: _following}} = Pleroma.List.follow(list, other_user)
1281
1282 assert [%{"id" => list_id, "title" => "Test List"}] =
1283 conn
1284 |> get("/api/v1/accounts/#{other_user.id}/lists")
1285 |> json_response_and_validate_schema(200)
1286 end
1287 end
1288
1289 describe "verify_credentials" do
1290 test "verify_credentials" do
1291 %{user: user, conn: conn} = oauth_access(["read:accounts"])
1292 [notification | _] = insert_list(7, :notification, user: user)
1293 Pleroma.Notification.set_read_up_to(user, notification.id)
1294 conn = get(conn, "/api/v1/accounts/verify_credentials")
1295
1296 response = json_response_and_validate_schema(conn, 200)
1297
1298 assert %{"id" => id, "source" => %{"privacy" => "public"}} = response
1299 assert response["pleroma"]["chat_token"]
1300 assert response["pleroma"]["unread_notifications_count"] == 6
1301 assert id == to_string(user.id)
1302 end
1303
1304 test "verify_credentials default scope unlisted" do
1305 user = insert(:user, default_scope: "unlisted")
1306 %{conn: conn} = oauth_access(["read:accounts"], user: user)
1307
1308 conn = get(conn, "/api/v1/accounts/verify_credentials")
1309
1310 assert %{"id" => id, "source" => %{"privacy" => "unlisted"}} =
1311 json_response_and_validate_schema(conn, 200)
1312
1313 assert id == to_string(user.id)
1314 end
1315
1316 test "locked accounts" do
1317 user = insert(:user, default_scope: "private")
1318 %{conn: conn} = oauth_access(["read:accounts"], user: user)
1319
1320 conn = get(conn, "/api/v1/accounts/verify_credentials")
1321
1322 assert %{"id" => id, "source" => %{"privacy" => "private"}} =
1323 json_response_and_validate_schema(conn, 200)
1324
1325 assert id == to_string(user.id)
1326 end
1327 end
1328
1329 describe "user relationships" do
1330 setup do: oauth_access(["read:follows"])
1331
1332 test "returns the relationships for the current user", %{user: user, conn: conn} do
1333 %{id: other_user_id} = other_user = insert(:user)
1334 {:ok, _user} = User.follow(user, other_user)
1335
1336 assert [%{"id" => ^other_user_id}] =
1337 conn
1338 |> get("/api/v1/accounts/relationships?id=#{other_user.id}")
1339 |> json_response_and_validate_schema(200)
1340
1341 assert [%{"id" => ^other_user_id}] =
1342 conn
1343 |> get("/api/v1/accounts/relationships?id[]=#{other_user.id}")
1344 |> json_response_and_validate_schema(200)
1345 end
1346
1347 test "returns an empty list on a bad request", %{conn: conn} do
1348 conn = get(conn, "/api/v1/accounts/relationships", %{})
1349
1350 assert [] = json_response_and_validate_schema(conn, 200)
1351 end
1352 end
1353
1354 test "getting a list of mutes" do
1355 %{user: user, conn: conn} = oauth_access(["read:mutes"])
1356 other_user = insert(:user)
1357
1358 {:ok, _user_relationships} = User.mute(user, other_user)
1359
1360 conn = get(conn, "/api/v1/mutes")
1361
1362 other_user_id = to_string(other_user.id)
1363 assert [%{"id" => ^other_user_id}] = json_response_and_validate_schema(conn, 200)
1364 end
1365
1366 test "getting a list of blocks" do
1367 %{user: user, conn: conn} = oauth_access(["read:blocks"])
1368 other_user = insert(:user)
1369
1370 {:ok, _user_relationship} = User.block(user, other_user)
1371
1372 conn =
1373 conn
1374 |> assign(:user, user)
1375 |> get("/api/v1/blocks")
1376
1377 other_user_id = to_string(other_user.id)
1378 assert [%{"id" => ^other_user_id}] = json_response_and_validate_schema(conn, 200)
1379 end
1380 end