Merge branch 'develop' into feature/admin-api-user-statuses
[akkoma] / test / web / twitter_api / util_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
6 use Pleroma.Web.ConnCase
7
8 alias Pleroma.Notification
9 alias Pleroma.Repo
10 alias Pleroma.User
11 alias Pleroma.Web.CommonAPI
12 import Pleroma.Factory
13 import Mock
14
15 setup do
16 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
17 :ok
18 end
19
20 describe "POST /api/pleroma/follow_import" do
21 test "it returns HTTP 200", %{conn: conn} do
22 user1 = insert(:user)
23 user2 = insert(:user)
24
25 response =
26 conn
27 |> assign(:user, user1)
28 |> post("/api/pleroma/follow_import", %{"list" => "#{user2.ap_id}"})
29 |> json_response(:ok)
30
31 assert response == "job started"
32 end
33
34 test "it imports new-style mastodon follow lists", %{conn: conn} do
35 user1 = insert(:user)
36 user2 = insert(:user)
37
38 response =
39 conn
40 |> assign(:user, user1)
41 |> post("/api/pleroma/follow_import", %{
42 "list" => "Account address,Show boosts\n#{user2.ap_id},true"
43 })
44 |> json_response(:ok)
45
46 assert response == "job started"
47 end
48
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)
53
54 for token <- [token1, token2] do
55 conn =
56 conn
57 |> put_req_header("authorization", "Bearer #{token.token}")
58 |> post("/api/pleroma/follow_import", %{"list" => "#{another_user.ap_id}"})
59
60 if token == token1 do
61 assert %{"error" => "Insufficient permissions: follow."} == json_response(conn, 403)
62 else
63 assert json_response(conn, 200)
64 end
65 end
66 end
67 end
68
69 describe "POST /api/pleroma/blocks_import" do
70 test "it returns HTTP 200", %{conn: conn} do
71 user1 = insert(:user)
72 user2 = insert(:user)
73
74 response =
75 conn
76 |> assign(:user, user1)
77 |> post("/api/pleroma/blocks_import", %{"list" => "#{user2.ap_id}"})
78 |> json_response(:ok)
79
80 assert response == "job started"
81 end
82 end
83
84 describe "POST /api/pleroma/notifications/read" do
85 test "it marks a single notification as read", %{conn: conn} do
86 user1 = insert(:user)
87 user2 = insert(:user)
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)
92
93 conn
94 |> assign(:user, user1)
95 |> post("/api/pleroma/notifications/read", %{"id" => "#{notification1.id}"})
96 |> json_response(:ok)
97
98 assert Repo.get(Notification, notification1.id).seen
99 refute Repo.get(Notification, notification2.id).seen
100 end
101 end
102
103 describe "PUT /api/pleroma/notification_settings" do
104 test "it updates notification settings", %{conn: conn} do
105 user = insert(:user)
106
107 conn
108 |> assign(:user, user)
109 |> put("/api/pleroma/notification_settings", %{
110 "followers" => false,
111 "bar" => 1
112 })
113 |> json_response(:ok)
114
115 user = Repo.get(User, user.id)
116
117 assert %{
118 "followers" => false,
119 "follows" => true,
120 "non_follows" => true,
121 "non_followers" => true
122 } == user.info.notification_settings
123 end
124 end
125
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)
130
131 response =
132 conn
133 |> get("/api/statusnet/config.json")
134 |> json_response(:ok)
135
136 assert response["site"]["safeDMMentionsEnabled"] == "1"
137
138 Pleroma.Config.put([:instance, :safe_dm_mentions], false)
139
140 response =
141 conn
142 |> get("/api/statusnet/config.json")
143 |> json_response(:ok)
144
145 assert response["site"]["safeDMMentionsEnabled"] == "0"
146
147 Pleroma.Config.put([:instance, :safe_dm_mentions], option)
148 end
149
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"})
153
154 response =
155 conn
156 |> get("/api/statusnet/config.json")
157 |> json_response(:ok)
158
159 refute response["site"]["pleromafe"]
160
161 Pleroma.Config.put([:instance, :managed_config], true)
162
163 response =
164 conn
165 |> get("/api/statusnet/config.json")
166 |> json_response(:ok)
167
168 assert response["site"]["pleromafe"] == %{"theme" => "asuka-hospital"}
169 end
170 end
171
172 describe "GET /api/pleroma/frontend_configurations" do
173 test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
174 config = [
175 frontend_a: %{
176 x: 1,
177 y: 2
178 },
179 frontend_b: %{
180 z: 3
181 }
182 ]
183
184 Pleroma.Config.put(:frontend_configurations, config)
185
186 response =
187 conn
188 |> get("/api/pleroma/frontend_configurations")
189 |> json_response(:ok)
190
191 assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
192 end
193 end
194
195 describe "/api/pleroma/emoji" do
196 test "returns json with custom emoji with tags", %{conn: conn} do
197 emoji =
198 conn
199 |> get("/api/pleroma/emoji")
200 |> json_response(200)
201
202 assert Enum.all?(emoji, fn
203 {_key,
204 %{
205 "image_url" => url,
206 "tags" => tags
207 }} ->
208 is_binary(url) and is_list(tags)
209 end)
210 end
211 end
212
213 describe "GET /ostatus_subscribe?acct=...." do
214 test "adds status to pleroma instance if the `acct` is a status", %{conn: conn} do
215 conn =
216 get(
217 conn,
218 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie/statuses/101849165031453009"
219 )
220
221 assert redirected_to(conn) =~ "/notice/"
222 end
223
224 test "show follow account page if the `acct` is a account link", %{conn: conn} do
225 response =
226 get(
227 conn,
228 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie"
229 )
230
231 assert html_response(response, 200) =~ "Log in to follow"
232 end
233 end
234
235 describe "GET /api/pleroma/healthcheck" do
236 setup do
237 config_healthcheck = Pleroma.Config.get([:instance, :healthcheck])
238
239 on_exit(fn ->
240 Pleroma.Config.put([:instance, :healthcheck], config_healthcheck)
241 end)
242
243 :ok
244 end
245
246 test "returns 503 when healthcheck disabled", %{conn: conn} do
247 Pleroma.Config.put([:instance, :healthcheck], false)
248
249 response =
250 conn
251 |> get("/api/pleroma/healthcheck")
252 |> json_response(503)
253
254 assert response == %{}
255 end
256
257 test "returns 200 when healthcheck enabled and all ok", %{conn: conn} do
258 Pleroma.Config.put([:instance, :healthcheck], true)
259
260 with_mock Pleroma.Healthcheck,
261 system_info: fn -> %Pleroma.Healthcheck{healthy: true} end do
262 response =
263 conn
264 |> get("/api/pleroma/healthcheck")
265 |> json_response(200)
266
267 assert %{
268 "active" => _,
269 "healthy" => true,
270 "idle" => _,
271 "memory_used" => _,
272 "pool_size" => _
273 } = response
274 end
275 end
276
277 test "returns 503 when healthcheck enabled and health is false", %{conn: conn} do
278 Pleroma.Config.put([:instance, :healthcheck], true)
279
280 with_mock Pleroma.Healthcheck,
281 system_info: fn -> %Pleroma.Healthcheck{healthy: false} end do
282 response =
283 conn
284 |> get("/api/pleroma/healthcheck")
285 |> json_response(503)
286
287 assert %{
288 "active" => _,
289 "healthy" => false,
290 "idle" => _,
291 "memory_used" => _,
292 "pool_size" => _
293 } = response
294 end
295 end
296 end
297
298 describe "POST /api/pleroma/disable_account" do
299 test "it returns HTTP 200", %{conn: conn} do
300 user = insert(:user)
301
302 response =
303 conn
304 |> assign(:user, user)
305 |> post("/api/pleroma/disable_account", %{"password" => "test"})
306 |> json_response(:ok)
307
308 assert response == %{"status" => "success"}
309
310 user = User.get_cached_by_id(user.id)
311
312 assert user.info.deactivated == true
313 end
314 end
315 end