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