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