1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.TwitterAPI.UserViewTest do
9 alias Pleroma.Web.CommonAPI.Utils
10 alias Pleroma.Web.TwitterAPI.UserView
12 import Pleroma.Factory
15 user = insert(:user, bio: "<span>Here's some html</span>")
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
26 test "A user with an avatar object", %{user: user} do
28 user = %{user | avatar: %{"url" => [%{"href" => image}]}}
29 represented = UserView.render("show.json", %{user: user})
30 assert represented["profile_image_url"] == image
33 test "A user with emoji in username" do
35 "<img height=\"32px\" width=\"32px\" alt=\"karjalanpiirakka\" title=\"karjalanpiirakka\" src=\"/file.png\" /> man"
44 "icon" => %{"url" => "/file.png"},
45 "name" => ":karjalanpiirakka:"
50 name: ":karjalanpiirakka: man"
53 represented = UserView.render("show.json", %{user: user})
54 assert represented["name_html"] == expected
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)
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)))
70 image = "http://localhost:4001/images/avi.png"
71 banner = "http://localhost:4001/images/banner.png"
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,
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,
90 "follows_you" => false,
91 "statusnet_blocking" => false,
93 "delete_others_notice" => false,
96 "statusnet_profile_url" => user.ap_id,
97 "cover_photo" => banner,
98 "background_image" => nil,
101 "default_scope" => "public",
102 "no_rich_text" => false,
103 "hide_follows" => false,
104 "hide_followers" => false,
107 "confirmation_pending" => false,
109 "subscribed" => false
113 assert represented == UserView.render("show.json", %{user: user})
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"
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,
139 "follows_you" => false,
140 "statusnet_blocking" => false,
142 "delete_others_notice" => false,
145 "statusnet_profile_url" => user.ap_id,
146 "cover_photo" => banner,
147 "background_image" => nil,
150 "default_scope" => "public",
151 "no_rich_text" => false,
152 "hide_follows" => false,
153 "hide_followers" => false,
156 "confirmation_pending" => false,
158 "subscribed" => false
162 assert represented == UserView.render("show.json", %{user: user, for: follower})
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"
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,
192 "delete_others_notice" => false,
195 "statusnet_profile_url" => follower.ap_id,
196 "cover_photo" => banner,
197 "background_image" => nil,
200 "default_scope" => "public",
201 "no_rich_text" => false,
202 "hide_follows" => false,
203 "hide_followers" => false,
206 "confirmation_pending" => false,
208 "subscribed" => false
212 assert represented == UserView.render("show.json", %{user: follower, for: user})
215 test "a user that you are subscribed to" do
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
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})
227 assert represented["rights"]["delete_others_notice"]
228 assert represented["role"] == "moderator"
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})
235 assert represented["rights"]["admin"]
236 assert represented["role"] == "admin"
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})
243 assert represented["role"] == nil
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})
250 assert represented["role"] == nil
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})
257 assert represented["pleroma"]["deactivated"] == false
260 test "A blocked user for the blocker" do
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"
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,
287 "delete_others_notice" => false,
290 "statusnet_profile_url" => user.ap_id,
291 "cover_photo" => banner,
292 "background_image" => nil,
295 "default_scope" => "public",
296 "no_rich_text" => false,
297 "hide_follows" => false,
298 "hide_followers" => false,
301 "confirmation_pending" => false,
306 blocker = User.get_by_id(blocker.id)
307 assert represented == UserView.render("show.json", %{user: user, for: blocker})
310 test "a user with mastodon fields" do
313 "name" => "Pronouns",
318 "value" => "https://example.org/"
327 Enum.map(fields, fn field -> Map.put(field, "type", "PropertyValue") end)
332 userview = UserView.render("show.json", %{user: user})
333 assert userview["fields"] == fields