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