d5d7236a0ffade608d4c54f80cc0aa95d0773f9b
[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 })
38
39 expected = %{
40 id: to_string(user.id),
41 username: "shp",
42 acct: user.nickname,
43 display_name: user.name,
44 locked: false,
45 created_at: "2017-08-15T15:47:06.000Z",
46 followers_count: 3,
47 following_count: 0,
48 statuses_count: 5,
49 note: "<span>valid html</span>. a<br/>b<br/>c<br/>d<br/>f &#39;&amp;&lt;&gt;&quot;",
50 url: user.ap_id,
51 avatar: "http://localhost:4001/images/avi.png",
52 avatar_static: "http://localhost:4001/images/avi.png",
53 header: "http://localhost:4001/images/banner.png",
54 header_static: "http://localhost:4001/images/banner.png",
55 emojis: [
56 %{
57 "static_url" => "/file.png",
58 "url" => "/file.png",
59 "shortcode" => "karjalanpiirakka",
60 "visible_in_picker" => false
61 }
62 ],
63 fields: [],
64 bot: false,
65 source: %{
66 note: "valid html. a\nb\nc\nd\nf '&<>\"",
67 sensitive: false,
68 pleroma: %{
69 actor_type: "Person",
70 discoverable: false
71 },
72 fields: []
73 },
74 pleroma: %{
75 ap_id: user.ap_id,
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 ap_id: user.ap_id,
153 background_image: nil,
154 confirmation_pending: false,
155 tags: [],
156 is_admin: false,
157 is_moderator: false,
158 hide_favorites: true,
159 hide_followers: false,
160 hide_follows: false,
161 hide_followers_count: false,
162 hide_follows_count: false,
163 relationship: %{},
164 skip_thread_containment: false
165 }
166 }
167
168 assert expected == AccountView.render("show.json", %{user: user})
169 end
170
171 test "Represent a Funkwhale channel" do
172 {:ok, user} =
173 User.get_or_fetch_by_ap_id(
174 "https://channels.tests.funkwhale.audio/federation/actors/compositions"
175 )
176
177 assert represented = AccountView.render("show.json", %{user: user})
178 assert represented.acct == "compositions@channels.tests.funkwhale.audio"
179 assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
180 end
181
182 test "Represent a deactivated user for an admin" do
183 admin = insert(:user, is_admin: true)
184 deactivated_user = insert(:user, deactivated: true)
185 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
186 assert represented[:pleroma][:deactivated] == true
187 end
188
189 test "Represent a smaller mention" do
190 user = insert(:user)
191
192 expected = %{
193 id: to_string(user.id),
194 acct: user.nickname,
195 username: user.nickname,
196 url: user.ap_id
197 }
198
199 assert expected == AccountView.render("mention.json", %{user: user})
200 end
201
202 describe "relationship" do
203 defp test_relationship_rendering(user, other_user, expected_result) do
204 opts = %{user: user, target: other_user, relationships: nil}
205 assert expected_result == AccountView.render("relationship.json", opts)
206
207 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
208 opts = Map.put(opts, :relationships, relationships_opt)
209 assert expected_result == AccountView.render("relationship.json", opts)
210
211 assert [expected_result] ==
212 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
213 end
214
215 @blank_response %{
216 following: false,
217 followed_by: false,
218 blocking: false,
219 blocked_by: false,
220 muting: false,
221 muting_notifications: false,
222 subscribing: false,
223 requested: false,
224 domain_blocking: false,
225 showing_reblogs: true,
226 endorsed: false
227 }
228
229 test "represent a relationship for the following and followed user" do
230 user = insert(:user)
231 other_user = insert(:user)
232
233 {:ok, user} = User.follow(user, other_user)
234 {:ok, other_user} = User.follow(other_user, user)
235 {:ok, _subscription} = User.subscribe(user, other_user)
236 {:ok, _user_relationships} = User.mute(user, other_user, true)
237 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
238
239 expected =
240 Map.merge(
241 @blank_response,
242 %{
243 following: true,
244 followed_by: true,
245 muting: true,
246 muting_notifications: true,
247 subscribing: true,
248 showing_reblogs: false,
249 id: to_string(other_user.id)
250 }
251 )
252
253 test_relationship_rendering(user, other_user, expected)
254 end
255
256 test "represent a relationship for the blocking and blocked user" do
257 user = insert(:user)
258 other_user = insert(:user)
259
260 {:ok, user} = User.follow(user, other_user)
261 {:ok, _subscription} = User.subscribe(user, other_user)
262 {:ok, _user_relationship} = User.block(user, other_user)
263 {:ok, _user_relationship} = User.block(other_user, user)
264
265 expected =
266 Map.merge(
267 @blank_response,
268 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
269 )
270
271 test_relationship_rendering(user, other_user, expected)
272 end
273
274 test "represent a relationship for the user blocking a domain" do
275 user = insert(:user)
276 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
277
278 {:ok, user} = User.block_domain(user, "bad.site")
279
280 expected =
281 Map.merge(
282 @blank_response,
283 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
284 )
285
286 test_relationship_rendering(user, other_user, expected)
287 end
288
289 test "represent a relationship for the user with a pending follow request" do
290 user = insert(:user)
291 other_user = insert(:user, locked: true)
292
293 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
294 user = User.get_cached_by_id(user.id)
295 other_user = User.get_cached_by_id(other_user.id)
296
297 expected =
298 Map.merge(
299 @blank_response,
300 %{requested: true, following: false, id: to_string(other_user.id)}
301 )
302
303 test_relationship_rendering(user, other_user, expected)
304 end
305 end
306
307 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
308 user = insert(:user, pleroma_settings_store: %{fe: "test"})
309
310 result =
311 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
312
313 assert result.pleroma.settings_store == %{:fe => "test"}
314
315 result = AccountView.render("show.json", %{user: user, with_pleroma_settings: true})
316 assert result.pleroma[:settings_store] == nil
317
318 result = AccountView.render("show.json", %{user: user, for: user})
319 assert result.pleroma[:settings_store] == nil
320 end
321
322 test "doesn't sanitize display names" do
323 user = insert(:user, name: "<marquee> username </marquee>")
324 result = AccountView.render("show.json", %{user: user})
325 assert result.display_name == "<marquee> username </marquee>"
326 end
327
328 test "never display nil user follow counts" do
329 user = insert(:user, following_count: 0, follower_count: 0)
330 result = AccountView.render("show.json", %{user: user})
331
332 assert result.following_count == 0
333 assert result.followers_count == 0
334 end
335
336 describe "hiding follows/following" do
337 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
338 user =
339 insert(:user, %{
340 hide_followers: true,
341 hide_followers_count: true,
342 hide_follows: true,
343 hide_follows_count: true
344 })
345
346 other_user = insert(:user)
347 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
348 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
349
350 assert %{
351 followers_count: 0,
352 following_count: 0,
353 pleroma: %{hide_follows_count: true, hide_followers_count: true}
354 } = AccountView.render("show.json", %{user: user})
355 end
356
357 test "shows when follows/followers are hidden" do
358 user = insert(:user, hide_followers: true, hide_follows: true)
359 other_user = insert(:user)
360 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
361 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
362
363 assert %{
364 followers_count: 1,
365 following_count: 1,
366 pleroma: %{hide_follows: true, hide_followers: true}
367 } = AccountView.render("show.json", %{user: user})
368 end
369
370 test "shows actual follower/following count to the account owner" do
371 user = insert(:user, hide_followers: true, hide_follows: true)
372 other_user = insert(:user)
373 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
374 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
375
376 assert %{
377 followers_count: 1,
378 following_count: 1
379 } = AccountView.render("show.json", %{user: user, for: user})
380 end
381
382 test "shows unread_conversation_count only to the account owner" do
383 user = insert(:user)
384 other_user = insert(:user)
385
386 {:ok, _activity} =
387 CommonAPI.post(other_user, %{
388 status: "Hey @#{user.nickname}.",
389 visibility: "direct"
390 })
391
392 user = User.get_cached_by_ap_id(user.ap_id)
393
394 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
395 :unread_conversation_count
396 ] == nil
397
398 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
399 :unread_conversation_count
400 ] == 1
401 end
402
403 test "shows unread_count only to the account owner" do
404 user = insert(:user)
405 insert_list(7, :notification, user: user)
406 other_user = insert(:user)
407
408 user = User.get_cached_by_ap_id(user.ap_id)
409
410 assert AccountView.render(
411 "show.json",
412 %{user: user, for: other_user}
413 )[:pleroma][:unread_notifications_count] == nil
414
415 assert AccountView.render(
416 "show.json",
417 %{user: user, for: user}
418 )[:pleroma][:unread_notifications_count] == 7
419 end
420 end
421
422 describe "follow requests counter" do
423 test "shows zero when no follow requests are pending" do
424 user = insert(:user)
425
426 assert %{follow_requests_count: 0} =
427 AccountView.render("show.json", %{user: user, for: user})
428
429 other_user = insert(:user)
430 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
431
432 assert %{follow_requests_count: 0} =
433 AccountView.render("show.json", %{user: user, for: user})
434 end
435
436 test "shows non-zero when follow requests are pending" do
437 user = insert(:user, locked: true)
438
439 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
440
441 other_user = insert(:user)
442 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
443
444 assert %{locked: true, follow_requests_count: 1} =
445 AccountView.render("show.json", %{user: user, for: user})
446 end
447
448 test "decreases when accepting a follow request" do
449 user = insert(:user, locked: true)
450
451 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
452
453 other_user = insert(:user)
454 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
455
456 assert %{locked: true, follow_requests_count: 1} =
457 AccountView.render("show.json", %{user: user, for: user})
458
459 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
460
461 assert %{locked: true, follow_requests_count: 0} =
462 AccountView.render("show.json", %{user: user, for: user})
463 end
464
465 test "decreases when rejecting a follow request" do
466 user = insert(:user, locked: true)
467
468 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
469
470 other_user = insert(:user)
471 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
472
473 assert %{locked: true, follow_requests_count: 1} =
474 AccountView.render("show.json", %{user: user, for: user})
475
476 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
477
478 assert %{locked: true, follow_requests_count: 0} =
479 AccountView.render("show.json", %{user: user, for: user})
480 end
481
482 test "shows non-zero when historical unapproved requests are present" do
483 user = insert(:user, locked: true)
484
485 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
486
487 other_user = insert(:user)
488 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
489
490 {:ok, user} = User.update_and_set_cache(user, %{locked: false})
491
492 assert %{locked: false, follow_requests_count: 1} =
493 AccountView.render("show.json", %{user: user, for: user})
494 end
495 end
496 end