fix formatting
[akkoma] / test / web / mastodon_api / views / account_view_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 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 relationship: %{},
83 skip_thread_containment: false
84 }
85 }
86
87 assert expected == AccountView.render("account.json", %{user: user})
88 end
89
90 test "Represent the user account for the account owner" do
91 user = insert(:user)
92
93 notification_settings = %{
94 "followers" => true,
95 "follows" => true,
96 "non_follows" => true,
97 "non_followers" => true
98 }
99
100 privacy = user.info.default_scope
101
102 assert %{
103 pleroma: %{notification_settings: ^notification_settings},
104 source: %{privacy: ^privacy}
105 } = AccountView.render("account.json", %{user: user, for: user})
106 end
107
108 test "Represent a Service(bot) account" do
109 user =
110 insert(:user, %{
111 info: %{note_count: 5, follower_count: 3, source_data: %{"type" => "Service"}},
112 nickname: "shp@shitposter.club",
113 inserted_at: ~N[2017-08-15 15:47:06.597036]
114 })
115
116 expected = %{
117 id: to_string(user.id),
118 username: "shp",
119 acct: user.nickname,
120 display_name: user.name,
121 locked: false,
122 created_at: "2017-08-15T15:47:06.000Z",
123 followers_count: 3,
124 following_count: 0,
125 statuses_count: 5,
126 note: user.bio,
127 url: user.ap_id,
128 avatar: "http://localhost:4001/images/avi.png",
129 avatar_static: "http://localhost:4001/images/avi.png",
130 header: "http://localhost:4001/images/banner.png",
131 header_static: "http://localhost:4001/images/banner.png",
132 emojis: [],
133 fields: [],
134 bot: true,
135 source: %{
136 note: user.bio,
137 sensitive: false,
138 pleroma: %{},
139 fields: []
140 },
141 pleroma: %{
142 background_image: nil,
143 confirmation_pending: false,
144 tags: [],
145 is_admin: false,
146 is_moderator: false,
147 hide_favorites: true,
148 hide_followers: false,
149 hide_follows: false,
150 relationship: %{},
151 skip_thread_containment: false
152 }
153 }
154
155 assert expected == AccountView.render("account.json", %{user: user})
156 end
157
158 test "Represent a deactivated user for an admin" do
159 admin = insert(:user, %{info: %{is_admin: true}})
160 deactivated_user = insert(:user, %{info: %{deactivated: true}})
161 represented = AccountView.render("account.json", %{user: deactivated_user, for: admin})
162 assert represented[:pleroma][:deactivated] == true
163 end
164
165 test "Represent a smaller mention" do
166 user = insert(:user)
167
168 expected = %{
169 id: to_string(user.id),
170 acct: user.nickname,
171 username: user.nickname,
172 url: user.ap_id
173 }
174
175 assert expected == AccountView.render("mention.json", %{user: user})
176 end
177
178 describe "relationship" do
179 test "represent a relationship for the following and followed user" do
180 user = insert(:user)
181 other_user = insert(:user)
182
183 {:ok, user} = User.follow(user, other_user)
184 {:ok, other_user} = User.follow(other_user, user)
185 {:ok, other_user} = User.subscribe(user, other_user)
186 {:ok, user} = User.mute(user, other_user, true)
187 {:ok, user} = CommonAPI.hide_reblogs(user, other_user)
188
189 expected = %{
190 id: to_string(other_user.id),
191 following: true,
192 followed_by: true,
193 blocking: false,
194 blocked_by: false,
195 muting: true,
196 muting_notifications: true,
197 subscribing: true,
198 requested: false,
199 domain_blocking: false,
200 showing_reblogs: false,
201 endorsed: false
202 }
203
204 assert expected ==
205 AccountView.render("relationship.json", %{user: user, target: other_user})
206 end
207
208 test "represent a relationship for the blocking and blocked user" do
209 user = insert(:user)
210 other_user = insert(:user)
211
212 {:ok, user} = User.follow(user, other_user)
213 {:ok, other_user} = User.subscribe(user, other_user)
214 {:ok, user} = User.block(user, other_user)
215 {:ok, other_user} = User.block(other_user, user)
216
217 expected = %{
218 id: to_string(other_user.id),
219 following: false,
220 followed_by: false,
221 blocking: true,
222 blocked_by: true,
223 muting: false,
224 muting_notifications: false,
225 subscribing: false,
226 requested: false,
227 domain_blocking: false,
228 showing_reblogs: true,
229 endorsed: false
230 }
231
232 assert expected ==
233 AccountView.render("relationship.json", %{user: user, target: other_user})
234 end
235
236 test "represent a relationship for the user blocking a domain" do
237 user = insert(:user)
238 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
239
240 {:ok, user} = User.block_domain(user, "bad.site")
241
242 assert %{domain_blocking: true, blocking: false} =
243 AccountView.render("relationship.json", %{user: user, target: other_user})
244 end
245
246 test "represent a relationship for the user with a pending follow request" do
247 user = insert(:user)
248 other_user = insert(:user, %{info: %User.Info{locked: true}})
249
250 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
251 user = User.get_cached_by_id(user.id)
252 other_user = User.get_cached_by_id(other_user.id)
253
254 expected = %{
255 id: to_string(other_user.id),
256 following: false,
257 followed_by: false,
258 blocking: false,
259 blocked_by: false,
260 muting: false,
261 muting_notifications: false,
262 subscribing: false,
263 requested: true,
264 domain_blocking: false,
265 showing_reblogs: true,
266 endorsed: false
267 }
268
269 assert expected ==
270 AccountView.render("relationship.json", %{user: user, target: other_user})
271 end
272 end
273
274 test "represent an embedded relationship" do
275 user =
276 insert(:user, %{
277 info: %{note_count: 5, follower_count: 0, source_data: %{"type" => "Service"}},
278 nickname: "shp@shitposter.club",
279 inserted_at: ~N[2017-08-15 15:47:06.597036]
280 })
281
282 other_user = insert(:user)
283 {:ok, other_user} = User.follow(other_user, user)
284 {:ok, other_user} = User.block(other_user, user)
285 {:ok, _} = User.follow(insert(:user), user)
286
287 expected = %{
288 id: to_string(user.id),
289 username: "shp",
290 acct: user.nickname,
291 display_name: user.name,
292 locked: false,
293 created_at: "2017-08-15T15:47:06.000Z",
294 followers_count: 1,
295 following_count: 0,
296 statuses_count: 5,
297 note: user.bio,
298 url: user.ap_id,
299 avatar: "http://localhost:4001/images/avi.png",
300 avatar_static: "http://localhost:4001/images/avi.png",
301 header: "http://localhost:4001/images/banner.png",
302 header_static: "http://localhost:4001/images/banner.png",
303 emojis: [],
304 fields: [],
305 bot: true,
306 source: %{
307 note: user.bio,
308 sensitive: false,
309 pleroma: %{},
310 fields: []
311 },
312 pleroma: %{
313 background_image: nil,
314 confirmation_pending: false,
315 tags: [],
316 is_admin: false,
317 is_moderator: false,
318 hide_favorites: true,
319 hide_followers: false,
320 hide_follows: false,
321 relationship: %{
322 id: to_string(user.id),
323 following: false,
324 followed_by: false,
325 blocking: true,
326 blocked_by: false,
327 subscribing: false,
328 muting: false,
329 muting_notifications: false,
330 requested: false,
331 domain_blocking: false,
332 showing_reblogs: true,
333 endorsed: false
334 },
335 skip_thread_containment: false
336 }
337 }
338
339 assert expected == AccountView.render("account.json", %{user: user, for: other_user})
340 end
341
342 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
343 user = insert(:user, %{info: %User.Info{pleroma_settings_store: %{fe: "test"}}})
344
345 result =
346 AccountView.render("account.json", %{user: user, for: user, with_pleroma_settings: true})
347
348 assert result.pleroma.settings_store == %{:fe => "test"}
349
350 result = AccountView.render("account.json", %{user: user, with_pleroma_settings: true})
351 assert result.pleroma[:settings_store] == nil
352
353 result = AccountView.render("account.json", %{user: user, for: user})
354 assert result.pleroma[:settings_store] == nil
355 end
356
357 test "sanitizes display names" do
358 user = insert(:user, name: "<marquee> username </marquee>")
359 result = AccountView.render("account.json", %{user: user})
360 refute result.display_name == "<marquee> username </marquee>"
361 end
362
363 describe "hiding follows/following" do
364 test "shows when follows/following are hidden and sets follower/following count to 0" do
365 user = insert(:user, info: %{hide_followers: true, hide_follows: true})
366 other_user = insert(:user)
367 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
368 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
369
370 assert %{
371 followers_count: 0,
372 following_count: 0,
373 pleroma: %{hide_follows: true, hide_followers: true}
374 } = AccountView.render("account.json", %{user: user})
375 end
376
377 test "shows actual follower/following count to the account owner" do
378 user = insert(:user, info: %{hide_followers: true, hide_follows: true})
379 other_user = insert(:user)
380 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
381 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
382
383 assert %{
384 followers_count: 1,
385 following_count: 1
386 } = AccountView.render("account.json", %{user: user, for: user})
387 end
388 end
389 end