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
10 alias Pleroma.Tests.ObanHelpers
12 alias Pleroma.Web.CommonAPI
13 import Pleroma.Factory
17 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
21 clear_config([:instance])
22 clear_config([:frontend_configurations, :pleroma_fe])
23 clear_config([:user, :deny_follow_blocked])
25 describe "POST /api/pleroma/follow_import" do
26 test "it returns HTTP 200", %{conn: conn} do
32 |> assign(:user, user1)
33 |> post("/api/pleroma/follow_import", %{"list" => "#{user2.ap_id}"})
36 assert response == "job started"
39 test "it imports follow lists from file", %{conn: conn} do
45 read!: fn "follow_list.txt" ->
46 "Account address,Show boosts\n#{user2.ap_id},true"
51 |> assign(:user, user1)
52 |> post("/api/pleroma/follow_import", %{"list" => %Plug.Upload{path: "follow_list.txt"}})
55 assert response == "job started"
57 assert ObanHelpers.member?(
59 "op" => "follow_import",
60 "follower_id" => user1.id,
61 "followed_identifiers" => [user2.ap_id]
63 all_enqueued(worker: Pleroma.Workers.BackgroundWorker)
68 test "it imports new-style mastodon follow lists", %{conn: conn} do
74 |> assign(:user, user1)
75 |> post("/api/pleroma/follow_import", %{
76 "list" => "Account address,Show boosts\n#{user2.ap_id},true"
80 assert response == "job started"
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)
88 for token <- [token1, token2] do
91 |> put_req_header("authorization", "Bearer #{token.token}")
92 |> post("/api/pleroma/follow_import", %{"list" => "#{another_user.ap_id}"})
95 assert %{"error" => "Insufficient permissions: follow."} == 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}
127 |> assign(:user, user1)
128 |> post("/api/pleroma/blocks_import", %{"list" => %Plug.Upload{path: "blocks_list.txt"}})
129 |> json_response(:ok)
131 assert response == "job started"
133 assert ObanHelpers.member?(
135 "op" => "blocks_import",
136 "blocker_id" => user1.id,
137 "blocked_identifiers" => [user2.ap_id, user3.ap_id]
139 all_enqueued(worker: Pleroma.Workers.BackgroundWorker)
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"}
562 ObanHelpers.perform_all()
564 user = User.get_cached_by_id(user.id)
566 assert user.info.deactivated == true
569 test "it returns returns when password invalid", %{conn: conn} do
574 |> assign(:user, user)
575 |> post("/api/pleroma/disable_account", %{"password" => "test1"})
576 |> json_response(:ok)
578 assert response == %{"error" => "Invalid password."}
579 user = User.get_cached_by_id(user.id)
581 refute user.info.deactivated
585 describe "GET /api/statusnet/version" do
586 test "it returns version in xml format", %{conn: conn} do
589 |> put_req_header("accept", "application/xml")
590 |> get("/api/statusnet/version")
593 assert response == "<version>#{Pleroma.Application.named_version()}</version>"
596 test "it returns version in json format", %{conn: conn} do
599 |> put_req_header("accept", "application/json")
600 |> get("/api/statusnet/version")
601 |> json_response(:ok)
603 assert response == "#{Pleroma.Application.named_version()}"
607 describe "POST /main/ostatus - remote_subscribe/2" do
608 test "renders subscribe form", %{conn: conn} do
613 |> post("/main/ostatus", %{"nickname" => user.nickname, "profile" => ""})
616 refute response =~ "Could not find user"
617 assert response =~ "Remotely follow #{user.nickname}"
620 test "renders subscribe form with error when user not found", %{conn: conn} do
623 |> post("/main/ostatus", %{"nickname" => "nickname", "profile" => ""})
626 assert response =~ "Could not find user"
627 refute response =~ "Remotely follow"
630 test "it redirect to webfinger url", %{conn: conn} do
632 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
636 |> post("/main/ostatus", %{
637 "user" => %{"nickname" => user.nickname, "profile" => user2.ap_id}
640 assert redirected_to(conn) ==
641 "https://social.heldscal.la/main/ostatussub?profile=#{user.ap_id}"
644 test "it renders form with error when use not found", %{conn: conn} do
645 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
649 |> post("/main/ostatus", %{"user" => %{"nickname" => "jimm", "profile" => user2.ap_id}})
652 assert response =~ "Something went wrong."
656 test "it returns new captcha", %{conn: conn} do
657 with_mock Pleroma.Captcha,
658 new: fn -> "test_captcha" end do
661 |> get("/api/pleroma/captcha")
664 assert resp == "\"test_captcha\""
665 assert called(Pleroma.Captcha.new())