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