7c420985d927992014316f43f5af380f543f4336
[akkoma] / test / web / mastodon_api / controllers / account_controller / update_credentials_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.MastodonAPIController.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 setup do: clear_config([:instance, :max_account_fields])
15
16 describe "updating credentials" do
17 setup do: oauth_access(["write:accounts"])
18 setup :request_content_type
19
20 test "sets user settings in a generic way", %{conn: conn} do
21 res_conn =
22 patch(conn, "/api/v1/accounts/update_credentials", %{
23 "pleroma_settings_store" => %{
24 pleroma_fe: %{
25 theme: "bla"
26 }
27 }
28 })
29
30 assert user_data = json_response_and_validate_schema(res_conn, 200)
31 assert user_data["pleroma"]["settings_store"] == %{"pleroma_fe" => %{"theme" => "bla"}}
32
33 user = Repo.get(User, user_data["id"])
34
35 res_conn =
36 conn
37 |> assign(:user, user)
38 |> patch("/api/v1/accounts/update_credentials", %{
39 "pleroma_settings_store" => %{
40 masto_fe: %{
41 theme: "bla"
42 }
43 }
44 })
45
46 assert user_data = json_response_and_validate_schema(res_conn, 200)
47
48 assert user_data["pleroma"]["settings_store"] ==
49 %{
50 "pleroma_fe" => %{"theme" => "bla"},
51 "masto_fe" => %{"theme" => "bla"}
52 }
53
54 user = Repo.get(User, user_data["id"])
55
56 clear_config([:instance, :federating], true)
57
58 with_mock Pleroma.Web.Federator,
59 publish: fn _activity -> :ok end do
60 res_conn =
61 conn
62 |> assign(:user, user)
63 |> patch("/api/v1/accounts/update_credentials", %{
64 "pleroma_settings_store" => %{
65 masto_fe: %{
66 theme: "blub"
67 }
68 }
69 })
70
71 assert user_data = json_response_and_validate_schema(res_conn, 200)
72
73 assert user_data["pleroma"]["settings_store"] ==
74 %{
75 "pleroma_fe" => %{"theme" => "bla"},
76 "masto_fe" => %{"theme" => "blub"}
77 }
78
79 assert_called(Pleroma.Web.Federator.publish(:_))
80 end
81 end
82
83 test "updates the user's bio", %{conn: conn} do
84 user2 = insert(:user)
85
86 conn =
87 patch(conn, "/api/v1/accounts/update_credentials", %{
88 "note" => "I drink #cofe with @#{user2.nickname}\n\nsuya.."
89 })
90
91 assert user_data = json_response_and_validate_schema(conn, 200)
92
93 assert user_data["note"] ==
94 ~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="#{
95 user2.id
96 }" href="#{user2.ap_id}" rel="ugc">@<span>#{user2.nickname}</span></a></span><br/><br/>suya..)
97 end
98
99 test "updates the user's locking status", %{conn: conn} do
100 conn = patch(conn, "/api/v1/accounts/update_credentials", %{locked: "true"})
101
102 assert user_data = json_response_and_validate_schema(conn, 200)
103 assert user_data["locked"] == true
104 end
105
106 test "updates the user's allow_following_move", %{user: user, conn: conn} do
107 assert user.allow_following_move == true
108
109 conn = patch(conn, "/api/v1/accounts/update_credentials", %{allow_following_move: "false"})
110
111 assert refresh_record(user).allow_following_move == false
112 assert user_data = json_response_and_validate_schema(conn, 200)
113 assert user_data["pleroma"]["allow_following_move"] == false
114 end
115
116 test "updates the user's default scope", %{conn: conn} do
117 conn = patch(conn, "/api/v1/accounts/update_credentials", %{default_scope: "unlisted"})
118
119 assert user_data = json_response_and_validate_schema(conn, 200)
120 assert user_data["source"]["privacy"] == "unlisted"
121 end
122
123 test "updates the user's privacy", %{conn: conn} do
124 conn = patch(conn, "/api/v1/accounts/update_credentials", %{source: %{privacy: "unlisted"}})
125
126 assert user_data = json_response_and_validate_schema(conn, 200)
127 assert user_data["source"]["privacy"] == "unlisted"
128 end
129
130 test "updates the user's hide_followers status", %{conn: conn} do
131 conn = patch(conn, "/api/v1/accounts/update_credentials", %{hide_followers: "true"})
132
133 assert user_data = json_response_and_validate_schema(conn, 200)
134 assert user_data["pleroma"]["hide_followers"] == true
135 end
136
137 test "updates the user's discoverable status", %{conn: conn} do
138 assert %{"source" => %{"pleroma" => %{"discoverable" => true}}} =
139 conn
140 |> patch("/api/v1/accounts/update_credentials", %{discoverable: "true"})
141 |> json_response_and_validate_schema(:ok)
142
143 assert %{"source" => %{"pleroma" => %{"discoverable" => false}}} =
144 conn
145 |> patch("/api/v1/accounts/update_credentials", %{discoverable: "false"})
146 |> json_response_and_validate_schema(:ok)
147 end
148
149 test "updates the user's hide_followers_count and hide_follows_count", %{conn: conn} do
150 conn =
151 patch(conn, "/api/v1/accounts/update_credentials", %{
152 hide_followers_count: "true",
153 hide_follows_count: "true"
154 })
155
156 assert user_data = json_response_and_validate_schema(conn, 200)
157 assert user_data["pleroma"]["hide_followers_count"] == true
158 assert user_data["pleroma"]["hide_follows_count"] == true
159 end
160
161 test "updates the user's skip_thread_containment option", %{user: user, conn: conn} do
162 response =
163 conn
164 |> patch("/api/v1/accounts/update_credentials", %{skip_thread_containment: "true"})
165 |> json_response_and_validate_schema(200)
166
167 assert response["pleroma"]["skip_thread_containment"] == true
168 assert refresh_record(user).skip_thread_containment
169 end
170
171 test "updates the user's hide_follows status", %{conn: conn} do
172 conn = patch(conn, "/api/v1/accounts/update_credentials", %{hide_follows: "true"})
173
174 assert user_data = json_response_and_validate_schema(conn, 200)
175 assert user_data["pleroma"]["hide_follows"] == true
176 end
177
178 test "updates the user's hide_favorites status", %{conn: conn} do
179 conn = patch(conn, "/api/v1/accounts/update_credentials", %{hide_favorites: "true"})
180
181 assert user_data = json_response_and_validate_schema(conn, 200)
182 assert user_data["pleroma"]["hide_favorites"] == true
183 end
184
185 test "updates the user's show_role status", %{conn: conn} do
186 conn = patch(conn, "/api/v1/accounts/update_credentials", %{show_role: "false"})
187
188 assert user_data = json_response_and_validate_schema(conn, 200)
189 assert user_data["source"]["pleroma"]["show_role"] == false
190 end
191
192 test "updates the user's no_rich_text status", %{conn: conn} do
193 conn = patch(conn, "/api/v1/accounts/update_credentials", %{no_rich_text: "true"})
194
195 assert user_data = json_response_and_validate_schema(conn, 200)
196 assert user_data["source"]["pleroma"]["no_rich_text"] == true
197 end
198
199 test "updates the user's name", %{conn: conn} do
200 conn =
201 patch(conn, "/api/v1/accounts/update_credentials", %{"display_name" => "markorepairs"})
202
203 assert user_data = json_response_and_validate_schema(conn, 200)
204 assert user_data["display_name"] == "markorepairs"
205 end
206
207 test "updates the user's avatar", %{user: user, conn: conn} do
208 new_avatar = %Plug.Upload{
209 content_type: "image/jpg",
210 path: Path.absname("test/fixtures/image.jpg"),
211 filename: "an_image.jpg"
212 }
213
214 conn = patch(conn, "/api/v1/accounts/update_credentials", %{"avatar" => new_avatar})
215
216 assert user_response = json_response_and_validate_schema(conn, 200)
217 assert user_response["avatar"] != User.avatar_url(user)
218 end
219
220 test "updates the user's banner", %{user: user, conn: conn} do
221 new_header = %Plug.Upload{
222 content_type: "image/jpg",
223 path: Path.absname("test/fixtures/image.jpg"),
224 filename: "an_image.jpg"
225 }
226
227 conn = patch(conn, "/api/v1/accounts/update_credentials", %{"header" => new_header})
228
229 assert user_response = json_response_and_validate_schema(conn, 200)
230 assert user_response["header"] != User.banner_url(user)
231 end
232
233 test "updates the user's background", %{conn: conn} do
234 new_header = %Plug.Upload{
235 content_type: "image/jpg",
236 path: Path.absname("test/fixtures/image.jpg"),
237 filename: "an_image.jpg"
238 }
239
240 conn =
241 patch(conn, "/api/v1/accounts/update_credentials", %{
242 "pleroma_background_image" => new_header
243 })
244
245 assert user_response = json_response_and_validate_schema(conn, 200)
246 assert user_response["pleroma"]["background_image"]
247 end
248
249 test "requires 'write:accounts' permission" do
250 token1 = insert(:oauth_token, scopes: ["read"])
251 token2 = insert(:oauth_token, scopes: ["write", "follow"])
252
253 for token <- [token1, token2] do
254 conn =
255 build_conn()
256 |> put_req_header("content-type", "multipart/form-data")
257 |> put_req_header("authorization", "Bearer #{token.token}")
258 |> patch("/api/v1/accounts/update_credentials", %{})
259
260 if token == token1 do
261 assert %{"error" => "Insufficient permissions: write:accounts."} ==
262 json_response_and_validate_schema(conn, 403)
263 else
264 assert json_response_and_validate_schema(conn, 200)
265 end
266 end
267 end
268
269 test "updates profile emojos", %{user: user, conn: conn} do
270 note = "*sips :blank:*"
271 name = "I am :firefox:"
272
273 ret_conn =
274 patch(conn, "/api/v1/accounts/update_credentials", %{
275 "note" => note,
276 "display_name" => name
277 })
278
279 assert json_response_and_validate_schema(ret_conn, 200)
280
281 conn = get(conn, "/api/v1/accounts/#{user.id}")
282
283 assert user_data = json_response_and_validate_schema(conn, 200)
284
285 assert user_data["note"] == note
286 assert user_data["display_name"] == name
287 assert [%{"shortcode" => "blank"}, %{"shortcode" => "firefox"}] = user_data["emojis"]
288 end
289
290 test "update fields", %{conn: conn} do
291 fields = [
292 %{"name" => "<a href=\"http://google.com\">foo</a>", "value" => "<script>bar</script>"},
293 %{"name" => "link.io", "value" => "cofe.io"}
294 ]
295
296 account_data =
297 conn
298 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
299 |> json_response_and_validate_schema(200)
300
301 assert account_data["fields"] == [
302 %{"name" => "<a href=\"http://google.com\">foo</a>", "value" => "bar"},
303 %{
304 "name" => "link.io",
305 "value" => ~S(<a href="http://cofe.io" rel="ugc">cofe.io</a>)
306 }
307 ]
308
309 assert account_data["source"]["fields"] == [
310 %{
311 "name" => "<a href=\"http://google.com\">foo</a>",
312 "value" => "<script>bar</script>"
313 },
314 %{"name" => "link.io", "value" => "cofe.io"}
315 ]
316 end
317
318 test "update fields via x-www-form-urlencoded", %{conn: conn} do
319 fields =
320 [
321 "fields_attributes[1][name]=link",
322 "fields_attributes[1][value]=http://cofe.io",
323 "fields_attributes[0][name]=foo",
324 "fields_attributes[0][value]=bar"
325 ]
326 |> Enum.join("&")
327
328 account =
329 conn
330 |> put_req_header("content-type", "application/x-www-form-urlencoded")
331 |> patch("/api/v1/accounts/update_credentials", fields)
332 |> json_response_and_validate_schema(200)
333
334 assert account["fields"] == [
335 %{"name" => "foo", "value" => "bar"},
336 %{
337 "name" => "link",
338 "value" => ~S(<a href="http://cofe.io" rel="ugc">http://cofe.io</a>)
339 }
340 ]
341
342 assert account["source"]["fields"] == [
343 %{"name" => "foo", "value" => "bar"},
344 %{"name" => "link", "value" => "http://cofe.io"}
345 ]
346 end
347
348 test "update fields with empty name", %{conn: conn} do
349 fields = [
350 %{"name" => "foo", "value" => ""},
351 %{"name" => "", "value" => "bar"}
352 ]
353
354 account =
355 conn
356 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
357 |> json_response_and_validate_schema(200)
358
359 assert account["fields"] == [
360 %{"name" => "foo", "value" => ""}
361 ]
362 end
363
364 test "update fields when invalid request", %{conn: conn} do
365 name_limit = Pleroma.Config.get([:instance, :account_field_name_length])
366 value_limit = Pleroma.Config.get([:instance, :account_field_value_length])
367
368 long_name = Enum.map(0..name_limit, fn _ -> "x" end) |> Enum.join()
369 long_value = Enum.map(0..value_limit, fn _ -> "x" end) |> Enum.join()
370
371 fields = [%{"name" => "foo", "value" => long_value}]
372
373 assert %{"error" => "Invalid request"} ==
374 conn
375 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
376 |> json_response_and_validate_schema(403)
377
378 fields = [%{"name" => long_name, "value" => "bar"}]
379
380 assert %{"error" => "Invalid request"} ==
381 conn
382 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
383 |> json_response_and_validate_schema(403)
384
385 Pleroma.Config.put([:instance, :max_account_fields], 1)
386
387 fields = [
388 %{"name" => "foo", "value" => "bar"},
389 %{"name" => "link", "value" => "cofe.io"}
390 ]
391
392 assert %{"error" => "Invalid request"} ==
393 conn
394 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
395 |> json_response_and_validate_schema(403)
396 end
397 end
398 end