docs: Remove quarantine section
[akkoma] / test / pleroma / web / mastodon_api / update_credentials_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.UpdateCredentialsTest do
6 alias Pleroma.Repo
7 alias Pleroma.User
8
9 use Pleroma.Web.ConnCase
10
11 import Mock
12 import Pleroma.Factory
13
14 describe "updating credentials" do
15 setup do: oauth_access(["write:accounts"])
16 setup :request_content_type
17
18 test "sets user settings in a generic way", %{conn: conn} do
19 res_conn =
20 patch(conn, "/api/v1/accounts/update_credentials", %{
21 "pleroma_settings_store" => %{
22 pleroma_fe: %{
23 theme: "bla"
24 }
25 }
26 })
27
28 assert user_data = json_response_and_validate_schema(res_conn, 200)
29 assert user_data["pleroma"]["settings_store"] == %{"pleroma_fe" => %{"theme" => "bla"}}
30
31 user = Repo.get(User, user_data["id"])
32
33 res_conn =
34 conn
35 |> assign(:user, user)
36 |> patch("/api/v1/accounts/update_credentials", %{
37 "pleroma_settings_store" => %{
38 masto_fe: %{
39 theme: "bla"
40 }
41 }
42 })
43
44 assert user_data = json_response_and_validate_schema(res_conn, 200)
45
46 assert user_data["pleroma"]["settings_store"] ==
47 %{
48 "pleroma_fe" => %{"theme" => "bla"},
49 "masto_fe" => %{"theme" => "bla"}
50 }
51
52 user = Repo.get(User, user_data["id"])
53
54 clear_config([:instance, :federating], true)
55
56 with_mock Pleroma.Web.Federator,
57 publish: fn _activity -> :ok end do
58 res_conn =
59 conn
60 |> assign(:user, user)
61 |> patch("/api/v1/accounts/update_credentials", %{
62 "pleroma_settings_store" => %{
63 masto_fe: %{
64 theme: "blub"
65 }
66 }
67 })
68
69 assert user_data = json_response_and_validate_schema(res_conn, 200)
70
71 assert user_data["pleroma"]["settings_store"] ==
72 %{
73 "pleroma_fe" => %{"theme" => "bla"},
74 "masto_fe" => %{"theme" => "blub"}
75 }
76
77 assert_called(Pleroma.Web.Federator.publish(:_))
78 end
79 end
80
81 test "updates the user's bio", %{conn: conn} do
82 user2 = insert(:user)
83
84 raw_bio = "I drink #cofe with @#{user2.nickname}\n\nsuya.."
85
86 conn = patch(conn, "/api/v1/accounts/update_credentials", %{"note" => raw_bio})
87
88 assert user_data = json_response_and_validate_schema(conn, 200)
89
90 assert user_data["note"] ==
91 ~s(I drink <a class="hashtag" data-tag="cofe" href="http://localhost:4001/tag/cofe">#cofe</a> with <span class="h-card"><a class="u-url mention" data-user="#{user2.id}" href="#{user2.ap_id}" rel="ugc">@<span>#{user2.nickname}</span></a></span><br/><br/>suya..)
92
93 assert user_data["source"]["note"] == raw_bio
94
95 user = Repo.get(User, user_data["id"])
96
97 assert user.raw_bio == raw_bio
98 end
99
100 test "updates the user's locking status", %{conn: conn} do
101 conn = patch(conn, "/api/v1/accounts/update_credentials", %{locked: "true"})
102
103 assert user_data = json_response_and_validate_schema(conn, 200)
104 assert user_data["locked"] == true
105 end
106
107 test "updates the user's allow_following_move", %{user: user, conn: conn} do
108 assert user.allow_following_move == true
109
110 conn = patch(conn, "/api/v1/accounts/update_credentials", %{allow_following_move: "false"})
111
112 assert refresh_record(user).allow_following_move == false
113 assert user_data = json_response_and_validate_schema(conn, 200)
114 assert user_data["pleroma"]["allow_following_move"] == false
115 end
116
117 test "updates the user's default scope", %{conn: conn} do
118 conn = patch(conn, "/api/v1/accounts/update_credentials", %{default_scope: "unlisted"})
119
120 assert user_data = json_response_and_validate_schema(conn, 200)
121 assert user_data["source"]["privacy"] == "unlisted"
122 end
123
124 test "updates the user's privacy", %{conn: conn} do
125 conn = patch(conn, "/api/v1/accounts/update_credentials", %{source: %{privacy: "unlisted"}})
126
127 assert user_data = json_response_and_validate_schema(conn, 200)
128 assert user_data["source"]["privacy"] == "unlisted"
129 end
130
131 test "updates the user's hide_followers status", %{conn: conn} do
132 conn = patch(conn, "/api/v1/accounts/update_credentials", %{hide_followers: "true"})
133
134 assert user_data = json_response_and_validate_schema(conn, 200)
135 assert user_data["pleroma"]["hide_followers"] == true
136 end
137
138 test "updates the user's discoverable status", %{conn: conn} do
139 assert %{"source" => %{"pleroma" => %{"discoverable" => true}}} =
140 conn
141 |> patch("/api/v1/accounts/update_credentials", %{discoverable: "true"})
142 |> json_response_and_validate_schema(:ok)
143
144 assert %{"source" => %{"pleroma" => %{"discoverable" => false}}} =
145 conn
146 |> patch("/api/v1/accounts/update_credentials", %{discoverable: "false"})
147 |> json_response_and_validate_schema(:ok)
148 end
149
150 test "updates the user's hide_followers_count and hide_follows_count", %{conn: conn} do
151 conn =
152 patch(conn, "/api/v1/accounts/update_credentials", %{
153 hide_followers_count: "true",
154 hide_follows_count: "true"
155 })
156
157 assert user_data = json_response_and_validate_schema(conn, 200)
158 assert user_data["pleroma"]["hide_followers_count"] == true
159 assert user_data["pleroma"]["hide_follows_count"] == true
160 end
161
162 test "updates the user's skip_thread_containment option", %{user: user, conn: conn} do
163 response =
164 conn
165 |> patch("/api/v1/accounts/update_credentials", %{skip_thread_containment: "true"})
166 |> json_response_and_validate_schema(200)
167
168 assert response["pleroma"]["skip_thread_containment"] == true
169 assert refresh_record(user).skip_thread_containment
170 end
171
172 test "updates the user's hide_follows status", %{conn: conn} do
173 conn = patch(conn, "/api/v1/accounts/update_credentials", %{hide_follows: "true"})
174
175 assert user_data = json_response_and_validate_schema(conn, 200)
176 assert user_data["pleroma"]["hide_follows"] == true
177 end
178
179 test "updates the user's hide_favorites status", %{conn: conn} do
180 conn = patch(conn, "/api/v1/accounts/update_credentials", %{hide_favorites: "true"})
181
182 assert user_data = json_response_and_validate_schema(conn, 200)
183 assert user_data["pleroma"]["hide_favorites"] == true
184 end
185
186 test "updates the user's show_role status", %{conn: conn} do
187 conn = patch(conn, "/api/v1/accounts/update_credentials", %{show_role: "false"})
188
189 assert user_data = json_response_and_validate_schema(conn, 200)
190 assert user_data["source"]["pleroma"]["show_role"] == false
191 end
192
193 test "updates the user's no_rich_text status", %{conn: conn} do
194 conn = patch(conn, "/api/v1/accounts/update_credentials", %{no_rich_text: "true"})
195
196 assert user_data = json_response_and_validate_schema(conn, 200)
197 assert user_data["source"]["pleroma"]["no_rich_text"] == true
198 end
199
200 test "updates the user's name", %{conn: conn} do
201 conn =
202 patch(conn, "/api/v1/accounts/update_credentials", %{"display_name" => "markorepairs"})
203
204 assert user_data = json_response_and_validate_schema(conn, 200)
205 assert user_data["display_name"] == "markorepairs"
206
207 update_activity = Repo.one(Pleroma.Activity)
208 assert update_activity.data["type"] == "Update"
209 assert update_activity.data["object"]["name"] == "markorepairs"
210 end
211
212 test "updates the user's AKAs", %{conn: conn} do
213 conn =
214 patch(conn, "/api/v1/accounts/update_credentials", %{
215 "also_known_as" => ["https://mushroom.kingdom/users/mario"]
216 })
217
218 assert user_data = json_response_and_validate_schema(conn, 200)
219 assert user_data["pleroma"]["also_known_as"] == ["https://mushroom.kingdom/users/mario"]
220 end
221
222 test "doesn't update non-url akas", %{conn: conn} do
223 conn =
224 patch(conn, "/api/v1/accounts/update_credentials", %{
225 "also_known_as" => ["aReallyCoolGuy"]
226 })
227
228 assert json_response_and_validate_schema(conn, 403)
229 end
230
231 test "updates the user's avatar", %{user: user, conn: conn} do
232 new_avatar = %Plug.Upload{
233 content_type: "image/jpeg",
234 path: Path.absname("test/fixtures/image.jpg"),
235 filename: "an_image.jpg"
236 }
237
238 assert user.avatar == %{}
239
240 res = patch(conn, "/api/v1/accounts/update_credentials", %{"avatar" => new_avatar})
241
242 assert user_response = json_response_and_validate_schema(res, 200)
243 assert user_response["avatar"] != User.avatar_url(user)
244
245 user = User.get_by_id(user.id)
246 refute user.avatar == %{}
247
248 # Also resets it
249 _res = patch(conn, "/api/v1/accounts/update_credentials", %{"avatar" => ""})
250
251 user = User.get_by_id(user.id)
252 assert user.avatar == nil
253 end
254
255 test "updates the user's banner", %{user: user, conn: conn} do
256 new_header = %Plug.Upload{
257 content_type: "image/jpeg",
258 path: Path.absname("test/fixtures/image.jpg"),
259 filename: "an_image.jpg"
260 }
261
262 res = patch(conn, "/api/v1/accounts/update_credentials", %{"header" => new_header})
263
264 assert user_response = json_response_and_validate_schema(res, 200)
265 assert user_response["header"] != User.banner_url(user)
266
267 # Also resets it
268 _res = patch(conn, "/api/v1/accounts/update_credentials", %{"header" => ""})
269
270 user = User.get_by_id(user.id)
271 assert user.banner == nil
272 end
273
274 test "updates the user's background", %{conn: conn, user: user} do
275 new_header = %Plug.Upload{
276 content_type: "image/jpeg",
277 path: Path.absname("test/fixtures/image.jpg"),
278 filename: "an_image.jpg"
279 }
280
281 res =
282 patch(conn, "/api/v1/accounts/update_credentials", %{
283 "pleroma_background_image" => new_header
284 })
285
286 assert user_response = json_response_and_validate_schema(res, 200)
287 assert user_response["pleroma"]["background_image"]
288 #
289 # Also resets it
290 _res =
291 patch(conn, "/api/v1/accounts/update_credentials", %{"pleroma_background_image" => ""})
292
293 user = User.get_by_id(user.id)
294 assert user.background == nil
295 end
296
297 test "requires 'write:accounts' permission" do
298 token1 = insert(:oauth_token, scopes: ["read"])
299 token2 = insert(:oauth_token, scopes: ["write", "follow"])
300
301 for token <- [token1, token2] do
302 conn =
303 build_conn()
304 |> put_req_header("content-type", "multipart/form-data")
305 |> put_req_header("authorization", "Bearer #{token.token}")
306 |> patch("/api/v1/accounts/update_credentials", %{})
307
308 if token == token1 do
309 assert %{"error" => "Insufficient permissions: write:accounts."} ==
310 json_response_and_validate_schema(conn, 403)
311 else
312 assert json_response_and_validate_schema(conn, 200)
313 end
314 end
315 end
316
317 test "updates profile emojos", %{user: user, conn: conn} do
318 note = "*sips :blank:*"
319 name = "I am :firefox:"
320
321 ret_conn =
322 patch(conn, "/api/v1/accounts/update_credentials", %{
323 "note" => note,
324 "display_name" => name
325 })
326
327 assert json_response_and_validate_schema(ret_conn, 200)
328
329 conn = get(conn, "/api/v1/accounts/#{user.id}")
330
331 assert user_data = json_response_and_validate_schema(conn, 200)
332
333 assert user_data["note"] == note
334 assert user_data["display_name"] == name
335 assert [%{"shortcode" => "blank"}, %{"shortcode" => "firefox"}] = user_data["emojis"]
336 end
337
338 test "update fields", %{conn: conn} do
339 fields = [
340 %{"name" => "<a href=\"http://google.com\">foo</a>", "value" => "<script>bar</script>"},
341 %{"name" => "link.io", "value" => "cofe.io"}
342 ]
343
344 account_data =
345 conn
346 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
347 |> json_response_and_validate_schema(200)
348
349 assert account_data["fields"] == [
350 %{"name" => "<a href=\"http://google.com\">foo</a>", "value" => "bar"},
351 %{
352 "name" => "link.io",
353 "value" => ~S(<a href="http://cofe.io" rel="ugc">cofe.io</a>)
354 }
355 ]
356
357 assert account_data["source"]["fields"] == [
358 %{
359 "name" => "<a href=\"http://google.com\">foo</a>",
360 "value" => "<script>bar</script>"
361 },
362 %{"name" => "link.io", "value" => "cofe.io"}
363 ]
364 end
365
366 test "emojis in fields labels", %{conn: conn} do
367 fields = [
368 %{"name" => ":firefox:", "value" => "is best 2hu"},
369 %{"name" => "they wins", "value" => ":blank:"}
370 ]
371
372 account_data =
373 conn
374 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
375 |> json_response_and_validate_schema(200)
376
377 assert account_data["fields"] == [
378 %{"name" => ":firefox:", "value" => "is best 2hu"},
379 %{"name" => "they wins", "value" => ":blank:"}
380 ]
381
382 assert account_data["source"]["fields"] == [
383 %{"name" => ":firefox:", "value" => "is best 2hu"},
384 %{"name" => "they wins", "value" => ":blank:"}
385 ]
386
387 assert [%{"shortcode" => "blank"}, %{"shortcode" => "firefox"}] = account_data["emojis"]
388 end
389
390 test "update fields via x-www-form-urlencoded", %{conn: conn} do
391 fields =
392 [
393 "fields_attributes[1][name]=link",
394 "fields_attributes[1][value]=http://cofe.io",
395 "fields_attributes[0][name]=foo",
396 "fields_attributes[0][value]=bar"
397 ]
398 |> Enum.join("&")
399
400 account =
401 conn
402 |> put_req_header("content-type", "application/x-www-form-urlencoded")
403 |> patch("/api/v1/accounts/update_credentials", fields)
404 |> json_response_and_validate_schema(200)
405
406 assert account["fields"] == [
407 %{"name" => "foo", "value" => "bar"},
408 %{
409 "name" => "link",
410 "value" => ~S(<a href="http://cofe.io" rel="ugc">http://cofe.io</a>)
411 }
412 ]
413
414 assert account["source"]["fields"] == [
415 %{"name" => "foo", "value" => "bar"},
416 %{"name" => "link", "value" => "http://cofe.io"}
417 ]
418 end
419
420 test "update fields with empty name", %{conn: conn} do
421 fields = [
422 %{"name" => "foo", "value" => ""},
423 %{"name" => "", "value" => "bar"}
424 ]
425
426 account =
427 conn
428 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
429 |> json_response_and_validate_schema(200)
430
431 assert account["fields"] == [
432 %{"name" => "foo", "value" => ""}
433 ]
434 end
435
436 test "update fields when invalid request", %{conn: conn} do
437 name_limit = Pleroma.Config.get([:instance, :account_field_name_length])
438 value_limit = Pleroma.Config.get([:instance, :account_field_value_length])
439
440 long_name = Enum.map(0..name_limit, fn _ -> "x" end) |> Enum.join()
441 long_value = Enum.map(0..value_limit, fn _ -> "x" end) |> Enum.join()
442
443 fields = [%{"name" => "foo", "value" => long_value}]
444
445 assert %{"error" => "Invalid request"} ==
446 conn
447 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
448 |> json_response_and_validate_schema(403)
449
450 fields = [%{"name" => long_name, "value" => "bar"}]
451
452 assert %{"error" => "Invalid request"} ==
453 conn
454 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
455 |> json_response_and_validate_schema(403)
456
457 clear_config([:instance, :max_account_fields], 1)
458
459 fields = [
460 %{"name" => "foo", "value" => "bar"},
461 %{"name" => "link", "value" => "cofe.io"}
462 ]
463
464 assert %{"error" => "Invalid request"} ==
465 conn
466 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
467 |> json_response_and_validate_schema(403)
468 end
469 end
470
471 describe "Mark account as bot" do
472 setup do: oauth_access(["write:accounts"])
473 setup :request_content_type
474
475 test "changing actor_type to Service makes account a bot", %{conn: conn} do
476 account =
477 conn
478 |> patch("/api/v1/accounts/update_credentials", %{actor_type: "Service"})
479 |> json_response_and_validate_schema(200)
480
481 assert account["bot"]
482 assert account["source"]["pleroma"]["actor_type"] == "Service"
483 end
484
485 test "changing actor_type to Person makes account a human", %{conn: conn} do
486 account =
487 conn
488 |> patch("/api/v1/accounts/update_credentials", %{actor_type: "Person"})
489 |> json_response_and_validate_schema(200)
490
491 refute account["bot"]
492 assert account["source"]["pleroma"]["actor_type"] == "Person"
493 end
494
495 test "changing actor_type to Application causes error", %{conn: conn} do
496 response =
497 conn
498 |> patch("/api/v1/accounts/update_credentials", %{actor_type: "Application"})
499 |> json_response_and_validate_schema(403)
500
501 assert %{"error" => "Invalid request"} == response
502 end
503
504 test "changing bot field to true changes actor_type to Service", %{conn: conn} do
505 account =
506 conn
507 |> patch("/api/v1/accounts/update_credentials", %{bot: "true"})
508 |> json_response_and_validate_schema(200)
509
510 assert account["bot"]
511 assert account["source"]["pleroma"]["actor_type"] == "Service"
512 end
513
514 test "changing bot field to false changes actor_type to Person", %{conn: conn} do
515 account =
516 conn
517 |> patch("/api/v1/accounts/update_credentials", %{bot: "false"})
518 |> json_response_and_validate_schema(200)
519
520 refute account["bot"]
521 assert account["source"]["pleroma"]["actor_type"] == "Person"
522 end
523
524 test "actor_type field has a higher priority than bot", %{conn: conn} do
525 account =
526 conn
527 |> patch("/api/v1/accounts/update_credentials", %{
528 actor_type: "Person",
529 bot: "true"
530 })
531 |> json_response_and_validate_schema(200)
532
533 refute account["bot"]
534 assert account["source"]["pleroma"]["actor_type"] == "Person"
535 end
536 end
537 end