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