Merge branch 'support/issue_449' into 'develop'
[akkoma] / test / web / twitter_api / twitter_api_controller_test.exs
1 defmodule Pleroma.Web.TwitterAPI.ControllerTest do
2 use Pleroma.Web.ConnCase
3 alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
4 alias Pleroma.Builders.{ActivityBuilder, UserBuilder}
5 alias Pleroma.{Repo, Activity, User, Object, Notification}
6 alias Pleroma.Web.ActivityPub.ActivityPub
7 alias Pleroma.Web.TwitterAPI.UserView
8 alias Pleroma.Web.TwitterAPI.NotificationView
9 alias Pleroma.Web.CommonAPI
10 alias Pleroma.Web.TwitterAPI.TwitterAPI
11 alias Comeonin.Pbkdf2
12 alias Ecto.Changeset
13
14 import Pleroma.Factory
15
16 @banner "data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7"
17
18 describe "POST /api/account/update_profile_banner" do
19 test "it updates the banner", %{conn: conn} do
20 user = insert(:user)
21
22 conn
23 |> assign(:user, user)
24 |> post(authenticated_twitter_api__path(conn, :update_banner), %{"banner" => @banner})
25 |> json_response(200)
26
27 user = refresh_record(user)
28 assert user.info.banner["type"] == "Image"
29 end
30 end
31
32 describe "POST /api/qvitter/update_background_image" do
33 test "it updates the background", %{conn: conn} do
34 user = insert(:user)
35
36 conn
37 |> assign(:user, user)
38 |> post(authenticated_twitter_api__path(conn, :update_background), %{"img" => @banner})
39 |> json_response(200)
40
41 user = refresh_record(user)
42 assert user.info.background["type"] == "Image"
43 end
44 end
45
46 describe "POST /api/account/verify_credentials" do
47 setup [:valid_user]
48
49 test "without valid credentials", %{conn: conn} do
50 conn = post(conn, "/api/account/verify_credentials.json")
51 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
52 end
53
54 test "with credentials", %{conn: conn, user: user} do
55 response =
56 conn
57 |> with_credentials(user.nickname, "test")
58 |> post("/api/account/verify_credentials.json")
59 |> json_response(200)
60
61 assert response == UserView.render("show.json", %{user: user, token: response["token"]})
62 end
63 end
64
65 describe "POST /statuses/update.json" do
66 setup [:valid_user]
67
68 test "without valid credentials", %{conn: conn} do
69 conn = post(conn, "/api/statuses/update.json")
70 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
71 end
72
73 test "with credentials", %{conn: conn, user: user} do
74 conn_with_creds = conn |> with_credentials(user.nickname, "test")
75 request_path = "/api/statuses/update.json"
76
77 error_response = %{
78 "request" => request_path,
79 "error" => "Client must provide a 'status' parameter with a value."
80 }
81
82 conn =
83 conn_with_creds
84 |> post(request_path)
85
86 assert json_response(conn, 400) == error_response
87
88 conn =
89 conn_with_creds
90 |> post(request_path, %{status: ""})
91
92 assert json_response(conn, 400) == error_response
93
94 conn =
95 conn_with_creds
96 |> post(request_path, %{status: " "})
97
98 assert json_response(conn, 400) == error_response
99
100 # we post with visibility private in order to avoid triggering relay
101 conn =
102 conn_with_creds
103 |> post(request_path, %{status: "Nice meme.", visibility: "private"})
104
105 assert json_response(conn, 200) ==
106 ActivityRepresenter.to_map(Repo.one(Activity), %{user: user})
107 end
108 end
109
110 describe "GET /statuses/public_timeline.json" do
111 test "returns statuses", %{conn: conn} do
112 user = insert(:user)
113 activities = ActivityBuilder.insert_list(30, %{}, %{user: user})
114 ActivityBuilder.insert_list(10, %{}, %{user: user})
115 since_id = List.last(activities).id
116
117 conn =
118 conn
119 |> get("/api/statuses/public_timeline.json", %{since_id: since_id})
120
121 response = json_response(conn, 200)
122
123 assert length(response) == 10
124 end
125
126 test "returns 403 to unauthenticated request when the instance is not public", %{conn: conn} do
127 instance =
128 Application.get_env(:pleroma, :instance)
129 |> Keyword.put(:public, false)
130
131 Application.put_env(:pleroma, :instance, instance)
132
133 conn
134 |> get("/api/statuses/public_timeline.json")
135 |> json_response(403)
136
137 instance =
138 Application.get_env(:pleroma, :instance)
139 |> Keyword.put(:public, true)
140
141 Application.put_env(:pleroma, :instance, instance)
142 end
143
144 test "returns 200 to unauthenticated request when the instance is public", %{conn: conn} do
145 conn
146 |> get("/api/statuses/public_timeline.json")
147 |> json_response(200)
148 end
149 end
150
151 describe "GET /statuses/public_and_external_timeline.json" do
152 test "returns 403 to unauthenticated request when the instance is not public", %{conn: conn} do
153 instance =
154 Application.get_env(:pleroma, :instance)
155 |> Keyword.put(:public, false)
156
157 Application.put_env(:pleroma, :instance, instance)
158
159 conn
160 |> get("/api/statuses/public_and_external_timeline.json")
161 |> json_response(403)
162
163 instance =
164 Application.get_env(:pleroma, :instance)
165 |> Keyword.put(:public, true)
166
167 Application.put_env(:pleroma, :instance, instance)
168 end
169
170 test "returns 200 to unauthenticated request when the instance is public", %{conn: conn} do
171 conn
172 |> get("/api/statuses/public_and_external_timeline.json")
173 |> json_response(200)
174 end
175 end
176
177 describe "GET /statuses/show/:id.json" do
178 test "returns one status", %{conn: conn} do
179 user = insert(:user)
180 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey!"})
181 actor = Repo.get_by!(User, ap_id: activity.data["actor"])
182
183 conn =
184 conn
185 |> get("/api/statuses/show/#{activity.id}.json")
186
187 response = json_response(conn, 200)
188
189 assert response == ActivityRepresenter.to_map(activity, %{user: actor})
190 end
191 end
192
193 describe "GET /users/show.json" do
194 test "gets user with screen_name", %{conn: conn} do
195 user = insert(:user)
196
197 conn =
198 conn
199 |> get("/api/users/show.json", %{"screen_name" => user.nickname})
200
201 response = json_response(conn, 200)
202
203 assert response["id"] == user.id
204 end
205
206 test "gets user with user_id", %{conn: conn} do
207 user = insert(:user)
208
209 conn =
210 conn
211 |> get("/api/users/show.json", %{"user_id" => user.id})
212
213 response = json_response(conn, 200)
214
215 assert response["id"] == user.id
216 end
217
218 test "gets a user for a logged in user", %{conn: conn} do
219 user = insert(:user)
220 logged_in = insert(:user)
221
222 {:ok, logged_in, user, _activity} = TwitterAPI.follow(logged_in, %{"user_id" => user.id})
223
224 conn =
225 conn
226 |> with_credentials(logged_in.nickname, "test")
227 |> get("/api/users/show.json", %{"user_id" => user.id})
228
229 response = json_response(conn, 200)
230
231 assert response["following"] == true
232 end
233 end
234
235 describe "GET /statusnet/conversation/:id.json" do
236 test "returns the statuses in the conversation", %{conn: conn} do
237 {:ok, _user} = UserBuilder.insert()
238 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
239 {:ok, _activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
240 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
241
242 conn =
243 conn
244 |> get("/api/statusnet/conversation/#{activity.data["context_id"]}.json")
245
246 response = json_response(conn, 200)
247
248 assert length(response) == 2
249 end
250 end
251
252 describe "GET /statuses/friends_timeline.json" do
253 setup [:valid_user]
254
255 test "without valid credentials", %{conn: conn} do
256 conn = get(conn, "/api/statuses/friends_timeline.json")
257 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
258 end
259
260 test "with credentials", %{conn: conn, user: current_user} do
261 user = insert(:user)
262
263 activities =
264 ActivityBuilder.insert_list(30, %{"to" => [User.ap_followers(user)]}, %{user: user})
265
266 returned_activities =
267 ActivityBuilder.insert_list(10, %{"to" => [User.ap_followers(user)]}, %{user: user})
268
269 other_user = insert(:user)
270 ActivityBuilder.insert_list(10, %{}, %{user: other_user})
271 since_id = List.last(activities).id
272
273 current_user =
274 Changeset.change(current_user, following: [User.ap_followers(user)])
275 |> Repo.update!()
276
277 conn =
278 conn
279 |> with_credentials(current_user.nickname, "test")
280 |> get("/api/statuses/friends_timeline.json", %{since_id: since_id})
281
282 response = json_response(conn, 200)
283
284 assert length(response) == 10
285
286 assert response ==
287 Enum.map(returned_activities, fn activity ->
288 ActivityRepresenter.to_map(activity, %{
289 user: User.get_cached_by_ap_id(activity.data["actor"]),
290 for: current_user
291 })
292 end)
293 end
294 end
295
296 describe "GET /statuses/dm_timeline.json" do
297 test "it show direct messages", %{conn: conn} do
298 user_one = insert(:user)
299 user_two = insert(:user)
300
301 {:ok, user_two} = User.follow(user_two, user_one)
302
303 {:ok, direct} =
304 CommonAPI.post(user_one, %{
305 "status" => "Hi @#{user_two.nickname}!",
306 "visibility" => "direct"
307 })
308
309 {:ok, direct_two} =
310 CommonAPI.post(user_two, %{
311 "status" => "Hi @#{user_one.nickname}!",
312 "visibility" => "direct"
313 })
314
315 {:ok, _follower_only} =
316 CommonAPI.post(user_one, %{
317 "status" => "Hi @#{user_two.nickname}!",
318 "visibility" => "private"
319 })
320
321 # Only direct should be visible here
322 res_conn =
323 conn
324 |> assign(:user, user_two)
325 |> get("/api/statuses/dm_timeline.json")
326
327 [status, status_two] = json_response(res_conn, 200)
328 assert status["id"] == direct_two.id
329 assert status_two["id"] == direct.id
330 end
331 end
332
333 describe "GET /statuses/mentions.json" do
334 setup [:valid_user]
335
336 test "without valid credentials", %{conn: conn} do
337 conn = get(conn, "/api/statuses/mentions.json")
338 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
339 end
340
341 test "with credentials", %{conn: conn, user: current_user} do
342 {:ok, activity} =
343 ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: current_user})
344
345 conn =
346 conn
347 |> with_credentials(current_user.nickname, "test")
348 |> get("/api/statuses/mentions.json")
349
350 response = json_response(conn, 200)
351
352 assert length(response) == 1
353
354 assert Enum.at(response, 0) ==
355 ActivityRepresenter.to_map(activity, %{
356 user: current_user,
357 mentioned: [current_user]
358 })
359 end
360 end
361
362 describe "GET /api/qvitter/statuses/notifications.json" do
363 setup [:valid_user]
364
365 test "without valid credentials", %{conn: conn} do
366 conn = get(conn, "/api/qvitter/statuses/notifications.json")
367 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
368 end
369
370 test "with credentials", %{conn: conn, user: current_user} do
371 other_user = insert(:user)
372
373 {:ok, _activity} =
374 ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: other_user})
375
376 conn =
377 conn
378 |> with_credentials(current_user.nickname, "test")
379 |> get("/api/qvitter/statuses/notifications.json")
380
381 response = json_response(conn, 200)
382
383 assert length(response) == 1
384
385 assert response ==
386 NotificationView.render("notification.json", %{
387 notifications: Notification.for_user(current_user),
388 for: current_user
389 })
390 end
391 end
392
393 describe "POST /api/qvitter/statuses/notifications/read" do
394 setup [:valid_user]
395
396 test "without valid credentials", %{conn: conn} do
397 conn = post(conn, "/api/qvitter/statuses/notifications/read", %{"latest_id" => 1_234_567})
398 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
399 end
400
401 test "with credentials, without any params", %{conn: conn, user: current_user} do
402 conn =
403 conn
404 |> with_credentials(current_user.nickname, "test")
405 |> post("/api/qvitter/statuses/notifications/read")
406
407 assert json_response(conn, 400) == %{
408 "error" => "You need to specify latest_id",
409 "request" => "/api/qvitter/statuses/notifications/read"
410 }
411 end
412
413 test "with credentials, with params", %{conn: conn, user: current_user} do
414 other_user = insert(:user)
415
416 {:ok, _activity} =
417 ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: other_user})
418
419 response_conn =
420 conn
421 |> with_credentials(current_user.nickname, "test")
422 |> get("/api/qvitter/statuses/notifications.json")
423
424 [notification] = response = json_response(response_conn, 200)
425
426 assert length(response) == 1
427
428 assert notification["is_seen"] == 0
429
430 response_conn =
431 conn
432 |> with_credentials(current_user.nickname, "test")
433 |> post("/api/qvitter/statuses/notifications/read", %{"latest_id" => notification["id"]})
434
435 [notification] = response = json_response(response_conn, 200)
436
437 assert length(response) == 1
438
439 assert notification["is_seen"] == 1
440 end
441 end
442
443 describe "GET /statuses/user_timeline.json" do
444 setup [:valid_user]
445
446 test "without any params", %{conn: conn} do
447 conn = get(conn, "/api/statuses/user_timeline.json")
448
449 assert json_response(conn, 400) == %{
450 "error" => "You need to specify screen_name or user_id",
451 "request" => "/api/statuses/user_timeline.json"
452 }
453 end
454
455 test "with user_id", %{conn: conn} do
456 user = insert(:user)
457 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
458
459 conn = get(conn, "/api/statuses/user_timeline.json", %{"user_id" => user.id})
460 response = json_response(conn, 200)
461 assert length(response) == 1
462 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
463 end
464
465 test "with screen_name", %{conn: conn} do
466 user = insert(:user)
467 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
468
469 conn = get(conn, "/api/statuses/user_timeline.json", %{"screen_name" => user.nickname})
470 response = json_response(conn, 200)
471 assert length(response) == 1
472 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
473 end
474
475 test "with credentials", %{conn: conn, user: current_user} do
476 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: current_user})
477
478 conn =
479 conn
480 |> with_credentials(current_user.nickname, "test")
481 |> get("/api/statuses/user_timeline.json")
482
483 response = json_response(conn, 200)
484
485 assert length(response) == 1
486 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: current_user})
487 end
488
489 test "with credentials with user_id", %{conn: conn, user: current_user} do
490 user = insert(:user)
491 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
492
493 conn =
494 conn
495 |> with_credentials(current_user.nickname, "test")
496 |> get("/api/statuses/user_timeline.json", %{"user_id" => user.id})
497
498 response = json_response(conn, 200)
499
500 assert length(response) == 1
501 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
502 end
503
504 test "with credentials screen_name", %{conn: conn, user: current_user} do
505 user = insert(:user)
506 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
507
508 conn =
509 conn
510 |> with_credentials(current_user.nickname, "test")
511 |> get("/api/statuses/user_timeline.json", %{"screen_name" => user.nickname})
512
513 response = json_response(conn, 200)
514
515 assert length(response) == 1
516 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
517 end
518 end
519
520 describe "POST /friendships/create.json" do
521 setup [:valid_user]
522
523 test "without valid credentials", %{conn: conn} do
524 conn = post(conn, "/api/friendships/create.json")
525 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
526 end
527
528 test "with credentials", %{conn: conn, user: current_user} do
529 followed = insert(:user)
530
531 conn =
532 conn
533 |> with_credentials(current_user.nickname, "test")
534 |> post("/api/friendships/create.json", %{user_id: followed.id})
535
536 current_user = Repo.get(User, current_user.id)
537 assert User.ap_followers(followed) in current_user.following
538
539 assert json_response(conn, 200) ==
540 UserView.render("show.json", %{user: followed, for: current_user})
541 end
542 end
543
544 describe "POST /friendships/destroy.json" do
545 setup [:valid_user]
546
547 test "without valid credentials", %{conn: conn} do
548 conn = post(conn, "/api/friendships/destroy.json")
549 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
550 end
551
552 test "with credentials", %{conn: conn, user: current_user} do
553 followed = insert(:user)
554
555 {:ok, current_user} = User.follow(current_user, followed)
556 assert User.ap_followers(followed) in current_user.following
557 ActivityPub.follow(current_user, followed)
558
559 conn =
560 conn
561 |> with_credentials(current_user.nickname, "test")
562 |> post("/api/friendships/destroy.json", %{user_id: followed.id})
563
564 current_user = Repo.get(User, current_user.id)
565 assert current_user.following == [current_user.ap_id]
566
567 assert json_response(conn, 200) ==
568 UserView.render("show.json", %{user: followed, for: current_user})
569 end
570 end
571
572 describe "POST /blocks/create.json" do
573 setup [:valid_user]
574
575 test "without valid credentials", %{conn: conn} do
576 conn = post(conn, "/api/blocks/create.json")
577 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
578 end
579
580 test "with credentials", %{conn: conn, user: current_user} do
581 blocked = insert(:user)
582
583 conn =
584 conn
585 |> with_credentials(current_user.nickname, "test")
586 |> post("/api/blocks/create.json", %{user_id: blocked.id})
587
588 current_user = Repo.get(User, current_user.id)
589 assert User.blocks?(current_user, blocked)
590
591 assert json_response(conn, 200) ==
592 UserView.render("show.json", %{user: blocked, for: current_user})
593 end
594 end
595
596 describe "POST /blocks/destroy.json" do
597 setup [:valid_user]
598
599 test "without valid credentials", %{conn: conn} do
600 conn = post(conn, "/api/blocks/destroy.json")
601 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
602 end
603
604 test "with credentials", %{conn: conn, user: current_user} do
605 blocked = insert(:user)
606
607 {:ok, current_user, blocked} = TwitterAPI.block(current_user, %{"user_id" => blocked.id})
608 assert User.blocks?(current_user, blocked)
609
610 conn =
611 conn
612 |> with_credentials(current_user.nickname, "test")
613 |> post("/api/blocks/destroy.json", %{user_id: blocked.id})
614
615 current_user = Repo.get(User, current_user.id)
616 assert current_user.info.blocks == []
617
618 assert json_response(conn, 200) ==
619 UserView.render("show.json", %{user: blocked, for: current_user})
620 end
621 end
622
623 describe "GET /help/test.json" do
624 test "returns \"ok\"", %{conn: conn} do
625 conn = get(conn, "/api/help/test.json")
626 assert json_response(conn, 200) == "ok"
627 end
628 end
629
630 describe "POST /api/qvitter/update_avatar.json" do
631 setup [:valid_user]
632
633 test "without valid credentials", %{conn: conn} do
634 conn = post(conn, "/api/qvitter/update_avatar.json")
635 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
636 end
637
638 test "with credentials", %{conn: conn, user: current_user} do
639 avatar_image = File.read!("test/fixtures/avatar_data_uri")
640
641 conn =
642 conn
643 |> with_credentials(current_user.nickname, "test")
644 |> post("/api/qvitter/update_avatar.json", %{img: avatar_image})
645
646 current_user = Repo.get(User, current_user.id)
647 assert is_map(current_user.avatar)
648
649 assert json_response(conn, 200) ==
650 UserView.render("show.json", %{user: current_user, for: current_user})
651 end
652 end
653
654 describe "GET /api/qvitter/mutes.json" do
655 setup [:valid_user]
656
657 test "unimplemented mutes without valid credentials", %{conn: conn} do
658 conn = get(conn, "/api/qvitter/mutes.json")
659 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
660 end
661
662 test "unimplemented mutes with credentials", %{conn: conn, user: current_user} do
663 response =
664 conn
665 |> with_credentials(current_user.nickname, "test")
666 |> get("/api/qvitter/mutes.json")
667 |> json_response(200)
668
669 assert [] = response
670 end
671 end
672
673 describe "POST /api/favorites/create/:id" do
674 setup [:valid_user]
675
676 test "without valid credentials", %{conn: conn} do
677 note_activity = insert(:note_activity)
678 conn = post(conn, "/api/favorites/create/#{note_activity.id}.json")
679 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
680 end
681
682 test "with credentials", %{conn: conn, user: current_user} do
683 note_activity = insert(:note_activity)
684
685 conn =
686 conn
687 |> with_credentials(current_user.nickname, "test")
688 |> post("/api/favorites/create/#{note_activity.id}.json")
689
690 assert json_response(conn, 200)
691 end
692
693 test "with credentials, invalid param", %{conn: conn, user: current_user} do
694 conn =
695 conn
696 |> with_credentials(current_user.nickname, "test")
697 |> post("/api/favorites/create/wrong.json")
698
699 assert json_response(conn, 400)
700 end
701
702 test "with credentials, invalid activity", %{conn: conn, user: current_user} do
703 conn =
704 conn
705 |> with_credentials(current_user.nickname, "test")
706 |> post("/api/favorites/create/1.json")
707
708 assert json_response(conn, 500)
709 end
710 end
711
712 describe "POST /api/favorites/destroy/:id" do
713 setup [:valid_user]
714
715 test "without valid credentials", %{conn: conn} do
716 note_activity = insert(:note_activity)
717 conn = post(conn, "/api/favorites/destroy/#{note_activity.id}.json")
718 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
719 end
720
721 test "with credentials", %{conn: conn, user: current_user} do
722 note_activity = insert(:note_activity)
723 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
724 ActivityPub.like(current_user, object)
725
726 conn =
727 conn
728 |> with_credentials(current_user.nickname, "test")
729 |> post("/api/favorites/destroy/#{note_activity.id}.json")
730
731 assert json_response(conn, 200)
732 end
733 end
734
735 describe "POST /api/statuses/retweet/:id" do
736 setup [:valid_user]
737
738 test "without valid credentials", %{conn: conn} do
739 note_activity = insert(:note_activity)
740 conn = post(conn, "/api/statuses/retweet/#{note_activity.id}.json")
741 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
742 end
743
744 test "with credentials", %{conn: conn, user: current_user} do
745 note_activity = insert(:note_activity)
746
747 request_path = "/api/statuses/retweet/#{note_activity.id}.json"
748
749 response =
750 conn
751 |> with_credentials(current_user.nickname, "test")
752 |> post(request_path)
753
754 activity = Repo.get(Activity, note_activity.id)
755 activity_user = Repo.get_by(User, ap_id: note_activity.data["actor"])
756
757 assert json_response(response, 200) ==
758 ActivityRepresenter.to_map(activity, %{user: activity_user, for: current_user})
759 end
760 end
761
762 describe "POST /api/statuses/unretweet/:id" do
763 setup [:valid_user]
764
765 test "without valid credentials", %{conn: conn} do
766 note_activity = insert(:note_activity)
767 conn = post(conn, "/api/statuses/unretweet/#{note_activity.id}.json")
768 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
769 end
770
771 test "with credentials", %{conn: conn, user: current_user} do
772 note_activity = insert(:note_activity)
773
774 request_path = "/api/statuses/retweet/#{note_activity.id}.json"
775
776 _response =
777 conn
778 |> with_credentials(current_user.nickname, "test")
779 |> post(request_path)
780
781 request_path = String.replace(request_path, "retweet", "unretweet")
782
783 response =
784 conn
785 |> with_credentials(current_user.nickname, "test")
786 |> post(request_path)
787
788 activity = Repo.get(Activity, note_activity.id)
789 activity_user = Repo.get_by(User, ap_id: note_activity.data["actor"])
790
791 assert json_response(response, 200) ==
792 ActivityRepresenter.to_map(activity, %{user: activity_user, for: current_user})
793 end
794 end
795
796 describe "POST /api/account/register" do
797 test "it creates a new user", %{conn: conn} do
798 data = %{
799 "nickname" => "lain",
800 "email" => "lain@wired.jp",
801 "fullname" => "lain iwakura",
802 "bio" => "close the world.",
803 "password" => "bear",
804 "confirm" => "bear"
805 }
806
807 conn =
808 conn
809 |> post("/api/account/register", data)
810
811 user = json_response(conn, 200)
812
813 fetched_user = Repo.get_by(User, nickname: "lain")
814 assert user == UserView.render("show.json", %{user: fetched_user})
815 end
816
817 test "it returns errors on a problem", %{conn: conn} do
818 data = %{
819 "email" => "lain@wired.jp",
820 "fullname" => "lain iwakura",
821 "bio" => "close the world.",
822 "password" => "bear",
823 "confirm" => "bear"
824 }
825
826 conn =
827 conn
828 |> post("/api/account/register", data)
829
830 errors = json_response(conn, 400)
831
832 assert is_binary(errors["error"])
833 end
834 end
835
836 describe "POST /api/account/password_reset, with valid parameters" do
837 setup %{conn: conn} do
838 user = insert(:user)
839 conn = post(conn, "/api/account/password_reset?email=#{user.email}")
840 %{conn: conn, user: user}
841 end
842
843 test "it returns 204", %{conn: conn} do
844 assert json_response(conn, :no_content)
845 end
846
847 test "it creates a PasswordResetToken record for user", %{user: user} do
848 token_record = Repo.get_by(Pleroma.PasswordResetToken, user_id: user.id)
849 assert token_record
850 end
851
852 test "it sends an email to user", %{user: user} do
853 token_record = Repo.get_by(Pleroma.PasswordResetToken, user_id: user.id)
854
855 Swoosh.TestAssertions.assert_email_sent(
856 Pleroma.UserEmail.password_reset_email(user, token_record.token)
857 )
858 end
859 end
860
861 describe "POST /api/account/password_reset, with invalid parameters" do
862 setup [:valid_user]
863
864 test "it returns 500 when user is not found", %{conn: conn, user: user} do
865 conn = post(conn, "/api/account/password_reset?email=nonexisting_#{user.email}")
866 assert json_response(conn, :internal_server_error)
867 end
868
869 test "it returns 500 when user is not local", %{conn: conn, user: user} do
870 {:ok, user} = Repo.update(Changeset.change(user, local: false))
871 conn = post(conn, "/api/account/password_reset?email=#{user.email}")
872 assert json_response(conn, :internal_server_error)
873 end
874 end
875
876 describe "GET /api/externalprofile/show" do
877 test "it returns the user", %{conn: conn} do
878 user = insert(:user)
879 other_user = insert(:user)
880
881 conn =
882 conn
883 |> assign(:user, user)
884 |> get("/api/externalprofile/show", %{profileurl: other_user.ap_id})
885
886 assert json_response(conn, 200) == UserView.render("show.json", %{user: other_user})
887 end
888 end
889
890 describe "GET /api/statuses/followers" do
891 test "it returns a user's followers", %{conn: conn} do
892 user = insert(:user)
893 follower_one = insert(:user)
894 follower_two = insert(:user)
895 _not_follower = insert(:user)
896
897 {:ok, follower_one} = User.follow(follower_one, user)
898 {:ok, follower_two} = User.follow(follower_two, user)
899
900 conn =
901 conn
902 |> assign(:user, user)
903 |> get("/api/statuses/followers")
904
905 expected = UserView.render("index.json", %{users: [follower_one, follower_two], for: user})
906 result = json_response(conn, 200)
907 assert Enum.sort(expected) == Enum.sort(result)
908 end
909
910 test "it returns a given user's followers with user_id", %{conn: conn} do
911 user = insert(:user)
912 follower_one = insert(:user)
913 follower_two = insert(:user)
914 not_follower = insert(:user)
915
916 {:ok, follower_one} = User.follow(follower_one, user)
917 {:ok, follower_two} = User.follow(follower_two, user)
918
919 conn =
920 conn
921 |> assign(:user, not_follower)
922 |> get("/api/statuses/followers", %{"user_id" => user.id})
923
924 assert MapSet.equal?(
925 MapSet.new(json_response(conn, 200)),
926 MapSet.new(
927 UserView.render("index.json", %{
928 users: [follower_one, follower_two],
929 for: not_follower
930 })
931 )
932 )
933 end
934
935 test "it returns empty for a hidden network", %{conn: conn} do
936 user = insert(:user, %{info: %{hide_network: true}})
937 follower_one = insert(:user)
938 follower_two = insert(:user)
939 not_follower = insert(:user)
940
941 {:ok, _follower_one} = User.follow(follower_one, user)
942 {:ok, _follower_two} = User.follow(follower_two, user)
943
944 response =
945 conn
946 |> assign(:user, not_follower)
947 |> get("/api/statuses/followers", %{"user_id" => user.id})
948 |> json_response(200)
949
950 assert [] == response
951 end
952
953 test "it returns the followers for a hidden network if requested by the user themselves", %{
954 conn: conn
955 } do
956 user = insert(:user, %{info: %{hide_network: true}})
957 follower_one = insert(:user)
958 follower_two = insert(:user)
959 _not_follower = insert(:user)
960
961 {:ok, _follower_one} = User.follow(follower_one, user)
962 {:ok, _follower_two} = User.follow(follower_two, user)
963
964 conn =
965 conn
966 |> assign(:user, user)
967 |> get("/api/statuses/followers", %{"user_id" => user.id})
968
969 refute [] == json_response(conn, 200)
970 end
971 end
972
973 describe "GET /api/statuses/friends" do
974 test "it returns the logged in user's friends", %{conn: conn} do
975 user = insert(:user)
976 followed_one = insert(:user)
977 followed_two = insert(:user)
978 _not_followed = insert(:user)
979
980 {:ok, user} = User.follow(user, followed_one)
981 {:ok, user} = User.follow(user, followed_two)
982
983 conn =
984 conn
985 |> assign(:user, user)
986 |> get("/api/statuses/friends")
987
988 expected = UserView.render("index.json", %{users: [followed_one, followed_two], for: user})
989 result = json_response(conn, 200)
990 assert Enum.sort(expected) == Enum.sort(result)
991 end
992
993 test "it returns a given user's friends with user_id", %{conn: conn} do
994 user = insert(:user)
995 followed_one = insert(:user)
996 followed_two = insert(:user)
997 _not_followed = insert(:user)
998
999 {:ok, user} = User.follow(user, followed_one)
1000 {:ok, user} = User.follow(user, followed_two)
1001
1002 conn =
1003 conn
1004 |> assign(:user, user)
1005 |> get("/api/statuses/friends", %{"user_id" => user.id})
1006
1007 assert MapSet.equal?(
1008 MapSet.new(json_response(conn, 200)),
1009 MapSet.new(
1010 UserView.render("index.json", %{users: [followed_one, followed_two], for: user})
1011 )
1012 )
1013 end
1014
1015 test "it returns empty for a hidden network", %{conn: conn} do
1016 user = insert(:user, %{info: %{hide_network: true}})
1017 followed_one = insert(:user)
1018 followed_two = insert(:user)
1019 not_followed = insert(:user)
1020
1021 {:ok, user} = User.follow(user, followed_one)
1022 {:ok, user} = User.follow(user, followed_two)
1023
1024 conn =
1025 conn
1026 |> assign(:user, not_followed)
1027 |> get("/api/statuses/friends", %{"user_id" => user.id})
1028
1029 assert [] == json_response(conn, 200)
1030 end
1031
1032 test "it returns friends for a hidden network if the user themselves request it", %{
1033 conn: conn
1034 } do
1035 user = insert(:user, %{info: %{hide_network: true}})
1036 followed_one = insert(:user)
1037 followed_two = insert(:user)
1038 _not_followed = insert(:user)
1039
1040 {:ok, _user} = User.follow(user, followed_one)
1041 {:ok, _user} = User.follow(user, followed_two)
1042
1043 response =
1044 conn
1045 |> assign(:user, user)
1046 |> get("/api/statuses/friends", %{"user_id" => user.id})
1047 |> json_response(200)
1048
1049 refute [] == response
1050 end
1051
1052 test "it returns a given user's friends with screen_name", %{conn: conn} do
1053 user = insert(:user)
1054 followed_one = insert(:user)
1055 followed_two = insert(:user)
1056 _not_followed = insert(:user)
1057
1058 {:ok, user} = User.follow(user, followed_one)
1059 {:ok, user} = User.follow(user, followed_two)
1060
1061 conn =
1062 conn
1063 |> assign(:user, user)
1064 |> get("/api/statuses/friends", %{"screen_name" => user.nickname})
1065
1066 assert MapSet.equal?(
1067 MapSet.new(json_response(conn, 200)),
1068 MapSet.new(
1069 UserView.render("index.json", %{users: [followed_one, followed_two], for: user})
1070 )
1071 )
1072 end
1073 end
1074
1075 describe "GET /friends/ids" do
1076 test "it returns a user's friends", %{conn: conn} do
1077 user = insert(:user)
1078 followed_one = insert(:user)
1079 followed_two = insert(:user)
1080 _not_followed = insert(:user)
1081
1082 {:ok, user} = User.follow(user, followed_one)
1083 {:ok, user} = User.follow(user, followed_two)
1084
1085 conn =
1086 conn
1087 |> assign(:user, user)
1088 |> get("/api/friends/ids")
1089
1090 expected = [followed_one.id, followed_two.id]
1091
1092 assert MapSet.equal?(
1093 MapSet.new(Poison.decode!(json_response(conn, 200))),
1094 MapSet.new(expected)
1095 )
1096 end
1097 end
1098
1099 describe "POST /api/account/update_profile.json" do
1100 test "it updates a user's profile", %{conn: conn} do
1101 user = insert(:user)
1102 user2 = insert(:user)
1103
1104 conn =
1105 conn
1106 |> assign(:user, user)
1107 |> post("/api/account/update_profile.json", %{
1108 "name" => "new name",
1109 "description" => "hi @#{user2.nickname}"
1110 })
1111
1112 user = Repo.get!(User, user.id)
1113 assert user.name == "new name"
1114
1115 assert user.bio ==
1116 "hi <span><a data-user='#{user2.id}' class='mention' href='#{user2.ap_id}'>@<span>#{
1117 user2.nickname
1118 }</span></a></span>"
1119
1120 assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user})
1121 end
1122
1123 test "it sets and un-sets hide_network", %{conn: conn} do
1124 user = insert(:user)
1125
1126 conn
1127 |> assign(:user, user)
1128 |> post("/api/account/update_profile.json", %{
1129 "hide_network" => "true"
1130 })
1131
1132 user = Repo.get!(User, user.id)
1133 assert user.info.hide_network == true
1134
1135 conn =
1136 conn
1137 |> assign(:user, user)
1138 |> post("/api/account/update_profile.json", %{
1139 "hide_network" => "false"
1140 })
1141
1142 user = Repo.get!(User, user.id)
1143 assert user.info.hide_network == false
1144 assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user})
1145 end
1146
1147 test "it locks an account", %{conn: conn} do
1148 user = insert(:user)
1149
1150 conn =
1151 conn
1152 |> assign(:user, user)
1153 |> post("/api/account/update_profile.json", %{
1154 "locked" => "true"
1155 })
1156
1157 user = Repo.get!(User, user.id)
1158 assert user.info.locked == true
1159
1160 assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user})
1161 end
1162
1163 test "it unlocks an account", %{conn: conn} do
1164 user = insert(:user)
1165
1166 conn =
1167 conn
1168 |> assign(:user, user)
1169 |> post("/api/account/update_profile.json", %{
1170 "locked" => "false"
1171 })
1172
1173 user = Repo.get!(User, user.id)
1174 assert user.info.locked == false
1175
1176 assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user})
1177 end
1178 end
1179
1180 defp valid_user(_context) do
1181 user = insert(:user)
1182 [user: user]
1183 end
1184
1185 defp with_credentials(conn, username, password) do
1186 header_content = "Basic " <> Base.encode64("#{username}:#{password}")
1187 put_req_header(conn, "authorization", header_content)
1188 end
1189
1190 describe "GET /api/search.json" do
1191 test "it returns search results", %{conn: conn} do
1192 user = insert(:user)
1193 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
1194
1195 {:ok, activity} = CommonAPI.post(user, %{"status" => "This is about 2hu"})
1196 {:ok, _} = CommonAPI.post(user_two, %{"status" => "This isn't"})
1197
1198 conn =
1199 conn
1200 |> get("/api/search.json", %{"q" => "2hu", "page" => "1", "rpp" => "1"})
1201
1202 assert [status] = json_response(conn, 200)
1203 assert status["id"] == activity.id
1204 end
1205 end
1206
1207 describe "GET /api/statusnet/tags/timeline/:tag.json" do
1208 test "it returns the tags timeline", %{conn: conn} do
1209 user = insert(:user)
1210 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
1211
1212 {:ok, activity} = CommonAPI.post(user, %{"status" => "This is about #2hu"})
1213 {:ok, _} = CommonAPI.post(user_two, %{"status" => "This isn't"})
1214
1215 conn =
1216 conn
1217 |> get("/api/statusnet/tags/timeline/2hu.json")
1218
1219 assert [status] = json_response(conn, 200)
1220 assert status["id"] == activity.id
1221 end
1222 end
1223
1224 test "Convert newlines to <br> in bio", %{conn: conn} do
1225 user = insert(:user)
1226
1227 _conn =
1228 conn
1229 |> assign(:user, user)
1230 |> post("/api/account/update_profile.json", %{
1231 "description" => "Hello,\r\nWorld! I\n am a test."
1232 })
1233
1234 user = Repo.get!(User, user.id)
1235 assert user.bio == "Hello,<br>World! I<br> am a test."
1236 end
1237
1238 describe "POST /api/pleroma/change_password" do
1239 setup [:valid_user]
1240
1241 test "without credentials", %{conn: conn} do
1242 conn = post(conn, "/api/pleroma/change_password")
1243 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
1244 end
1245
1246 test "with credentials and invalid password", %{conn: conn, user: current_user} do
1247 conn =
1248 conn
1249 |> with_credentials(current_user.nickname, "test")
1250 |> post("/api/pleroma/change_password", %{
1251 "password" => "hi",
1252 "new_password" => "newpass",
1253 "new_password_confirmation" => "newpass"
1254 })
1255
1256 assert json_response(conn, 200) == %{"error" => "Invalid password."}
1257 end
1258
1259 test "with credentials, valid password and new password and confirmation not matching", %{
1260 conn: conn,
1261 user: current_user
1262 } do
1263 conn =
1264 conn
1265 |> with_credentials(current_user.nickname, "test")
1266 |> post("/api/pleroma/change_password", %{
1267 "password" => "test",
1268 "new_password" => "newpass",
1269 "new_password_confirmation" => "notnewpass"
1270 })
1271
1272 assert json_response(conn, 200) == %{
1273 "error" => "New password does not match confirmation."
1274 }
1275 end
1276
1277 test "with credentials, valid password and invalid new password", %{
1278 conn: conn,
1279 user: current_user
1280 } do
1281 conn =
1282 conn
1283 |> with_credentials(current_user.nickname, "test")
1284 |> post("/api/pleroma/change_password", %{
1285 "password" => "test",
1286 "new_password" => "",
1287 "new_password_confirmation" => ""
1288 })
1289
1290 assert json_response(conn, 200) == %{
1291 "error" => "New password can't be blank."
1292 }
1293 end
1294
1295 test "with credentials, valid password and matching new password and confirmation", %{
1296 conn: conn,
1297 user: current_user
1298 } do
1299 conn =
1300 conn
1301 |> with_credentials(current_user.nickname, "test")
1302 |> post("/api/pleroma/change_password", %{
1303 "password" => "test",
1304 "new_password" => "newpass",
1305 "new_password_confirmation" => "newpass"
1306 })
1307
1308 assert json_response(conn, 200) == %{"status" => "success"}
1309 fetched_user = Repo.get(User, current_user.id)
1310 assert Pbkdf2.checkpw("newpass", fetched_user.password_hash) == true
1311 end
1312 end
1313
1314 describe "POST /api/pleroma/delete_account" do
1315 setup [:valid_user]
1316
1317 test "without credentials", %{conn: conn} do
1318 conn = post(conn, "/api/pleroma/delete_account")
1319 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
1320 end
1321
1322 test "with credentials and invalid password", %{conn: conn, user: current_user} do
1323 conn =
1324 conn
1325 |> with_credentials(current_user.nickname, "test")
1326 |> post("/api/pleroma/delete_account", %{"password" => "hi"})
1327
1328 assert json_response(conn, 200) == %{"error" => "Invalid password."}
1329 end
1330
1331 test "with credentials and valid password", %{conn: conn, user: current_user} do
1332 conn =
1333 conn
1334 |> with_credentials(current_user.nickname, "test")
1335 |> post("/api/pleroma/delete_account", %{"password" => "test"})
1336
1337 assert json_response(conn, 200) == %{"status" => "success"}
1338 # Wait a second for the started task to end
1339 :timer.sleep(1000)
1340 end
1341 end
1342
1343 describe "GET /api/pleroma/friend_requests" do
1344 test "it lists friend requests" do
1345 user = insert(:user)
1346 other_user = insert(:user)
1347
1348 {:ok, _activity} = ActivityPub.follow(other_user, user)
1349
1350 user = Repo.get(User, user.id)
1351 other_user = Repo.get(User, other_user.id)
1352
1353 assert User.following?(other_user, user) == false
1354
1355 conn =
1356 build_conn()
1357 |> assign(:user, user)
1358 |> get("/api/pleroma/friend_requests")
1359
1360 assert [relationship] = json_response(conn, 200)
1361 assert other_user.id == relationship["id"]
1362 end
1363 end
1364
1365 describe "POST /api/pleroma/friendships/approve" do
1366 test "it approves a friend request" do
1367 user = insert(:user)
1368 other_user = insert(:user)
1369
1370 {:ok, _activity} = ActivityPub.follow(other_user, user)
1371
1372 user = Repo.get(User, user.id)
1373 other_user = Repo.get(User, other_user.id)
1374
1375 assert User.following?(other_user, user) == false
1376
1377 conn =
1378 build_conn()
1379 |> assign(:user, user)
1380 |> post("/api/pleroma/friendships/approve", %{"user_id" => to_string(other_user.id)})
1381
1382 assert relationship = json_response(conn, 200)
1383 assert other_user.id == relationship["id"]
1384 assert relationship["follows_you"] == true
1385 end
1386 end
1387
1388 describe "POST /api/pleroma/friendships/deny" do
1389 test "it denies a friend request" do
1390 user = insert(:user)
1391 other_user = insert(:user)
1392
1393 {:ok, _activity} = ActivityPub.follow(other_user, user)
1394
1395 user = Repo.get(User, user.id)
1396 other_user = Repo.get(User, other_user.id)
1397
1398 assert User.following?(other_user, user) == false
1399
1400 conn =
1401 build_conn()
1402 |> assign(:user, user)
1403 |> post("/api/pleroma/friendships/deny", %{"user_id" => to_string(other_user.id)})
1404
1405 assert relationship = json_response(conn, 200)
1406 assert other_user.id == relationship["id"]
1407 assert relationship["follows_you"] == false
1408 end
1409 end
1410
1411 describe "GET /api/pleroma/search_user" do
1412 test "it returns users, ordered by similarity", %{conn: conn} do
1413 user = insert(:user, %{name: "eal"})
1414 user_two = insert(:user, %{name: "ean"})
1415 user_three = insert(:user, %{name: "ebn"})
1416
1417 resp =
1418 conn
1419 |> get(twitter_api_search__path(conn, :search_user), query: "eal")
1420 |> json_response(200)
1421
1422 assert length(resp) == 3
1423 assert [user.id, user_two.id, user_three.id] == Enum.map(resp, fn %{"id" => id} -> id end)
1424 end
1425 end
1426
1427 describe "POST /api/media/upload" do
1428 setup context do
1429 Pleroma.DataCase.ensure_local_uploader(context)
1430 end
1431
1432 test "it performs the upload and sets `data[actor]` with AP id of uploader user", %{
1433 conn: conn
1434 } do
1435 user = insert(:user)
1436
1437 upload_filename = "test/fixtures/image_tmp.jpg"
1438 File.cp!("test/fixtures/image.jpg", upload_filename)
1439
1440 file = %Plug.Upload{
1441 content_type: "image/jpg",
1442 path: Path.absname(upload_filename),
1443 filename: "image.jpg"
1444 }
1445
1446 response =
1447 conn
1448 |> assign(:user, user)
1449 |> put_req_header("content-type", "application/octet-stream")
1450 |> post("/api/media/upload", %{
1451 "media" => file
1452 })
1453 |> json_response(:ok)
1454
1455 assert response["media_id"]
1456 object = Repo.get(Object, response["media_id"])
1457 assert object
1458 assert object.data["actor"] == User.ap_id(user)
1459 end
1460 end
1461
1462 describe "POST /api/media/metadata/create" do
1463 setup do
1464 object = insert(:note)
1465 user = User.get_by_ap_id(object.data["actor"])
1466 %{object: object, user: user}
1467 end
1468
1469 test "it returns :forbidden status on attempt to modify someone else's upload", %{
1470 conn: conn,
1471 object: object
1472 } do
1473 initial_description = object.data["name"]
1474 another_user = insert(:user)
1475
1476 conn
1477 |> assign(:user, another_user)
1478 |> post("/api/media/metadata/create", %{"media_id" => object.id})
1479 |> json_response(:forbidden)
1480
1481 object = Repo.get(Object, object.id)
1482 assert object.data["name"] == initial_description
1483 end
1484
1485 test "it updates `data[name]` of referenced Object with provided value", %{
1486 conn: conn,
1487 object: object,
1488 user: user
1489 } do
1490 description = "Informative description of the image. Initial value: #{object.data["name"]}}"
1491
1492 conn
1493 |> assign(:user, user)
1494 |> post("/api/media/metadata/create", %{
1495 "media_id" => object.id,
1496 "alt_text" => %{"text" => description}
1497 })
1498 |> json_response(:no_content)
1499
1500 object = Repo.get(Object, object.id)
1501 assert object.data["name"] == description
1502 end
1503 end
1504 end