Merge develop
[akkoma] / test / web / twitter_api / util_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.TwitterAPI.UtilControllerTest do
6 use Pleroma.Web.ConnCase
7 use Oban.Testing, repo: Pleroma.Repo
8
9 alias Pleroma.Repo
10 alias Pleroma.Tests.ObanHelpers
11 alias Pleroma.User
12 alias Pleroma.Web.CommonAPI
13 import ExUnit.CaptureLog
14 import Pleroma.Factory
15 import Mock
16
17 setup do
18 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
19 :ok
20 end
21
22 clear_config([:instance])
23 clear_config([:frontend_configurations, :pleroma_fe])
24 clear_config([:user, :deny_follow_blocked])
25
26 describe "POST /api/pleroma/follow_import" do
27 test "it returns HTTP 200", %{conn: conn} do
28 user1 = insert(:user)
29 user2 = insert(:user)
30
31 response =
32 conn
33 |> assign(:user, user1)
34 |> post("/api/pleroma/follow_import", %{"list" => "#{user2.ap_id}"})
35 |> json_response(:ok)
36
37 assert response == "job started"
38 end
39
40 test "it imports follow lists from file", %{conn: conn} do
41 user1 = insert(:user)
42 user2 = insert(:user)
43
44 with_mocks([
45 {File, [],
46 read!: fn "follow_list.txt" ->
47 "Account address,Show boosts\n#{user2.ap_id},true"
48 end}
49 ]) do
50 response =
51 conn
52 |> assign(:user, user1)
53 |> post("/api/pleroma/follow_import", %{"list" => %Plug.Upload{path: "follow_list.txt"}})
54 |> json_response(:ok)
55
56 assert response == "job started"
57
58 assert ObanHelpers.member?(
59 %{
60 "op" => "follow_import",
61 "follower_id" => user1.id,
62 "followed_identifiers" => [user2.ap_id]
63 },
64 all_enqueued(worker: Pleroma.Workers.BackgroundWorker)
65 )
66 end
67 end
68
69 test "it imports new-style mastodon follow lists", %{conn: conn} do
70 user1 = insert(:user)
71 user2 = insert(:user)
72
73 response =
74 conn
75 |> assign(:user, user1)
76 |> post("/api/pleroma/follow_import", %{
77 "list" => "Account address,Show boosts\n#{user2.ap_id},true"
78 })
79 |> json_response(:ok)
80
81 assert response == "job started"
82 end
83
84 test "requires 'follow' permission", %{conn: conn} do
85 token1 = insert(:oauth_token, scopes: ["read", "write"])
86 token2 = insert(:oauth_token, scopes: ["follow"])
87 another_user = insert(:user)
88
89 for token <- [token1, token2] do
90 conn =
91 conn
92 |> put_req_header("authorization", "Bearer #{token.token}")
93 |> post("/api/pleroma/follow_import", %{"list" => "#{another_user.ap_id}"})
94
95 if token == token1 do
96 assert %{"error" => "Insufficient permissions: follow."} == json_response(conn, 403)
97 else
98 assert json_response(conn, 200)
99 end
100 end
101 end
102 end
103
104 describe "POST /api/pleroma/blocks_import" do
105 test "it returns HTTP 200", %{conn: conn} do
106 user1 = insert(:user)
107 user2 = insert(:user)
108
109 response =
110 conn
111 |> assign(:user, user1)
112 |> post("/api/pleroma/blocks_import", %{"list" => "#{user2.ap_id}"})
113 |> json_response(:ok)
114
115 assert response == "job started"
116 end
117
118 test "it imports blocks users from file", %{conn: conn} do
119 user1 = insert(:user)
120 user2 = insert(:user)
121 user3 = insert(:user)
122
123 with_mocks([
124 {File, [], read!: fn "blocks_list.txt" -> "#{user2.ap_id} #{user3.ap_id}" end}
125 ]) do
126 response =
127 conn
128 |> assign(:user, user1)
129 |> post("/api/pleroma/blocks_import", %{"list" => %Plug.Upload{path: "blocks_list.txt"}})
130 |> json_response(:ok)
131
132 assert response == "job started"
133
134 assert ObanHelpers.member?(
135 %{
136 "op" => "blocks_import",
137 "blocker_id" => user1.id,
138 "blocked_identifiers" => [user2.ap_id, user3.ap_id]
139 },
140 all_enqueued(worker: Pleroma.Workers.BackgroundWorker)
141 )
142 end
143 end
144 end
145
146 describe "PUT /api/pleroma/notification_settings" do
147 test "it updates notification settings", %{conn: conn} do
148 user = insert(:user)
149
150 conn
151 |> assign(:user, user)
152 |> put("/api/pleroma/notification_settings", %{
153 "followers" => false,
154 "bar" => 1
155 })
156 |> json_response(:ok)
157
158 user = Repo.get(User, user.id)
159
160 assert %{
161 "followers" => false,
162 "follows" => true,
163 "non_follows" => true,
164 "non_followers" => true
165 } == user.info.notification_settings
166 end
167 end
168
169 describe "GET /api/statusnet/config" do
170 test "it returns config in xml format", %{conn: conn} do
171 instance = Pleroma.Config.get(:instance)
172
173 response =
174 conn
175 |> put_req_header("accept", "application/xml")
176 |> get("/api/statusnet/config")
177 |> response(:ok)
178
179 assert response ==
180 "<config>\n<site>\n<name>#{Keyword.get(instance, :name)}</name>\n<site>#{
181 Pleroma.Web.base_url()
182 }</site>\n<textlimit>#{Keyword.get(instance, :limit)}</textlimit>\n<closed>#{
183 !Keyword.get(instance, :registrations_open)
184 }</closed>\n</site>\n</config>\n"
185 end
186
187 test "it returns config in json format", %{conn: conn} do
188 instance = Pleroma.Config.get(:instance)
189 Pleroma.Config.put([:instance, :managed_config], true)
190 Pleroma.Config.put([:instance, :registrations_open], false)
191 Pleroma.Config.put([:instance, :invites_enabled], true)
192 Pleroma.Config.put([:instance, :public], false)
193 Pleroma.Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
194
195 response =
196 conn
197 |> put_req_header("accept", "application/json")
198 |> get("/api/statusnet/config")
199 |> json_response(:ok)
200
201 expected_data = %{
202 "site" => %{
203 "accountActivationRequired" => "0",
204 "closed" => "1",
205 "description" => Keyword.get(instance, :description),
206 "invitesEnabled" => "1",
207 "name" => Keyword.get(instance, :name),
208 "pleromafe" => %{"theme" => "asuka-hospital"},
209 "private" => "1",
210 "safeDMMentionsEnabled" => "0",
211 "server" => Pleroma.Web.base_url(),
212 "textlimit" => to_string(Keyword.get(instance, :limit)),
213 "uploadlimit" => %{
214 "avatarlimit" => to_string(Keyword.get(instance, :avatar_upload_limit)),
215 "backgroundlimit" => to_string(Keyword.get(instance, :background_upload_limit)),
216 "bannerlimit" => to_string(Keyword.get(instance, :banner_upload_limit)),
217 "uploadlimit" => to_string(Keyword.get(instance, :upload_limit))
218 },
219 "vapidPublicKey" => Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
220 }
221 }
222
223 assert response == expected_data
224 end
225
226 test "returns the state of safe_dm_mentions flag", %{conn: conn} do
227 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
228
229 response =
230 conn
231 |> get("/api/statusnet/config.json")
232 |> json_response(:ok)
233
234 assert response["site"]["safeDMMentionsEnabled"] == "1"
235
236 Pleroma.Config.put([:instance, :safe_dm_mentions], false)
237
238 response =
239 conn
240 |> get("/api/statusnet/config.json")
241 |> json_response(:ok)
242
243 assert response["site"]["safeDMMentionsEnabled"] == "0"
244 end
245
246 test "it returns the managed config", %{conn: conn} do
247 Pleroma.Config.put([:instance, :managed_config], false)
248 Pleroma.Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
249
250 response =
251 conn
252 |> get("/api/statusnet/config.json")
253 |> json_response(:ok)
254
255 refute response["site"]["pleromafe"]
256
257 Pleroma.Config.put([:instance, :managed_config], true)
258
259 response =
260 conn
261 |> get("/api/statusnet/config.json")
262 |> json_response(:ok)
263
264 assert response["site"]["pleromafe"] == %{"theme" => "asuka-hospital"}
265 end
266 end
267
268 describe "GET /api/pleroma/frontend_configurations" do
269 test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
270 config = [
271 frontend_a: %{
272 x: 1,
273 y: 2
274 },
275 frontend_b: %{
276 z: 3
277 }
278 ]
279
280 Pleroma.Config.put(:frontend_configurations, config)
281
282 response =
283 conn
284 |> get("/api/pleroma/frontend_configurations")
285 |> json_response(:ok)
286
287 assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
288 end
289 end
290
291 describe "/api/pleroma/emoji" do
292 test "returns json with custom emoji with tags", %{conn: conn} do
293 emoji =
294 conn
295 |> get("/api/pleroma/emoji")
296 |> json_response(200)
297
298 assert Enum.all?(emoji, fn
299 {_key,
300 %{
301 "image_url" => url,
302 "tags" => tags
303 }} ->
304 is_binary(url) and is_list(tags)
305 end)
306 end
307 end
308
309 describe "GET /ostatus_subscribe - remote_follow/2" do
310 test "adds status to pleroma instance if the `acct` is a status", %{conn: conn} do
311 conn =
312 get(
313 conn,
314 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie/statuses/101849165031453009"
315 )
316
317 assert redirected_to(conn) =~ "/notice/"
318 end
319
320 test "show follow account page if the `acct` is a account link", %{conn: conn} do
321 response =
322 get(
323 conn,
324 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie"
325 )
326
327 assert html_response(response, 200) =~ "Log in to follow"
328 end
329
330 test "show follow page if the `acct` is a account link", %{conn: conn} do
331 user = insert(:user)
332
333 response =
334 conn
335 |> assign(:user, user)
336 |> get("/ostatus_subscribe?acct=https://mastodon.social/users/emelie")
337
338 assert html_response(response, 200) =~ "Remote follow"
339 end
340
341 test "show follow page with error when user cannot fecth by `acct` link", %{conn: conn} do
342 user = insert(:user)
343
344 assert capture_log(fn ->
345 response =
346 conn
347 |> assign(:user, user)
348 |> get("/ostatus_subscribe?acct=https://mastodon.social/users/not_found")
349
350 assert html_response(response, 200) =~ "Error fetching user"
351 end) =~ "Object has been deleted"
352 end
353 end
354
355 describe "POST /ostatus_subscribe - do_remote_follow/2 with assigned user " do
356 test "follows user", %{conn: conn} do
357 user = insert(:user)
358 user2 = insert(:user)
359
360 response =
361 conn
362 |> assign(:user, user)
363 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
364 |> response(200)
365
366 assert response =~ "Account followed!"
367 assert user2.follower_address in refresh_record(user).following
368 end
369
370 test "returns error when user is deactivated", %{conn: conn} do
371 user = insert(:user, info: %{deactivated: true})
372 user2 = insert(:user)
373
374 response =
375 conn
376 |> assign(:user, user)
377 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
378 |> response(200)
379
380 assert response =~ "Error following account"
381 end
382
383 test "returns error when user is blocked", %{conn: conn} do
384 Pleroma.Config.put([:user, :deny_follow_blocked], true)
385 user = insert(:user)
386 user2 = insert(:user)
387
388 {:ok, _user} = Pleroma.User.block(user2, user)
389
390 response =
391 conn
392 |> assign(:user, user)
393 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
394 |> response(200)
395
396 assert response =~ "Error following account"
397 end
398
399 test "returns error when followee not found", %{conn: conn} do
400 user = insert(:user)
401
402 response =
403 conn
404 |> assign(:user, user)
405 |> post("/ostatus_subscribe", %{"user" => %{"id" => "jimm"}})
406 |> response(200)
407
408 assert response =~ "Error following account"
409 end
410
411 test "returns success result when user already in followers", %{conn: conn} do
412 user = insert(:user)
413 user2 = insert(:user)
414 {:ok, _, _, _} = CommonAPI.follow(user, user2)
415
416 response =
417 conn
418 |> assign(:user, refresh_record(user))
419 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
420 |> response(200)
421
422 assert response =~ "Account followed!"
423 end
424 end
425
426 describe "POST /ostatus_subscribe - do_remote_follow/2 without assigned user " do
427 test "follows", %{conn: conn} do
428 user = insert(:user)
429 user2 = insert(:user)
430
431 response =
432 conn
433 |> post("/ostatus_subscribe", %{
434 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
435 })
436 |> response(200)
437
438 assert response =~ "Account followed!"
439 assert user2.follower_address in refresh_record(user).following
440 end
441
442 test "returns error when followee not found", %{conn: conn} do
443 user = insert(:user)
444
445 response =
446 conn
447 |> post("/ostatus_subscribe", %{
448 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => "jimm"}
449 })
450 |> response(200)
451
452 assert response =~ "Error following account"
453 end
454
455 test "returns error when login invalid", %{conn: conn} do
456 user = insert(:user)
457
458 response =
459 conn
460 |> post("/ostatus_subscribe", %{
461 "authorization" => %{"name" => "jimm", "password" => "test", "id" => user.id}
462 })
463 |> response(200)
464
465 assert response =~ "Wrong username or password"
466 end
467
468 test "returns error when password invalid", %{conn: conn} do
469 user = insert(:user)
470 user2 = insert(:user)
471
472 response =
473 conn
474 |> post("/ostatus_subscribe", %{
475 "authorization" => %{"name" => user.nickname, "password" => "42", "id" => user2.id}
476 })
477 |> response(200)
478
479 assert response =~ "Wrong username or password"
480 end
481
482 test "returns error when user is blocked", %{conn: conn} do
483 Pleroma.Config.put([:user, :deny_follow_blocked], true)
484 user = insert(:user)
485 user2 = insert(:user)
486 {:ok, _user} = Pleroma.User.block(user2, user)
487
488 response =
489 conn
490 |> post("/ostatus_subscribe", %{
491 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
492 })
493 |> response(200)
494
495 assert response =~ "Error following account"
496 end
497 end
498
499 describe "GET /api/pleroma/healthcheck" do
500 clear_config([:instance, :healthcheck])
501
502 test "returns 503 when healthcheck disabled", %{conn: conn} do
503 Pleroma.Config.put([:instance, :healthcheck], false)
504
505 response =
506 conn
507 |> get("/api/pleroma/healthcheck")
508 |> json_response(503)
509
510 assert response == %{}
511 end
512
513 test "returns 200 when healthcheck enabled and all ok", %{conn: conn} do
514 Pleroma.Config.put([:instance, :healthcheck], true)
515
516 with_mock Pleroma.Healthcheck,
517 system_info: fn -> %Pleroma.Healthcheck{healthy: true} end do
518 response =
519 conn
520 |> get("/api/pleroma/healthcheck")
521 |> json_response(200)
522
523 assert %{
524 "active" => _,
525 "healthy" => true,
526 "idle" => _,
527 "memory_used" => _,
528 "pool_size" => _
529 } = response
530 end
531 end
532
533 test "returns 503 when healthcheck enabled and health is false", %{conn: conn} do
534 Pleroma.Config.put([:instance, :healthcheck], true)
535
536 with_mock Pleroma.Healthcheck,
537 system_info: fn -> %Pleroma.Healthcheck{healthy: false} end do
538 response =
539 conn
540 |> get("/api/pleroma/healthcheck")
541 |> json_response(503)
542
543 assert %{
544 "active" => _,
545 "healthy" => false,
546 "idle" => _,
547 "memory_used" => _,
548 "pool_size" => _
549 } = response
550 end
551 end
552 end
553
554 describe "POST /api/pleroma/disable_account" do
555 test "it returns HTTP 200", %{conn: conn} do
556 user = insert(:user)
557
558 response =
559 conn
560 |> assign(:user, user)
561 |> post("/api/pleroma/disable_account", %{"password" => "test"})
562 |> json_response(:ok)
563
564 assert response == %{"status" => "success"}
565 ObanHelpers.perform_all()
566
567 user = User.get_cached_by_id(user.id)
568
569 assert user.info.deactivated == true
570 end
571
572 test "it returns returns when password invalid", %{conn: conn} do
573 user = insert(:user)
574
575 response =
576 conn
577 |> assign(:user, user)
578 |> post("/api/pleroma/disable_account", %{"password" => "test1"})
579 |> json_response(:ok)
580
581 assert response == %{"error" => "Invalid password."}
582 user = User.get_cached_by_id(user.id)
583
584 refute user.info.deactivated
585 end
586 end
587
588 describe "GET /api/statusnet/version" do
589 test "it returns version in xml format", %{conn: conn} do
590 response =
591 conn
592 |> put_req_header("accept", "application/xml")
593 |> get("/api/statusnet/version")
594 |> response(:ok)
595
596 assert response == "<version>#{Pleroma.Application.named_version()}</version>"
597 end
598
599 test "it returns version in json format", %{conn: conn} do
600 response =
601 conn
602 |> put_req_header("accept", "application/json")
603 |> get("/api/statusnet/version")
604 |> json_response(:ok)
605
606 assert response == "#{Pleroma.Application.named_version()}"
607 end
608 end
609
610 describe "POST /main/ostatus - remote_subscribe/2" do
611 test "renders subscribe form", %{conn: conn} do
612 user = insert(:user)
613
614 response =
615 conn
616 |> post("/main/ostatus", %{"nickname" => user.nickname, "profile" => ""})
617 |> response(:ok)
618
619 refute response =~ "Could not find user"
620 assert response =~ "Remotely follow #{user.nickname}"
621 end
622
623 test "renders subscribe form with error when user not found", %{conn: conn} do
624 response =
625 conn
626 |> post("/main/ostatus", %{"nickname" => "nickname", "profile" => ""})
627 |> response(:ok)
628
629 assert response =~ "Could not find user"
630 refute response =~ "Remotely follow"
631 end
632
633 test "it redirect to webfinger url", %{conn: conn} do
634 user = insert(:user)
635 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
636
637 conn =
638 conn
639 |> post("/main/ostatus", %{
640 "user" => %{"nickname" => user.nickname, "profile" => user2.ap_id}
641 })
642
643 assert redirected_to(conn) ==
644 "https://social.heldscal.la/main/ostatussub?profile=#{user.ap_id}"
645 end
646
647 test "it renders form with error when use not found", %{conn: conn} do
648 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
649
650 response =
651 conn
652 |> post("/main/ostatus", %{"user" => %{"nickname" => "jimm", "profile" => user2.ap_id}})
653 |> response(:ok)
654
655 assert response =~ "Something went wrong."
656 end
657 end
658
659 test "it returns new captcha", %{conn: conn} do
660 with_mock Pleroma.Captcha,
661 new: fn -> "test_captcha" end do
662 resp =
663 conn
664 |> get("/api/pleroma/captcha")
665 |> response(200)
666
667 assert resp == "\"test_captcha\""
668 assert called(Pleroma.Captcha.new())
669 end
670 end
671
672 defp with_credentials(conn, username, password) do
673 header_content = "Basic " <> Base.encode64("#{username}:#{password}")
674 put_req_header(conn, "authorization", header_content)
675 end
676
677 defp valid_user(_context) do
678 user = insert(:user)
679 [user: user]
680 end
681
682 describe "POST /api/pleroma/change_email" do
683 setup [:valid_user]
684
685 test "without credentials", %{conn: conn} do
686 conn = post(conn, "/api/pleroma/change_email")
687 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
688 end
689
690 test "with credentials and invalid password", %{conn: conn, user: current_user} do
691 conn =
692 conn
693 |> with_credentials(current_user.nickname, "test")
694 |> post("/api/pleroma/change_email", %{
695 "password" => "hi",
696 "email" => "test@test.com"
697 })
698
699 assert json_response(conn, 200) == %{"error" => "Invalid password."}
700 end
701
702 test "with credentials, valid password and invalid email", %{
703 conn: conn,
704 user: current_user
705 } do
706 conn =
707 conn
708 |> with_credentials(current_user.nickname, "test")
709 |> post("/api/pleroma/change_email", %{
710 "password" => "test",
711 "email" => "foobar"
712 })
713
714 assert json_response(conn, 200) == %{"error" => "Email has invalid format."}
715 end
716
717 test "with credentials, valid password and no email", %{
718 conn: conn,
719 user: current_user
720 } do
721 conn =
722 conn
723 |> with_credentials(current_user.nickname, "test")
724 |> post("/api/pleroma/change_email", %{
725 "password" => "test"
726 })
727
728 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
729 end
730
731 test "with credentials, valid password and blank email", %{
732 conn: conn,
733 user: current_user
734 } do
735 conn =
736 conn
737 |> with_credentials(current_user.nickname, "test")
738 |> post("/api/pleroma/change_email", %{
739 "password" => "test",
740 "email" => ""
741 })
742
743 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
744 end
745
746 test "with credentials, valid password and non unique email", %{
747 conn: conn,
748 user: current_user
749 } do
750 user = insert(:user)
751
752 conn =
753 conn
754 |> with_credentials(current_user.nickname, "test")
755 |> post("/api/pleroma/change_email", %{
756 "password" => "test",
757 "email" => user.email
758 })
759
760 assert json_response(conn, 200) == %{"error" => "Email has already been taken."}
761 end
762
763 test "with credentials, valid password and valid email", %{
764 conn: conn,
765 user: current_user
766 } do
767 conn =
768 conn
769 |> with_credentials(current_user.nickname, "test")
770 |> post("/api/pleroma/change_email", %{
771 "password" => "test",
772 "email" => "cofe@foobar.com"
773 })
774
775 assert json_response(conn, 200) == %{"status" => "success"}
776 end
777 end
778 end