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' or 'write:follows' permissions", %{conn: conn} do
82 token1 = insert(:oauth_token, scopes: ["read", "write"])
83 token2 = insert(:oauth_token, scopes: ["follow"])
84 token3 = insert(:oauth_token, scopes: ["something"])
85 another_user = insert(:user)
87 for token <- [token1, token2, token3] do
90 |> put_req_header("authorization", "Bearer #{token.token}")
91 |> post("/api/pleroma/follow_import", %{"list" => "#{another_user.ap_id}"})
94 assert %{"error" => "Insufficient permissions: follow | write:follows."} ==
95 json_response(conn, 403)
97 assert json_response(conn, 200)
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)
110 |> assign(:user, user1)
111 |> post("/api/pleroma/blocks_import", %{"list" => "#{user2.ap_id}"})
112 |> json_response(:ok)
114 assert response == "job started"
117 test "it imports blocks users from file", %{conn: conn} do
118 user1 = insert(:user)
119 user2 = insert(:user)
120 user3 = insert(:user)
123 {File, [], read!: fn "blocks_list.txt" -> "#{user2.ap_id} #{user3.ap_id}" end},
124 {PleromaJobQueue, [:passthrough], []}
128 |> assign(:user, user1)
129 |> post("/api/pleroma/blocks_import", %{"list" => %Plug.Upload{path: "blocks_list.txt"}})
130 |> json_response(:ok)
133 PleromaJobQueue.enqueue(
136 [:blocks_import, user1, [user2.ap_id, user3.ap_id]]
140 assert response == "job started"
145 describe "PUT /api/pleroma/notification_settings" do
146 test "it updates notification settings", %{conn: conn} do
150 |> assign(:user, user)
151 |> put("/api/pleroma/notification_settings", %{
152 "followers" => false,
155 |> json_response(:ok)
157 user = Repo.get(User, user.id)
160 "followers" => false,
162 "non_follows" => true,
163 "non_followers" => true
164 } == user.info.notification_settings
168 describe "GET /api/statusnet/config" do
169 test "it returns config in xml format", %{conn: conn} do
170 instance = Pleroma.Config.get(:instance)
174 |> put_req_header("accept", "application/xml")
175 |> get("/api/statusnet/config")
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"
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"})
196 |> put_req_header("accept", "application/json")
197 |> get("/api/statusnet/config")
198 |> json_response(:ok)
202 "accountActivationRequired" => "0",
204 "description" => Keyword.get(instance, :description),
205 "invitesEnabled" => "1",
206 "name" => Keyword.get(instance, :name),
207 "pleromafe" => %{"theme" => "asuka-hospital"},
209 "safeDMMentionsEnabled" => "0",
210 "server" => Pleroma.Web.base_url(),
211 "textlimit" => to_string(Keyword.get(instance, :limit)),
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))
218 "vapidPublicKey" => Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
222 assert response == expected_data
225 test "returns the state of safe_dm_mentions flag", %{conn: conn} do
226 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
230 |> get("/api/statusnet/config.json")
231 |> json_response(:ok)
233 assert response["site"]["safeDMMentionsEnabled"] == "1"
235 Pleroma.Config.put([:instance, :safe_dm_mentions], false)
239 |> get("/api/statusnet/config.json")
240 |> json_response(:ok)
242 assert response["site"]["safeDMMentionsEnabled"] == "0"
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"})
251 |> get("/api/statusnet/config.json")
252 |> json_response(:ok)
254 refute response["site"]["pleromafe"]
256 Pleroma.Config.put([:instance, :managed_config], true)
260 |> get("/api/statusnet/config.json")
261 |> json_response(:ok)
263 assert response["site"]["pleromafe"] == %{"theme" => "asuka-hospital"}
267 describe "GET /api/pleroma/frontend_configurations" do
268 test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
279 Pleroma.Config.put(:frontend_configurations, config)
283 |> get("/api/pleroma/frontend_configurations")
284 |> json_response(:ok)
286 assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
290 describe "/api/pleroma/emoji" do
291 test "returns json with custom emoji with tags", %{conn: conn} do
294 |> get("/api/pleroma/emoji")
295 |> json_response(200)
297 assert Enum.all?(emoji, fn
303 is_binary(url) and is_list(tags)
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
313 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie/statuses/101849165031453009"
316 assert redirected_to(conn) =~ "/notice/"
319 test "show follow account page if the `acct` is a account link", %{conn: conn} do
323 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie"
326 assert html_response(response, 200) =~ "Log in to follow"
329 test "show follow page if the `acct` is a account link", %{conn: conn} do
334 |> assign(:user, user)
335 |> get("/ostatus_subscribe?acct=https://mastodon.social/users/emelie")
337 assert html_response(response, 200) =~ "Remote follow"
340 test "show follow page with error when user cannot fecth by `acct` link", %{conn: conn} do
345 |> assign(:user, user)
346 |> get("/ostatus_subscribe?acct=https://mastodon.social/users/not_found")
348 assert html_response(response, 200) =~ "Error fetching user"
352 describe "POST /ostatus_subscribe - do_remote_follow/2 with assigned user " do
353 test "follows user", %{conn: conn} do
355 user2 = insert(:user)
359 |> assign(:user, user)
360 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
363 assert response =~ "Account followed!"
364 assert user2.follower_address in refresh_record(user).following
367 test "returns error when user is deactivated", %{conn: conn} do
368 user = insert(:user, info: %{deactivated: true})
369 user2 = insert(:user)
373 |> assign(:user, user)
374 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
377 assert response =~ "Error following account"
380 test "returns error when user is blocked", %{conn: conn} do
381 Pleroma.Config.put([:user, :deny_follow_blocked], true)
383 user2 = insert(:user)
385 {:ok, _user} = Pleroma.User.block(user2, user)
389 |> assign(:user, user)
390 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
393 assert response =~ "Error following account"
396 test "returns error when followee not found", %{conn: conn} do
401 |> assign(:user, user)
402 |> post("/ostatus_subscribe", %{"user" => %{"id" => "jimm"}})
405 assert response =~ "Error following account"
408 test "returns success result when user already in followers", %{conn: conn} do
410 user2 = insert(:user)
411 {:ok, _, _, _} = CommonAPI.follow(user, user2)
415 |> assign(:user, refresh_record(user))
416 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
419 assert response =~ "Account followed!"
423 describe "POST /ostatus_subscribe - do_remote_follow/2 without assigned user " do
424 test "follows", %{conn: conn} do
426 user2 = insert(:user)
430 |> post("/ostatus_subscribe", %{
431 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
435 assert response =~ "Account followed!"
436 assert user2.follower_address in refresh_record(user).following
439 test "returns error when followee not found", %{conn: conn} do
444 |> post("/ostatus_subscribe", %{
445 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => "jimm"}
449 assert response =~ "Error following account"
452 test "returns error when login invalid", %{conn: conn} do
457 |> post("/ostatus_subscribe", %{
458 "authorization" => %{"name" => "jimm", "password" => "test", "id" => user.id}
462 assert response =~ "Wrong username or password"
465 test "returns error when password invalid", %{conn: conn} do
467 user2 = insert(:user)
471 |> post("/ostatus_subscribe", %{
472 "authorization" => %{"name" => user.nickname, "password" => "42", "id" => user2.id}
476 assert response =~ "Wrong username or password"
479 test "returns error when user is blocked", %{conn: conn} do
480 Pleroma.Config.put([:user, :deny_follow_blocked], true)
482 user2 = insert(:user)
483 {:ok, _user} = Pleroma.User.block(user2, user)
487 |> post("/ostatus_subscribe", %{
488 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
492 assert response =~ "Error following account"
496 describe "GET /api/pleroma/healthcheck" do
497 clear_config([:instance, :healthcheck])
499 test "returns 503 when healthcheck disabled", %{conn: conn} do
500 Pleroma.Config.put([:instance, :healthcheck], false)
504 |> get("/api/pleroma/healthcheck")
505 |> json_response(503)
507 assert response == %{}
510 test "returns 200 when healthcheck enabled and all ok", %{conn: conn} do
511 Pleroma.Config.put([:instance, :healthcheck], true)
513 with_mock Pleroma.Healthcheck,
514 system_info: fn -> %Pleroma.Healthcheck{healthy: true} end do
517 |> get("/api/pleroma/healthcheck")
518 |> json_response(200)
530 test "returns 503 when healthcheck enabled and health is false", %{conn: conn} do
531 Pleroma.Config.put([:instance, :healthcheck], true)
533 with_mock Pleroma.Healthcheck,
534 system_info: fn -> %Pleroma.Healthcheck{healthy: false} end do
537 |> get("/api/pleroma/healthcheck")
538 |> json_response(503)
551 describe "POST /api/pleroma/disable_account" do
552 test "it returns HTTP 200", %{conn: conn} do
557 |> assign(:user, user)
558 |> post("/api/pleroma/disable_account", %{"password" => "test"})
559 |> json_response(:ok)
561 assert response == %{"status" => "success"}
563 user = User.get_cached_by_id(user.id)
565 assert user.info.deactivated == true
568 test "it returns returns when password invalid", %{conn: conn} do
573 |> assign(:user, user)
574 |> post("/api/pleroma/disable_account", %{"password" => "test1"})
575 |> json_response(:ok)
577 assert response == %{"error" => "Invalid password."}
578 user = User.get_cached_by_id(user.id)
580 refute user.info.deactivated
584 describe "GET /api/statusnet/version" do
585 test "it returns version in xml format", %{conn: conn} do
588 |> put_req_header("accept", "application/xml")
589 |> get("/api/statusnet/version")
592 assert response == "<version>#{Pleroma.Application.named_version()}</version>"
595 test "it returns version in json format", %{conn: conn} do
598 |> put_req_header("accept", "application/json")
599 |> get("/api/statusnet/version")
600 |> json_response(:ok)
602 assert response == "#{Pleroma.Application.named_version()}"
606 describe "POST /main/ostatus - remote_subscribe/2" do
607 test "renders subscribe form", %{conn: conn} do
612 |> post("/main/ostatus", %{"nickname" => user.nickname, "profile" => ""})
615 refute response =~ "Could not find user"
616 assert response =~ "Remotely follow #{user.nickname}"
619 test "renders subscribe form with error when user not found", %{conn: conn} do
622 |> post("/main/ostatus", %{"nickname" => "nickname", "profile" => ""})
625 assert response =~ "Could not find user"
626 refute response =~ "Remotely follow"
629 test "it redirect to webfinger url", %{conn: conn} do
631 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
635 |> post("/main/ostatus", %{
636 "user" => %{"nickname" => user.nickname, "profile" => user2.ap_id}
639 assert redirected_to(conn) ==
640 "https://social.heldscal.la/main/ostatussub?profile=#{user.ap_id}"
643 test "it renders form with error when use not found", %{conn: conn} do
644 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
648 |> post("/main/ostatus", %{"user" => %{"nickname" => "jimm", "profile" => user2.ap_id}})
651 assert response =~ "Something went wrong."
655 test "it returns new captcha", %{conn: conn} do
656 with_mock Pleroma.Captcha,
657 new: fn -> "test_captcha" end do
660 |> get("/api/pleroma/captcha")
663 assert resp == "\"test_captcha\""
664 assert called(Pleroma.Captcha.new())