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