1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
6 use Pleroma.Web.ConnCase
7 use Oban.Testing, repo: Pleroma.Repo
9 alias Pleroma.Tests.ObanHelpers
12 import Pleroma.Factory
16 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
20 clear_config([:instance])
21 clear_config([:frontend_configurations, :pleroma_fe])
23 describe "POST /api/pleroma/follow_import" do
24 setup do: oauth_access(["follow"])
26 test "it returns HTTP 200", %{conn: conn} do
31 |> post("/api/pleroma/follow_import", %{"list" => "#{user2.ap_id}"})
34 assert response == "job started"
37 test "it imports follow lists from file", %{user: user1, conn: conn} do
42 read!: fn "follow_list.txt" ->
43 "Account address,Show boosts\n#{user2.ap_id},true"
48 |> post("/api/pleroma/follow_import", %{"list" => %Plug.Upload{path: "follow_list.txt"}})
51 assert response == "job started"
53 assert ObanHelpers.member?(
55 "op" => "follow_import",
56 "follower_id" => user1.id,
57 "followed_identifiers" => [user2.ap_id]
59 all_enqueued(worker: Pleroma.Workers.BackgroundWorker)
64 test "it imports new-style mastodon follow lists", %{conn: conn} do
69 |> post("/api/pleroma/follow_import", %{
70 "list" => "Account address,Show boosts\n#{user2.ap_id},true"
74 assert response == "job started"
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)
83 for token <- [token1, token2, token3] do
86 |> put_req_header("authorization", "Bearer #{token.token}")
87 |> post("/api/pleroma/follow_import", %{"list" => "#{another_user.ap_id}"})
90 assert %{"error" => "Insufficient permissions: follow | write:follows."} ==
91 json_response(conn, 403)
93 assert json_response(conn, 200)
99 describe "POST /api/pleroma/blocks_import" do
100 # Note: "follow" or "write:blocks" permission is required
101 setup do: oauth_access(["write:blocks"])
103 test "it returns HTTP 200", %{conn: conn} do
104 user2 = insert(:user)
108 |> post("/api/pleroma/blocks_import", %{"list" => "#{user2.ap_id}"})
109 |> json_response(:ok)
111 assert response == "job started"
114 test "it imports blocks users from file", %{user: user1, conn: conn} do
115 user2 = insert(:user)
116 user3 = insert(:user)
119 {File, [], read!: fn "blocks_list.txt" -> "#{user2.ap_id} #{user3.ap_id}" end}
123 |> post("/api/pleroma/blocks_import", %{"list" => %Plug.Upload{path: "blocks_list.txt"}})
124 |> json_response(:ok)
126 assert response == "job started"
128 assert ObanHelpers.member?(
130 "op" => "blocks_import",
131 "blocker_id" => user1.id,
132 "blocked_identifiers" => [user2.ap_id, user3.ap_id]
134 all_enqueued(worker: Pleroma.Workers.BackgroundWorker)
140 describe "PUT /api/pleroma/notification_settings" do
141 setup do: oauth_access(["write:accounts"])
143 test "it updates notification settings", %{user: user, conn: conn} do
145 |> put("/api/pleroma/notification_settings", %{
146 "followers" => false,
149 |> json_response(:ok)
151 user = refresh_record(user)
153 assert %Pleroma.User.NotificationSetting{
158 privacy_option: false
159 } == user.notification_settings
162 test "it updates notification privacy option", %{user: user, conn: conn} do
164 |> put("/api/pleroma/notification_settings", %{"privacy_option" => "1"})
165 |> json_response(:ok)
167 user = refresh_record(user)
169 assert %Pleroma.User.NotificationSetting{
175 } == user.notification_settings
179 describe "GET /api/statusnet/config" do
180 test "it returns config in xml format", %{conn: conn} do
181 instance = Pleroma.Config.get(:instance)
185 |> put_req_header("accept", "application/xml")
186 |> get("/api/statusnet/config")
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"
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"})
207 |> put_req_header("accept", "application/json")
208 |> get("/api/statusnet/config")
209 |> json_response(:ok)
213 "accountActivationRequired" => "0",
215 "description" => Keyword.get(instance, :description),
216 "invitesEnabled" => "1",
217 "name" => Keyword.get(instance, :name),
218 "pleromafe" => %{"theme" => "asuka-hospital"},
220 "safeDMMentionsEnabled" => "0",
221 "server" => Pleroma.Web.base_url(),
222 "textlimit" => to_string(Keyword.get(instance, :limit)),
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))
229 "vapidPublicKey" => Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
233 assert response == expected_data
236 test "returns the state of safe_dm_mentions flag", %{conn: conn} do
237 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
241 |> get("/api/statusnet/config.json")
242 |> json_response(:ok)
244 assert response["site"]["safeDMMentionsEnabled"] == "1"
246 Pleroma.Config.put([:instance, :safe_dm_mentions], false)
250 |> get("/api/statusnet/config.json")
251 |> json_response(:ok)
253 assert response["site"]["safeDMMentionsEnabled"] == "0"
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"})
262 |> get("/api/statusnet/config.json")
263 |> json_response(:ok)
265 refute response["site"]["pleromafe"]
267 Pleroma.Config.put([:instance, :managed_config], true)
271 |> get("/api/statusnet/config.json")
272 |> json_response(:ok)
274 assert response["site"]["pleromafe"] == %{"theme" => "asuka-hospital"}
278 describe "GET /api/pleroma/frontend_configurations" do
279 test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
290 Pleroma.Config.put(:frontend_configurations, config)
294 |> get("/api/pleroma/frontend_configurations")
295 |> json_response(:ok)
297 assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
301 describe "/api/pleroma/emoji" do
302 test "returns json with custom emoji with tags", %{conn: conn} do
305 |> get("/api/pleroma/emoji")
306 |> json_response(200)
308 assert Enum.all?(emoji, fn
314 is_binary(url) and is_list(tags)
319 describe "GET /api/pleroma/healthcheck" do
320 clear_config([:instance, :healthcheck])
322 test "returns 503 when healthcheck disabled", %{conn: conn} do
323 Pleroma.Config.put([:instance, :healthcheck], false)
327 |> get("/api/pleroma/healthcheck")
328 |> json_response(503)
330 assert response == %{}
333 test "returns 200 when healthcheck enabled and all ok", %{conn: conn} do
334 Pleroma.Config.put([:instance, :healthcheck], true)
336 with_mock Pleroma.Healthcheck,
337 system_info: fn -> %Pleroma.Healthcheck{healthy: true} end do
340 |> get("/api/pleroma/healthcheck")
341 |> json_response(200)
353 test "returns 503 when healthcheck enabled and health is false", %{conn: conn} do
354 Pleroma.Config.put([:instance, :healthcheck], true)
356 with_mock Pleroma.Healthcheck,
357 system_info: fn -> %Pleroma.Healthcheck{healthy: false} end do
360 |> get("/api/pleroma/healthcheck")
361 |> json_response(503)
374 describe "POST /api/pleroma/disable_account" do
375 setup do: oauth_access(["write:accounts"])
377 test "with valid permissions and password, it disables the account", %{conn: conn, user: user} do
380 |> post("/api/pleroma/disable_account", %{"password" => "test"})
381 |> json_response(:ok)
383 assert response == %{"status" => "success"}
384 ObanHelpers.perform_all()
386 user = User.get_cached_by_id(user.id)
388 assert user.deactivated == true
391 test "with valid permissions and invalid password, it returns an error", %{conn: conn} do
396 |> post("/api/pleroma/disable_account", %{"password" => "test1"})
397 |> json_response(:ok)
399 assert response == %{"error" => "Invalid password."}
400 user = User.get_cached_by_id(user.id)
402 refute user.deactivated
406 describe "GET /api/statusnet/version" do
407 test "it returns version in xml format", %{conn: conn} do
410 |> put_req_header("accept", "application/xml")
411 |> get("/api/statusnet/version")
414 assert response == "<version>#{Pleroma.Application.named_version()}</version>"
417 test "it returns version in json format", %{conn: conn} do
420 |> put_req_header("accept", "application/json")
421 |> get("/api/statusnet/version")
422 |> json_response(:ok)
424 assert response == "#{Pleroma.Application.named_version()}"
428 describe "POST /main/ostatus - remote_subscribe/2" do
429 test "renders subscribe form", %{conn: conn} do
434 |> post("/main/ostatus", %{"nickname" => user.nickname, "profile" => ""})
437 refute response =~ "Could not find user"
438 assert response =~ "Remotely follow #{user.nickname}"
441 test "renders subscribe form with error when user not found", %{conn: conn} do
444 |> post("/main/ostatus", %{"nickname" => "nickname", "profile" => ""})
447 assert response =~ "Could not find user"
448 refute response =~ "Remotely follow"
451 test "it redirect to webfinger url", %{conn: conn} do
453 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
457 |> post("/main/ostatus", %{
458 "user" => %{"nickname" => user.nickname, "profile" => user2.ap_id}
461 assert redirected_to(conn) ==
462 "https://social.heldscal.la/main/ostatussub?profile=#{user.ap_id}"
465 test "it renders form with error when user not found", %{conn: conn} do
466 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
470 |> post("/main/ostatus", %{"user" => %{"nickname" => "jimm", "profile" => user2.ap_id}})
473 assert response =~ "Something went wrong."
477 test "it returns new captcha", %{conn: conn} do
478 with_mock Pleroma.Captcha,
479 new: fn -> "test_captcha" end do
482 |> get("/api/pleroma/captcha")
485 assert resp == "\"test_captcha\""
486 assert called(Pleroma.Captcha.new())
490 describe "POST /api/pleroma/change_email" do
491 setup do: oauth_access(["write:accounts"])
493 test "without permissions", %{conn: conn} do
496 |> assign(:token, nil)
497 |> post("/api/pleroma/change_email")
499 assert json_response(conn, 403) == %{"error" => "Insufficient permissions: write:accounts."}
502 test "with proper permissions and invalid password", %{conn: conn} do
504 post(conn, "/api/pleroma/change_email", %{
506 "email" => "test@test.com"
509 assert json_response(conn, 200) == %{"error" => "Invalid password."}
512 test "with proper permissions, valid password and invalid email", %{
516 post(conn, "/api/pleroma/change_email", %{
517 "password" => "test",
521 assert json_response(conn, 200) == %{"error" => "Email has invalid format."}
524 test "with proper permissions, valid password and no email", %{
528 post(conn, "/api/pleroma/change_email", %{
532 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
535 test "with proper permissions, valid password and blank email", %{
539 post(conn, "/api/pleroma/change_email", %{
540 "password" => "test",
544 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
547 test "with proper permissions, valid password and non unique email", %{
553 post(conn, "/api/pleroma/change_email", %{
554 "password" => "test",
555 "email" => user.email
558 assert json_response(conn, 200) == %{"error" => "Email has already been taken."}
561 test "with proper permissions, valid password and valid email", %{
565 post(conn, "/api/pleroma/change_email", %{
566 "password" => "test",
567 "email" => "cofe@foobar.com"
570 assert json_response(conn, 200) == %{"status" => "success"}
574 describe "POST /api/pleroma/change_password" do
575 setup do: oauth_access(["write:accounts"])
577 test "without permissions", %{conn: conn} do
580 |> assign(:token, nil)
581 |> post("/api/pleroma/change_password")
583 assert json_response(conn, 403) == %{"error" => "Insufficient permissions: write:accounts."}
586 test "with proper permissions and invalid password", %{conn: conn} do
588 post(conn, "/api/pleroma/change_password", %{
590 "new_password" => "newpass",
591 "new_password_confirmation" => "newpass"
594 assert json_response(conn, 200) == %{"error" => "Invalid password."}
597 test "with proper permissions, valid password and new password and confirmation not matching",
602 post(conn, "/api/pleroma/change_password", %{
603 "password" => "test",
604 "new_password" => "newpass",
605 "new_password_confirmation" => "notnewpass"
608 assert json_response(conn, 200) == %{
609 "error" => "New password does not match confirmation."
613 test "with proper permissions, valid password and invalid new password", %{
617 post(conn, "/api/pleroma/change_password", %{
618 "password" => "test",
619 "new_password" => "",
620 "new_password_confirmation" => ""
623 assert json_response(conn, 200) == %{
624 "error" => "New password can't be blank."
628 test "with proper permissions, valid password and matching new password and confirmation", %{
633 post(conn, "/api/pleroma/change_password", %{
634 "password" => "test",
635 "new_password" => "newpass",
636 "new_password_confirmation" => "newpass"
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
645 describe "POST /api/pleroma/delete_account" do
646 setup do: oauth_access(["write:accounts"])
648 test "without permissions", %{conn: conn} do
651 |> assign(:token, nil)
652 |> post("/api/pleroma/delete_account")
654 assert json_response(conn, 403) ==
655 %{"error" => "Insufficient permissions: write:accounts."}
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)
662 assert json_response(ret_conn, 200) == %{"error" => "Invalid password."}
666 test "with proper permissions and valid password", %{conn: conn} do
667 conn = post(conn, "/api/pleroma/delete_account", %{"password" => "test"})
669 assert json_response(conn, 200) == %{"status" => "success"}