da3f6fa61ef633c36e0ae72ec457f0d88aa92f6a
[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 "block_from_strangers" => true,
195 "bar" => 1
196 })
197 |> json_response(:ok)
198
199 user = refresh_record(user)
200
201 assert %Pleroma.User.NotificationSetting{
202 block_from_strangers: true,
203 privacy_option: false
204 } == user.notification_settings
205 end
206
207 test "it updates notification privacy option", %{user: user, conn: conn} do
208 conn
209 |> put("/api/pleroma/notification_settings", %{"privacy_option" => "1"})
210 |> json_response(:ok)
211
212 user = refresh_record(user)
213
214 assert %Pleroma.User.NotificationSetting{
215 block_from_strangers: false,
216 privacy_option: true
217 } == user.notification_settings
218 end
219 end
220
221 describe "GET /api/statusnet/config" do
222 test "it returns config in xml format", %{conn: conn} do
223 instance = Config.get(:instance)
224
225 response =
226 conn
227 |> put_req_header("accept", "application/xml")
228 |> get("/api/statusnet/config")
229 |> response(:ok)
230
231 assert response ==
232 "<config>\n<site>\n<name>#{Keyword.get(instance, :name)}</name>\n<site>#{
233 Pleroma.Web.base_url()
234 }</site>\n<textlimit>#{Keyword.get(instance, :limit)}</textlimit>\n<closed>#{
235 !Keyword.get(instance, :registrations_open)
236 }</closed>\n</site>\n</config>\n"
237 end
238
239 test "it returns config in json format", %{conn: conn} do
240 instance = Config.get(:instance)
241 Config.put([:instance, :managed_config], true)
242 Config.put([:instance, :registrations_open], false)
243 Config.put([:instance, :invites_enabled], true)
244 Config.put([:instance, :public], false)
245 Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
246
247 response =
248 conn
249 |> put_req_header("accept", "application/json")
250 |> get("/api/statusnet/config")
251 |> json_response(:ok)
252
253 expected_data = %{
254 "site" => %{
255 "accountActivationRequired" => "0",
256 "closed" => "1",
257 "description" => Keyword.get(instance, :description),
258 "invitesEnabled" => "1",
259 "name" => Keyword.get(instance, :name),
260 "pleromafe" => %{"theme" => "asuka-hospital"},
261 "private" => "1",
262 "safeDMMentionsEnabled" => "0",
263 "server" => Pleroma.Web.base_url(),
264 "textlimit" => to_string(Keyword.get(instance, :limit)),
265 "uploadlimit" => %{
266 "avatarlimit" => to_string(Keyword.get(instance, :avatar_upload_limit)),
267 "backgroundlimit" => to_string(Keyword.get(instance, :background_upload_limit)),
268 "bannerlimit" => to_string(Keyword.get(instance, :banner_upload_limit)),
269 "uploadlimit" => to_string(Keyword.get(instance, :upload_limit))
270 },
271 "vapidPublicKey" => Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
272 }
273 }
274
275 assert response == expected_data
276 end
277
278 test "returns the state of safe_dm_mentions flag", %{conn: conn} do
279 Config.put([:instance, :safe_dm_mentions], true)
280
281 response =
282 conn
283 |> get("/api/statusnet/config.json")
284 |> json_response(:ok)
285
286 assert response["site"]["safeDMMentionsEnabled"] == "1"
287
288 Config.put([:instance, :safe_dm_mentions], false)
289
290 response =
291 conn
292 |> get("/api/statusnet/config.json")
293 |> json_response(:ok)
294
295 assert response["site"]["safeDMMentionsEnabled"] == "0"
296 end
297
298 test "it returns the managed config", %{conn: conn} do
299 Config.put([:instance, :managed_config], false)
300 Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
301
302 response =
303 conn
304 |> get("/api/statusnet/config.json")
305 |> json_response(:ok)
306
307 refute response["site"]["pleromafe"]
308
309 Config.put([:instance, :managed_config], true)
310
311 response =
312 conn
313 |> get("/api/statusnet/config.json")
314 |> json_response(:ok)
315
316 assert response["site"]["pleromafe"] == %{"theme" => "asuka-hospital"}
317 end
318 end
319
320 describe "GET /api/pleroma/frontend_configurations" do
321 test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
322 config = [
323 frontend_a: %{
324 x: 1,
325 y: 2
326 },
327 frontend_b: %{
328 z: 3
329 }
330 ]
331
332 Config.put(:frontend_configurations, config)
333
334 response =
335 conn
336 |> get("/api/pleroma/frontend_configurations")
337 |> json_response(:ok)
338
339 assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
340 end
341 end
342
343 describe "/api/pleroma/emoji" do
344 test "returns json with custom emoji with tags", %{conn: conn} do
345 emoji =
346 conn
347 |> get("/api/pleroma/emoji")
348 |> json_response(200)
349
350 assert Enum.all?(emoji, fn
351 {_key,
352 %{
353 "image_url" => url,
354 "tags" => tags
355 }} ->
356 is_binary(url) and is_list(tags)
357 end)
358 end
359 end
360
361 describe "GET /api/pleroma/healthcheck" do
362 setup do: clear_config([:instance, :healthcheck])
363
364 test "returns 503 when healthcheck disabled", %{conn: conn} do
365 Config.put([:instance, :healthcheck], false)
366
367 response =
368 conn
369 |> get("/api/pleroma/healthcheck")
370 |> json_response(503)
371
372 assert response == %{}
373 end
374
375 test "returns 200 when healthcheck enabled and all ok", %{conn: conn} do
376 Config.put([:instance, :healthcheck], true)
377
378 with_mock Pleroma.Healthcheck,
379 system_info: fn -> %Pleroma.Healthcheck{healthy: true} end do
380 response =
381 conn
382 |> get("/api/pleroma/healthcheck")
383 |> json_response(200)
384
385 assert %{
386 "active" => _,
387 "healthy" => true,
388 "idle" => _,
389 "memory_used" => _,
390 "pool_size" => _
391 } = response
392 end
393 end
394
395 test "returns 503 when healthcheck enabled and health is false", %{conn: conn} do
396 Config.put([:instance, :healthcheck], true)
397
398 with_mock Pleroma.Healthcheck,
399 system_info: fn -> %Pleroma.Healthcheck{healthy: false} end do
400 response =
401 conn
402 |> get("/api/pleroma/healthcheck")
403 |> json_response(503)
404
405 assert %{
406 "active" => _,
407 "healthy" => false,
408 "idle" => _,
409 "memory_used" => _,
410 "pool_size" => _
411 } = response
412 end
413 end
414 end
415
416 describe "POST /api/pleroma/disable_account" do
417 setup do: oauth_access(["write:accounts"])
418
419 test "with valid permissions and password, it disables the account", %{conn: conn, user: user} do
420 response =
421 conn
422 |> post("/api/pleroma/disable_account", %{"password" => "test"})
423 |> json_response(:ok)
424
425 assert response == %{"status" => "success"}
426 ObanHelpers.perform_all()
427
428 user = User.get_cached_by_id(user.id)
429
430 assert user.deactivated == true
431 end
432
433 test "with valid permissions and invalid password, it returns an error", %{conn: conn} do
434 user = insert(:user)
435
436 response =
437 conn
438 |> post("/api/pleroma/disable_account", %{"password" => "test1"})
439 |> json_response(:ok)
440
441 assert response == %{"error" => "Invalid password."}
442 user = User.get_cached_by_id(user.id)
443
444 refute user.deactivated
445 end
446 end
447
448 describe "GET /api/statusnet/version" do
449 test "it returns version in xml format", %{conn: conn} do
450 response =
451 conn
452 |> put_req_header("accept", "application/xml")
453 |> get("/api/statusnet/version")
454 |> response(:ok)
455
456 assert response == "<version>#{Pleroma.Application.named_version()}</version>"
457 end
458
459 test "it returns version in json format", %{conn: conn} do
460 response =
461 conn
462 |> put_req_header("accept", "application/json")
463 |> get("/api/statusnet/version")
464 |> json_response(:ok)
465
466 assert response == "#{Pleroma.Application.named_version()}"
467 end
468 end
469
470 describe "POST /main/ostatus - remote_subscribe/2" do
471 setup do: clear_config([:instance, :federating], true)
472
473 test "renders subscribe form", %{conn: conn} do
474 user = insert(:user)
475
476 response =
477 conn
478 |> post("/main/ostatus", %{"nickname" => user.nickname, "profile" => ""})
479 |> response(:ok)
480
481 refute response =~ "Could not find user"
482 assert response =~ "Remotely follow #{user.nickname}"
483 end
484
485 test "renders subscribe form with error when user not found", %{conn: conn} do
486 response =
487 conn
488 |> post("/main/ostatus", %{"nickname" => "nickname", "profile" => ""})
489 |> response(:ok)
490
491 assert response =~ "Could not find user"
492 refute response =~ "Remotely follow"
493 end
494
495 test "it redirect to webfinger url", %{conn: conn} do
496 user = insert(:user)
497 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
498
499 conn =
500 conn
501 |> post("/main/ostatus", %{
502 "user" => %{"nickname" => user.nickname, "profile" => user2.ap_id}
503 })
504
505 assert redirected_to(conn) ==
506 "https://social.heldscal.la/main/ostatussub?profile=#{user.ap_id}"
507 end
508
509 test "it renders form with error when user not found", %{conn: conn} do
510 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
511
512 response =
513 conn
514 |> post("/main/ostatus", %{"user" => %{"nickname" => "jimm", "profile" => user2.ap_id}})
515 |> response(:ok)
516
517 assert response =~ "Something went wrong."
518 end
519 end
520
521 test "it returns new captcha", %{conn: conn} do
522 with_mock Pleroma.Captcha,
523 new: fn -> "test_captcha" end do
524 resp =
525 conn
526 |> get("/api/pleroma/captcha")
527 |> response(200)
528
529 assert resp == "\"test_captcha\""
530 assert called(Pleroma.Captcha.new())
531 end
532 end
533
534 describe "POST /api/pleroma/change_email" do
535 setup do: oauth_access(["write:accounts"])
536
537 test "without permissions", %{conn: conn} do
538 conn =
539 conn
540 |> assign(:token, nil)
541 |> post("/api/pleroma/change_email")
542
543 assert json_response(conn, 403) == %{"error" => "Insufficient permissions: write:accounts."}
544 end
545
546 test "with proper permissions and invalid password", %{conn: conn} do
547 conn =
548 post(conn, "/api/pleroma/change_email", %{
549 "password" => "hi",
550 "email" => "test@test.com"
551 })
552
553 assert json_response(conn, 200) == %{"error" => "Invalid password."}
554 end
555
556 test "with proper permissions, valid password and invalid email", %{
557 conn: conn
558 } do
559 conn =
560 post(conn, "/api/pleroma/change_email", %{
561 "password" => "test",
562 "email" => "foobar"
563 })
564
565 assert json_response(conn, 200) == %{"error" => "Email has invalid format."}
566 end
567
568 test "with proper permissions, valid password and no email", %{
569 conn: conn
570 } do
571 conn =
572 post(conn, "/api/pleroma/change_email", %{
573 "password" => "test"
574 })
575
576 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
577 end
578
579 test "with proper permissions, valid password and blank email", %{
580 conn: conn
581 } do
582 conn =
583 post(conn, "/api/pleroma/change_email", %{
584 "password" => "test",
585 "email" => ""
586 })
587
588 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
589 end
590
591 test "with proper permissions, valid password and non unique email", %{
592 conn: conn
593 } do
594 user = insert(:user)
595
596 conn =
597 post(conn, "/api/pleroma/change_email", %{
598 "password" => "test",
599 "email" => user.email
600 })
601
602 assert json_response(conn, 200) == %{"error" => "Email has already been taken."}
603 end
604
605 test "with proper permissions, valid password and valid email", %{
606 conn: conn
607 } do
608 conn =
609 post(conn, "/api/pleroma/change_email", %{
610 "password" => "test",
611 "email" => "cofe@foobar.com"
612 })
613
614 assert json_response(conn, 200) == %{"status" => "success"}
615 end
616 end
617
618 describe "POST /api/pleroma/change_password" do
619 setup do: oauth_access(["write:accounts"])
620
621 test "without permissions", %{conn: conn} do
622 conn =
623 conn
624 |> assign(:token, nil)
625 |> post("/api/pleroma/change_password")
626
627 assert json_response(conn, 403) == %{"error" => "Insufficient permissions: write:accounts."}
628 end
629
630 test "with proper permissions and invalid password", %{conn: conn} do
631 conn =
632 post(conn, "/api/pleroma/change_password", %{
633 "password" => "hi",
634 "new_password" => "newpass",
635 "new_password_confirmation" => "newpass"
636 })
637
638 assert json_response(conn, 200) == %{"error" => "Invalid password."}
639 end
640
641 test "with proper permissions, valid password and new password and confirmation not matching",
642 %{
643 conn: conn
644 } do
645 conn =
646 post(conn, "/api/pleroma/change_password", %{
647 "password" => "test",
648 "new_password" => "newpass",
649 "new_password_confirmation" => "notnewpass"
650 })
651
652 assert json_response(conn, 200) == %{
653 "error" => "New password does not match confirmation."
654 }
655 end
656
657 test "with proper permissions, valid password and invalid new password", %{
658 conn: conn
659 } do
660 conn =
661 post(conn, "/api/pleroma/change_password", %{
662 "password" => "test",
663 "new_password" => "",
664 "new_password_confirmation" => ""
665 })
666
667 assert json_response(conn, 200) == %{
668 "error" => "New password can't be blank."
669 }
670 end
671
672 test "with proper permissions, valid password and matching new password and confirmation", %{
673 conn: conn,
674 user: user
675 } do
676 conn =
677 post(conn, "/api/pleroma/change_password", %{
678 "password" => "test",
679 "new_password" => "newpass",
680 "new_password_confirmation" => "newpass"
681 })
682
683 assert json_response(conn, 200) == %{"status" => "success"}
684 fetched_user = User.get_cached_by_id(user.id)
685 assert Pbkdf2.verify_pass("newpass", fetched_user.password_hash) == true
686 end
687 end
688
689 describe "POST /api/pleroma/delete_account" do
690 setup do: oauth_access(["write:accounts"])
691
692 test "without permissions", %{conn: conn} do
693 conn =
694 conn
695 |> assign(:token, nil)
696 |> post("/api/pleroma/delete_account")
697
698 assert json_response(conn, 403) ==
699 %{"error" => "Insufficient permissions: write:accounts."}
700 end
701
702 test "with proper permissions and wrong or missing password", %{conn: conn} do
703 for params <- [%{"password" => "hi"}, %{}] do
704 ret_conn = post(conn, "/api/pleroma/delete_account", params)
705
706 assert json_response(ret_conn, 200) == %{"error" => "Invalid password."}
707 end
708 end
709
710 test "with proper permissions and valid password", %{conn: conn} do
711 conn = post(conn, "/api/pleroma/delete_account", %{"password" => "test"})
712
713 assert json_response(conn, 200) == %{"status" => "success"}
714 end
715 end
716 end