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