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
8 alias Pleroma.Notification
11 alias Pleroma.Web.CommonAPI
12 import Pleroma.Factory
16 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
20 describe "POST /api/pleroma/follow_import" do
21 test "it returns HTTP 200", %{conn: conn} do
27 |> assign(:user, user1)
28 |> post("/api/pleroma/follow_import", %{"list" => "#{user2.ap_id}"})
31 assert response == "job started"
34 test "it imports new-style mastodon follow lists", %{conn: conn} do
40 |> assign(:user, user1)
41 |> post("/api/pleroma/follow_import", %{
42 "list" => "Account address,Show boosts\n#{user2.ap_id},true"
46 assert response == "job started"
49 test "requires 'follow' permission", %{conn: conn} do
50 token1 = insert(:oauth_token, scopes: ["read", "write"])
51 token2 = insert(:oauth_token, scopes: ["follow"])
52 another_user = insert(:user)
54 for token <- [token1, token2] do
57 |> put_req_header("authorization", "Bearer #{token.token}")
58 |> post("/api/pleroma/follow_import", %{"list" => "#{another_user.ap_id}"})
61 assert %{"error" => "Insufficient permissions: follow."} == json_response(conn, 403)
63 assert json_response(conn, 200)
69 describe "POST /api/pleroma/blocks_import" do
70 test "it returns HTTP 200", %{conn: conn} do
76 |> assign(:user, user1)
77 |> post("/api/pleroma/blocks_import", %{"list" => "#{user2.ap_id}"})
80 assert response == "job started"
84 describe "POST /api/pleroma/notifications/read" do
85 test "it marks a single notification as read", %{conn: conn} do
88 {:ok, activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
89 {:ok, activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
90 {:ok, [notification1]} = Notification.create_notifications(activity1)
91 {:ok, [notification2]} = Notification.create_notifications(activity2)
94 |> assign(:user, user1)
95 |> post("/api/pleroma/notifications/read", %{"id" => "#{notification1.id}"})
98 assert Repo.get(Notification, notification1.id).seen
99 refute Repo.get(Notification, notification2.id).seen
103 describe "PUT /api/pleroma/notification_settings" do
104 test "it updates notification settings", %{conn: conn} do
108 |> assign(:user, user)
109 |> put("/api/pleroma/notification_settings", %{
110 "followers" => false,
113 |> json_response(:ok)
115 user = Repo.get(User, user.id)
118 "followers" => false,
120 "non_follows" => true,
121 "non_followers" => true
122 } == user.info.notification_settings
126 describe "GET /api/statusnet/config.json" do
127 test "returns the state of safe_dm_mentions flag", %{conn: conn} do
128 option = Pleroma.Config.get([:instance, :safe_dm_mentions])
129 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
133 |> get("/api/statusnet/config.json")
134 |> json_response(:ok)
136 assert response["site"]["safeDMMentionsEnabled"] == "1"
138 Pleroma.Config.put([:instance, :safe_dm_mentions], false)
142 |> get("/api/statusnet/config.json")
143 |> json_response(:ok)
145 assert response["site"]["safeDMMentionsEnabled"] == "0"
147 Pleroma.Config.put([:instance, :safe_dm_mentions], option)
150 test "it returns the managed config", %{conn: conn} do
151 Pleroma.Config.put([:instance, :managed_config], false)
152 Pleroma.Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
156 |> get("/api/statusnet/config.json")
157 |> json_response(:ok)
159 refute response["site"]["pleromafe"]
161 Pleroma.Config.put([:instance, :managed_config], true)
165 |> get("/api/statusnet/config.json")
166 |> json_response(:ok)
168 assert response["site"]["pleromafe"] == %{"theme" => "asuka-hospital"}
172 describe "GET /api/pleroma/frontend_configurations" do
173 test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
184 Pleroma.Config.put(:frontend_configurations, config)
188 |> get("/api/pleroma/frontend_configurations")
189 |> json_response(:ok)
191 assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
195 describe "/api/pleroma/emoji" do
196 test "returns json with custom emoji with tags", %{conn: conn} do
199 |> get("/api/pleroma/emoji")
200 |> json_response(200)
202 assert Enum.all?(emoji, fn
208 is_binary(url) and is_list(tags)
213 describe "GET /ostatus_subscribe?acct=...." do
214 test "adds status to pleroma instance if the `acct` is a status", %{conn: conn} do
218 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie/statuses/101849165031453009"
221 assert redirected_to(conn) =~ "/notice/"
224 test "show follow account page if the `acct` is a account link", %{conn: conn} do
228 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie"
231 assert html_response(response, 200) =~ "Log in to follow"
235 describe "GET /api/pleroma/healthcheck" do
237 config_healthcheck = Pleroma.Config.get([:instance, :healthcheck])
240 Pleroma.Config.put([:instance, :healthcheck], config_healthcheck)
246 test "returns 503 when healthcheck disabled", %{conn: conn} do
247 Pleroma.Config.put([:instance, :healthcheck], false)
251 |> get("/api/pleroma/healthcheck")
252 |> json_response(503)
254 assert response == %{}
257 test "returns 200 when healthcheck enabled and all ok", %{conn: conn} do
258 Pleroma.Config.put([:instance, :healthcheck], true)
260 with_mock Pleroma.Healthcheck,
261 system_info: fn -> %Pleroma.Healthcheck{healthy: true} end do
264 |> get("/api/pleroma/healthcheck")
265 |> json_response(200)
277 test "returns 503 when healthcheck enabled and health is false", %{conn: conn} do
278 Pleroma.Config.put([:instance, :healthcheck], true)
280 with_mock Pleroma.Healthcheck,
281 system_info: fn -> %Pleroma.Healthcheck{healthy: false} end do
284 |> get("/api/pleroma/healthcheck")
285 |> json_response(503)
298 describe "POST /api/pleroma/disable_account" do
299 test "it returns HTTP 200", %{conn: conn} do
304 |> assign(:user, user)
305 |> post("/api/pleroma/disable_account", %{"password" => "test"})
306 |> json_response(:ok)
308 assert response == %{"status" => "success"}
310 user = User.get_cached_by_id(user.id)
312 assert user.info.deactivated == true