130cbe8d171b614435ebb5a28a1ed98404d2c86c
[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 default post expiry", %{conn: conn} do
213 conn = patch(conn, "/api/v1/accounts/update_credentials", %{"status_ttl_days" => "1"})
214
215 assert user_data = json_response_and_validate_schema(conn, 200)
216 assert user_data["akkoma"]["status_ttl_days"] == 1
217 end
218
219 test "resets the user's default post expiry", %{conn: conn} do
220 conn = patch(conn, "/api/v1/accounts/update_credentials", %{"status_ttl_days" => "-1"})
221
222 assert user_data = json_response_and_validate_schema(conn, 200)
223 assert is_nil(user_data["akkoma"]["status_ttl_days"])
224 end
225
226 test "does not allow negative integers other than -1 for TTL", %{conn: conn} do
227 conn = patch(conn, "/api/v1/accounts/update_credentials", %{"status_ttl_days" => "-2"})
228
229 assert json_response_and_validate_schema(conn, 403)
230 end
231
232 test "updates the user's AKAs", %{conn: conn} do
233 conn =
234 patch(conn, "/api/v1/accounts/update_credentials", %{
235 "also_known_as" => ["https://mushroom.kingdom/users/mario"]
236 })
237
238 assert user_data = json_response_and_validate_schema(conn, 200)
239 assert user_data["pleroma"]["also_known_as"] == ["https://mushroom.kingdom/users/mario"]
240 end
241
242 test "doesn't update non-url akas", %{conn: conn} do
243 conn =
244 patch(conn, "/api/v1/accounts/update_credentials", %{
245 "also_known_as" => ["aReallyCoolGuy"]
246 })
247
248 assert json_response_and_validate_schema(conn, 403)
249 end
250
251 test "updates the user's avatar", %{user: user, conn: conn} do
252 new_avatar = %Plug.Upload{
253 content_type: "image/jpeg",
254 path: Path.absname("test/fixtures/image.jpg"),
255 filename: "an_image.jpg"
256 }
257
258 assert user.avatar == %{}
259
260 res = patch(conn, "/api/v1/accounts/update_credentials", %{"avatar" => new_avatar})
261
262 assert user_response = json_response_and_validate_schema(res, 200)
263 assert user_response["avatar"] != User.avatar_url(user)
264
265 user = User.get_by_id(user.id)
266 refute user.avatar == %{}
267
268 # Also resets it
269 _res = patch(conn, "/api/v1/accounts/update_credentials", %{"avatar" => ""})
270
271 user = User.get_by_id(user.id)
272 assert user.avatar == nil
273 end
274
275 test "updates the user's avatar, upload_limit, returns a HTTP 413", %{conn: conn, user: user} do
276 upload_limit = Config.get([:instance, :upload_limit]) * 8 + 8
277
278 assert :ok ==
279 File.write(Path.absname("test/tmp/large_binary.data"), <<0::size(upload_limit)>>)
280
281 new_avatar_oversized = %Plug.Upload{
282 content_type: nil,
283 path: Path.absname("test/tmp/large_binary.data"),
284 filename: "large_binary.data"
285 }
286
287 assert user.avatar == %{}
288
289 res =
290 patch(conn, "/api/v1/accounts/update_credentials", %{"avatar" => new_avatar_oversized})
291
292 assert user_response = json_response_and_validate_schema(res, 413)
293 assert user_response["avatar"] != User.avatar_url(user)
294
295 user = User.get_by_id(user.id)
296 assert user.avatar == %{}
297
298 clear_config([:instance, :upload_limit], upload_limit)
299
300 assert :ok == File.rm(Path.absname("test/tmp/large_binary.data"))
301 end
302
303 test "updates the user's banner", %{user: user, conn: conn} do
304 new_header = %Plug.Upload{
305 content_type: "image/jpeg",
306 path: Path.absname("test/fixtures/image.jpg"),
307 filename: "an_image.jpg"
308 }
309
310 res = patch(conn, "/api/v1/accounts/update_credentials", %{"header" => new_header})
311
312 assert user_response = json_response_and_validate_schema(res, 200)
313 assert user_response["header"] != User.banner_url(user)
314
315 # Also resets it
316 _res = patch(conn, "/api/v1/accounts/update_credentials", %{"header" => ""})
317
318 user = User.get_by_id(user.id)
319 assert user.banner == nil
320 end
321
322 test "updates the user's banner, upload_limit, returns a HTTP 413", %{conn: conn, user: user} do
323 upload_limit = Config.get([:instance, :upload_limit]) * 8 + 8
324
325 assert :ok ==
326 File.write(Path.absname("test/tmp/large_binary.data"), <<0::size(upload_limit)>>)
327
328 new_header_oversized = %Plug.Upload{
329 content_type: nil,
330 path: Path.absname("test/tmp/large_binary.data"),
331 filename: "large_binary.data"
332 }
333
334 res =
335 patch(conn, "/api/v1/accounts/update_credentials", %{"header" => new_header_oversized})
336
337 assert user_response = json_response_and_validate_schema(res, 413)
338 assert user_response["header"] != User.banner_url(user)
339
340 user = User.get_by_id(user.id)
341 assert user.banner == %{}
342
343 clear_config([:instance, :upload_limit], upload_limit)
344
345 assert :ok == File.rm(Path.absname("test/tmp/large_binary.data"))
346 end
347
348 test "updates the user's background", %{conn: conn, user: user} do
349 new_header = %Plug.Upload{
350 content_type: "image/jpeg",
351 path: Path.absname("test/fixtures/image.jpg"),
352 filename: "an_image.jpg"
353 }
354
355 res =
356 patch(conn, "/api/v1/accounts/update_credentials", %{
357 "pleroma_background_image" => new_header
358 })
359
360 assert user_response = json_response_and_validate_schema(res, 200)
361 assert user_response["pleroma"]["background_image"]
362 #
363 # Also resets it
364 _res =
365 patch(conn, "/api/v1/accounts/update_credentials", %{"pleroma_background_image" => ""})
366
367 user = User.get_by_id(user.id)
368 assert user.background == nil
369 end
370
371 test "updates the user's background, upload_limit, returns a HTTP 413", %{
372 conn: conn,
373 user: user
374 } do
375 upload_limit = Config.get([:instance, :upload_limit]) * 8 + 8
376
377 assert :ok ==
378 File.write(Path.absname("test/tmp/large_binary.data"), <<0::size(upload_limit)>>)
379
380 new_background_oversized = %Plug.Upload{
381 content_type: nil,
382 path: Path.absname("test/tmp/large_binary.data"),
383 filename: "large_binary.data"
384 }
385
386 res =
387 patch(conn, "/api/v1/accounts/update_credentials", %{
388 "pleroma_background_image" => new_background_oversized
389 })
390
391 assert user_response = json_response_and_validate_schema(res, 413)
392 assert user.background == %{}
393
394 clear_config([:instance, :upload_limit], upload_limit)
395
396 assert :ok == File.rm(Path.absname("test/tmp/large_binary.data"))
397 end
398
399 test "requires 'write:accounts' permission" do
400 token1 = insert(:oauth_token, scopes: ["read"])
401 token2 = insert(:oauth_token, scopes: ["write", "follow"])
402
403 for token <- [token1, token2] do
404 conn =
405 build_conn()
406 |> put_req_header("content-type", "multipart/form-data")
407 |> put_req_header("authorization", "Bearer #{token.token}")
408 |> patch("/api/v1/accounts/update_credentials", %{})
409
410 if token == token1 do
411 assert %{"error" => "Insufficient permissions: write:accounts."} ==
412 json_response_and_validate_schema(conn, 403)
413 else
414 assert json_response_and_validate_schema(conn, 200)
415 end
416 end
417 end
418
419 test "updates profile emojos", %{user: user, conn: conn} do
420 note = "*sips :blank:*"
421 name = "I am :firefox:"
422
423 ret_conn =
424 patch(conn, "/api/v1/accounts/update_credentials", %{
425 "note" => note,
426 "display_name" => name
427 })
428
429 assert json_response_and_validate_schema(ret_conn, 200)
430
431 conn = get(conn, "/api/v1/accounts/#{user.id}")
432
433 assert user_data = json_response_and_validate_schema(conn, 200)
434
435 assert user_data["note"] == note
436 assert user_data["display_name"] == name
437 assert [%{"shortcode" => "blank"}, %{"shortcode" => "firefox"}] = user_data["emojis"]
438 end
439
440 test "update fields", %{conn: conn} do
441 fields = [
442 %{"name" => "<a href=\"http://google.com\">foo</a>", "value" => "<script>bar</script>"},
443 %{"name" => "link.io", "value" => "cofe.io"}
444 ]
445
446 account_data =
447 conn
448 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
449 |> json_response_and_validate_schema(200)
450
451 assert account_data["fields"] == [
452 %{"name" => "<a href=\"http://google.com\">foo</a>", "value" => "bar"},
453 %{
454 "name" => "link.io",
455 "value" => ~S(<a href="http://cofe.io" rel="ugc">cofe.io</a>)
456 }
457 ]
458
459 assert account_data["source"]["fields"] == [
460 %{
461 "name" => "<a href=\"http://google.com\">foo</a>",
462 "value" => "<script>bar</script>"
463 },
464 %{"name" => "link.io", "value" => "cofe.io"}
465 ]
466 end
467
468 test "emojis in fields labels", %{conn: conn} do
469 fields = [
470 %{"name" => ":firefox:", "value" => "is best 2hu"},
471 %{"name" => "they wins", "value" => ":blank:"}
472 ]
473
474 account_data =
475 conn
476 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
477 |> json_response_and_validate_schema(200)
478
479 assert account_data["fields"] == [
480 %{"name" => ":firefox:", "value" => "is best 2hu"},
481 %{"name" => "they wins", "value" => ":blank:"}
482 ]
483
484 assert account_data["source"]["fields"] == [
485 %{"name" => ":firefox:", "value" => "is best 2hu"},
486 %{"name" => "they wins", "value" => ":blank:"}
487 ]
488
489 assert [%{"shortcode" => "blank"}, %{"shortcode" => "firefox"}] = account_data["emojis"]
490 end
491
492 test "update fields via x-www-form-urlencoded", %{conn: conn} do
493 fields =
494 [
495 "fields_attributes[1][name]=link",
496 "fields_attributes[1][value]=http://cofe.io",
497 "fields_attributes[0][name]=foo",
498 "fields_attributes[0][value]=bar"
499 ]
500 |> Enum.join("&")
501
502 account =
503 conn
504 |> put_req_header("content-type", "application/x-www-form-urlencoded")
505 |> patch("/api/v1/accounts/update_credentials", fields)
506 |> json_response_and_validate_schema(200)
507
508 assert account["fields"] == [
509 %{"name" => "foo", "value" => "bar"},
510 %{
511 "name" => "link",
512 "value" => ~S(<a href="http://cofe.io" rel="ugc">http://cofe.io</a>)
513 }
514 ]
515
516 assert account["source"]["fields"] == [
517 %{"name" => "foo", "value" => "bar"},
518 %{"name" => "link", "value" => "http://cofe.io"}
519 ]
520 end
521
522 test "update fields with empty name", %{conn: conn} do
523 fields = [
524 %{"name" => "foo", "value" => ""},
525 %{"name" => "", "value" => "bar"}
526 ]
527
528 account =
529 conn
530 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
531 |> json_response_and_validate_schema(200)
532
533 assert account["fields"] == [
534 %{"name" => "foo", "value" => ""}
535 ]
536 end
537
538 test "update fields when invalid request", %{conn: conn} do
539 name_limit = Pleroma.Config.get([:instance, :account_field_name_length])
540 value_limit = Pleroma.Config.get([:instance, :account_field_value_length])
541
542 long_name = Enum.map(0..name_limit, fn _ -> "x" end) |> Enum.join()
543 long_value = Enum.map(0..value_limit, fn _ -> "x" end) |> Enum.join()
544
545 fields = [%{"name" => "foo", "value" => long_value}]
546
547 assert %{"error" => "Invalid request"} ==
548 conn
549 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
550 |> json_response_and_validate_schema(403)
551
552 fields = [%{"name" => long_name, "value" => "bar"}]
553
554 assert %{"error" => "Invalid request"} ==
555 conn
556 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
557 |> json_response_and_validate_schema(403)
558
559 clear_config([:instance, :max_account_fields], 1)
560
561 fields = [
562 %{"name" => "foo", "value" => "bar"},
563 %{"name" => "link", "value" => "cofe.io"}
564 ]
565
566 assert %{"error" => "Invalid request"} ==
567 conn
568 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
569 |> json_response_and_validate_schema(403)
570 end
571 end
572
573 describe "Mark account as bot" do
574 setup do: oauth_access(["write:accounts"])
575 setup :request_content_type
576
577 test "changing actor_type to Service makes account a bot", %{conn: conn} do
578 account =
579 conn
580 |> patch("/api/v1/accounts/update_credentials", %{actor_type: "Service"})
581 |> json_response_and_validate_schema(200)
582
583 assert account["bot"]
584 assert account["source"]["pleroma"]["actor_type"] == "Service"
585 end
586
587 test "changing actor_type to Person makes account a human", %{conn: conn} do
588 account =
589 conn
590 |> patch("/api/v1/accounts/update_credentials", %{actor_type: "Person"})
591 |> json_response_and_validate_schema(200)
592
593 refute account["bot"]
594 assert account["source"]["pleroma"]["actor_type"] == "Person"
595 end
596
597 test "changing actor_type to Application causes error", %{conn: conn} do
598 response =
599 conn
600 |> patch("/api/v1/accounts/update_credentials", %{actor_type: "Application"})
601 |> json_response_and_validate_schema(403)
602
603 assert %{"error" => "Invalid request"} == response
604 end
605
606 test "changing bot field to true changes actor_type to Service", %{conn: conn} do
607 account =
608 conn
609 |> patch("/api/v1/accounts/update_credentials", %{bot: "true"})
610 |> json_response_and_validate_schema(200)
611
612 assert account["bot"]
613 assert account["source"]["pleroma"]["actor_type"] == "Service"
614 end
615
616 test "changing bot field to false changes actor_type to Person", %{conn: conn} do
617 account =
618 conn
619 |> patch("/api/v1/accounts/update_credentials", %{bot: "false"})
620 |> json_response_and_validate_schema(200)
621
622 refute account["bot"]
623 assert account["source"]["pleroma"]["actor_type"] == "Person"
624 end
625
626 test "actor_type field has a higher priority than bot", %{conn: conn} do
627 account =
628 conn
629 |> patch("/api/v1/accounts/update_credentials", %{
630 actor_type: "Person",
631 bot: "true"
632 })
633 |> json_response_and_validate_schema(200)
634
635 refute account["bot"]
636 assert account["source"]["pleroma"]["actor_type"] == "Person"
637 end
638 end
639 end