Format subscriptions related stuff
[akkoma] / test / web / twitter_api / views / user_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.TwitterAPI.UserViewTest do
6 use Pleroma.DataCase
7
8 alias Pleroma.User
9 alias Pleroma.Web.CommonAPI.Utils
10 alias Pleroma.Web.TwitterAPI.UserView
11
12 import Pleroma.Factory
13
14 setup do
15 user = insert(:user, bio: "<span>Here's some html</span>")
16 [user: user]
17 end
18
19 test "A user with only a nickname", %{user: user} do
20 user = %{user | name: nil, nickname: "scarlett@catgirl.science"}
21 represented = UserView.render("show.json", %{user: user})
22 assert represented["name"] == user.nickname
23 assert represented["name_html"] == user.nickname
24 end
25
26 test "A user with an avatar object", %{user: user} do
27 image = "image"
28 user = %{user | avatar: %{"url" => [%{"href" => image}]}}
29 represented = UserView.render("show.json", %{user: user})
30 assert represented["profile_image_url"] == image
31 end
32
33 test "A user with emoji in username" do
34 expected =
35 "<img height=\"32px\" width=\"32px\" alt=\"karjalanpiirakka\" title=\"karjalanpiirakka\" src=\"/file.png\" /> man"
36
37 user =
38 insert(:user, %{
39 info: %{
40 source_data: %{
41 "tag" => [
42 %{
43 "type" => "Emoji",
44 "icon" => %{"url" => "/file.png"},
45 "name" => ":karjalanpiirakka:"
46 }
47 ]
48 }
49 },
50 name: ":karjalanpiirakka: man"
51 })
52
53 represented = UserView.render("show.json", %{user: user})
54 assert represented["name_html"] == expected
55 end
56
57 test "A user" do
58 note_activity = insert(:note_activity)
59 user = User.get_cached_by_ap_id(note_activity.data["actor"])
60 {:ok, user} = User.update_note_count(user)
61 follower = insert(:user)
62 second_follower = insert(:user)
63
64 User.follow(follower, user)
65 User.follow(second_follower, user)
66 User.follow(user, follower)
67 {:ok, user} = User.update_follower_count(user)
68 Cachex.put(:user_cache, "user_info:#{user.id}", User.user_info(Repo.get!(User, user.id)))
69
70 image = "http://localhost:4001/images/avi.png"
71 banner = "http://localhost:4001/images/banner.png"
72
73 represented = %{
74 "id" => user.id,
75 "name" => user.name,
76 "screen_name" => user.nickname,
77 "name_html" => user.name,
78 "description" => HtmlSanitizeEx.strip_tags(user.bio |> String.replace("<br>", "\n")),
79 "description_html" => HtmlSanitizeEx.basic_html(user.bio),
80 "created_at" => user.inserted_at |> Utils.format_naive_asctime(),
81 "favourites_count" => 0,
82 "statuses_count" => 1,
83 "friends_count" => 1,
84 "followers_count" => 2,
85 "profile_image_url" => image,
86 "profile_image_url_https" => image,
87 "profile_image_url_profile_size" => image,
88 "profile_image_url_original" => image,
89 "following" => false,
90 "follows_you" => false,
91 "statusnet_blocking" => false,
92 "rights" => %{
93 "delete_others_notice" => false,
94 "admin" => false
95 },
96 "statusnet_profile_url" => user.ap_id,
97 "cover_photo" => banner,
98 "background_image" => nil,
99 "is_local" => true,
100 "locked" => false,
101 "default_scope" => "public",
102 "no_rich_text" => false,
103 "hide_follows" => false,
104 "hide_followers" => false,
105 "fields" => [],
106 "pleroma" => %{
107 "confirmation_pending" => false,
108 "tags" => [],
109 "subscribed" => false
110 }
111 }
112
113 assert represented == UserView.render("show.json", %{user: user})
114 end
115
116 test "A user for a given other follower", %{user: user} do
117 follower = insert(:user, %{following: [User.ap_followers(user)]})
118 {:ok, user} = User.update_follower_count(user)
119 image = "http://localhost:4001/images/avi.png"
120 banner = "http://localhost:4001/images/banner.png"
121
122 represented = %{
123 "id" => user.id,
124 "name" => user.name,
125 "screen_name" => user.nickname,
126 "name_html" => user.name,
127 "description" => HtmlSanitizeEx.strip_tags(user.bio |> String.replace("<br>", "\n")),
128 "description_html" => HtmlSanitizeEx.basic_html(user.bio),
129 "created_at" => user.inserted_at |> Utils.format_naive_asctime(),
130 "favourites_count" => 0,
131 "statuses_count" => 0,
132 "friends_count" => 0,
133 "followers_count" => 1,
134 "profile_image_url" => image,
135 "profile_image_url_https" => image,
136 "profile_image_url_profile_size" => image,
137 "profile_image_url_original" => image,
138 "following" => true,
139 "follows_you" => false,
140 "statusnet_blocking" => false,
141 "rights" => %{
142 "delete_others_notice" => false,
143 "admin" => false
144 },
145 "statusnet_profile_url" => user.ap_id,
146 "cover_photo" => banner,
147 "background_image" => nil,
148 "is_local" => true,
149 "locked" => false,
150 "default_scope" => "public",
151 "no_rich_text" => false,
152 "hide_follows" => false,
153 "hide_followers" => false,
154 "fields" => [],
155 "pleroma" => %{
156 "confirmation_pending" => false,
157 "tags" => [],
158 "subscribed" => false
159 }
160 }
161
162 assert represented == UserView.render("show.json", %{user: user, for: follower})
163 end
164
165 test "A user that follows you", %{user: user} do
166 follower = insert(:user)
167 {:ok, follower} = User.follow(follower, user)
168 {:ok, user} = User.update_follower_count(user)
169 image = "http://localhost:4001/images/avi.png"
170 banner = "http://localhost:4001/images/banner.png"
171
172 represented = %{
173 "id" => follower.id,
174 "name" => follower.name,
175 "screen_name" => follower.nickname,
176 "name_html" => follower.name,
177 "description" => HtmlSanitizeEx.strip_tags(follower.bio |> String.replace("<br>", "\n")),
178 "description_html" => HtmlSanitizeEx.basic_html(follower.bio),
179 "created_at" => follower.inserted_at |> Utils.format_naive_asctime(),
180 "favourites_count" => 0,
181 "statuses_count" => 0,
182 "friends_count" => 1,
183 "followers_count" => 0,
184 "profile_image_url" => image,
185 "profile_image_url_https" => image,
186 "profile_image_url_profile_size" => image,
187 "profile_image_url_original" => image,
188 "following" => false,
189 "follows_you" => true,
190 "statusnet_blocking" => false,
191 "rights" => %{
192 "delete_others_notice" => false,
193 "admin" => false
194 },
195 "statusnet_profile_url" => follower.ap_id,
196 "cover_photo" => banner,
197 "background_image" => nil,
198 "is_local" => true,
199 "locked" => false,
200 "default_scope" => "public",
201 "no_rich_text" => false,
202 "hide_follows" => false,
203 "hide_followers" => false,
204 "fields" => [],
205 "pleroma" => %{
206 "confirmation_pending" => false,
207 "tags" => [],
208 "subscribed" => false
209 }
210 }
211
212 assert represented == UserView.render("show.json", %{user: follower, for: user})
213 end
214
215 test "a user that you are subscribed to" do
216 user = insert(:user)
217 subscriber = insert(:user)
218 {:ok, subscriber} = User.subscribe(subscriber, user)
219 represented = UserView.render("show.json", %{user: user, for: subscriber})
220 assert represented["pleroma"]["subscribed"] == true
221 end
222
223 test "a user that is a moderator" do
224 user = insert(:user, %{info: %{is_moderator: true}})
225 represented = UserView.render("show.json", %{user: user, for: user})
226
227 assert represented["rights"]["delete_others_notice"]
228 assert represented["role"] == "moderator"
229 end
230
231 test "a user that is a admin" do
232 user = insert(:user, %{info: %{is_admin: true}})
233 represented = UserView.render("show.json", %{user: user, for: user})
234
235 assert represented["rights"]["admin"]
236 assert represented["role"] == "admin"
237 end
238
239 test "A moderator with hidden role for another user", %{user: user} do
240 admin = insert(:user, %{info: %{is_moderator: true, show_role: false}})
241 represented = UserView.render("show.json", %{user: admin, for: user})
242
243 assert represented["role"] == nil
244 end
245
246 test "An admin with hidden role for another user", %{user: user} do
247 admin = insert(:user, %{info: %{is_admin: true, show_role: false}})
248 represented = UserView.render("show.json", %{user: admin, for: user})
249
250 assert represented["role"] == nil
251 end
252
253 test "A regular user for the admin", %{user: user} do
254 admin = insert(:user, %{info: %{is_admin: true}})
255 represented = UserView.render("show.json", %{user: user, for: admin})
256
257 assert represented["pleroma"]["deactivated"] == false
258 end
259
260 test "A blocked user for the blocker" do
261 user = insert(:user)
262 blocker = insert(:user)
263 User.block(blocker, user)
264 image = "http://localhost:4001/images/avi.png"
265 banner = "http://localhost:4001/images/banner.png"
266
267 represented = %{
268 "id" => user.id,
269 "name" => user.name,
270 "screen_name" => user.nickname,
271 "name_html" => user.name,
272 "description" => HtmlSanitizeEx.strip_tags(user.bio |> String.replace("<br>", "\n")),
273 "description_html" => HtmlSanitizeEx.basic_html(user.bio),
274 "created_at" => user.inserted_at |> Utils.format_naive_asctime(),
275 "favourites_count" => 0,
276 "statuses_count" => 0,
277 "friends_count" => 0,
278 "followers_count" => 0,
279 "profile_image_url" => image,
280 "profile_image_url_https" => image,
281 "profile_image_url_profile_size" => image,
282 "profile_image_url_original" => image,
283 "following" => false,
284 "follows_you" => false,
285 "statusnet_blocking" => true,
286 "rights" => %{
287 "delete_others_notice" => false,
288 "admin" => false
289 },
290 "statusnet_profile_url" => user.ap_id,
291 "cover_photo" => banner,
292 "background_image" => nil,
293 "is_local" => true,
294 "locked" => false,
295 "default_scope" => "public",
296 "no_rich_text" => false,
297 "hide_follows" => false,
298 "hide_followers" => false,
299 "fields" => [],
300 "pleroma" => %{
301 "confirmation_pending" => false,
302 "tags" => []
303 }
304 }
305
306 blocker = User.get_by_id(blocker.id)
307 assert represented == UserView.render("show.json", %{user: user, for: blocker})
308 end
309
310 test "a user with mastodon fields" do
311 fields = [
312 %{
313 "name" => "Pronouns",
314 "value" => "she/her"
315 },
316 %{
317 "name" => "Website",
318 "value" => "https://example.org/"
319 }
320 ]
321
322 user =
323 insert(:user, %{
324 info: %{
325 source_data: %{
326 "attachment" =>
327 Enum.map(fields, fn field -> Map.put(field, "type", "PropertyValue") end)
328 }
329 }
330 })
331
332 userview = UserView.render("show.json", %{user: user})
333 assert userview["fields"] == fields
334 end
335 end