logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

account_view_test.exs (26724B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
  5. use Pleroma.DataCase, async: false
  6. alias Pleroma.UnstubbedConfigMock, as: ConfigMock
  7. alias Pleroma.User
  8. alias Pleroma.UserRelationship
  9. alias Pleroma.Web.CommonAPI
  10. alias Pleroma.Web.MastodonAPI.AccountView
  11. import Mox
  12. import Pleroma.Factory
  13. import Tesla.Mock
  14. setup do
  15. mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
  16. :ok
  17. end
  18. test "Represent a user account" do
  19. background_image = %{
  20. "url" => [%{"href" => "https://example.com/images/asuka_hospital.png"}]
  21. }
  22. user =
  23. insert(:user, %{
  24. follower_count: 3,
  25. note_count: 5,
  26. background: background_image,
  27. nickname: "shp@shitposter.club",
  28. name: ":karjalanpiirakka: shp",
  29. bio:
  30. "<script src=\"invalid-html\"></script><span>valid html</span>. a<br>b<br/>c<br >d<br />f '&<>\"",
  31. inserted_at: ~N[2017-08-15 15:47:06.597036],
  32. emoji: %{"karjalanpiirakka" => "/file.png"},
  33. raw_bio: "valid html. a\nb\nc\nd\nf '&<>\"",
  34. also_known_as: ["https://shitposter.zone/users/shp"],
  35. last_status_at: NaiveDateTime.utc_now()
  36. })
  37. expected = %{
  38. id: to_string(user.id),
  39. username: "shp",
  40. acct: user.nickname,
  41. display_name: user.name,
  42. locked: false,
  43. created_at: "2017-08-15T15:47:06.000Z",
  44. followers_count: 3,
  45. following_count: 0,
  46. statuses_count: 5,
  47. note: "<span>valid html</span>. a<br/>b<br/>c<br/>d<br/>f &#39;&amp;&lt;&gt;&quot;",
  48. url: user.ap_id,
  49. avatar: "http://localhost:4001/images/avi.png",
  50. avatar_static: "http://localhost:4001/images/avi.png",
  51. header: "http://localhost:4001/images/banner.png",
  52. header_static: "http://localhost:4001/images/banner.png",
  53. emojis: [
  54. %{
  55. static_url: "/file.png",
  56. url: "/file.png",
  57. shortcode: "karjalanpiirakka",
  58. visible_in_picker: false
  59. }
  60. ],
  61. fields: [],
  62. bot: false,
  63. source: %{
  64. note: "valid html. a\nb\nc\nd\nf '&<>\"",
  65. sensitive: false,
  66. pleroma: %{
  67. actor_type: "Person",
  68. discoverable: true
  69. },
  70. fields: []
  71. },
  72. fqn: "shp@shitposter.club",
  73. last_status_at: user.last_status_at |> NaiveDateTime.to_date() |> Date.to_iso8601(),
  74. pleroma: %{
  75. ap_id: user.ap_id,
  76. also_known_as: ["https://shitposter.zone/users/shp"],
  77. background_image: "https://example.com/images/asuka_hospital.png",
  78. favicon: nil,
  79. is_confirmed: true,
  80. tags: [],
  81. is_admin: false,
  82. is_moderator: false,
  83. privileges: [],
  84. is_suggested: false,
  85. hide_favorites: true,
  86. hide_followers: false,
  87. hide_follows: false,
  88. hide_followers_count: false,
  89. hide_follows_count: false,
  90. relationship: %{},
  91. skip_thread_containment: false,
  92. accepts_chat_messages: nil
  93. }
  94. }
  95. assert expected == AccountView.render("show.json", %{user: user, skip_visibility_check: true})
  96. end
  97. describe "roles and privileges" do
  98. setup do
  99. clear_config([:instance, :moderator_privileges], [:cofe, :only_moderator])
  100. clear_config([:instance, :admin_privileges], [:cofe, :only_admin])
  101. %{
  102. user: insert(:user),
  103. moderator: insert(:user, is_moderator: true),
  104. admin: insert(:user, is_admin: true),
  105. moderator_admin: insert(:user, is_moderator: true, is_admin: true),
  106. user_no_show_roles: insert(:user, show_role: false),
  107. moderator_admin_no_show_roles:
  108. insert(:user, is_moderator: true, is_admin: true, show_role: false)
  109. }
  110. end
  111. test "shows roles and privileges when show_role: true", %{
  112. user: user,
  113. moderator: moderator,
  114. admin: admin,
  115. moderator_admin: moderator_admin,
  116. user_no_show_roles: user_no_show_roles,
  117. moderator_admin_no_show_roles: moderator_admin_no_show_roles
  118. } do
  119. assert %{pleroma: %{is_moderator: false, is_admin: false}} =
  120. AccountView.render("show.json", %{user: user, skip_visibility_check: true})
  121. assert [] ==
  122. AccountView.render("show.json", %{user: user, skip_visibility_check: true})[
  123. :pleroma
  124. ][:privileges]
  125. |> Enum.sort()
  126. assert %{pleroma: %{is_moderator: true, is_admin: false}} =
  127. AccountView.render("show.json", %{user: moderator, skip_visibility_check: true})
  128. assert [:cofe, :only_moderator] ==
  129. AccountView.render("show.json", %{user: moderator, skip_visibility_check: true})[
  130. :pleroma
  131. ][:privileges]
  132. |> Enum.sort()
  133. assert %{pleroma: %{is_moderator: false, is_admin: true}} =
  134. AccountView.render("show.json", %{user: admin, skip_visibility_check: true})
  135. assert [:cofe, :only_admin] ==
  136. AccountView.render("show.json", %{user: admin, skip_visibility_check: true})[
  137. :pleroma
  138. ][:privileges]
  139. |> Enum.sort()
  140. assert %{pleroma: %{is_moderator: true, is_admin: true}} =
  141. AccountView.render("show.json", %{
  142. user: moderator_admin,
  143. skip_visibility_check: true
  144. })
  145. assert [:cofe, :only_admin, :only_moderator] ==
  146. AccountView.render("show.json", %{
  147. user: moderator_admin,
  148. skip_visibility_check: true
  149. })[:pleroma][:privileges]
  150. |> Enum.sort()
  151. refute match?(
  152. %{pleroma: %{is_moderator: _}},
  153. AccountView.render("show.json", %{
  154. user: user_no_show_roles,
  155. skip_visibility_check: true
  156. })
  157. )
  158. refute match?(
  159. %{pleroma: %{is_admin: _}},
  160. AccountView.render("show.json", %{
  161. user: user_no_show_roles,
  162. skip_visibility_check: true
  163. })
  164. )
  165. refute match?(
  166. %{pleroma: %{privileges: _}},
  167. AccountView.render("show.json", %{
  168. user: user_no_show_roles,
  169. skip_visibility_check: true
  170. })
  171. )
  172. refute match?(
  173. %{pleroma: %{is_moderator: _}},
  174. AccountView.render("show.json", %{
  175. user: moderator_admin_no_show_roles,
  176. skip_visibility_check: true
  177. })
  178. )
  179. refute match?(
  180. %{pleroma: %{is_admin: _}},
  181. AccountView.render("show.json", %{
  182. user: moderator_admin_no_show_roles,
  183. skip_visibility_check: true
  184. })
  185. )
  186. refute match?(
  187. %{pleroma: %{privileges: _}},
  188. AccountView.render("show.json", %{
  189. user: moderator_admin_no_show_roles,
  190. skip_visibility_check: true
  191. })
  192. )
  193. end
  194. test "shows roles and privileges when viewing own account, even when show_role: false", %{
  195. user_no_show_roles: user_no_show_roles,
  196. moderator_admin_no_show_roles: moderator_admin_no_show_roles
  197. } do
  198. assert %{pleroma: %{is_moderator: false, is_admin: false, privileges: []}} =
  199. AccountView.render("show.json", %{
  200. user: user_no_show_roles,
  201. skip_visibility_check: true,
  202. for: user_no_show_roles
  203. })
  204. assert %{
  205. pleroma: %{
  206. is_moderator: true,
  207. is_admin: true,
  208. privileges: privileges
  209. }
  210. } =
  211. AccountView.render("show.json", %{
  212. user: moderator_admin_no_show_roles,
  213. skip_visibility_check: true,
  214. for: moderator_admin_no_show_roles
  215. })
  216. assert [:cofe, :only_admin, :only_moderator] == privileges |> Enum.sort()
  217. end
  218. end
  219. describe "favicon" do
  220. setup do
  221. [user: insert(:user)]
  222. end
  223. test "is parsed when :instance_favicons is enabled", %{user: user} do
  224. clear_config([:instances_favicons, :enabled], true)
  225. assert %{
  226. pleroma: %{
  227. favicon:
  228. "https://shitposter.club/plugins/Qvitter/img/gnusocial-favicons/favicon-16x16.png"
  229. }
  230. } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
  231. end
  232. test "is nil when :instances_favicons is disabled", %{user: user} do
  233. assert %{pleroma: %{favicon: nil}} =
  234. AccountView.render("show.json", %{user: user, skip_visibility_check: true})
  235. end
  236. end
  237. test "Represent the user account for the account owner" do
  238. user = insert(:user)
  239. notification_settings = %{
  240. block_from_strangers: false,
  241. hide_notification_contents: false
  242. }
  243. privacy = user.default_scope
  244. assert %{
  245. pleroma: %{notification_settings: ^notification_settings, allow_following_move: true},
  246. source: %{privacy: ^privacy}
  247. } = AccountView.render("show.json", %{user: user, for: user})
  248. end
  249. test "Represent a Service(bot) account" do
  250. user =
  251. insert(:user, %{
  252. follower_count: 3,
  253. note_count: 5,
  254. actor_type: "Service",
  255. nickname: "shp@shitposter.club",
  256. inserted_at: ~N[2017-08-15 15:47:06.597036]
  257. })
  258. expected = %{
  259. id: to_string(user.id),
  260. username: "shp",
  261. acct: user.nickname,
  262. display_name: user.name,
  263. locked: false,
  264. created_at: "2017-08-15T15:47:06.000Z",
  265. followers_count: 3,
  266. following_count: 0,
  267. statuses_count: 5,
  268. note: user.bio,
  269. url: user.ap_id,
  270. avatar: "http://localhost:4001/images/avi.png",
  271. avatar_static: "http://localhost:4001/images/avi.png",
  272. header: "http://localhost:4001/images/banner.png",
  273. header_static: "http://localhost:4001/images/banner.png",
  274. emojis: [],
  275. fields: [],
  276. bot: true,
  277. source: %{
  278. note: user.bio,
  279. sensitive: false,
  280. pleroma: %{
  281. actor_type: "Service",
  282. discoverable: true
  283. },
  284. fields: []
  285. },
  286. fqn: "shp@shitposter.club",
  287. last_status_at: nil,
  288. pleroma: %{
  289. ap_id: user.ap_id,
  290. also_known_as: [],
  291. background_image: nil,
  292. favicon: nil,
  293. is_confirmed: true,
  294. tags: [],
  295. is_admin: false,
  296. is_moderator: false,
  297. privileges: [],
  298. is_suggested: false,
  299. hide_favorites: true,
  300. hide_followers: false,
  301. hide_follows: false,
  302. hide_followers_count: false,
  303. hide_follows_count: false,
  304. relationship: %{},
  305. skip_thread_containment: false,
  306. accepts_chat_messages: nil
  307. }
  308. }
  309. assert expected == AccountView.render("show.json", %{user: user, skip_visibility_check: true})
  310. end
  311. test "Represent a Funkwhale channel" do
  312. {:ok, user} =
  313. User.get_or_fetch_by_ap_id(
  314. "https://channels.tests.funkwhale.audio/federation/actors/compositions"
  315. )
  316. assert represented =
  317. AccountView.render("show.json", %{user: user, skip_visibility_check: true})
  318. assert represented.acct == "compositions@channels.tests.funkwhale.audio"
  319. assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
  320. end
  321. test "Represent a deactivated user for a privileged user" do
  322. clear_config([:instance, :moderator_privileges], [:users_manage_activation_state])
  323. admin = insert(:user, is_moderator: true)
  324. deactivated_user = insert(:user, is_active: false)
  325. represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
  326. assert represented[:pleroma][:deactivated] == true
  327. end
  328. test "Represent a smaller mention" do
  329. user = insert(:user)
  330. expected = %{
  331. id: to_string(user.id),
  332. acct: user.nickname,
  333. username: user.nickname,
  334. url: user.ap_id
  335. }
  336. assert expected == AccountView.render("mention.json", %{user: user})
  337. end
  338. test "demands :for or :skip_visibility_check option for account rendering" do
  339. clear_config([:restrict_unauthenticated, :profiles, :local], false)
  340. user = insert(:user)
  341. user_id = user.id
  342. assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: nil})
  343. assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: user})
  344. assert %{id: ^user_id} =
  345. AccountView.render("show.json", %{user: user, skip_visibility_check: true})
  346. assert_raise RuntimeError, ~r/:skip_visibility_check or :for option is required/, fn ->
  347. AccountView.render("show.json", %{user: user})
  348. end
  349. end
  350. describe "relationship" do
  351. defp test_relationship_rendering(user, other_user, expected_result) do
  352. opts = %{user: user, target: other_user, relationships: nil}
  353. assert expected_result == AccountView.render("relationship.json", opts)
  354. relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
  355. opts = Map.put(opts, :relationships, relationships_opt)
  356. assert expected_result == AccountView.render("relationship.json", opts)
  357. assert [expected_result] ==
  358. AccountView.render("relationships.json", %{user: user, targets: [other_user]})
  359. end
  360. @blank_response %{
  361. following: false,
  362. followed_by: false,
  363. blocking: false,
  364. blocked_by: false,
  365. muting: false,
  366. muting_notifications: false,
  367. subscribing: false,
  368. notifying: false,
  369. requested: false,
  370. domain_blocking: false,
  371. showing_reblogs: true,
  372. endorsed: false,
  373. note: ""
  374. }
  375. test "represent a relationship for the following and followed user" do
  376. user = insert(:user)
  377. other_user = insert(:user)
  378. {:ok, user, other_user} = User.follow(user, other_user)
  379. {:ok, other_user, user} = User.follow(other_user, user)
  380. {:ok, _subscription} = User.subscribe(user, other_user)
  381. {:ok, _user_relationships} = User.mute(user, other_user, %{notifications: true})
  382. {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
  383. expected =
  384. Map.merge(
  385. @blank_response,
  386. %{
  387. following: true,
  388. followed_by: true,
  389. muting: true,
  390. muting_notifications: true,
  391. subscribing: true,
  392. notifying: true,
  393. showing_reblogs: false,
  394. id: to_string(other_user.id)
  395. }
  396. )
  397. test_relationship_rendering(user, other_user, expected)
  398. end
  399. test "represent a relationship for the blocking and blocked user" do
  400. user = insert(:user)
  401. other_user = insert(:user)
  402. {:ok, user, other_user} = User.follow(user, other_user)
  403. {:ok, _subscription} = User.subscribe(user, other_user)
  404. {:ok, _user_relationship} = User.block(user, other_user)
  405. {:ok, _user_relationship} = User.block(other_user, user)
  406. expected =
  407. Map.merge(
  408. @blank_response,
  409. %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
  410. )
  411. test_relationship_rendering(user, other_user, expected)
  412. end
  413. test "represent a relationship for the user blocking a domain" do
  414. user = insert(:user)
  415. other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
  416. {:ok, user} = User.block_domain(user, "bad.site")
  417. expected =
  418. Map.merge(
  419. @blank_response,
  420. %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
  421. )
  422. test_relationship_rendering(user, other_user, expected)
  423. end
  424. test "represent a relationship for the user with a pending follow request" do
  425. user = insert(:user)
  426. other_user = insert(:user, is_locked: true)
  427. {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
  428. user = User.get_cached_by_id(user.id)
  429. other_user = User.get_cached_by_id(other_user.id)
  430. expected =
  431. Map.merge(
  432. @blank_response,
  433. %{requested: true, following: false, id: to_string(other_user.id)}
  434. )
  435. test_relationship_rendering(user, other_user, expected)
  436. end
  437. end
  438. test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
  439. user = insert(:user, pleroma_settings_store: %{fe: "test"})
  440. result =
  441. AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
  442. assert result.pleroma.settings_store == %{:fe => "test"}
  443. result = AccountView.render("show.json", %{user: user, for: nil, with_pleroma_settings: true})
  444. assert result.pleroma[:settings_store] == nil
  445. result = AccountView.render("show.json", %{user: user, for: user})
  446. assert result.pleroma[:settings_store] == nil
  447. end
  448. test "doesn't sanitize display names" do
  449. user = insert(:user, name: "<marquee> username </marquee>")
  450. result = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
  451. assert result.display_name == "<marquee> username </marquee>"
  452. end
  453. test "never display nil user follow counts" do
  454. user = insert(:user, following_count: 0, follower_count: 0)
  455. result = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
  456. assert result.following_count == 0
  457. assert result.followers_count == 0
  458. end
  459. describe "hiding follows/following" do
  460. test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
  461. user =
  462. insert(:user, %{
  463. hide_followers: true,
  464. hide_followers_count: true,
  465. hide_follows: true,
  466. hide_follows_count: true
  467. })
  468. other_user = insert(:user)
  469. {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
  470. {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
  471. assert %{
  472. followers_count: 0,
  473. following_count: 0,
  474. pleroma: %{hide_follows_count: true, hide_followers_count: true}
  475. } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
  476. end
  477. test "shows when follows/followers are hidden" do
  478. user = insert(:user, hide_followers: true, hide_follows: true)
  479. other_user = insert(:user)
  480. {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
  481. {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
  482. assert %{
  483. followers_count: 1,
  484. following_count: 1,
  485. pleroma: %{hide_follows: true, hide_followers: true}
  486. } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
  487. end
  488. test "shows actual follower/following count to the account owner" do
  489. user = insert(:user, hide_followers: true, hide_follows: true)
  490. other_user = insert(:user)
  491. {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
  492. assert User.following?(user, other_user)
  493. assert Pleroma.FollowingRelationship.follower_count(other_user) == 1
  494. {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
  495. assert %{
  496. followers_count: 1,
  497. following_count: 1
  498. } = AccountView.render("show.json", %{user: user, for: user})
  499. end
  500. test "shows unread_conversation_count only to the account owner" do
  501. user = insert(:user)
  502. other_user = insert(:user)
  503. {:ok, _activity} =
  504. CommonAPI.post(other_user, %{
  505. status: "Hey @#{user.nickname}.",
  506. visibility: "direct"
  507. })
  508. user = User.get_cached_by_ap_id(user.ap_id)
  509. assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
  510. :unread_conversation_count
  511. ] == nil
  512. assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
  513. :unread_conversation_count
  514. ] == 1
  515. end
  516. test "shows unread_count only to the account owner" do
  517. user = insert(:user)
  518. insert_list(7, :notification, user: user, activity: insert(:note_activity))
  519. other_user = insert(:user)
  520. user = User.get_cached_by_ap_id(user.ap_id)
  521. assert AccountView.render(
  522. "show.json",
  523. %{user: user, for: other_user}
  524. )[:pleroma][:unread_notifications_count] == nil
  525. assert AccountView.render(
  526. "show.json",
  527. %{user: user, for: user}
  528. )[:pleroma][:unread_notifications_count] == 7
  529. end
  530. test "shows email only to the account owner" do
  531. user = insert(:user)
  532. other_user = insert(:user)
  533. user = User.get_cached_by_ap_id(user.ap_id)
  534. assert AccountView.render(
  535. "show.json",
  536. %{user: user, for: other_user}
  537. )[:pleroma][:email] == nil
  538. assert AccountView.render(
  539. "show.json",
  540. %{user: user, for: user}
  541. )[:pleroma][:email] == user.email
  542. end
  543. end
  544. describe "hiding birthday" do
  545. test "doesn't show birthday if hidden" do
  546. user =
  547. insert(:user, %{
  548. birthday: "2001-02-12",
  549. show_birthday: false
  550. })
  551. other_user = insert(:user)
  552. user = User.get_cached_by_ap_id(user.ap_id)
  553. assert AccountView.render(
  554. "show.json",
  555. %{user: user, for: other_user}
  556. )[:birthday] == nil
  557. end
  558. test "shows hidden birthday to the account owner" do
  559. user =
  560. insert(:user, %{
  561. birthday: "2001-02-12",
  562. show_birthday: false
  563. })
  564. user = User.get_cached_by_ap_id(user.ap_id)
  565. assert AccountView.render(
  566. "show.json",
  567. %{user: user, for: user}
  568. )[:birthday] == nil
  569. end
  570. end
  571. describe "follow requests counter" do
  572. test "shows zero when no follow requests are pending" do
  573. user = insert(:user)
  574. assert %{follow_requests_count: 0} =
  575. AccountView.render("show.json", %{user: user, for: user})
  576. other_user = insert(:user)
  577. {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
  578. assert %{follow_requests_count: 0} =
  579. AccountView.render("show.json", %{user: user, for: user})
  580. end
  581. test "shows non-zero when follow requests are pending" do
  582. user = insert(:user, is_locked: true)
  583. assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
  584. other_user = insert(:user)
  585. {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
  586. assert %{locked: true, follow_requests_count: 1} =
  587. AccountView.render("show.json", %{user: user, for: user})
  588. end
  589. test "decreases when accepting a follow request" do
  590. user = insert(:user, is_locked: true)
  591. assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
  592. other_user = insert(:user)
  593. {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
  594. assert %{locked: true, follow_requests_count: 1} =
  595. AccountView.render("show.json", %{user: user, for: user})
  596. {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
  597. assert %{locked: true, follow_requests_count: 0} =
  598. AccountView.render("show.json", %{user: user, for: user})
  599. end
  600. test "decreases when rejecting a follow request" do
  601. user = insert(:user, is_locked: true)
  602. assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
  603. other_user = insert(:user)
  604. {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
  605. assert %{locked: true, follow_requests_count: 1} =
  606. AccountView.render("show.json", %{user: user, for: user})
  607. {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
  608. assert %{locked: true, follow_requests_count: 0} =
  609. AccountView.render("show.json", %{user: user, for: user})
  610. end
  611. test "shows non-zero when historical unapproved requests are present" do
  612. user = insert(:user, is_locked: true)
  613. assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
  614. other_user = insert(:user)
  615. {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
  616. {:ok, user} = User.update_and_set_cache(user, %{is_locked: false})
  617. assert %{locked: false, follow_requests_count: 1} =
  618. AccountView.render("show.json", %{user: user, for: user})
  619. end
  620. end
  621. test "uses mediaproxy urls when it's enabled (regardless of media preview proxy state)" do
  622. clear_config([:media_proxy, :enabled], true)
  623. clear_config([:media_preview_proxy, :enabled])
  624. ConfigMock
  625. |> stub_with(Pleroma.Test.StaticConfig)
  626. user =
  627. insert(:user,
  628. avatar: %{"url" => [%{"href" => "https://evil.website/avatar.png"}]},
  629. banner: %{"url" => [%{"href" => "https://evil.website/banner.png"}]},
  630. emoji: %{"joker_smile" => "https://evil.website/society.png"}
  631. )
  632. Enum.each([true, false], fn media_preview_enabled ->
  633. clear_config([:media_preview_proxy, :enabled], media_preview_enabled)
  634. AccountView.render("show.json", %{user: user, skip_visibility_check: true})
  635. |> Enum.all?(fn
  636. {key, url} when key in [:avatar, :avatar_static, :header, :header_static] ->
  637. String.starts_with?(url, Pleroma.Web.Endpoint.url())
  638. {:emojis, emojis} ->
  639. Enum.all?(emojis, fn %{url: url, static_url: static_url} ->
  640. String.starts_with?(url, Pleroma.Web.Endpoint.url()) &&
  641. String.starts_with?(static_url, Pleroma.Web.Endpoint.url())
  642. end)
  643. _ ->
  644. true
  645. end)
  646. |> assert()
  647. end)
  648. end
  649. test "renders mute expiration date" do
  650. user = insert(:user)
  651. other_user = insert(:user)
  652. {:ok, _user_relationships} =
  653. User.mute(user, other_user, %{notifications: true, duration: 24 * 60 * 60})
  654. %{
  655. mute_expires_at: mute_expires_at
  656. } = AccountView.render("show.json", %{user: other_user, for: user, mutes: true})
  657. assert DateTime.diff(
  658. mute_expires_at,
  659. DateTime.utc_now() |> DateTime.add(24 * 60 * 60)
  660. ) in -3..3
  661. end
  662. end