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