Merge branch 'add-blocked-by-to-relationship' into 'develop'
[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 smaller mention" do
157 user = insert(:user)
158
159 expected = %{
160 id: to_string(user.id),
161 acct: user.nickname,
162 username: user.nickname,
163 url: user.ap_id
164 }
165
166 assert expected == AccountView.render("mention.json", %{user: user})
167 end
168
169 describe "relationship" do
170 test "represent a relationship for the following and followed user" do
171 user = insert(:user)
172 other_user = insert(:user)
173
174 {:ok, user} = User.follow(user, other_user)
175 {:ok, other_user} = User.follow(other_user, user)
176 {:ok, other_user} = User.subscribe(user, other_user)
177 {:ok, user} = User.mute(user, other_user, true)
178 {:ok, user} = CommonAPI.hide_reblogs(user, other_user)
179
180 expected = %{
181 id: to_string(other_user.id),
182 following: true,
183 followed_by: true,
184 blocking: false,
185 blocked_by: false,
186 muting: true,
187 muting_notifications: true,
188 subscribing: true,
189 requested: false,
190 domain_blocking: false,
191 showing_reblogs: false,
192 endorsed: false
193 }
194
195 assert expected ==
196 AccountView.render("relationship.json", %{user: user, target: other_user})
197 end
198
199 test "represent a relationship for the blocking and blocked user" do
200 user = insert(:user)
201 other_user = insert(:user)
202
203 {:ok, user} = User.follow(user, other_user)
204 {:ok, other_user} = User.subscribe(user, other_user)
205 {:ok, user} = User.block(user, other_user)
206 {:ok, other_user} = User.block(other_user, user)
207
208 expected = %{
209 id: to_string(other_user.id),
210 following: false,
211 followed_by: false,
212 blocking: true,
213 blocked_by: true,
214 muting: false,
215 muting_notifications: false,
216 subscribing: false,
217 requested: false,
218 domain_blocking: false,
219 showing_reblogs: true,
220 endorsed: false
221 }
222
223 assert expected ==
224 AccountView.render("relationship.json", %{user: user, target: other_user})
225 end
226
227 test "represent a relationship for the user with a pending follow request" do
228 user = insert(:user)
229 other_user = insert(:user, %{info: %User.Info{locked: true}})
230
231 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
232 user = User.get_cached_by_id(user.id)
233 other_user = User.get_cached_by_id(other_user.id)
234
235 expected = %{
236 id: to_string(other_user.id),
237 following: false,
238 followed_by: false,
239 blocking: false,
240 blocked_by: false,
241 muting: false,
242 muting_notifications: false,
243 subscribing: false,
244 requested: true,
245 domain_blocking: false,
246 showing_reblogs: true,
247 endorsed: false
248 }
249
250 assert expected ==
251 AccountView.render("relationship.json", %{user: user, target: other_user})
252 end
253 end
254
255 test "represent an embedded relationship" do
256 user =
257 insert(:user, %{
258 info: %{note_count: 5, follower_count: 0, source_data: %{"type" => "Service"}},
259 nickname: "shp@shitposter.club",
260 inserted_at: ~N[2017-08-15 15:47:06.597036]
261 })
262
263 other_user = insert(:user)
264 {:ok, other_user} = User.follow(other_user, user)
265 {:ok, other_user} = User.block(other_user, user)
266 {:ok, _} = User.follow(insert(:user), user)
267
268 expected = %{
269 id: to_string(user.id),
270 username: "shp",
271 acct: user.nickname,
272 display_name: user.name,
273 locked: false,
274 created_at: "2017-08-15T15:47:06.000Z",
275 followers_count: 1,
276 following_count: 0,
277 statuses_count: 5,
278 note: user.bio,
279 url: user.ap_id,
280 avatar: "http://localhost:4001/images/avi.png",
281 avatar_static: "http://localhost:4001/images/avi.png",
282 header: "http://localhost:4001/images/banner.png",
283 header_static: "http://localhost:4001/images/banner.png",
284 emojis: [],
285 fields: [],
286 bot: true,
287 source: %{
288 note: user.bio,
289 sensitive: false,
290 pleroma: %{}
291 },
292 pleroma: %{
293 background_image: nil,
294 confirmation_pending: false,
295 tags: [],
296 is_admin: false,
297 is_moderator: false,
298 hide_favorites: true,
299 hide_followers: false,
300 hide_follows: false,
301 relationship: %{
302 id: to_string(user.id),
303 following: false,
304 followed_by: false,
305 blocking: true,
306 blocked_by: false,
307 subscribing: false,
308 muting: false,
309 muting_notifications: false,
310 requested: false,
311 domain_blocking: false,
312 showing_reblogs: true,
313 endorsed: false
314 },
315 skip_thread_containment: false
316 }
317 }
318
319 assert expected == AccountView.render("account.json", %{user: user, for: other_user})
320 end
321
322 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
323 user = insert(:user, %{info: %User.Info{pleroma_settings_store: %{fe: "test"}}})
324
325 result =
326 AccountView.render("account.json", %{user: user, for: user, with_pleroma_settings: true})
327
328 assert result.pleroma.settings_store == %{:fe => "test"}
329
330 result = AccountView.render("account.json", %{user: user, with_pleroma_settings: true})
331 assert result.pleroma[:settings_store] == nil
332
333 result = AccountView.render("account.json", %{user: user, for: user})
334 assert result.pleroma[:settings_store] == nil
335 end
336
337 test "sanitizes display names" do
338 user = insert(:user, name: "<marquee> username </marquee>")
339 result = AccountView.render("account.json", %{user: user})
340 refute result.display_name == "<marquee> username </marquee>"
341 end
342 end