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