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