Merge branch 'develop' into feature/reports-groups-and-multiple-state-update
[akkoma] / test / web / mastodon_api / controllers / account_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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.Repo
9 alias Pleroma.User
10 alias Pleroma.Web.ActivityPub.ActivityPub
11 alias Pleroma.Web.CommonAPI
12 alias Pleroma.Web.OAuth.Token
13
14 import Pleroma.Factory
15
16 describe "account fetching" do
17 test "works by id" do
18 user = insert(:user)
19
20 conn =
21 build_conn()
22 |> get("/api/v1/accounts/#{user.id}")
23
24 assert %{"id" => id} = json_response(conn, 200)
25 assert id == to_string(user.id)
26
27 conn =
28 build_conn()
29 |> get("/api/v1/accounts/-1")
30
31 assert %{"error" => "Can't find user"} = json_response(conn, 404)
32 end
33
34 test "works by nickname" do
35 user = insert(:user)
36
37 conn =
38 build_conn()
39 |> get("/api/v1/accounts/#{user.nickname}")
40
41 assert %{"id" => id} = json_response(conn, 200)
42 assert id == user.id
43 end
44
45 test "works by nickname for remote users" do
46 limit_to_local = Pleroma.Config.get([:instance, :limit_to_local_content])
47 Pleroma.Config.put([:instance, :limit_to_local_content], false)
48 user = insert(:user, nickname: "user@example.com", local: false)
49
50 conn =
51 build_conn()
52 |> get("/api/v1/accounts/#{user.nickname}")
53
54 Pleroma.Config.put([:instance, :limit_to_local_content], limit_to_local)
55 assert %{"id" => id} = json_response(conn, 200)
56 assert id == user.id
57 end
58
59 test "respects limit_to_local_content == :all for remote user nicknames" do
60 limit_to_local = Pleroma.Config.get([:instance, :limit_to_local_content])
61 Pleroma.Config.put([:instance, :limit_to_local_content], :all)
62
63 user = insert(:user, nickname: "user@example.com", local: false)
64
65 conn =
66 build_conn()
67 |> get("/api/v1/accounts/#{user.nickname}")
68
69 Pleroma.Config.put([:instance, :limit_to_local_content], limit_to_local)
70 assert json_response(conn, 404)
71 end
72
73 test "respects limit_to_local_content == :unauthenticated for remote user nicknames" do
74 limit_to_local = Pleroma.Config.get([:instance, :limit_to_local_content])
75 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
76
77 user = insert(:user, nickname: "user@example.com", local: false)
78 reading_user = insert(:user)
79
80 conn =
81 build_conn()
82 |> get("/api/v1/accounts/#{user.nickname}")
83
84 assert json_response(conn, 404)
85
86 conn =
87 build_conn()
88 |> assign(:user, reading_user)
89 |> get("/api/v1/accounts/#{user.nickname}")
90
91 Pleroma.Config.put([:instance, :limit_to_local_content], limit_to_local)
92 assert %{"id" => id} = json_response(conn, 200)
93 assert id == user.id
94 end
95
96 test "accounts fetches correct account for nicknames beginning with numbers", %{conn: conn} do
97 # Need to set an old-style integer ID to reproduce the problem
98 # (these are no longer assigned to new accounts but were preserved
99 # for existing accounts during the migration to flakeIDs)
100 user_one = insert(:user, %{id: 1212})
101 user_two = insert(:user, %{nickname: "#{user_one.id}garbage"})
102
103 resp_one =
104 conn
105 |> get("/api/v1/accounts/#{user_one.id}")
106
107 resp_two =
108 conn
109 |> get("/api/v1/accounts/#{user_two.nickname}")
110
111 resp_three =
112 conn
113 |> get("/api/v1/accounts/#{user_two.id}")
114
115 acc_one = json_response(resp_one, 200)
116 acc_two = json_response(resp_two, 200)
117 acc_three = json_response(resp_three, 200)
118 refute acc_one == acc_two
119 assert acc_two == acc_three
120 end
121 end
122
123 describe "user timelines" do
124 test "gets a users statuses", %{conn: conn} do
125 user_one = insert(:user)
126 user_two = insert(:user)
127 user_three = insert(:user)
128
129 {:ok, user_three} = User.follow(user_three, user_one)
130
131 {:ok, activity} = CommonAPI.post(user_one, %{"status" => "HI!!!"})
132
133 {:ok, direct_activity} =
134 CommonAPI.post(user_one, %{
135 "status" => "Hi, @#{user_two.nickname}.",
136 "visibility" => "direct"
137 })
138
139 {:ok, private_activity} =
140 CommonAPI.post(user_one, %{"status" => "private", "visibility" => "private"})
141
142 resp =
143 conn
144 |> get("/api/v1/accounts/#{user_one.id}/statuses")
145
146 assert [%{"id" => id}] = json_response(resp, 200)
147 assert id == to_string(activity.id)
148
149 resp =
150 conn
151 |> assign(:user, user_two)
152 |> get("/api/v1/accounts/#{user_one.id}/statuses")
153
154 assert [%{"id" => id_one}, %{"id" => id_two}] = json_response(resp, 200)
155 assert id_one == to_string(direct_activity.id)
156 assert id_two == to_string(activity.id)
157
158 resp =
159 conn
160 |> assign(:user, user_three)
161 |> get("/api/v1/accounts/#{user_one.id}/statuses")
162
163 assert [%{"id" => id_one}, %{"id" => id_two}] = json_response(resp, 200)
164 assert id_one == to_string(private_activity.id)
165 assert id_two == to_string(activity.id)
166 end
167
168 test "unimplemented pinned statuses feature", %{conn: conn} do
169 note = insert(:note_activity)
170 user = User.get_cached_by_ap_id(note.data["actor"])
171
172 conn =
173 conn
174 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
175
176 assert json_response(conn, 200) == []
177 end
178
179 test "gets an users media", %{conn: conn} do
180 note = insert(:note_activity)
181 user = User.get_cached_by_ap_id(note.data["actor"])
182
183 file = %Plug.Upload{
184 content_type: "image/jpg",
185 path: Path.absname("test/fixtures/image.jpg"),
186 filename: "an_image.jpg"
187 }
188
189 {:ok, %{id: media_id}} = ActivityPub.upload(file, actor: user.ap_id)
190
191 {:ok, image_post} = CommonAPI.post(user, %{"status" => "cofe", "media_ids" => [media_id]})
192
193 conn =
194 conn
195 |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "true"})
196
197 assert [%{"id" => id}] = json_response(conn, 200)
198 assert id == to_string(image_post.id)
199
200 conn =
201 build_conn()
202 |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "1"})
203
204 assert [%{"id" => id}] = json_response(conn, 200)
205 assert id == to_string(image_post.id)
206 end
207
208 test "gets a user's statuses without reblogs", %{conn: conn} do
209 user = insert(:user)
210 {:ok, post} = CommonAPI.post(user, %{"status" => "HI!!!"})
211 {:ok, _, _} = CommonAPI.repeat(post.id, user)
212
213 conn =
214 conn
215 |> get("/api/v1/accounts/#{user.id}/statuses", %{"exclude_reblogs" => "true"})
216
217 assert [%{"id" => id}] = json_response(conn, 200)
218 assert id == to_string(post.id)
219
220 conn =
221 conn
222 |> get("/api/v1/accounts/#{user.id}/statuses", %{"exclude_reblogs" => "1"})
223
224 assert [%{"id" => id}] = json_response(conn, 200)
225 assert id == to_string(post.id)
226 end
227
228 test "filters user's statuses by a hashtag", %{conn: conn} do
229 user = insert(:user)
230 {:ok, post} = CommonAPI.post(user, %{"status" => "#hashtag"})
231 {:ok, _post} = CommonAPI.post(user, %{"status" => "hashtag"})
232
233 conn =
234 conn
235 |> get("/api/v1/accounts/#{user.id}/statuses", %{"tagged" => "hashtag"})
236
237 assert [%{"id" => id}] = json_response(conn, 200)
238 assert id == to_string(post.id)
239 end
240 end
241
242 describe "followers" do
243 test "getting followers", %{conn: conn} do
244 user = insert(:user)
245 other_user = insert(:user)
246 {:ok, user} = User.follow(user, other_user)
247
248 conn =
249 conn
250 |> get("/api/v1/accounts/#{other_user.id}/followers")
251
252 assert [%{"id" => id}] = json_response(conn, 200)
253 assert id == to_string(user.id)
254 end
255
256 test "getting followers, hide_followers", %{conn: conn} do
257 user = insert(:user)
258 other_user = insert(:user, %{info: %{hide_followers: true}})
259 {:ok, _user} = User.follow(user, other_user)
260
261 conn =
262 conn
263 |> get("/api/v1/accounts/#{other_user.id}/followers")
264
265 assert [] == json_response(conn, 200)
266 end
267
268 test "getting followers, hide_followers, same user requesting", %{conn: conn} do
269 user = insert(:user)
270 other_user = insert(:user, %{info: %{hide_followers: true}})
271 {:ok, _user} = User.follow(user, other_user)
272
273 conn =
274 conn
275 |> assign(:user, other_user)
276 |> get("/api/v1/accounts/#{other_user.id}/followers")
277
278 refute [] == json_response(conn, 200)
279 end
280
281 test "getting followers, pagination", %{conn: conn} do
282 user = insert(:user)
283 follower1 = insert(:user)
284 follower2 = insert(:user)
285 follower3 = insert(:user)
286 {:ok, _} = User.follow(follower1, user)
287 {:ok, _} = User.follow(follower2, user)
288 {:ok, _} = User.follow(follower3, user)
289
290 conn =
291 conn
292 |> assign(:user, user)
293
294 res_conn =
295 conn
296 |> get("/api/v1/accounts/#{user.id}/followers?since_id=#{follower1.id}")
297
298 assert [%{"id" => id3}, %{"id" => id2}] = json_response(res_conn, 200)
299 assert id3 == follower3.id
300 assert id2 == follower2.id
301
302 res_conn =
303 conn
304 |> get("/api/v1/accounts/#{user.id}/followers?max_id=#{follower3.id}")
305
306 assert [%{"id" => id2}, %{"id" => id1}] = json_response(res_conn, 200)
307 assert id2 == follower2.id
308 assert id1 == follower1.id
309
310 res_conn =
311 conn
312 |> get("/api/v1/accounts/#{user.id}/followers?limit=1&max_id=#{follower3.id}")
313
314 assert [%{"id" => id2}] = json_response(res_conn, 200)
315 assert id2 == follower2.id
316
317 assert [link_header] = get_resp_header(res_conn, "link")
318 assert link_header =~ ~r/min_id=#{follower2.id}/
319 assert link_header =~ ~r/max_id=#{follower2.id}/
320 end
321 end
322
323 describe "following" do
324 test "getting following", %{conn: conn} do
325 user = insert(:user)
326 other_user = insert(:user)
327 {:ok, user} = User.follow(user, other_user)
328
329 conn =
330 conn
331 |> get("/api/v1/accounts/#{user.id}/following")
332
333 assert [%{"id" => id}] = json_response(conn, 200)
334 assert id == to_string(other_user.id)
335 end
336
337 test "getting following, hide_follows", %{conn: conn} do
338 user = insert(:user, %{info: %{hide_follows: true}})
339 other_user = insert(:user)
340 {:ok, user} = User.follow(user, other_user)
341
342 conn =
343 conn
344 |> get("/api/v1/accounts/#{user.id}/following")
345
346 assert [] == json_response(conn, 200)
347 end
348
349 test "getting following, hide_follows, same user requesting", %{conn: conn} do
350 user = insert(:user, %{info: %{hide_follows: true}})
351 other_user = insert(:user)
352 {:ok, user} = User.follow(user, other_user)
353
354 conn =
355 conn
356 |> assign(:user, user)
357 |> get("/api/v1/accounts/#{user.id}/following")
358
359 refute [] == json_response(conn, 200)
360 end
361
362 test "getting following, pagination", %{conn: conn} do
363 user = insert(:user)
364 following1 = insert(:user)
365 following2 = insert(:user)
366 following3 = insert(:user)
367 {:ok, _} = User.follow(user, following1)
368 {:ok, _} = User.follow(user, following2)
369 {:ok, _} = User.follow(user, following3)
370
371 conn =
372 conn
373 |> assign(:user, user)
374
375 res_conn =
376 conn
377 |> get("/api/v1/accounts/#{user.id}/following?since_id=#{following1.id}")
378
379 assert [%{"id" => id3}, %{"id" => id2}] = json_response(res_conn, 200)
380 assert id3 == following3.id
381 assert id2 == following2.id
382
383 res_conn =
384 conn
385 |> get("/api/v1/accounts/#{user.id}/following?max_id=#{following3.id}")
386
387 assert [%{"id" => id2}, %{"id" => id1}] = json_response(res_conn, 200)
388 assert id2 == following2.id
389 assert id1 == following1.id
390
391 res_conn =
392 conn
393 |> get("/api/v1/accounts/#{user.id}/following?limit=1&max_id=#{following3.id}")
394
395 assert [%{"id" => id2}] = json_response(res_conn, 200)
396 assert id2 == following2.id
397
398 assert [link_header] = get_resp_header(res_conn, "link")
399 assert link_header =~ ~r/min_id=#{following2.id}/
400 assert link_header =~ ~r/max_id=#{following2.id}/
401 end
402 end
403
404 describe "follow/unfollow" do
405 test "following / unfollowing a user", %{conn: conn} do
406 user = insert(:user)
407 other_user = insert(:user)
408
409 conn =
410 conn
411 |> assign(:user, user)
412 |> post("/api/v1/accounts/#{other_user.id}/follow")
413
414 assert %{"id" => _id, "following" => true} = json_response(conn, 200)
415
416 user = User.get_cached_by_id(user.id)
417
418 conn =
419 build_conn()
420 |> assign(:user, user)
421 |> post("/api/v1/accounts/#{other_user.id}/unfollow")
422
423 assert %{"id" => _id, "following" => false} = json_response(conn, 200)
424
425 user = User.get_cached_by_id(user.id)
426
427 conn =
428 build_conn()
429 |> assign(:user, user)
430 |> post("/api/v1/follows", %{"uri" => other_user.nickname})
431
432 assert %{"id" => id} = json_response(conn, 200)
433 assert id == to_string(other_user.id)
434 end
435
436 test "following without reblogs" do
437 follower = insert(:user)
438 followed = insert(:user)
439 other_user = insert(:user)
440
441 conn =
442 build_conn()
443 |> assign(:user, follower)
444 |> post("/api/v1/accounts/#{followed.id}/follow?reblogs=false")
445
446 assert %{"showing_reblogs" => false} = json_response(conn, 200)
447
448 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hey"})
449 {:ok, reblog, _} = CommonAPI.repeat(activity.id, followed)
450
451 conn =
452 build_conn()
453 |> assign(:user, User.get_cached_by_id(follower.id))
454 |> get("/api/v1/timelines/home")
455
456 assert [] == json_response(conn, 200)
457
458 conn =
459 build_conn()
460 |> assign(:user, follower)
461 |> post("/api/v1/accounts/#{followed.id}/follow?reblogs=true")
462
463 assert %{"showing_reblogs" => true} = json_response(conn, 200)
464
465 conn =
466 build_conn()
467 |> assign(:user, User.get_cached_by_id(follower.id))
468 |> get("/api/v1/timelines/home")
469
470 expected_activity_id = reblog.id
471 assert [%{"id" => ^expected_activity_id}] = json_response(conn, 200)
472 end
473
474 test "following / unfollowing errors" do
475 user = insert(:user)
476
477 conn =
478 build_conn()
479 |> assign(:user, user)
480
481 # self follow
482 conn_res = post(conn, "/api/v1/accounts/#{user.id}/follow")
483 assert %{"error" => "Record not found"} = json_response(conn_res, 404)
484
485 # self unfollow
486 user = User.get_cached_by_id(user.id)
487 conn_res = post(conn, "/api/v1/accounts/#{user.id}/unfollow")
488 assert %{"error" => "Record not found"} = json_response(conn_res, 404)
489
490 # self follow via uri
491 user = User.get_cached_by_id(user.id)
492 conn_res = post(conn, "/api/v1/follows", %{"uri" => user.nickname})
493 assert %{"error" => "Record not found"} = json_response(conn_res, 404)
494
495 # follow non existing user
496 conn_res = post(conn, "/api/v1/accounts/doesntexist/follow")
497 assert %{"error" => "Record not found"} = json_response(conn_res, 404)
498
499 # follow non existing user via uri
500 conn_res = post(conn, "/api/v1/follows", %{"uri" => "doesntexist"})
501 assert %{"error" => "Record not found"} = json_response(conn_res, 404)
502
503 # unfollow non existing user
504 conn_res = post(conn, "/api/v1/accounts/doesntexist/unfollow")
505 assert %{"error" => "Record not found"} = json_response(conn_res, 404)
506 end
507 end
508
509 describe "mute/unmute" do
510 test "with notifications", %{conn: conn} do
511 user = insert(:user)
512 other_user = insert(:user)
513
514 conn =
515 conn
516 |> assign(:user, user)
517 |> post("/api/v1/accounts/#{other_user.id}/mute")
518
519 response = json_response(conn, 200)
520
521 assert %{"id" => _id, "muting" => true, "muting_notifications" => true} = response
522 user = User.get_cached_by_id(user.id)
523
524 conn =
525 build_conn()
526 |> assign(:user, user)
527 |> post("/api/v1/accounts/#{other_user.id}/unmute")
528
529 response = json_response(conn, 200)
530 assert %{"id" => _id, "muting" => false, "muting_notifications" => false} = response
531 end
532
533 test "without notifications", %{conn: conn} do
534 user = insert(:user)
535 other_user = insert(:user)
536
537 conn =
538 conn
539 |> assign(:user, user)
540 |> post("/api/v1/accounts/#{other_user.id}/mute", %{"notifications" => "false"})
541
542 response = json_response(conn, 200)
543
544 assert %{"id" => _id, "muting" => true, "muting_notifications" => false} = response
545 user = User.get_cached_by_id(user.id)
546
547 conn =
548 build_conn()
549 |> assign(:user, user)
550 |> post("/api/v1/accounts/#{other_user.id}/unmute")
551
552 response = json_response(conn, 200)
553 assert %{"id" => _id, "muting" => false, "muting_notifications" => false} = response
554 end
555 end
556
557 describe "pinned statuses" do
558 setup do
559 user = insert(:user)
560 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
561
562 [user: user, activity: activity]
563 end
564
565 test "returns pinned statuses", %{conn: conn, user: user, activity: activity} do
566 {:ok, _} = CommonAPI.pin(activity.id, user)
567
568 result =
569 conn
570 |> assign(:user, user)
571 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
572 |> json_response(200)
573
574 id_str = to_string(activity.id)
575
576 assert [%{"id" => ^id_str, "pinned" => true}] = result
577 end
578 end
579
580 test "blocking / unblocking a user", %{conn: conn} do
581 user = insert(:user)
582 other_user = insert(:user)
583
584 conn =
585 conn
586 |> assign(:user, user)
587 |> post("/api/v1/accounts/#{other_user.id}/block")
588
589 assert %{"id" => _id, "blocking" => true} = json_response(conn, 200)
590
591 user = User.get_cached_by_id(user.id)
592
593 conn =
594 build_conn()
595 |> assign(:user, user)
596 |> post("/api/v1/accounts/#{other_user.id}/unblock")
597
598 assert %{"id" => _id, "blocking" => false} = json_response(conn, 200)
599 end
600
601 describe "create account by app" do
602 setup do
603 valid_params = %{
604 username: "lain",
605 email: "lain@example.org",
606 password: "PlzDontHackLain",
607 agreement: true
608 }
609
610 [valid_params: valid_params]
611 end
612
613 test "Account registration via Application", %{conn: conn} do
614 conn =
615 conn
616 |> post("/api/v1/apps", %{
617 client_name: "client_name",
618 redirect_uris: "urn:ietf:wg:oauth:2.0:oob",
619 scopes: "read, write, follow"
620 })
621
622 %{
623 "client_id" => client_id,
624 "client_secret" => client_secret,
625 "id" => _,
626 "name" => "client_name",
627 "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob",
628 "vapid_key" => _,
629 "website" => nil
630 } = json_response(conn, 200)
631
632 conn =
633 conn
634 |> post("/oauth/token", %{
635 grant_type: "client_credentials",
636 client_id: client_id,
637 client_secret: client_secret
638 })
639
640 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
641 json_response(conn, 200)
642
643 assert token
644 token_from_db = Repo.get_by(Token, token: token)
645 assert token_from_db
646 assert refresh
647 assert scope == "read write follow"
648
649 conn =
650 build_conn()
651 |> put_req_header("authorization", "Bearer " <> token)
652 |> post("/api/v1/accounts", %{
653 username: "lain",
654 email: "lain@example.org",
655 password: "PlzDontHackLain",
656 bio: "Test Bio",
657 agreement: true
658 })
659
660 %{
661 "access_token" => token,
662 "created_at" => _created_at,
663 "scope" => _scope,
664 "token_type" => "Bearer"
665 } = json_response(conn, 200)
666
667 token_from_db = Repo.get_by(Token, token: token)
668 assert token_from_db
669 token_from_db = Repo.preload(token_from_db, :user)
670 assert token_from_db.user
671
672 assert token_from_db.user.info.confirmation_pending
673 end
674
675 test "returns error when user already registred", %{conn: conn, valid_params: valid_params} do
676 _user = insert(:user, email: "lain@example.org")
677 app_token = insert(:oauth_token, user: nil)
678
679 conn =
680 conn
681 |> put_req_header("authorization", "Bearer " <> app_token.token)
682
683 res = post(conn, "/api/v1/accounts", valid_params)
684 assert json_response(res, 400) == %{"error" => "{\"email\":[\"has already been taken\"]}"}
685 end
686
687 test "rate limit", %{conn: conn} do
688 app_token = insert(:oauth_token, user: nil)
689
690 conn =
691 put_req_header(conn, "authorization", "Bearer " <> app_token.token)
692 |> Map.put(:remote_ip, {15, 15, 15, 15})
693
694 for i <- 1..5 do
695 conn =
696 conn
697 |> post("/api/v1/accounts", %{
698 username: "#{i}lain",
699 email: "#{i}lain@example.org",
700 password: "PlzDontHackLain",
701 agreement: true
702 })
703
704 %{
705 "access_token" => token,
706 "created_at" => _created_at,
707 "scope" => _scope,
708 "token_type" => "Bearer"
709 } = json_response(conn, 200)
710
711 token_from_db = Repo.get_by(Token, token: token)
712 assert token_from_db
713 token_from_db = Repo.preload(token_from_db, :user)
714 assert token_from_db.user
715
716 assert token_from_db.user.info.confirmation_pending
717 end
718
719 conn =
720 conn
721 |> post("/api/v1/accounts", %{
722 username: "6lain",
723 email: "6lain@example.org",
724 password: "PlzDontHackLain",
725 agreement: true
726 })
727
728 assert json_response(conn, :too_many_requests) == %{"error" => "Throttled"}
729 end
730
731 test "returns bad_request if missing required params", %{
732 conn: conn,
733 valid_params: valid_params
734 } do
735 app_token = insert(:oauth_token, user: nil)
736
737 conn =
738 conn
739 |> put_req_header("authorization", "Bearer " <> app_token.token)
740
741 res = post(conn, "/api/v1/accounts", valid_params)
742 assert json_response(res, 200)
743
744 [{127, 0, 0, 1}, {127, 0, 0, 2}, {127, 0, 0, 3}, {127, 0, 0, 4}]
745 |> Stream.zip(valid_params)
746 |> Enum.each(fn {ip, {attr, _}} ->
747 res =
748 conn
749 |> Map.put(:remote_ip, ip)
750 |> post("/api/v1/accounts", Map.delete(valid_params, attr))
751 |> json_response(400)
752
753 assert res == %{"error" => "Missing parameters"}
754 end)
755 end
756
757 test "returns forbidden if token is invalid", %{conn: conn, valid_params: valid_params} do
758 conn =
759 conn
760 |> put_req_header("authorization", "Bearer " <> "invalid-token")
761
762 res = post(conn, "/api/v1/accounts", valid_params)
763 assert json_response(res, 403) == %{"error" => "Invalid credentials"}
764 end
765 end
766
767 describe "GET /api/v1/accounts/:id/lists - account_lists" do
768 test "returns lists to which the account belongs", %{conn: conn} do
769 user = insert(:user)
770 other_user = insert(:user)
771 assert {:ok, %Pleroma.List{} = list} = Pleroma.List.create("Test List", user)
772 {:ok, %{following: _following}} = Pleroma.List.follow(list, other_user)
773
774 res =
775 conn
776 |> assign(:user, user)
777 |> get("/api/v1/accounts/#{other_user.id}/lists")
778 |> json_response(200)
779
780 assert res == [%{"id" => to_string(list.id), "title" => "Test List"}]
781 end
782 end
783
784 describe "verify_credentials" do
785 test "verify_credentials", %{conn: conn} do
786 user = insert(:user)
787
788 conn =
789 conn
790 |> assign(:user, user)
791 |> get("/api/v1/accounts/verify_credentials")
792
793 response = json_response(conn, 200)
794
795 assert %{"id" => id, "source" => %{"privacy" => "public"}} = response
796 assert response["pleroma"]["chat_token"]
797 assert id == to_string(user.id)
798 end
799
800 test "verify_credentials default scope unlisted", %{conn: conn} do
801 user = insert(:user, %{info: %User.Info{default_scope: "unlisted"}})
802
803 conn =
804 conn
805 |> assign(:user, user)
806 |> get("/api/v1/accounts/verify_credentials")
807
808 assert %{"id" => id, "source" => %{"privacy" => "unlisted"}} = json_response(conn, 200)
809 assert id == to_string(user.id)
810 end
811
812 test "locked accounts", %{conn: conn} do
813 user = insert(:user, %{info: %User.Info{default_scope: "private"}})
814
815 conn =
816 conn
817 |> assign(:user, user)
818 |> get("/api/v1/accounts/verify_credentials")
819
820 assert %{"id" => id, "source" => %{"privacy" => "private"}} = json_response(conn, 200)
821 assert id == to_string(user.id)
822 end
823 end
824
825 describe "user relationships" do
826 test "returns the relationships for the current user", %{conn: conn} do
827 user = insert(:user)
828 other_user = insert(:user)
829 {:ok, user} = User.follow(user, other_user)
830
831 conn =
832 conn
833 |> assign(:user, user)
834 |> get("/api/v1/accounts/relationships", %{"id" => [other_user.id]})
835
836 assert [relationship] = json_response(conn, 200)
837
838 assert to_string(other_user.id) == relationship["id"]
839 end
840
841 test "returns an empty list on a bad request", %{conn: conn} do
842 user = insert(:user)
843
844 conn =
845 conn
846 |> assign(:user, user)
847 |> get("/api/v1/accounts/relationships", %{})
848
849 assert [] = json_response(conn, 200)
850 end
851 end
852
853 test "getting a list of mutes", %{conn: conn} do
854 user = insert(:user)
855 other_user = insert(:user)
856
857 {:ok, user} = User.mute(user, other_user)
858
859 conn =
860 conn
861 |> assign(:user, user)
862 |> get("/api/v1/mutes")
863
864 other_user_id = to_string(other_user.id)
865 assert [%{"id" => ^other_user_id}] = json_response(conn, 200)
866 end
867
868 test "getting a list of blocks", %{conn: conn} do
869 user = insert(:user)
870 other_user = insert(:user)
871
872 {:ok, user} = User.block(user, other_user)
873
874 conn =
875 conn
876 |> assign(:user, user)
877 |> get("/api/v1/blocks")
878
879 other_user_id = to_string(other_user.id)
880 assert [%{"id" => ^other_user_id}] = json_response(conn, 200)
881 end
882 end