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