1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.MastodonAPIController.UpdateCredentialsTest do
9 use Pleroma.Web.ConnCase
11 import Pleroma.Factory
13 setup do: clear_config([:instance, :max_account_fields])
15 describe "updating credentials" do
16 setup do: oauth_access(["write:accounts"])
17 setup :request_content_type
19 test "sets user settings in a generic way", %{conn: conn} do
21 patch(conn, "/api/v1/accounts/update_credentials", %{
22 "pleroma_settings_store" => %{
29 assert user_data = json_response_and_validate_schema(res_conn, 200)
30 assert user_data["pleroma"]["settings_store"] == %{"pleroma_fe" => %{"theme" => "bla"}}
32 user = Repo.get(User, user_data["id"])
36 |> assign(:user, user)
37 |> patch("/api/v1/accounts/update_credentials", %{
38 "pleroma_settings_store" => %{
45 assert user_data = json_response_and_validate_schema(res_conn, 200)
47 assert user_data["pleroma"]["settings_store"] ==
49 "pleroma_fe" => %{"theme" => "bla"},
50 "masto_fe" => %{"theme" => "bla"}
53 user = Repo.get(User, user_data["id"])
57 |> assign(:user, user)
58 |> patch("/api/v1/accounts/update_credentials", %{
59 "pleroma_settings_store" => %{
66 assert user_data = json_response_and_validate_schema(res_conn, 200)
68 assert user_data["pleroma"]["settings_store"] ==
70 "pleroma_fe" => %{"theme" => "bla"},
71 "masto_fe" => %{"theme" => "blub"}
75 test "updates the user's bio", %{conn: conn} do
79 patch(conn, "/api/v1/accounts/update_credentials", %{
80 "note" => "I drink #cofe with @#{user2.nickname}\n\nsuya.."
83 assert user_data = json_response_and_validate_schema(conn, 200)
85 assert user_data["note"] ==
86 ~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="#{
88 }" href="#{user2.ap_id}" rel="ugc">@<span>#{user2.nickname}</span></a></span><br/><br/>suya..)
91 test "updates the user's locking status", %{conn: conn} do
92 conn = patch(conn, "/api/v1/accounts/update_credentials", %{locked: "true"})
94 assert user_data = json_response_and_validate_schema(conn, 200)
95 assert user_data["locked"] == true
98 test "updates the user's allow_following_move", %{user: user, conn: conn} do
99 assert user.allow_following_move == true
101 conn = patch(conn, "/api/v1/accounts/update_credentials", %{allow_following_move: "false"})
103 assert refresh_record(user).allow_following_move == false
104 assert user_data = json_response_and_validate_schema(conn, 200)
105 assert user_data["pleroma"]["allow_following_move"] == false
108 test "updates the user's default scope", %{conn: conn} do
109 conn = patch(conn, "/api/v1/accounts/update_credentials", %{default_scope: "unlisted"})
111 assert user_data = json_response_and_validate_schema(conn, 200)
112 assert user_data["source"]["privacy"] == "unlisted"
115 test "updates the user's hide_followers status", %{conn: conn} do
116 conn = patch(conn, "/api/v1/accounts/update_credentials", %{hide_followers: "true"})
118 assert user_data = json_response_and_validate_schema(conn, 200)
119 assert user_data["pleroma"]["hide_followers"] == true
122 test "updates the user's discoverable status", %{conn: conn} do
123 assert %{"source" => %{"pleroma" => %{"discoverable" => true}}} =
125 |> patch("/api/v1/accounts/update_credentials", %{discoverable: "true"})
126 |> json_response_and_validate_schema(:ok)
128 assert %{"source" => %{"pleroma" => %{"discoverable" => false}}} =
130 |> patch("/api/v1/accounts/update_credentials", %{discoverable: "false"})
131 |> json_response_and_validate_schema(:ok)
134 test "updates the user's hide_followers_count and hide_follows_count", %{conn: conn} do
136 patch(conn, "/api/v1/accounts/update_credentials", %{
137 hide_followers_count: "true",
138 hide_follows_count: "true"
141 assert user_data = json_response_and_validate_schema(conn, 200)
142 assert user_data["pleroma"]["hide_followers_count"] == true
143 assert user_data["pleroma"]["hide_follows_count"] == true
146 test "updates the user's skip_thread_containment option", %{user: user, conn: conn} do
149 |> patch("/api/v1/accounts/update_credentials", %{skip_thread_containment: "true"})
150 |> json_response_and_validate_schema(200)
152 assert response["pleroma"]["skip_thread_containment"] == true
153 assert refresh_record(user).skip_thread_containment
156 test "updates the user's hide_follows status", %{conn: conn} do
157 conn = patch(conn, "/api/v1/accounts/update_credentials", %{hide_follows: "true"})
159 assert user_data = json_response_and_validate_schema(conn, 200)
160 assert user_data["pleroma"]["hide_follows"] == true
163 test "updates the user's hide_favorites status", %{conn: conn} do
164 conn = patch(conn, "/api/v1/accounts/update_credentials", %{hide_favorites: "true"})
166 assert user_data = json_response_and_validate_schema(conn, 200)
167 assert user_data["pleroma"]["hide_favorites"] == true
170 test "updates the user's show_role status", %{conn: conn} do
171 conn = patch(conn, "/api/v1/accounts/update_credentials", %{show_role: "false"})
173 assert user_data = json_response_and_validate_schema(conn, 200)
174 assert user_data["source"]["pleroma"]["show_role"] == false
177 test "updates the user's no_rich_text status", %{conn: conn} do
178 conn = patch(conn, "/api/v1/accounts/update_credentials", %{no_rich_text: "true"})
180 assert user_data = json_response_and_validate_schema(conn, 200)
181 assert user_data["source"]["pleroma"]["no_rich_text"] == true
184 test "updates the user's name", %{conn: conn} do
186 patch(conn, "/api/v1/accounts/update_credentials", %{"display_name" => "markorepairs"})
188 assert user_data = json_response_and_validate_schema(conn, 200)
189 assert user_data["display_name"] == "markorepairs"
192 test "updates the user's avatar", %{user: user, conn: conn} do
193 new_avatar = %Plug.Upload{
194 content_type: "image/jpg",
195 path: Path.absname("test/fixtures/image.jpg"),
196 filename: "an_image.jpg"
199 conn = patch(conn, "/api/v1/accounts/update_credentials", %{"avatar" => new_avatar})
201 assert user_response = json_response_and_validate_schema(conn, 200)
202 assert user_response["avatar"] != User.avatar_url(user)
205 test "updates the user's banner", %{user: user, conn: conn} do
206 new_header = %Plug.Upload{
207 content_type: "image/jpg",
208 path: Path.absname("test/fixtures/image.jpg"),
209 filename: "an_image.jpg"
212 conn = patch(conn, "/api/v1/accounts/update_credentials", %{"header" => new_header})
214 assert user_response = json_response_and_validate_schema(conn, 200)
215 assert user_response["header"] != User.banner_url(user)
218 test "updates the user's background", %{conn: conn} do
219 new_header = %Plug.Upload{
220 content_type: "image/jpg",
221 path: Path.absname("test/fixtures/image.jpg"),
222 filename: "an_image.jpg"
226 patch(conn, "/api/v1/accounts/update_credentials", %{
227 "pleroma_background_image" => new_header
230 assert user_response = json_response_and_validate_schema(conn, 200)
231 assert user_response["pleroma"]["background_image"]
234 test "requires 'write:accounts' permission" do
235 token1 = insert(:oauth_token, scopes: ["read"])
236 token2 = insert(:oauth_token, scopes: ["write", "follow"])
238 for token <- [token1, token2] do
241 |> put_req_header("content-type", "multipart/form-data")
242 |> put_req_header("authorization", "Bearer #{token.token}")
243 |> patch("/api/v1/accounts/update_credentials", %{})
245 if token == token1 do
246 assert %{"error" => "Insufficient permissions: write:accounts."} ==
247 json_response_and_validate_schema(conn, 403)
249 assert json_response_and_validate_schema(conn, 200)
254 test "updates profile emojos", %{user: user, conn: conn} do
255 note = "*sips :blank:*"
256 name = "I am :firefox:"
259 patch(conn, "/api/v1/accounts/update_credentials", %{
261 "display_name" => name
264 assert json_response_and_validate_schema(ret_conn, 200)
266 conn = get(conn, "/api/v1/accounts/#{user.id}")
268 assert user_data = json_response_and_validate_schema(conn, 200)
270 assert user_data["note"] == note
271 assert user_data["display_name"] == name
272 assert [%{"shortcode" => "blank"}, %{"shortcode" => "firefox"}] = user_data["emojis"]
275 test "update fields", %{conn: conn} do
277 %{"name" => "<a href=\"http://google.com\">foo</a>", "value" => "<script>bar</script>"},
278 %{"name" => "link.io", "value" => "cofe.io"}
283 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
284 |> json_response_and_validate_schema(200)
286 assert account_data["fields"] == [
287 %{"name" => "<a href=\"http://google.com\">foo</a>", "value" => "bar"},
290 "value" => ~S(<a href="http://cofe.io" rel="ugc">cofe.io</a>)
294 assert account_data["source"]["fields"] == [
296 "name" => "<a href=\"http://google.com\">foo</a>",
297 "value" => "<script>bar</script>"
299 %{"name" => "link.io", "value" => "cofe.io"}
303 test "update fields via x-www-form-urlencoded", %{conn: conn} do
306 "fields_attributes[1][name]=link",
307 "fields_attributes[1][value]=http://cofe.io",
308 "fields_attributes[0][name]=foo",
309 "fields_attributes[0][value]=bar"
315 |> put_req_header("content-type", "application/x-www-form-urlencoded")
316 |> patch("/api/v1/accounts/update_credentials", fields)
317 |> json_response_and_validate_schema(200)
319 assert account["fields"] == [
320 %{"name" => "foo", "value" => "bar"},
323 "value" => ~S(<a href="http://cofe.io" rel="ugc">http://cofe.io</a>)
327 assert account["source"]["fields"] == [
328 %{"name" => "foo", "value" => "bar"},
329 %{"name" => "link", "value" => "http://cofe.io"}
333 test "update fields with empty name", %{conn: conn} do
335 %{"name" => "foo", "value" => ""},
336 %{"name" => "", "value" => "bar"}
341 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
342 |> json_response_and_validate_schema(200)
344 assert account["fields"] == [
345 %{"name" => "foo", "value" => ""}
349 test "update fields when invalid request", %{conn: conn} do
350 name_limit = Pleroma.Config.get([:instance, :account_field_name_length])
351 value_limit = Pleroma.Config.get([:instance, :account_field_value_length])
353 long_name = Enum.map(0..name_limit, fn _ -> "x" end) |> Enum.join()
354 long_value = Enum.map(0..value_limit, fn _ -> "x" end) |> Enum.join()
356 fields = [%{"name" => "foo", "value" => long_value}]
358 assert %{"error" => "Invalid request"} ==
360 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
361 |> json_response_and_validate_schema(403)
363 fields = [%{"name" => long_name, "value" => "bar"}]
365 assert %{"error" => "Invalid request"} ==
367 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
368 |> json_response_and_validate_schema(403)
370 Pleroma.Config.put([:instance, :max_account_fields], 1)
373 %{"name" => "foo", "value" => "bar"},
374 %{"name" => "link", "value" => "cofe.io"}
377 assert %{"error" => "Invalid request"} ==
379 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
380 |> json_response_and_validate_schema(403)