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