Merge branch 'improve-user-info-migration' into 'develop'
[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
241 test "the user views their own timelines and excludes direct messages", %{conn: conn} do
242 user = insert(:user)
243 {:ok, public_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "public"})
244 {:ok, _direct_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
245
246 conn =
247 conn
248 |> assign(:user, user)
249 |> get("/api/v1/accounts/#{user.id}/statuses", %{"exclude_visibilities" => ["direct"]})
250
251 assert [%{"id" => id}] = json_response(conn, 200)
252 assert id == to_string(public_activity.id)
253 end
254 end
255
256 describe "followers" do
257 test "getting followers", %{conn: conn} do
258 user = insert(:user)
259 other_user = insert(:user)
260 {:ok, user} = User.follow(user, other_user)
261
262 conn =
263 conn
264 |> get("/api/v1/accounts/#{other_user.id}/followers")
265
266 assert [%{"id" => id}] = json_response(conn, 200)
267 assert id == to_string(user.id)
268 end
269
270 test "getting followers, hide_followers", %{conn: conn} do
271 user = insert(:user)
272 other_user = insert(:user, hide_followers: true)
273 {:ok, _user} = User.follow(user, other_user)
274
275 conn =
276 conn
277 |> get("/api/v1/accounts/#{other_user.id}/followers")
278
279 assert [] == json_response(conn, 200)
280 end
281
282 test "getting followers, hide_followers, same user requesting", %{conn: conn} do
283 user = insert(:user)
284 other_user = insert(:user, hide_followers: true)
285 {:ok, _user} = User.follow(user, other_user)
286
287 conn =
288 conn
289 |> assign(:user, other_user)
290 |> get("/api/v1/accounts/#{other_user.id}/followers")
291
292 refute [] == json_response(conn, 200)
293 end
294
295 test "getting followers, pagination", %{conn: conn} do
296 user = insert(:user)
297 follower1 = insert(:user)
298 follower2 = insert(:user)
299 follower3 = insert(:user)
300 {:ok, _} = User.follow(follower1, user)
301 {:ok, _} = User.follow(follower2, user)
302 {:ok, _} = User.follow(follower3, user)
303
304 conn =
305 conn
306 |> assign(:user, user)
307
308 res_conn =
309 conn
310 |> get("/api/v1/accounts/#{user.id}/followers?since_id=#{follower1.id}")
311
312 assert [%{"id" => id3}, %{"id" => id2}] = json_response(res_conn, 200)
313 assert id3 == follower3.id
314 assert id2 == follower2.id
315
316 res_conn =
317 conn
318 |> get("/api/v1/accounts/#{user.id}/followers?max_id=#{follower3.id}")
319
320 assert [%{"id" => id2}, %{"id" => id1}] = json_response(res_conn, 200)
321 assert id2 == follower2.id
322 assert id1 == follower1.id
323
324 res_conn =
325 conn
326 |> get("/api/v1/accounts/#{user.id}/followers?limit=1&max_id=#{follower3.id}")
327
328 assert [%{"id" => id2}] = json_response(res_conn, 200)
329 assert id2 == follower2.id
330
331 assert [link_header] = get_resp_header(res_conn, "link")
332 assert link_header =~ ~r/min_id=#{follower2.id}/
333 assert link_header =~ ~r/max_id=#{follower2.id}/
334 end
335 end
336
337 describe "following" do
338 test "getting following", %{conn: conn} do
339 user = insert(:user)
340 other_user = insert(:user)
341 {:ok, user} = User.follow(user, other_user)
342
343 conn =
344 conn
345 |> get("/api/v1/accounts/#{user.id}/following")
346
347 assert [%{"id" => id}] = json_response(conn, 200)
348 assert id == to_string(other_user.id)
349 end
350
351 test "getting following, hide_follows", %{conn: conn} do
352 user = insert(:user, hide_follows: true)
353 other_user = insert(:user)
354 {:ok, user} = User.follow(user, other_user)
355
356 conn =
357 conn
358 |> get("/api/v1/accounts/#{user.id}/following")
359
360 assert [] == json_response(conn, 200)
361 end
362
363 test "getting following, hide_follows, same user requesting", %{conn: conn} do
364 user = insert(:user, hide_follows: true)
365 other_user = insert(:user)
366 {:ok, user} = User.follow(user, other_user)
367
368 conn =
369 conn
370 |> assign(:user, user)
371 |> get("/api/v1/accounts/#{user.id}/following")
372
373 refute [] == json_response(conn, 200)
374 end
375
376 test "getting following, pagination", %{conn: conn} do
377 user = insert(:user)
378 following1 = insert(:user)
379 following2 = insert(:user)
380 following3 = insert(:user)
381 {:ok, _} = User.follow(user, following1)
382 {:ok, _} = User.follow(user, following2)
383 {:ok, _} = User.follow(user, following3)
384
385 conn =
386 conn
387 |> assign(:user, user)
388
389 res_conn =
390 conn
391 |> get("/api/v1/accounts/#{user.id}/following?since_id=#{following1.id}")
392
393 assert [%{"id" => id3}, %{"id" => id2}] = json_response(res_conn, 200)
394 assert id3 == following3.id
395 assert id2 == following2.id
396
397 res_conn =
398 conn
399 |> get("/api/v1/accounts/#{user.id}/following?max_id=#{following3.id}")
400
401 assert [%{"id" => id2}, %{"id" => id1}] = json_response(res_conn, 200)
402 assert id2 == following2.id
403 assert id1 == following1.id
404
405 res_conn =
406 conn
407 |> get("/api/v1/accounts/#{user.id}/following?limit=1&max_id=#{following3.id}")
408
409 assert [%{"id" => id2}] = json_response(res_conn, 200)
410 assert id2 == following2.id
411
412 assert [link_header] = get_resp_header(res_conn, "link")
413 assert link_header =~ ~r/min_id=#{following2.id}/
414 assert link_header =~ ~r/max_id=#{following2.id}/
415 end
416 end
417
418 describe "follow/unfollow" do
419 test "following / unfollowing a user", %{conn: conn} do
420 user = insert(:user)
421 other_user = insert(:user)
422
423 conn =
424 conn
425 |> assign(:user, user)
426 |> post("/api/v1/accounts/#{other_user.id}/follow")
427
428 assert %{"id" => _id, "following" => true} = json_response(conn, 200)
429
430 user = User.get_cached_by_id(user.id)
431
432 conn =
433 build_conn()
434 |> assign(:user, user)
435 |> post("/api/v1/accounts/#{other_user.id}/unfollow")
436
437 assert %{"id" => _id, "following" => false} = json_response(conn, 200)
438
439 user = User.get_cached_by_id(user.id)
440
441 conn =
442 build_conn()
443 |> assign(:user, user)
444 |> post("/api/v1/follows", %{"uri" => other_user.nickname})
445
446 assert %{"id" => id} = json_response(conn, 200)
447 assert id == to_string(other_user.id)
448 end
449
450 test "following without reblogs" do
451 follower = insert(:user)
452 followed = insert(:user)
453 other_user = insert(:user)
454
455 conn =
456 build_conn()
457 |> assign(:user, follower)
458 |> post("/api/v1/accounts/#{followed.id}/follow?reblogs=false")
459
460 assert %{"showing_reblogs" => false} = json_response(conn, 200)
461
462 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hey"})
463 {:ok, reblog, _} = CommonAPI.repeat(activity.id, followed)
464
465 conn =
466 build_conn()
467 |> assign(:user, User.get_cached_by_id(follower.id))
468 |> get("/api/v1/timelines/home")
469
470 assert [] == json_response(conn, 200)
471
472 conn =
473 build_conn()
474 |> assign(:user, follower)
475 |> post("/api/v1/accounts/#{followed.id}/follow?reblogs=true")
476
477 assert %{"showing_reblogs" => true} = json_response(conn, 200)
478
479 conn =
480 build_conn()
481 |> assign(:user, User.get_cached_by_id(follower.id))
482 |> get("/api/v1/timelines/home")
483
484 expected_activity_id = reblog.id
485 assert [%{"id" => ^expected_activity_id}] = json_response(conn, 200)
486 end
487
488 test "following / unfollowing errors" do
489 user = insert(:user)
490
491 conn =
492 build_conn()
493 |> assign(:user, user)
494
495 # self follow
496 conn_res = post(conn, "/api/v1/accounts/#{user.id}/follow")
497 assert %{"error" => "Record not found"} = json_response(conn_res, 404)
498
499 # self unfollow
500 user = User.get_cached_by_id(user.id)
501 conn_res = post(conn, "/api/v1/accounts/#{user.id}/unfollow")
502 assert %{"error" => "Record not found"} = json_response(conn_res, 404)
503
504 # self follow via uri
505 user = User.get_cached_by_id(user.id)
506 conn_res = post(conn, "/api/v1/follows", %{"uri" => user.nickname})
507 assert %{"error" => "Record not found"} = json_response(conn_res, 404)
508
509 # follow non existing user
510 conn_res = post(conn, "/api/v1/accounts/doesntexist/follow")
511 assert %{"error" => "Record not found"} = json_response(conn_res, 404)
512
513 # follow non existing user via uri
514 conn_res = post(conn, "/api/v1/follows", %{"uri" => "doesntexist"})
515 assert %{"error" => "Record not found"} = json_response(conn_res, 404)
516
517 # unfollow non existing user
518 conn_res = post(conn, "/api/v1/accounts/doesntexist/unfollow")
519 assert %{"error" => "Record not found"} = json_response(conn_res, 404)
520 end
521 end
522
523 describe "mute/unmute" do
524 test "with notifications", %{conn: conn} do
525 user = insert(:user)
526 other_user = insert(:user)
527
528 conn =
529 conn
530 |> assign(:user, user)
531 |> post("/api/v1/accounts/#{other_user.id}/mute")
532
533 response = json_response(conn, 200)
534
535 assert %{"id" => _id, "muting" => true, "muting_notifications" => true} = response
536 user = User.get_cached_by_id(user.id)
537
538 conn =
539 build_conn()
540 |> assign(:user, user)
541 |> post("/api/v1/accounts/#{other_user.id}/unmute")
542
543 response = json_response(conn, 200)
544 assert %{"id" => _id, "muting" => false, "muting_notifications" => false} = response
545 end
546
547 test "without notifications", %{conn: conn} do
548 user = insert(:user)
549 other_user = insert(:user)
550
551 conn =
552 conn
553 |> assign(:user, user)
554 |> post("/api/v1/accounts/#{other_user.id}/mute", %{"notifications" => "false"})
555
556 response = json_response(conn, 200)
557
558 assert %{"id" => _id, "muting" => true, "muting_notifications" => false} = response
559 user = User.get_cached_by_id(user.id)
560
561 conn =
562 build_conn()
563 |> assign(:user, user)
564 |> post("/api/v1/accounts/#{other_user.id}/unmute")
565
566 response = json_response(conn, 200)
567 assert %{"id" => _id, "muting" => false, "muting_notifications" => false} = response
568 end
569 end
570
571 describe "pinned statuses" do
572 setup do
573 user = insert(:user)
574 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
575
576 [user: user, activity: activity]
577 end
578
579 test "returns pinned statuses", %{conn: conn, user: user, activity: activity} do
580 {:ok, _} = CommonAPI.pin(activity.id, user)
581
582 result =
583 conn
584 |> assign(:user, user)
585 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
586 |> json_response(200)
587
588 id_str = to_string(activity.id)
589
590 assert [%{"id" => ^id_str, "pinned" => true}] = result
591 end
592 end
593
594 test "blocking / unblocking a user", %{conn: conn} do
595 user = insert(:user)
596 other_user = insert(:user)
597
598 conn =
599 conn
600 |> assign(:user, user)
601 |> post("/api/v1/accounts/#{other_user.id}/block")
602
603 assert %{"id" => _id, "blocking" => true} = json_response(conn, 200)
604
605 user = User.get_cached_by_id(user.id)
606
607 conn =
608 build_conn()
609 |> assign(:user, user)
610 |> post("/api/v1/accounts/#{other_user.id}/unblock")
611
612 assert %{"id" => _id, "blocking" => false} = json_response(conn, 200)
613 end
614
615 describe "create account by app" do
616 setup do
617 valid_params = %{
618 username: "lain",
619 email: "lain@example.org",
620 password: "PlzDontHackLain",
621 agreement: true
622 }
623
624 [valid_params: valid_params]
625 end
626
627 test "Account registration via Application", %{conn: conn} do
628 conn =
629 conn
630 |> post("/api/v1/apps", %{
631 client_name: "client_name",
632 redirect_uris: "urn:ietf:wg:oauth:2.0:oob",
633 scopes: "read, write, follow"
634 })
635
636 %{
637 "client_id" => client_id,
638 "client_secret" => client_secret,
639 "id" => _,
640 "name" => "client_name",
641 "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob",
642 "vapid_key" => _,
643 "website" => nil
644 } = json_response(conn, 200)
645
646 conn =
647 conn
648 |> post("/oauth/token", %{
649 grant_type: "client_credentials",
650 client_id: client_id,
651 client_secret: client_secret
652 })
653
654 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
655 json_response(conn, 200)
656
657 assert token
658 token_from_db = Repo.get_by(Token, token: token)
659 assert token_from_db
660 assert refresh
661 assert scope == "read write follow"
662
663 conn =
664 build_conn()
665 |> put_req_header("authorization", "Bearer " <> token)
666 |> post("/api/v1/accounts", %{
667 username: "lain",
668 email: "lain@example.org",
669 password: "PlzDontHackLain",
670 bio: "Test Bio",
671 agreement: true
672 })
673
674 %{
675 "access_token" => token,
676 "created_at" => _created_at,
677 "scope" => _scope,
678 "token_type" => "Bearer"
679 } = json_response(conn, 200)
680
681 token_from_db = Repo.get_by(Token, token: token)
682 assert token_from_db
683 token_from_db = Repo.preload(token_from_db, :user)
684 assert token_from_db.user
685
686 assert token_from_db.user.confirmation_pending
687 end
688
689 test "returns error when user already registred", %{conn: conn, valid_params: valid_params} do
690 _user = insert(:user, email: "lain@example.org")
691 app_token = insert(:oauth_token, user: nil)
692
693 conn =
694 conn
695 |> put_req_header("authorization", "Bearer " <> app_token.token)
696
697 res = post(conn, "/api/v1/accounts", valid_params)
698 assert json_response(res, 400) == %{"error" => "{\"email\":[\"has already been taken\"]}"}
699 end
700
701 test "rate limit", %{conn: conn} do
702 app_token = insert(:oauth_token, user: nil)
703
704 conn =
705 put_req_header(conn, "authorization", "Bearer " <> app_token.token)
706 |> Map.put(:remote_ip, {15, 15, 15, 15})
707
708 for i <- 1..5 do
709 conn =
710 conn
711 |> post("/api/v1/accounts", %{
712 username: "#{i}lain",
713 email: "#{i}lain@example.org",
714 password: "PlzDontHackLain",
715 agreement: true
716 })
717
718 %{
719 "access_token" => token,
720 "created_at" => _created_at,
721 "scope" => _scope,
722 "token_type" => "Bearer"
723 } = json_response(conn, 200)
724
725 token_from_db = Repo.get_by(Token, token: token)
726 assert token_from_db
727 token_from_db = Repo.preload(token_from_db, :user)
728 assert token_from_db.user
729
730 assert token_from_db.user.confirmation_pending
731 end
732
733 conn =
734 conn
735 |> post("/api/v1/accounts", %{
736 username: "6lain",
737 email: "6lain@example.org",
738 password: "PlzDontHackLain",
739 agreement: true
740 })
741
742 assert json_response(conn, :too_many_requests) == %{"error" => "Throttled"}
743 end
744
745 test "returns bad_request if missing required params", %{
746 conn: conn,
747 valid_params: valid_params
748 } do
749 app_token = insert(:oauth_token, user: nil)
750
751 conn =
752 conn
753 |> put_req_header("authorization", "Bearer " <> app_token.token)
754
755 res = post(conn, "/api/v1/accounts", valid_params)
756 assert json_response(res, 200)
757
758 [{127, 0, 0, 1}, {127, 0, 0, 2}, {127, 0, 0, 3}, {127, 0, 0, 4}]
759 |> Stream.zip(valid_params)
760 |> Enum.each(fn {ip, {attr, _}} ->
761 res =
762 conn
763 |> Map.put(:remote_ip, ip)
764 |> post("/api/v1/accounts", Map.delete(valid_params, attr))
765 |> json_response(400)
766
767 assert res == %{"error" => "Missing parameters"}
768 end)
769 end
770
771 test "returns forbidden if token is invalid", %{conn: conn, valid_params: valid_params} do
772 conn =
773 conn
774 |> put_req_header("authorization", "Bearer " <> "invalid-token")
775
776 res = post(conn, "/api/v1/accounts", valid_params)
777 assert json_response(res, 403) == %{"error" => "Invalid credentials"}
778 end
779 end
780
781 describe "GET /api/v1/accounts/:id/lists - account_lists" do
782 test "returns lists to which the account belongs", %{conn: conn} do
783 user = insert(:user)
784 other_user = insert(:user)
785 assert {:ok, %Pleroma.List{} = list} = Pleroma.List.create("Test List", user)
786 {:ok, %{following: _following}} = Pleroma.List.follow(list, other_user)
787
788 res =
789 conn
790 |> assign(:user, user)
791 |> get("/api/v1/accounts/#{other_user.id}/lists")
792 |> json_response(200)
793
794 assert res == [%{"id" => to_string(list.id), "title" => "Test List"}]
795 end
796 end
797
798 describe "verify_credentials" do
799 test "verify_credentials", %{conn: conn} do
800 user = insert(:user)
801
802 conn =
803 conn
804 |> assign(:user, user)
805 |> get("/api/v1/accounts/verify_credentials")
806
807 response = json_response(conn, 200)
808
809 assert %{"id" => id, "source" => %{"privacy" => "public"}} = response
810 assert response["pleroma"]["chat_token"]
811 assert id == to_string(user.id)
812 end
813
814 test "verify_credentials default scope unlisted", %{conn: conn} do
815 user = insert(:user, default_scope: "unlisted")
816
817 conn =
818 conn
819 |> assign(:user, user)
820 |> get("/api/v1/accounts/verify_credentials")
821
822 assert %{"id" => id, "source" => %{"privacy" => "unlisted"}} = json_response(conn, 200)
823 assert id == to_string(user.id)
824 end
825
826 test "locked accounts", %{conn: conn} do
827 user = insert(:user, default_scope: "private")
828
829 conn =
830 conn
831 |> assign(:user, user)
832 |> get("/api/v1/accounts/verify_credentials")
833
834 assert %{"id" => id, "source" => %{"privacy" => "private"}} = json_response(conn, 200)
835 assert id == to_string(user.id)
836 end
837 end
838
839 describe "user relationships" do
840 test "returns the relationships for the current user", %{conn: conn} do
841 user = insert(:user)
842 other_user = insert(:user)
843 {:ok, user} = User.follow(user, other_user)
844
845 conn =
846 conn
847 |> assign(:user, user)
848 |> get("/api/v1/accounts/relationships", %{"id" => [other_user.id]})
849
850 assert [relationship] = json_response(conn, 200)
851
852 assert to_string(other_user.id) == relationship["id"]
853 end
854
855 test "returns an empty list on a bad request", %{conn: conn} do
856 user = insert(:user)
857
858 conn =
859 conn
860 |> assign(:user, user)
861 |> get("/api/v1/accounts/relationships", %{})
862
863 assert [] = json_response(conn, 200)
864 end
865 end
866
867 test "getting a list of mutes", %{conn: conn} do
868 user = insert(:user)
869 other_user = insert(:user)
870
871 {:ok, user} = User.mute(user, other_user)
872
873 conn =
874 conn
875 |> assign(:user, user)
876 |> get("/api/v1/mutes")
877
878 other_user_id = to_string(other_user.id)
879 assert [%{"id" => ^other_user_id}] = json_response(conn, 200)
880 end
881
882 test "getting a list of blocks", %{conn: conn} do
883 user = insert(:user)
884 other_user = insert(:user)
885
886 {:ok, user} = User.block(user, other_user)
887
888 conn =
889 conn
890 |> assign(:user, user)
891 |> get("/api/v1/blocks")
892
893 other_user_id = to_string(other_user.id)
894 assert [%{"id" => ^other_user_id}] = json_response(conn, 200)
895 end
896 end