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