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
10 alias Pleroma.Web.CommonAPI
11 import Pleroma.Factory
15 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
19 clear_config([:instance])
20 clear_config([:frontend_configurations, :pleroma_fe])
21 clear_config([:user, :deny_follow_blocked])
23 describe "POST /api/pleroma/follow_import" do
24 test "it returns HTTP 200", %{conn: conn} do
30 |> assign(:user, user1)
31 |> post("/api/pleroma/follow_import", %{"list" => "#{user2.ap_id}"})
34 assert response == "job started"
37 test "it imports follow lists from file", %{conn: conn} do
43 read!: fn "follow_list.txt" ->
44 "Account address,Show boosts\n#{user2.ap_id},true"
46 {PleromaJobQueue, [:passthrough], []}
50 |> assign(:user, user1)
51 |> post("/api/pleroma/follow_import", %{"list" => %Plug.Upload{path: "follow_list.txt"}})
55 PleromaJobQueue.enqueue(
58 [:follow_import, user1, [user2.ap_id]]
62 assert response == "job started"
66 test "it imports new-style mastodon follow lists", %{conn: conn} do
72 |> assign(:user, user1)
73 |> post("/api/pleroma/follow_import", %{
74 "list" => "Account address,Show boosts\n#{user2.ap_id},true"
78 assert response == "job started"
81 test "requires 'follow' permission", %{conn: conn} do
82 token1 = insert(:oauth_token, scopes: ["read", "write"])
83 token2 = insert(:oauth_token, scopes: ["follow"])
84 another_user = insert(:user)
86 for token <- [token1, token2] do
89 |> put_req_header("authorization", "Bearer #{token.token}")
90 |> post("/api/pleroma/follow_import", %{"list" => "#{another_user.ap_id}"})
93 assert %{"error" => "Insufficient permissions: follow."} == json_response(conn, 403)
95 assert json_response(conn, 200)
101 describe "POST /api/pleroma/blocks_import" do
102 test "it returns HTTP 200", %{conn: conn} do
103 user1 = insert(:user)
104 user2 = insert(:user)
108 |> assign(:user, user1)
109 |> post("/api/pleroma/blocks_import", %{"list" => "#{user2.ap_id}"})
110 |> json_response(:ok)
112 assert response == "job started"
115 test "it imports blocks users from file", %{conn: conn} do
116 user1 = insert(:user)
117 user2 = insert(:user)
118 user3 = insert(:user)
121 {File, [], read!: fn "blocks_list.txt" -> "#{user2.ap_id} #{user3.ap_id}" end},
122 {PleromaJobQueue, [:passthrough], []}
126 |> assign(:user, user1)
127 |> post("/api/pleroma/blocks_import", %{"list" => %Plug.Upload{path: "blocks_list.txt"}})
128 |> json_response(:ok)
131 PleromaJobQueue.enqueue(
134 [:blocks_import, user1, [user2.ap_id, user3.ap_id]]
138 assert response == "job started"
143 describe "PUT /api/pleroma/notification_settings" do
144 test "it updates notification settings", %{conn: conn} do
148 |> assign(:user, user)
149 |> put("/api/pleroma/notification_settings", %{
150 "followers" => false,
153 |> json_response(:ok)
155 user = Repo.get(User, user.id)
158 "followers" => false,
160 "non_follows" => true,
161 "non_followers" => true
162 } == user.info.notification_settings
166 describe "GET /api/statusnet/config" do
167 test "it returns config in xml format", %{conn: conn} do
168 instance = Pleroma.Config.get(:instance)
172 |> put_req_header("accept", "application/xml")
173 |> get("/api/statusnet/config")
177 "<config>\n<site>\n<name>#{Keyword.get(instance, :name)}</name>\n<site>#{
178 Pleroma.Web.base_url()
179 }</site>\n<textlimit>#{Keyword.get(instance, :limit)}</textlimit>\n<closed>#{
180 !Keyword.get(instance, :registrations_open)
181 }</closed>\n</site>\n</config>\n"
184 test "it returns config in json format", %{conn: conn} do
185 instance = Pleroma.Config.get(:instance)
186 Pleroma.Config.put([:instance, :managed_config], true)
187 Pleroma.Config.put([:instance, :registrations_open], false)
188 Pleroma.Config.put([:instance, :invites_enabled], true)
189 Pleroma.Config.put([:instance, :public], false)
190 Pleroma.Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
194 |> put_req_header("accept", "application/json")
195 |> get("/api/statusnet/config")
196 |> json_response(:ok)
200 "accountActivationRequired" => "0",
202 "description" => Keyword.get(instance, :description),
203 "invitesEnabled" => "1",
204 "name" => Keyword.get(instance, :name),
205 "pleromafe" => %{"theme" => "asuka-hospital"},
207 "safeDMMentionsEnabled" => "0",
208 "server" => Pleroma.Web.base_url(),
209 "textlimit" => to_string(Keyword.get(instance, :limit)),
211 "avatarlimit" => to_string(Keyword.get(instance, :avatar_upload_limit)),
212 "backgroundlimit" => to_string(Keyword.get(instance, :background_upload_limit)),
213 "bannerlimit" => to_string(Keyword.get(instance, :banner_upload_limit)),
214 "uploadlimit" => to_string(Keyword.get(instance, :upload_limit))
216 "vapidPublicKey" => Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
220 assert response == expected_data
223 test "returns the state of safe_dm_mentions flag", %{conn: conn} do
224 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
228 |> get("/api/statusnet/config.json")
229 |> json_response(:ok)
231 assert response["site"]["safeDMMentionsEnabled"] == "1"
233 Pleroma.Config.put([:instance, :safe_dm_mentions], false)
237 |> get("/api/statusnet/config.json")
238 |> json_response(:ok)
240 assert response["site"]["safeDMMentionsEnabled"] == "0"
243 test "it returns the managed config", %{conn: conn} do
244 Pleroma.Config.put([:instance, :managed_config], false)
245 Pleroma.Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
249 |> get("/api/statusnet/config.json")
250 |> json_response(:ok)
252 refute response["site"]["pleromafe"]
254 Pleroma.Config.put([:instance, :managed_config], true)
258 |> get("/api/statusnet/config.json")
259 |> json_response(:ok)
261 assert response["site"]["pleromafe"] == %{"theme" => "asuka-hospital"}
265 describe "GET /api/pleroma/frontend_configurations" do
266 test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
277 Pleroma.Config.put(:frontend_configurations, config)
281 |> get("/api/pleroma/frontend_configurations")
282 |> json_response(:ok)
284 assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
288 describe "/api/pleroma/emoji" do
289 test "returns json with custom emoji with tags", %{conn: conn} do
292 |> get("/api/pleroma/emoji")
293 |> json_response(200)
295 assert Enum.all?(emoji, fn
301 is_binary(url) and is_list(tags)
306 describe "GET /ostatus_subscribe - remote_follow/2" do
307 test "adds status to pleroma instance if the `acct` is a status", %{conn: conn} do
311 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie/statuses/101849165031453009"
314 assert redirected_to(conn) =~ "/notice/"
317 test "show follow account page if the `acct` is a account link", %{conn: conn} do
321 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie"
324 assert html_response(response, 200) =~ "Log in to follow"
327 test "show follow page if the `acct` is a account link", %{conn: conn} do
332 |> assign(:user, user)
333 |> get("/ostatus_subscribe?acct=https://mastodon.social/users/emelie")
335 assert html_response(response, 200) =~ "Remote follow"
338 test "show follow page with error when user cannot fecth by `acct` link", %{conn: conn} do
343 |> assign(:user, user)
344 |> get("/ostatus_subscribe?acct=https://mastodon.social/users/not_found")
346 assert html_response(response, 200) =~ "Error fetching user"
350 describe "POST /ostatus_subscribe - do_remote_follow/2 with assigned user " do
351 test "follows user", %{conn: conn} do
353 user2 = insert(:user)
357 |> assign(:user, user)
358 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
361 assert response =~ "Account followed!"
362 assert user2.follower_address in refresh_record(user).following
365 test "returns error when user is deactivated", %{conn: conn} do
366 user = insert(:user, info: %{deactivated: true})
367 user2 = insert(:user)
371 |> assign(:user, user)
372 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
375 assert response =~ "Error following account"
378 test "returns error when user is blocked", %{conn: conn} do
379 Pleroma.Config.put([:user, :deny_follow_blocked], true)
381 user2 = insert(:user)
383 {:ok, _user} = Pleroma.User.block(user2, user)
387 |> assign(:user, user)
388 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
391 assert response =~ "Error following account"
394 test "returns error when followee not found", %{conn: conn} do
399 |> assign(:user, user)
400 |> post("/ostatus_subscribe", %{"user" => %{"id" => "jimm"}})
403 assert response =~ "Error following account"
406 test "returns success result when user already in followers", %{conn: conn} do
408 user2 = insert(:user)
409 {:ok, _, _, _} = CommonAPI.follow(user, user2)
413 |> assign(:user, refresh_record(user))
414 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
417 assert response =~ "Account followed!"
421 describe "POST /ostatus_subscribe - do_remote_follow/2 without assigned user " do
422 test "follows", %{conn: conn} do
424 user2 = insert(:user)
428 |> post("/ostatus_subscribe", %{
429 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
433 assert response =~ "Account followed!"
434 assert user2.follower_address in refresh_record(user).following
437 test "returns error when followee not found", %{conn: conn} do
442 |> post("/ostatus_subscribe", %{
443 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => "jimm"}
447 assert response =~ "Error following account"
450 test "returns error when login invalid", %{conn: conn} do
455 |> post("/ostatus_subscribe", %{
456 "authorization" => %{"name" => "jimm", "password" => "test", "id" => user.id}
460 assert response =~ "Wrong username or password"
463 test "returns error when password invalid", %{conn: conn} do
465 user2 = insert(:user)
469 |> post("/ostatus_subscribe", %{
470 "authorization" => %{"name" => user.nickname, "password" => "42", "id" => user2.id}
474 assert response =~ "Wrong username or password"
477 test "returns error when user is blocked", %{conn: conn} do
478 Pleroma.Config.put([:user, :deny_follow_blocked], true)
480 user2 = insert(:user)
481 {:ok, _user} = Pleroma.User.block(user2, user)
485 |> post("/ostatus_subscribe", %{
486 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
490 assert response =~ "Error following account"
494 describe "GET /api/pleroma/healthcheck" do
495 clear_config([:instance, :healthcheck])
497 test "returns 503 when healthcheck disabled", %{conn: conn} do
498 Pleroma.Config.put([:instance, :healthcheck], false)
502 |> get("/api/pleroma/healthcheck")
503 |> json_response(503)
505 assert response == %{}
508 test "returns 200 when healthcheck enabled and all ok", %{conn: conn} do
509 Pleroma.Config.put([:instance, :healthcheck], true)
511 with_mock Pleroma.Healthcheck,
512 system_info: fn -> %Pleroma.Healthcheck{healthy: true} end do
515 |> get("/api/pleroma/healthcheck")
516 |> json_response(200)
528 test "returns 503 when healthcheck enabled and health is false", %{conn: conn} do
529 Pleroma.Config.put([:instance, :healthcheck], true)
531 with_mock Pleroma.Healthcheck,
532 system_info: fn -> %Pleroma.Healthcheck{healthy: false} end do
535 |> get("/api/pleroma/healthcheck")
536 |> json_response(503)
549 describe "POST /api/pleroma/disable_account" do
550 test "it returns HTTP 200", %{conn: conn} do
555 |> assign(:user, user)
556 |> post("/api/pleroma/disable_account", %{"password" => "test"})
557 |> json_response(:ok)
559 assert response == %{"status" => "success"}
561 user = User.get_cached_by_id(user.id)
563 assert user.info.deactivated == true
566 test "it returns returns when password invalid", %{conn: conn} do
571 |> assign(:user, user)
572 |> post("/api/pleroma/disable_account", %{"password" => "test1"})
573 |> json_response(:ok)
575 assert response == %{"error" => "Invalid password."}
576 user = User.get_cached_by_id(user.id)
578 refute user.info.deactivated
582 describe "GET /api/statusnet/version" do
583 test "it returns version in xml format", %{conn: conn} do
586 |> put_req_header("accept", "application/xml")
587 |> get("/api/statusnet/version")
590 assert response == "<version>#{Pleroma.Application.named_version()}</version>"
593 test "it returns version in json format", %{conn: conn} do
596 |> put_req_header("accept", "application/json")
597 |> get("/api/statusnet/version")
598 |> json_response(:ok)
600 assert response == "#{Pleroma.Application.named_version()}"
604 describe "POST /main/ostatus - remote_subscribe/2" do
605 test "renders subscribe form", %{conn: conn} do
610 |> post("/main/ostatus", %{"nickname" => user.nickname, "profile" => ""})
613 refute response =~ "Could not find user"
614 assert response =~ "Remotely follow #{user.nickname}"
617 test "renders subscribe form with error when user not found", %{conn: conn} do
620 |> post("/main/ostatus", %{"nickname" => "nickname", "profile" => ""})
623 assert response =~ "Could not find user"
624 refute response =~ "Remotely follow"
627 test "it redirect to webfinger url", %{conn: conn} do
629 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
633 |> post("/main/ostatus", %{
634 "user" => %{"nickname" => user.nickname, "profile" => user2.ap_id}
637 assert redirected_to(conn) ==
638 "https://social.heldscal.la/main/ostatussub?profile=#{user.ap_id}"
641 test "it renders form with error when use not found", %{conn: conn} do
642 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
646 |> post("/main/ostatus", %{"user" => %{"nickname" => "jimm", "profile" => user2.ap_id}})
649 assert response =~ "Something went wrong."
653 test "it returns new captcha", %{conn: conn} do
654 with_mock Pleroma.Captcha,
655 new: fn -> "test_captcha" end do
658 |> get("/api/pleroma/captcha")
661 assert resp == "\"test_captcha\""
662 assert called(Pleroma.Captcha.new())
666 defp with_credentials(conn, username, password) do
667 header_content = "Basic " <> Base.encode64("#{username}:#{password}")
668 put_req_header(conn, "authorization", header_content)
671 defp valid_user(_context) do
676 describe "POST /api/pleroma/change_email" do
679 test "without credentials", %{conn: conn} do
680 conn = post(conn, "/api/pleroma/change_email")
681 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
684 test "with credentials and invalid password", %{conn: conn, user: current_user} do
687 |> with_credentials(current_user.nickname, "test")
688 |> post("/api/pleroma/change_email", %{
690 "email" => "test@test.com"
693 assert json_response(conn, 200) == %{"error" => "Invalid password."}
696 test "with credentials, valid password and invalid email", %{
702 |> with_credentials(current_user.nickname, "test")
703 |> post("/api/pleroma/change_email", %{
704 "password" => "test",
708 assert json_response(conn, 200) == %{"error" => "Email has invalid format."}
711 test "with credentials, valid password and no email", %{
717 |> with_credentials(current_user.nickname, "test")
718 |> post("/api/pleroma/change_email", %{
722 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
725 test "with credentials, valid password and blank email", %{
731 |> with_credentials(current_user.nickname, "test")
732 |> post("/api/pleroma/change_email", %{
733 "password" => "test",
737 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
740 test "with credentials, valid password and non unique email", %{
748 |> with_credentials(current_user.nickname, "test")
749 |> post("/api/pleroma/change_email", %{
750 "password" => "test",
751 "email" => user.email
754 assert json_response(conn, 200) == %{"error" => "Email has already been taken."}
757 test "with credentials, valid password and valid email", %{
763 |> with_credentials(current_user.nickname, "test")
764 |> post("/api/pleroma/change_email", %{
765 "password" => "test",
766 "email" => "cofe@foobar.com"
769 assert json_response(conn, 200) == %{"status" => "success"}