User: Add raw_bio, storing unformatted bio
[akkoma] / test / web / mastodon_api / views / account_view_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.AccountViewTest do
6 use Pleroma.DataCase
7
8 alias Pleroma.User
9 alias Pleroma.UserRelationship
10 alias Pleroma.Web.CommonAPI
11 alias Pleroma.Web.MastodonAPI.AccountView
12
13 import Pleroma.Factory
14 import Tesla.Mock
15
16 setup do
17 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
18 :ok
19 end
20
21 test "Represent a user account" do
22 background_image = %{
23 "url" => [%{"href" => "https://example.com/images/asuka_hospital.png"}]
24 }
25
26 user =
27 insert(:user, %{
28 follower_count: 3,
29 note_count: 5,
30 background: background_image,
31 nickname: "shp@shitposter.club",
32 name: ":karjalanpiirakka: shp",
33 bio:
34 "<script src=\"invalid-html\"></script><span>valid html</span>. a<br>b<br/>c<br >d<br />f '&<>\"",
35 inserted_at: ~N[2017-08-15 15:47:06.597036],
36 emoji: %{"karjalanpiirakka" => "/file.png"},
37 raw_bio: "valid html. a\nb\nc\nd\nf '&<>\""
38 })
39
40 expected = %{
41 id: to_string(user.id),
42 username: "shp",
43 acct: user.nickname,
44 display_name: user.name,
45 locked: false,
46 created_at: "2017-08-15T15:47:06.000Z",
47 followers_count: 3,
48 following_count: 0,
49 statuses_count: 5,
50 note: "<span>valid html</span>. a<br/>b<br/>c<br/>d<br/>f &#39;&amp;&lt;&gt;&quot;",
51 url: user.ap_id,
52 avatar: "http://localhost:4001/images/avi.png",
53 avatar_static: "http://localhost:4001/images/avi.png",
54 header: "http://localhost:4001/images/banner.png",
55 header_static: "http://localhost:4001/images/banner.png",
56 emojis: [
57 %{
58 static_url: "/file.png",
59 url: "/file.png",
60 shortcode: "karjalanpiirakka",
61 visible_in_picker: false
62 }
63 ],
64 fields: [],
65 bot: false,
66 source: %{
67 note: "valid html. a\nb\nc\nd\nf '&<>\"",
68 sensitive: false,
69 pleroma: %{
70 actor_type: "Person",
71 discoverable: false
72 },
73 fields: []
74 },
75 pleroma: %{
76 background_image: "https://example.com/images/asuka_hospital.png",
77 confirmation_pending: false,
78 tags: [],
79 is_admin: false,
80 is_moderator: false,
81 hide_favorites: true,
82 hide_followers: false,
83 hide_follows: false,
84 hide_followers_count: false,
85 hide_follows_count: false,
86 relationship: %{},
87 skip_thread_containment: false
88 }
89 }
90
91 assert expected == AccountView.render("show.json", %{user: user})
92 end
93
94 test "Represent the user account for the account owner" do
95 user = insert(:user)
96
97 notification_settings = %{
98 followers: true,
99 follows: true,
100 non_followers: true,
101 non_follows: true,
102 privacy_option: false
103 }
104
105 privacy = user.default_scope
106
107 assert %{
108 pleroma: %{notification_settings: ^notification_settings, allow_following_move: true},
109 source: %{privacy: ^privacy}
110 } = AccountView.render("show.json", %{user: user, for: user})
111 end
112
113 test "Represent a Service(bot) account" do
114 user =
115 insert(:user, %{
116 follower_count: 3,
117 note_count: 5,
118 actor_type: "Service",
119 nickname: "shp@shitposter.club",
120 inserted_at: ~N[2017-08-15 15:47:06.597036]
121 })
122
123 expected = %{
124 id: to_string(user.id),
125 username: "shp",
126 acct: user.nickname,
127 display_name: user.name,
128 locked: false,
129 created_at: "2017-08-15T15:47:06.000Z",
130 followers_count: 3,
131 following_count: 0,
132 statuses_count: 5,
133 note: user.bio,
134 url: user.ap_id,
135 avatar: "http://localhost:4001/images/avi.png",
136 avatar_static: "http://localhost:4001/images/avi.png",
137 header: "http://localhost:4001/images/banner.png",
138 header_static: "http://localhost:4001/images/banner.png",
139 emojis: [],
140 fields: [],
141 bot: true,
142 source: %{
143 note: user.bio,
144 sensitive: false,
145 pleroma: %{
146 actor_type: "Service",
147 discoverable: false
148 },
149 fields: []
150 },
151 pleroma: %{
152 background_image: nil,
153 confirmation_pending: false,
154 tags: [],
155 is_admin: false,
156 is_moderator: false,
157 hide_favorites: true,
158 hide_followers: false,
159 hide_follows: false,
160 hide_followers_count: false,
161 hide_follows_count: false,
162 relationship: %{},
163 skip_thread_containment: false
164 }
165 }
166
167 assert expected == AccountView.render("show.json", %{user: user})
168 end
169
170 test "Represent a Funkwhale channel" do
171 {:ok, user} =
172 User.get_or_fetch_by_ap_id(
173 "https://channels.tests.funkwhale.audio/federation/actors/compositions"
174 )
175
176 assert represented = AccountView.render("show.json", %{user: user})
177 assert represented.acct == "compositions@channels.tests.funkwhale.audio"
178 assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
179 end
180
181 test "Represent a deactivated user for an admin" do
182 admin = insert(:user, is_admin: true)
183 deactivated_user = insert(:user, deactivated: true)
184 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
185 assert represented[:pleroma][:deactivated] == true
186 end
187
188 test "Represent a smaller mention" do
189 user = insert(:user)
190
191 expected = %{
192 id: to_string(user.id),
193 acct: user.nickname,
194 username: user.nickname,
195 url: user.ap_id
196 }
197
198 assert expected == AccountView.render("mention.json", %{user: user})
199 end
200
201 describe "relationship" do
202 defp test_relationship_rendering(user, other_user, expected_result) do
203 opts = %{user: user, target: other_user, relationships: nil}
204 assert expected_result == AccountView.render("relationship.json", opts)
205
206 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
207 opts = Map.put(opts, :relationships, relationships_opt)
208 assert expected_result == AccountView.render("relationship.json", opts)
209
210 assert [expected_result] ==
211 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
212 end
213
214 @blank_response %{
215 following: false,
216 followed_by: false,
217 blocking: false,
218 blocked_by: false,
219 muting: false,
220 muting_notifications: false,
221 subscribing: false,
222 requested: false,
223 domain_blocking: false,
224 showing_reblogs: true,
225 endorsed: false
226 }
227
228 test "represent a relationship for the following and followed user" do
229 user = insert(:user)
230 other_user = insert(:user)
231
232 {:ok, user} = User.follow(user, other_user)
233 {:ok, other_user} = User.follow(other_user, user)
234 {:ok, _subscription} = User.subscribe(user, other_user)
235 {:ok, _user_relationships} = User.mute(user, other_user, true)
236 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
237
238 expected =
239 Map.merge(
240 @blank_response,
241 %{
242 following: true,
243 followed_by: true,
244 muting: true,
245 muting_notifications: true,
246 subscribing: true,
247 showing_reblogs: false,
248 id: to_string(other_user.id)
249 }
250 )
251
252 test_relationship_rendering(user, other_user, expected)
253 end
254
255 test "represent a relationship for the blocking and blocked user" do
256 user = insert(:user)
257 other_user = insert(:user)
258
259 {:ok, user} = User.follow(user, other_user)
260 {:ok, _subscription} = User.subscribe(user, other_user)
261 {:ok, _user_relationship} = User.block(user, other_user)
262 {:ok, _user_relationship} = User.block(other_user, user)
263
264 expected =
265 Map.merge(
266 @blank_response,
267 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
268 )
269
270 test_relationship_rendering(user, other_user, expected)
271 end
272
273 test "represent a relationship for the user blocking a domain" do
274 user = insert(:user)
275 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
276
277 {:ok, user} = User.block_domain(user, "bad.site")
278
279 expected =
280 Map.merge(
281 @blank_response,
282 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
283 )
284
285 test_relationship_rendering(user, other_user, expected)
286 end
287
288 test "represent a relationship for the user with a pending follow request" do
289 user = insert(:user)
290 other_user = insert(:user, locked: true)
291
292 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
293 user = User.get_cached_by_id(user.id)
294 other_user = User.get_cached_by_id(other_user.id)
295
296 expected =
297 Map.merge(
298 @blank_response,
299 %{requested: true, following: false, id: to_string(other_user.id)}
300 )
301
302 test_relationship_rendering(user, other_user, expected)
303 end
304 end
305
306 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
307 user = insert(:user, pleroma_settings_store: %{fe: "test"})
308
309 result =
310 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
311
312 assert result.pleroma.settings_store == %{:fe => "test"}
313
314 result = AccountView.render("show.json", %{user: user, with_pleroma_settings: true})
315 assert result.pleroma[:settings_store] == nil
316
317 result = AccountView.render("show.json", %{user: user, for: user})
318 assert result.pleroma[:settings_store] == nil
319 end
320
321 test "doesn't sanitize display names" do
322 user = insert(:user, name: "<marquee> username </marquee>")
323 result = AccountView.render("show.json", %{user: user})
324 assert result.display_name == "<marquee> username </marquee>"
325 end
326
327 test "never display nil user follow counts" do
328 user = insert(:user, following_count: 0, follower_count: 0)
329 result = AccountView.render("show.json", %{user: user})
330
331 assert result.following_count == 0
332 assert result.followers_count == 0
333 end
334
335 describe "hiding follows/following" do
336 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
337 user =
338 insert(:user, %{
339 hide_followers: true,
340 hide_followers_count: true,
341 hide_follows: true,
342 hide_follows_count: true
343 })
344
345 other_user = insert(:user)
346 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
347 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
348
349 assert %{
350 followers_count: 0,
351 following_count: 0,
352 pleroma: %{hide_follows_count: true, hide_followers_count: true}
353 } = AccountView.render("show.json", %{user: user})
354 end
355
356 test "shows when follows/followers are hidden" do
357 user = insert(:user, hide_followers: true, hide_follows: true)
358 other_user = insert(:user)
359 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
360 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
361
362 assert %{
363 followers_count: 1,
364 following_count: 1,
365 pleroma: %{hide_follows: true, hide_followers: true}
366 } = AccountView.render("show.json", %{user: user})
367 end
368
369 test "shows actual follower/following count to the account owner" do
370 user = insert(:user, hide_followers: true, hide_follows: true)
371 other_user = insert(:user)
372 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
373 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
374
375 assert %{
376 followers_count: 1,
377 following_count: 1
378 } = AccountView.render("show.json", %{user: user, for: user})
379 end
380
381 test "shows unread_conversation_count only to the account owner" do
382 user = insert(:user)
383 other_user = insert(:user)
384
385 {:ok, _activity} =
386 CommonAPI.post(other_user, %{
387 status: "Hey @#{user.nickname}.",
388 visibility: "direct"
389 })
390
391 user = User.get_cached_by_ap_id(user.ap_id)
392
393 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
394 :unread_conversation_count
395 ] == nil
396
397 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
398 :unread_conversation_count
399 ] == 1
400 end
401
402 test "shows unread_count only to the account owner" do
403 user = insert(:user)
404 insert_list(7, :notification, user: user)
405 other_user = insert(:user)
406
407 user = User.get_cached_by_ap_id(user.ap_id)
408
409 assert AccountView.render(
410 "show.json",
411 %{user: user, for: other_user}
412 )[:pleroma][:unread_notifications_count] == nil
413
414 assert AccountView.render(
415 "show.json",
416 %{user: user, for: user}
417 )[:pleroma][:unread_notifications_count] == 7
418 end
419 end
420
421 describe "follow requests counter" do
422 test "shows zero when no follow requests are pending" do
423 user = insert(:user)
424
425 assert %{follow_requests_count: 0} =
426 AccountView.render("show.json", %{user: user, for: user})
427
428 other_user = insert(:user)
429 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
430
431 assert %{follow_requests_count: 0} =
432 AccountView.render("show.json", %{user: user, for: user})
433 end
434
435 test "shows non-zero when follow requests are pending" do
436 user = insert(:user, locked: true)
437
438 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
439
440 other_user = insert(:user)
441 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
442
443 assert %{locked: true, follow_requests_count: 1} =
444 AccountView.render("show.json", %{user: user, for: user})
445 end
446
447 test "decreases when accepting a follow request" do
448 user = insert(:user, locked: true)
449
450 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
451
452 other_user = insert(:user)
453 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
454
455 assert %{locked: true, follow_requests_count: 1} =
456 AccountView.render("show.json", %{user: user, for: user})
457
458 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
459
460 assert %{locked: true, follow_requests_count: 0} =
461 AccountView.render("show.json", %{user: user, for: user})
462 end
463
464 test "decreases when rejecting a follow request" do
465 user = insert(:user, locked: true)
466
467 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
468
469 other_user = insert(:user)
470 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
471
472 assert %{locked: true, follow_requests_count: 1} =
473 AccountView.render("show.json", %{user: user, for: user})
474
475 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
476
477 assert %{locked: true, follow_requests_count: 0} =
478 AccountView.render("show.json", %{user: user, for: user})
479 end
480
481 test "shows non-zero when historical unapproved requests are present" do
482 user = insert(:user, locked: true)
483
484 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
485
486 other_user = insert(:user)
487 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
488
489 {:ok, user} = User.update_and_set_cache(user, %{locked: false})
490
491 assert %{locked: false, follow_requests_count: 1} =
492 AccountView.render("show.json", %{user: user, for: user})
493 end
494 end
495
496 test "uses mediaproxy urls when it's enabled" do
497 clear_config([:media_proxy, :enabled], true)
498
499 user =
500 insert(:user,
501 avatar: %{"url" => [%{"href" => "https://evil.website/avatar.png"}]},
502 banner: %{"url" => [%{"href" => "https://evil.website/banner.png"}]},
503 emoji: %{"joker_smile" => "https://evil.website/society.png"}
504 )
505
506 AccountView.render("show.json", %{user: user})
507 |> Enum.all?(fn
508 {key, url} when key in [:avatar, :avatar_static, :header, :header_static] ->
509 String.starts_with?(url, Pleroma.Web.base_url())
510
511 {:emojis, emojis} ->
512 Enum.all?(emojis, fn %{url: url, static_url: static_url} ->
513 String.starts_with?(url, Pleroma.Web.base_url()) &&
514 String.starts_with?(static_url, Pleroma.Web.base_url())
515 end)
516
517 _ ->
518 true
519 end)
520 |> assert()
521 end
522 end