logo

pleroma

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

visibilty_test.exs (6533B)


  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.VisibilityTest do
  5. use Pleroma.DataCase
  6. alias Pleroma.Activity
  7. alias Pleroma.Web.ActivityPub.Visibility
  8. alias Pleroma.Web.CommonAPI
  9. import Pleroma.Factory
  10. setup do
  11. user = insert(:user)
  12. mentioned = insert(:user)
  13. following = insert(:user)
  14. unrelated = insert(:user)
  15. {:ok, following} = Pleroma.User.follow(following, user)
  16. {:ok, list} = Pleroma.List.create("foo", user)
  17. Pleroma.List.follow(list, unrelated)
  18. {:ok, public} =
  19. CommonAPI.post(user, %{status: "@#{mentioned.nickname}", visibility: "public"})
  20. {:ok, private} =
  21. CommonAPI.post(user, %{status: "@#{mentioned.nickname}", visibility: "private"})
  22. {:ok, direct} =
  23. CommonAPI.post(user, %{status: "@#{mentioned.nickname}", visibility: "direct"})
  24. {:ok, unlisted} =
  25. CommonAPI.post(user, %{status: "@#{mentioned.nickname}", visibility: "unlisted"})
  26. {:ok, list} =
  27. CommonAPI.post(user, %{
  28. status: "@#{mentioned.nickname}",
  29. visibility: "list:#{list.id}"
  30. })
  31. %{
  32. public: public,
  33. private: private,
  34. direct: direct,
  35. unlisted: unlisted,
  36. user: user,
  37. mentioned: mentioned,
  38. following: following,
  39. unrelated: unrelated,
  40. list: list
  41. }
  42. end
  43. test "is_direct?", %{
  44. public: public,
  45. private: private,
  46. direct: direct,
  47. unlisted: unlisted,
  48. list: list
  49. } do
  50. assert Visibility.is_direct?(direct)
  51. refute Visibility.is_direct?(public)
  52. refute Visibility.is_direct?(private)
  53. refute Visibility.is_direct?(unlisted)
  54. assert Visibility.is_direct?(list)
  55. end
  56. test "is_public?", %{
  57. public: public,
  58. private: private,
  59. direct: direct,
  60. unlisted: unlisted,
  61. list: list
  62. } do
  63. refute Visibility.is_public?(direct)
  64. assert Visibility.is_public?(public)
  65. refute Visibility.is_public?(private)
  66. assert Visibility.is_public?(unlisted)
  67. refute Visibility.is_public?(list)
  68. end
  69. test "is_private?", %{
  70. public: public,
  71. private: private,
  72. direct: direct,
  73. unlisted: unlisted,
  74. list: list
  75. } do
  76. refute Visibility.is_private?(direct)
  77. refute Visibility.is_private?(public)
  78. assert Visibility.is_private?(private)
  79. refute Visibility.is_private?(unlisted)
  80. refute Visibility.is_private?(list)
  81. end
  82. test "is_list?", %{
  83. public: public,
  84. private: private,
  85. direct: direct,
  86. unlisted: unlisted,
  87. list: list
  88. } do
  89. refute Visibility.is_list?(direct)
  90. refute Visibility.is_list?(public)
  91. refute Visibility.is_list?(private)
  92. refute Visibility.is_list?(unlisted)
  93. assert Visibility.is_list?(list)
  94. end
  95. test "visible_for_user?", %{
  96. public: public,
  97. private: private,
  98. direct: direct,
  99. unlisted: unlisted,
  100. user: user,
  101. mentioned: mentioned,
  102. following: following,
  103. unrelated: unrelated,
  104. list: list
  105. } do
  106. # All visible to author
  107. assert Visibility.visible_for_user?(public, user)
  108. assert Visibility.visible_for_user?(private, user)
  109. assert Visibility.visible_for_user?(unlisted, user)
  110. assert Visibility.visible_for_user?(direct, user)
  111. assert Visibility.visible_for_user?(list, user)
  112. # All visible to a mentioned user
  113. assert Visibility.visible_for_user?(public, mentioned)
  114. assert Visibility.visible_for_user?(private, mentioned)
  115. assert Visibility.visible_for_user?(unlisted, mentioned)
  116. assert Visibility.visible_for_user?(direct, mentioned)
  117. assert Visibility.visible_for_user?(list, mentioned)
  118. # DM not visible for just follower
  119. assert Visibility.visible_for_user?(public, following)
  120. assert Visibility.visible_for_user?(private, following)
  121. assert Visibility.visible_for_user?(unlisted, following)
  122. refute Visibility.visible_for_user?(direct, following)
  123. refute Visibility.visible_for_user?(list, following)
  124. # Public and unlisted visible for unrelated user
  125. assert Visibility.visible_for_user?(public, unrelated)
  126. assert Visibility.visible_for_user?(unlisted, unrelated)
  127. refute Visibility.visible_for_user?(private, unrelated)
  128. refute Visibility.visible_for_user?(direct, unrelated)
  129. # Visible for a list member
  130. assert Visibility.visible_for_user?(list, unrelated)
  131. end
  132. test "doesn't die when the user doesn't exist",
  133. %{
  134. direct: direct,
  135. user: user
  136. } do
  137. Repo.delete(user)
  138. Cachex.clear(:user_cache)
  139. refute Visibility.is_private?(direct)
  140. end
  141. test "get_visibility", %{
  142. public: public,
  143. private: private,
  144. direct: direct,
  145. unlisted: unlisted,
  146. list: list
  147. } do
  148. assert Visibility.get_visibility(public) == "public"
  149. assert Visibility.get_visibility(private) == "private"
  150. assert Visibility.get_visibility(direct) == "direct"
  151. assert Visibility.get_visibility(unlisted) == "unlisted"
  152. assert Visibility.get_visibility(list) == "list"
  153. end
  154. test "get_visibility with directMessage flag" do
  155. assert Visibility.get_visibility(%{data: %{"directMessage" => true}}) == "direct"
  156. end
  157. test "get_visibility with listMessage flag" do
  158. assert Visibility.get_visibility(%{data: %{"listMessage" => ""}}) == "list"
  159. end
  160. describe "entire_thread_visible_for_user?/2" do
  161. test "returns false if not found activity", %{user: user} do
  162. refute Visibility.entire_thread_visible_for_user?(%Activity{}, user)
  163. end
  164. test "returns true if activity hasn't 'Create' type", %{user: user} do
  165. activity = insert(:like_activity)
  166. assert Visibility.entire_thread_visible_for_user?(activity, user)
  167. end
  168. test "returns false when invalid recipients", %{user: user} do
  169. author = insert(:user)
  170. activity =
  171. insert(:note_activity,
  172. note:
  173. insert(:note,
  174. user: author,
  175. data: %{"to" => ["test-user"]}
  176. )
  177. )
  178. refute Visibility.entire_thread_visible_for_user?(activity, user)
  179. end
  180. test "returns true if user following to author" do
  181. author = insert(:user)
  182. user = insert(:user)
  183. Pleroma.User.follow(user, author)
  184. activity =
  185. insert(:note_activity,
  186. note:
  187. insert(:note,
  188. user: author,
  189. data: %{"to" => [user.ap_id]}
  190. )
  191. )
  192. assert Visibility.entire_thread_visible_for_user?(activity, user)
  193. end
  194. end
  195. end