1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.UpdateCredentialsTest do
9 use Pleroma.Web.ConnCase
12 import Pleroma.Factory
14 describe "updating credentials" do
15 setup do: oauth_access(["write:accounts"])
16 setup :request_content_type
18 test "sets user settings in a generic way", %{conn: conn} do
20 patch(conn, "/api/v1/accounts/update_credentials", %{
21 "pleroma_settings_store" => %{
28 assert user_data = json_response_and_validate_schema(res_conn, 200)
29 assert user_data["pleroma"]["settings_store"] == %{"pleroma_fe" => %{"theme" => "bla"}}
31 user = Repo.get(User, user_data["id"])
35 |> assign(:user, user)
36 |> patch("/api/v1/accounts/update_credentials", %{
37 "pleroma_settings_store" => %{
44 assert user_data = json_response_and_validate_schema(res_conn, 200)
46 assert user_data["pleroma"]["settings_store"] ==
48 "pleroma_fe" => %{"theme" => "bla"},
49 "masto_fe" => %{"theme" => "bla"}
52 user = Repo.get(User, user_data["id"])
54 clear_config([:instance, :federating], true)
56 with_mock Pleroma.Web.Federator,
57 publish: fn _activity -> :ok end do
60 |> assign(:user, user)
61 |> patch("/api/v1/accounts/update_credentials", %{
62 "pleroma_settings_store" => %{
69 assert user_data = json_response_and_validate_schema(res_conn, 200)
71 assert user_data["pleroma"]["settings_store"] ==
73 "pleroma_fe" => %{"theme" => "bla"},
74 "masto_fe" => %{"theme" => "blub"}
77 assert_called(Pleroma.Web.Federator.publish(:_))
81 test "updates the user's bio", %{conn: conn} do
84 raw_bio = "I drink #cofe with @#{user2.nickname}\n\nsuya.."
86 conn = patch(conn, "/api/v1/accounts/update_credentials", %{"note" => raw_bio})
88 assert user_data = json_response_and_validate_schema(conn, 200)
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="#{
93 }" href="#{user2.ap_id}" rel="ugc">@<span>#{user2.nickname}</span></a></span><br/><br/>suya..)
95 assert user_data["source"]["note"] == raw_bio
97 user = Repo.get(User, user_data["id"])
99 assert user.raw_bio == raw_bio
102 test "updates the user's locking status", %{conn: conn} do
103 conn = patch(conn, "/api/v1/accounts/update_credentials", %{locked: "true"})
105 assert user_data = json_response_and_validate_schema(conn, 200)
106 assert user_data["locked"] == true
109 test "updates the user's chat acceptance status", %{conn: conn} do
110 conn = patch(conn, "/api/v1/accounts/update_credentials", %{accepts_chat_messages: "false"})
112 assert user_data = json_response_and_validate_schema(conn, 200)
113 assert user_data["pleroma"]["accepts_chat_messages"] == false
116 test "updates the user's allow_following_move", %{user: user, conn: conn} do
117 assert user.allow_following_move == true
119 conn = patch(conn, "/api/v1/accounts/update_credentials", %{allow_following_move: "false"})
121 assert refresh_record(user).allow_following_move == false
122 assert user_data = json_response_and_validate_schema(conn, 200)
123 assert user_data["pleroma"]["allow_following_move"] == false
126 test "updates the user's default scope", %{conn: conn} do
127 conn = patch(conn, "/api/v1/accounts/update_credentials", %{default_scope: "unlisted"})
129 assert user_data = json_response_and_validate_schema(conn, 200)
130 assert user_data["source"]["privacy"] == "unlisted"
133 test "updates the user's privacy", %{conn: conn} do
134 conn = patch(conn, "/api/v1/accounts/update_credentials", %{source: %{privacy: "unlisted"}})
136 assert user_data = json_response_and_validate_schema(conn, 200)
137 assert user_data["source"]["privacy"] == "unlisted"
140 test "updates the user's hide_followers status", %{conn: conn} do
141 conn = patch(conn, "/api/v1/accounts/update_credentials", %{hide_followers: "true"})
143 assert user_data = json_response_and_validate_schema(conn, 200)
144 assert user_data["pleroma"]["hide_followers"] == true
147 test "updates the user's discoverable status", %{conn: conn} do
148 assert %{"source" => %{"pleroma" => %{"discoverable" => true}}} =
150 |> patch("/api/v1/accounts/update_credentials", %{discoverable: "true"})
151 |> json_response_and_validate_schema(:ok)
153 assert %{"source" => %{"pleroma" => %{"discoverable" => false}}} =
155 |> patch("/api/v1/accounts/update_credentials", %{discoverable: "false"})
156 |> json_response_and_validate_schema(:ok)
159 test "updates the user's hide_followers_count and hide_follows_count", %{conn: conn} do
161 patch(conn, "/api/v1/accounts/update_credentials", %{
162 hide_followers_count: "true",
163 hide_follows_count: "true"
166 assert user_data = json_response_and_validate_schema(conn, 200)
167 assert user_data["pleroma"]["hide_followers_count"] == true
168 assert user_data["pleroma"]["hide_follows_count"] == true
171 test "updates the user's skip_thread_containment option", %{user: user, conn: conn} do
174 |> patch("/api/v1/accounts/update_credentials", %{skip_thread_containment: "true"})
175 |> json_response_and_validate_schema(200)
177 assert response["pleroma"]["skip_thread_containment"] == true
178 assert refresh_record(user).skip_thread_containment
181 test "updates the user's hide_follows status", %{conn: conn} do
182 conn = patch(conn, "/api/v1/accounts/update_credentials", %{hide_follows: "true"})
184 assert user_data = json_response_and_validate_schema(conn, 200)
185 assert user_data["pleroma"]["hide_follows"] == true
188 test "updates the user's hide_favorites status", %{conn: conn} do
189 conn = patch(conn, "/api/v1/accounts/update_credentials", %{hide_favorites: "true"})
191 assert user_data = json_response_and_validate_schema(conn, 200)
192 assert user_data["pleroma"]["hide_favorites"] == true
195 test "updates the user's show_role status", %{conn: conn} do
196 conn = patch(conn, "/api/v1/accounts/update_credentials", %{show_role: "false"})
198 assert user_data = json_response_and_validate_schema(conn, 200)
199 assert user_data["source"]["pleroma"]["show_role"] == false
202 test "updates the user's no_rich_text status", %{conn: conn} do
203 conn = patch(conn, "/api/v1/accounts/update_credentials", %{no_rich_text: "true"})
205 assert user_data = json_response_and_validate_schema(conn, 200)
206 assert user_data["source"]["pleroma"]["no_rich_text"] == true
209 test "updates the user's name", %{conn: conn} do
211 patch(conn, "/api/v1/accounts/update_credentials", %{"display_name" => "markorepairs"})
213 assert user_data = json_response_and_validate_schema(conn, 200)
214 assert user_data["display_name"] == "markorepairs"
216 update_activity = Repo.one(Pleroma.Activity)
217 assert update_activity.data["type"] == "Update"
218 assert update_activity.data["object"]["name"] == "markorepairs"
221 test "updates the user's AKAs", %{conn: conn} do
223 patch(conn, "/api/v1/accounts/update_credentials", %{
224 "also_known_as" => ["https://mushroom.kingdom/users/mario"]
227 assert user_data = json_response_and_validate_schema(conn, 200)
228 assert user_data["pleroma"]["also_known_as"] == ["https://mushroom.kingdom/users/mario"]
231 test "doesn't update non-url akas", %{conn: conn} do
233 patch(conn, "/api/v1/accounts/update_credentials", %{
234 "also_known_as" => ["aReallyCoolGuy"]
237 assert json_response_and_validate_schema(conn, 403)
240 test "updates the user's avatar", %{user: user, conn: conn} do
241 new_avatar = %Plug.Upload{
242 content_type: "image/jpeg",
243 path: Path.absname("test/fixtures/image.jpg"),
244 filename: "an_image.jpg"
247 assert user.avatar == %{}
249 res = patch(conn, "/api/v1/accounts/update_credentials", %{"avatar" => new_avatar})
251 assert user_response = json_response_and_validate_schema(res, 200)
252 assert user_response["avatar"] != User.avatar_url(user)
254 user = User.get_by_id(user.id)
255 refute user.avatar == %{}
258 _res = patch(conn, "/api/v1/accounts/update_credentials", %{"avatar" => ""})
260 user = User.get_by_id(user.id)
261 assert user.avatar == nil
264 test "updates the user's banner", %{user: user, conn: conn} do
265 new_header = %Plug.Upload{
266 content_type: "image/jpeg",
267 path: Path.absname("test/fixtures/image.jpg"),
268 filename: "an_image.jpg"
271 res = patch(conn, "/api/v1/accounts/update_credentials", %{"header" => new_header})
273 assert user_response = json_response_and_validate_schema(res, 200)
274 assert user_response["header"] != User.banner_url(user)
277 _res = patch(conn, "/api/v1/accounts/update_credentials", %{"header" => ""})
279 user = User.get_by_id(user.id)
280 assert user.banner == nil
283 test "updates the user's background", %{conn: conn, user: user} do
284 new_header = %Plug.Upload{
285 content_type: "image/jpeg",
286 path: Path.absname("test/fixtures/image.jpg"),
287 filename: "an_image.jpg"
291 patch(conn, "/api/v1/accounts/update_credentials", %{
292 "pleroma_background_image" => new_header
295 assert user_response = json_response_and_validate_schema(res, 200)
296 assert user_response["pleroma"]["background_image"]
300 patch(conn, "/api/v1/accounts/update_credentials", %{"pleroma_background_image" => ""})
302 user = User.get_by_id(user.id)
303 assert user.background == nil
306 test "requires 'write:accounts' permission" do
307 token1 = insert(:oauth_token, scopes: ["read"])
308 token2 = insert(:oauth_token, scopes: ["write", "follow"])
310 for token <- [token1, token2] do
313 |> put_req_header("content-type", "multipart/form-data")
314 |> put_req_header("authorization", "Bearer #{token.token}")
315 |> patch("/api/v1/accounts/update_credentials", %{})
317 if token == token1 do
318 assert %{"error" => "Insufficient permissions: write:accounts."} ==
319 json_response_and_validate_schema(conn, 403)
321 assert json_response_and_validate_schema(conn, 200)
326 test "updates profile emojos", %{user: user, conn: conn} do
327 note = "*sips :blank:*"
328 name = "I am :firefox:"
331 patch(conn, "/api/v1/accounts/update_credentials", %{
333 "display_name" => name
336 assert json_response_and_validate_schema(ret_conn, 200)
338 conn = get(conn, "/api/v1/accounts/#{user.id}")
340 assert user_data = json_response_and_validate_schema(conn, 200)
342 assert user_data["note"] == note
343 assert user_data["display_name"] == name
344 assert [%{"shortcode" => "blank"}, %{"shortcode" => "firefox"}] = user_data["emojis"]
347 test "update fields", %{conn: conn} do
349 %{"name" => "<a href=\"http://google.com\">foo</a>", "value" => "<script>bar</script>"},
350 %{"name" => "link.io", "value" => "cofe.io"}
355 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
356 |> json_response_and_validate_schema(200)
358 assert account_data["fields"] == [
359 %{"name" => "<a href=\"http://google.com\">foo</a>", "value" => "bar"},
362 "value" => ~S(<a href="http://cofe.io" rel="ugc">cofe.io</a>)
366 assert account_data["source"]["fields"] == [
368 "name" => "<a href=\"http://google.com\">foo</a>",
369 "value" => "<script>bar</script>"
371 %{"name" => "link.io", "value" => "cofe.io"}
375 test "emojis in fields labels", %{conn: conn} do
377 %{"name" => ":firefox:", "value" => "is best 2hu"},
378 %{"name" => "they wins", "value" => ":blank:"}
383 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
384 |> json_response_and_validate_schema(200)
386 assert account_data["fields"] == [
387 %{"name" => ":firefox:", "value" => "is best 2hu"},
388 %{"name" => "they wins", "value" => ":blank:"}
391 assert account_data["source"]["fields"] == [
392 %{"name" => ":firefox:", "value" => "is best 2hu"},
393 %{"name" => "they wins", "value" => ":blank:"}
396 assert [%{"shortcode" => "blank"}, %{"shortcode" => "firefox"}] = account_data["emojis"]
399 test "update fields via x-www-form-urlencoded", %{conn: conn} do
402 "fields_attributes[1][name]=link",
403 "fields_attributes[1][value]=http://cofe.io",
404 "fields_attributes[0][name]=foo",
405 "fields_attributes[0][value]=bar"
411 |> put_req_header("content-type", "application/x-www-form-urlencoded")
412 |> patch("/api/v1/accounts/update_credentials", fields)
413 |> json_response_and_validate_schema(200)
415 assert account["fields"] == [
416 %{"name" => "foo", "value" => "bar"},
419 "value" => ~S(<a href="http://cofe.io" rel="ugc">http://cofe.io</a>)
423 assert account["source"]["fields"] == [
424 %{"name" => "foo", "value" => "bar"},
425 %{"name" => "link", "value" => "http://cofe.io"}
429 test "update fields with empty name", %{conn: conn} do
431 %{"name" => "foo", "value" => ""},
432 %{"name" => "", "value" => "bar"}
437 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
438 |> json_response_and_validate_schema(200)
440 assert account["fields"] == [
441 %{"name" => "foo", "value" => ""}
445 test "update fields when invalid request", %{conn: conn} do
446 name_limit = Pleroma.Config.get([:instance, :account_field_name_length])
447 value_limit = Pleroma.Config.get([:instance, :account_field_value_length])
449 long_name = Enum.map(0..name_limit, fn _ -> "x" end) |> Enum.join()
450 long_value = Enum.map(0..value_limit, fn _ -> "x" end) |> Enum.join()
452 fields = [%{"name" => "foo", "value" => long_value}]
454 assert %{"error" => "Invalid request"} ==
456 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
457 |> json_response_and_validate_schema(403)
459 fields = [%{"name" => long_name, "value" => "bar"}]
461 assert %{"error" => "Invalid request"} ==
463 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
464 |> json_response_and_validate_schema(403)
466 clear_config([:instance, :max_account_fields], 1)
469 %{"name" => "foo", "value" => "bar"},
470 %{"name" => "link", "value" => "cofe.io"}
473 assert %{"error" => "Invalid request"} ==
475 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
476 |> json_response_and_validate_schema(403)
480 describe "Mark account as bot" do
481 setup do: oauth_access(["write:accounts"])
482 setup :request_content_type
484 test "changing actor_type to Service makes account a bot", %{conn: conn} do
487 |> patch("/api/v1/accounts/update_credentials", %{actor_type: "Service"})
488 |> json_response_and_validate_schema(200)
490 assert account["bot"]
491 assert account["source"]["pleroma"]["actor_type"] == "Service"
494 test "changing actor_type to Person makes account a human", %{conn: conn} do
497 |> patch("/api/v1/accounts/update_credentials", %{actor_type: "Person"})
498 |> json_response_and_validate_schema(200)
500 refute account["bot"]
501 assert account["source"]["pleroma"]["actor_type"] == "Person"
504 test "changing actor_type to Application causes error", %{conn: conn} do
507 |> patch("/api/v1/accounts/update_credentials", %{actor_type: "Application"})
508 |> json_response_and_validate_schema(403)
510 assert %{"error" => "Invalid request"} == response
513 test "changing bot field to true changes actor_type to Service", %{conn: conn} do
516 |> patch("/api/v1/accounts/update_credentials", %{bot: "true"})
517 |> json_response_and_validate_schema(200)
519 assert account["bot"]
520 assert account["source"]["pleroma"]["actor_type"] == "Service"
523 test "changing bot field to false changes actor_type to Person", %{conn: conn} do
526 |> patch("/api/v1/accounts/update_credentials", %{bot: "false"})
527 |> json_response_and_validate_schema(200)
529 refute account["bot"]
530 assert account["source"]["pleroma"]["actor_type"] == "Person"
533 test "actor_type field has a higher priority than bot", %{conn: conn} do
536 |> patch("/api/v1/accounts/update_credentials", %{
537 actor_type: "Person",
540 |> json_response_and_validate_schema(200)
542 refute account["bot"]
543 assert account["source"]["pleroma"]["actor_type"] == "Person"