logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

user_view_test.exs (6323B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.ActivityPub.UserViewTest do
  5. use Pleroma.DataCase
  6. import Pleroma.Factory
  7. alias Pleroma.User
  8. alias Pleroma.Web.ActivityPub.UserView
  9. alias Pleroma.Web.CommonAPI
  10. test "Renders a user, including the public key" do
  11. user = insert(:user)
  12. {:ok, user} = User.ensure_keys_present(user)
  13. result = UserView.render("user.json", %{user: user})
  14. assert result["id"] == user.ap_id
  15. assert result["preferredUsername"] == user.nickname
  16. assert String.contains?(result["publicKey"]["publicKeyPem"], "BEGIN PUBLIC KEY")
  17. end
  18. test "Renders profile fields" do
  19. fields = [
  20. %{"name" => "foo", "value" => "bar"}
  21. ]
  22. {:ok, user} =
  23. insert(:user)
  24. |> User.update_changeset(%{fields: fields})
  25. |> User.update_and_set_cache()
  26. assert %{
  27. "attachment" => [%{"name" => "foo", "type" => "PropertyValue", "value" => "bar"}]
  28. } = UserView.render("user.json", %{user: user})
  29. end
  30. test "Renders with emoji tags" do
  31. user = insert(:user, emoji: %{"bib" => "/test"})
  32. assert %{
  33. "tag" => [
  34. %{
  35. "icon" => %{"type" => "Image", "url" => "/test"},
  36. "id" => "/test",
  37. "name" => ":bib:",
  38. "type" => "Emoji",
  39. "updated" => "1970-01-01T00:00:00Z"
  40. }
  41. ]
  42. } = UserView.render("user.json", %{user: user})
  43. end
  44. test "Does not add an avatar image if the user hasn't set one" do
  45. user = insert(:user)
  46. {:ok, user} = User.ensure_keys_present(user)
  47. result = UserView.render("user.json", %{user: user})
  48. refute result["icon"]
  49. refute result["image"]
  50. user =
  51. insert(:user,
  52. avatar: %{"url" => [%{"href" => "https://someurl"}]},
  53. banner: %{"url" => [%{"href" => "https://somebanner"}]}
  54. )
  55. {:ok, user} = User.ensure_keys_present(user)
  56. result = UserView.render("user.json", %{user: user})
  57. assert result["icon"]["url"] == "https://someurl"
  58. assert result["image"]["url"] == "https://somebanner"
  59. end
  60. test "renders an invisible user with the invisible property set to true" do
  61. user = insert(:user, invisible: true)
  62. assert %{"invisible" => true} = UserView.render("service.json", %{user: user})
  63. end
  64. describe "endpoints" do
  65. test "local users have a usable endpoints structure" do
  66. user = insert(:user)
  67. {:ok, user} = User.ensure_keys_present(user)
  68. result = UserView.render("user.json", %{user: user})
  69. assert result["id"] == user.ap_id
  70. %{
  71. "sharedInbox" => _,
  72. "oauthAuthorizationEndpoint" => _,
  73. "oauthRegistrationEndpoint" => _,
  74. "oauthTokenEndpoint" => _
  75. } = result["endpoints"]
  76. end
  77. test "remote users have an empty endpoints structure" do
  78. user = insert(:user, local: false)
  79. {:ok, user} = User.ensure_keys_present(user)
  80. result = UserView.render("user.json", %{user: user})
  81. assert result["id"] == user.ap_id
  82. assert result["endpoints"] == %{}
  83. end
  84. test "instance users do not expose oAuth endpoints" do
  85. user = insert(:user, nickname: nil, local: true)
  86. {:ok, user} = User.ensure_keys_present(user)
  87. result = UserView.render("user.json", %{user: user})
  88. refute result["endpoints"]["oauthAuthorizationEndpoint"]
  89. refute result["endpoints"]["oauthRegistrationEndpoint"]
  90. refute result["endpoints"]["oauthTokenEndpoint"]
  91. end
  92. end
  93. describe "followers" do
  94. test "sets totalItems to zero when followers are hidden" do
  95. user = insert(:user)
  96. other_user = insert(:user)
  97. {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
  98. assert %{"totalItems" => 1} = UserView.render("followers.json", %{user: user})
  99. user = Map.merge(user, %{hide_followers_count: true, hide_followers: true})
  100. refute UserView.render("followers.json", %{user: user}) |> Map.has_key?("totalItems")
  101. end
  102. test "sets correct totalItems when followers are hidden but the follower counter is not" do
  103. user = insert(:user)
  104. other_user = insert(:user)
  105. {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
  106. assert %{"totalItems" => 1} = UserView.render("followers.json", %{user: user})
  107. user = Map.merge(user, %{hide_followers_count: false, hide_followers: true})
  108. assert %{"totalItems" => 1} = UserView.render("followers.json", %{user: user})
  109. end
  110. end
  111. describe "following" do
  112. test "sets totalItems to zero when follows are hidden" do
  113. user = insert(:user)
  114. other_user = insert(:user)
  115. {:ok, user, _other_user, _activity} = CommonAPI.follow(user, other_user)
  116. assert %{"totalItems" => 1} = UserView.render("following.json", %{user: user})
  117. user = Map.merge(user, %{hide_follows_count: true, hide_follows: true})
  118. assert %{"totalItems" => 0} = UserView.render("following.json", %{user: user})
  119. end
  120. test "sets correct totalItems when follows are hidden but the follow counter is not" do
  121. user = insert(:user)
  122. other_user = insert(:user)
  123. {:ok, user, _other_user, _activity} = CommonAPI.follow(user, other_user)
  124. assert %{"totalItems" => 1} = UserView.render("following.json", %{user: user})
  125. user = Map.merge(user, %{hide_follows_count: false, hide_follows: true})
  126. assert %{"totalItems" => 1} = UserView.render("following.json", %{user: user})
  127. end
  128. end
  129. describe "acceptsChatMessages" do
  130. test "it returns this value if it is set" do
  131. true_user = insert(:user, accepts_chat_messages: true)
  132. false_user = insert(:user, accepts_chat_messages: false)
  133. nil_user = insert(:user, accepts_chat_messages: nil)
  134. assert %{"capabilities" => %{"acceptsChatMessages" => true}} =
  135. UserView.render("user.json", user: true_user)
  136. assert %{"capabilities" => %{"acceptsChatMessages" => false}} =
  137. UserView.render("user.json", user: false_user)
  138. refute Map.has_key?(
  139. UserView.render("user.json", user: nil_user)["capabilities"],
  140. "acceptsChatMessages"
  141. )
  142. end
  143. end
  144. end