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