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