Merge branch 'develop' into test/activity_pub/transmogrifier.ex
[akkoma] / test / web / mastodon_api / views / account_view_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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 import Pleroma.Factory
8 alias Pleroma.User
9 alias Pleroma.Web.CommonAPI
10 alias Pleroma.Web.MastodonAPI.AccountView
11
12 test "Represent a user account" do
13 source_data = %{
14 "tag" => [
15 %{
16 "type" => "Emoji",
17 "icon" => %{"url" => "/file.png"},
18 "name" => ":karjalanpiirakka:"
19 }
20 ]
21 }
22
23 background_image = %{
24 "url" => [%{"href" => "https://example.com/images/asuka_hospital.png"}]
25 }
26
27 user =
28 insert(:user, %{
29 info: %{
30 note_count: 5,
31 follower_count: 3,
32 source_data: source_data,
33 background: background_image
34 },
35 nickname: "shp@shitposter.club",
36 name: ":karjalanpiirakka: shp",
37 bio: "<script src=\"invalid-html\"></script><span>valid html</span>",
38 inserted_at: ~N[2017-08-15 15:47:06.597036]
39 })
40
41 expected = %{
42 id: to_string(user.id),
43 username: "shp",
44 acct: user.nickname,
45 display_name: user.name,
46 locked: false,
47 created_at: "2017-08-15T15:47:06.000Z",
48 followers_count: 3,
49 following_count: 0,
50 statuses_count: 5,
51 note: "<span>valid html</span>",
52 url: user.ap_id,
53 avatar: "http://localhost:4001/images/avi.png",
54 avatar_static: "http://localhost:4001/images/avi.png",
55 header: "http://localhost:4001/images/banner.png",
56 header_static: "http://localhost:4001/images/banner.png",
57 emojis: [
58 %{
59 "static_url" => "/file.png",
60 "url" => "/file.png",
61 "shortcode" => "karjalanpiirakka",
62 "visible_in_picker" => false
63 }
64 ],
65 fields: [],
66 bot: false,
67 source: %{
68 note: "valid html",
69 sensitive: false,
70 pleroma: %{},
71 fields: []
72 },
73 pleroma: %{
74 background_image: "https://example.com/images/asuka_hospital.png",
75 confirmation_pending: false,
76 tags: [],
77 is_admin: false,
78 is_moderator: false,
79 hide_favorites: true,
80 hide_followers: false,
81 hide_follows: false,
82 hide_followers_count: false,
83 hide_follows_count: false,
84 relationship: %{},
85 skip_thread_containment: false
86 }
87 }
88
89 assert expected == AccountView.render("account.json", %{user: user})
90 end
91
92 test "Represent the user account for the account owner" do
93 user = insert(:user)
94
95 notification_settings = %{
96 "followers" => true,
97 "follows" => true,
98 "non_follows" => true,
99 "non_followers" => true
100 }
101
102 privacy = user.info.default_scope
103
104 assert %{
105 pleroma: %{notification_settings: ^notification_settings},
106 source: %{privacy: ^privacy}
107 } = AccountView.render("account.json", %{user: user, for: user})
108 end
109
110 test "Represent a Service(bot) account" do
111 user =
112 insert(:user, %{
113 info: %{note_count: 5, follower_count: 3, source_data: %{"type" => "Service"}},
114 nickname: "shp@shitposter.club",
115 inserted_at: ~N[2017-08-15 15:47:06.597036]
116 })
117
118 expected = %{
119 id: to_string(user.id),
120 username: "shp",
121 acct: user.nickname,
122 display_name: user.name,
123 locked: false,
124 created_at: "2017-08-15T15:47:06.000Z",
125 followers_count: 3,
126 following_count: 0,
127 statuses_count: 5,
128 note: user.bio,
129 url: user.ap_id,
130 avatar: "http://localhost:4001/images/avi.png",
131 avatar_static: "http://localhost:4001/images/avi.png",
132 header: "http://localhost:4001/images/banner.png",
133 header_static: "http://localhost:4001/images/banner.png",
134 emojis: [],
135 fields: [],
136 bot: true,
137 source: %{
138 note: user.bio,
139 sensitive: false,
140 pleroma: %{},
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("account.json", %{user: user})
160 end
161
162 test "Represent a deactivated user for an admin" do
163 admin = insert(:user, %{info: %{is_admin: true}})
164 deactivated_user = insert(:user, %{info: %{deactivated: true}})
165 represented = AccountView.render("account.json", %{user: deactivated_user, for: admin})
166 assert represented[:pleroma][:deactivated] == true
167 end
168
169 test "Represent a smaller mention" do
170 user = insert(:user)
171
172 expected = %{
173 id: to_string(user.id),
174 acct: user.nickname,
175 username: user.nickname,
176 url: user.ap_id
177 }
178
179 assert expected == AccountView.render("mention.json", %{user: user})
180 end
181
182 describe "relationship" do
183 test "represent a relationship for the following and followed user" do
184 user = insert(:user)
185 other_user = insert(:user)
186
187 {:ok, user} = User.follow(user, other_user)
188 {:ok, other_user} = User.follow(other_user, user)
189 {:ok, other_user} = User.subscribe(user, other_user)
190 {:ok, user} = User.mute(user, other_user, true)
191 {:ok, user} = CommonAPI.hide_reblogs(user, other_user)
192
193 expected = %{
194 id: to_string(other_user.id),
195 following: true,
196 followed_by: true,
197 blocking: false,
198 blocked_by: false,
199 muting: true,
200 muting_notifications: true,
201 subscribing: true,
202 requested: false,
203 domain_blocking: false,
204 showing_reblogs: false,
205 endorsed: false
206 }
207
208 assert expected ==
209 AccountView.render("relationship.json", %{user: user, target: other_user})
210 end
211
212 test "represent a relationship for the blocking and blocked user" do
213 user = insert(:user)
214 other_user = insert(:user)
215
216 {:ok, user} = User.follow(user, other_user)
217 {:ok, other_user} = User.subscribe(user, other_user)
218 {:ok, user} = User.block(user, other_user)
219 {:ok, other_user} = User.block(other_user, user)
220
221 expected = %{
222 id: to_string(other_user.id),
223 following: false,
224 followed_by: false,
225 blocking: true,
226 blocked_by: true,
227 muting: false,
228 muting_notifications: false,
229 subscribing: false,
230 requested: false,
231 domain_blocking: false,
232 showing_reblogs: true,
233 endorsed: false
234 }
235
236 assert expected ==
237 AccountView.render("relationship.json", %{user: user, target: other_user})
238 end
239
240 test "represent a relationship for the user blocking a domain" do
241 user = insert(:user)
242 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
243
244 {:ok, user} = User.block_domain(user, "bad.site")
245
246 assert %{domain_blocking: true, blocking: false} =
247 AccountView.render("relationship.json", %{user: user, target: other_user})
248 end
249
250 test "represent a relationship for the user with a pending follow request" do
251 user = insert(:user)
252 other_user = insert(:user, %{info: %User.Info{locked: true}})
253
254 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
255 user = User.get_cached_by_id(user.id)
256 other_user = User.get_cached_by_id(other_user.id)
257
258 expected = %{
259 id: to_string(other_user.id),
260 following: false,
261 followed_by: false,
262 blocking: false,
263 blocked_by: false,
264 muting: false,
265 muting_notifications: false,
266 subscribing: false,
267 requested: true,
268 domain_blocking: false,
269 showing_reblogs: true,
270 endorsed: false
271 }
272
273 assert expected ==
274 AccountView.render("relationship.json", %{user: user, target: other_user})
275 end
276 end
277
278 test "represent an embedded relationship" do
279 user =
280 insert(:user, %{
281 info: %{note_count: 5, follower_count: 0, source_data: %{"type" => "Service"}},
282 nickname: "shp@shitposter.club",
283 inserted_at: ~N[2017-08-15 15:47:06.597036]
284 })
285
286 other_user = insert(:user)
287 {:ok, other_user} = User.follow(other_user, user)
288 {:ok, other_user} = User.block(other_user, user)
289 {:ok, _} = User.follow(insert(:user), user)
290
291 expected = %{
292 id: to_string(user.id),
293 username: "shp",
294 acct: user.nickname,
295 display_name: user.name,
296 locked: false,
297 created_at: "2017-08-15T15:47:06.000Z",
298 followers_count: 1,
299 following_count: 0,
300 statuses_count: 5,
301 note: user.bio,
302 url: user.ap_id,
303 avatar: "http://localhost:4001/images/avi.png",
304 avatar_static: "http://localhost:4001/images/avi.png",
305 header: "http://localhost:4001/images/banner.png",
306 header_static: "http://localhost:4001/images/banner.png",
307 emojis: [],
308 fields: [],
309 bot: true,
310 source: %{
311 note: user.bio,
312 sensitive: false,
313 pleroma: %{},
314 fields: []
315 },
316 pleroma: %{
317 background_image: nil,
318 confirmation_pending: false,
319 tags: [],
320 is_admin: false,
321 is_moderator: false,
322 hide_favorites: true,
323 hide_followers: false,
324 hide_follows: false,
325 hide_followers_count: false,
326 hide_follows_count: false,
327 relationship: %{
328 id: to_string(user.id),
329 following: false,
330 followed_by: false,
331 blocking: true,
332 blocked_by: false,
333 subscribing: false,
334 muting: false,
335 muting_notifications: false,
336 requested: false,
337 domain_blocking: false,
338 showing_reblogs: true,
339 endorsed: false
340 },
341 skip_thread_containment: false
342 }
343 }
344
345 assert expected == AccountView.render("account.json", %{user: user, for: other_user})
346 end
347
348 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
349 user = insert(:user, %{info: %User.Info{pleroma_settings_store: %{fe: "test"}}})
350
351 result =
352 AccountView.render("account.json", %{user: user, for: user, with_pleroma_settings: true})
353
354 assert result.pleroma.settings_store == %{:fe => "test"}
355
356 result = AccountView.render("account.json", %{user: user, with_pleroma_settings: true})
357 assert result.pleroma[:settings_store] == nil
358
359 result = AccountView.render("account.json", %{user: user, for: user})
360 assert result.pleroma[:settings_store] == nil
361 end
362
363 test "sanitizes display names" do
364 user = insert(:user, name: "<marquee> username </marquee>")
365 result = AccountView.render("account.json", %{user: user})
366 refute result.display_name == "<marquee> username </marquee>"
367 end
368
369 describe "hiding follows/following" do
370 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
371 info = %{
372 hide_followers: true,
373 hide_followers_count: true,
374 hide_follows: true,
375 hide_follows_count: true
376 }
377
378 user = insert(:user, info: info)
379
380 other_user = insert(:user)
381 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
382 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
383
384 assert %{
385 followers_count: 0,
386 following_count: 0,
387 pleroma: %{hide_follows_count: true, hide_followers_count: true}
388 } = AccountView.render("account.json", %{user: user})
389 end
390
391 test "shows when follows/followers are hidden" do
392 user = insert(:user, info: %{hide_followers: true, hide_follows: true})
393 other_user = insert(:user)
394 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
395 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
396
397 assert %{
398 followers_count: 1,
399 following_count: 1,
400 pleroma: %{hide_follows: true, hide_followers: true}
401 } = AccountView.render("account.json", %{user: user})
402 end
403
404 test "shows actual follower/following count to the account owner" do
405 user = insert(:user, info: %{hide_followers: true, hide_follows: true})
406 other_user = insert(:user)
407 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
408 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
409
410 assert %{
411 followers_count: 1,
412 following_count: 1
413 } = AccountView.render("account.json", %{user: user, for: user})
414 end
415 end
416 end