logo

pleroma

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

anti_link_spam_policy_test.exs (4555B)


  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.ActivityPub.MRF.AntiLinkSpamPolicyTest do
  5. use Pleroma.DataCase
  6. import Pleroma.Factory
  7. import ExUnit.CaptureLog
  8. alias Pleroma.Web.ActivityPub.MRF
  9. alias Pleroma.Web.ActivityPub.MRF.AntiLinkSpamPolicy
  10. @linkless_message %{
  11. "type" => "Create",
  12. "object" => %{
  13. "content" => "hi world!"
  14. }
  15. }
  16. @linkful_message %{
  17. "type" => "Create",
  18. "object" => %{
  19. "content" => "<a href='https://example.com'>hi world!</a>"
  20. }
  21. }
  22. @response_message %{
  23. "type" => "Create",
  24. "object" => %{
  25. "name" => "yes",
  26. "type" => "Answer"
  27. }
  28. }
  29. describe "with new user" do
  30. test "it allows posts without links" do
  31. user = insert(:user, local: false)
  32. assert user.note_count == 0
  33. message =
  34. @linkless_message
  35. |> Map.put("actor", user.ap_id)
  36. {:ok, _message} = AntiLinkSpamPolicy.filter(message)
  37. end
  38. test "it disallows posts with links" do
  39. user = insert(:user, local: false)
  40. assert user.note_count == 0
  41. message = %{
  42. "type" => "Create",
  43. "actor" => user.ap_id,
  44. "object" => %{
  45. "formerRepresentations" => %{
  46. "type" => "OrderedCollection",
  47. "orderedItems" => [
  48. %{
  49. "content" => "<a href='https://example.com'>hi world!</a>"
  50. }
  51. ]
  52. },
  53. "content" => "mew"
  54. }
  55. }
  56. {:reject, _} = MRF.filter_one(AntiLinkSpamPolicy, message)
  57. end
  58. test "it allows posts with links for local users" do
  59. user = insert(:user)
  60. assert user.note_count == 0
  61. message =
  62. @linkful_message
  63. |> Map.put("actor", user.ap_id)
  64. {:ok, _message} = AntiLinkSpamPolicy.filter(message)
  65. end
  66. test "it disallows posts with links in history" do
  67. user = insert(:user, local: false)
  68. assert user.note_count == 0
  69. message =
  70. @linkful_message
  71. |> Map.put("actor", user.ap_id)
  72. {:reject, _} = AntiLinkSpamPolicy.filter(message)
  73. end
  74. end
  75. describe "with old user" do
  76. test "it allows posts without links" do
  77. user = insert(:user, note_count: 1)
  78. assert user.note_count == 1
  79. message =
  80. @linkless_message
  81. |> Map.put("actor", user.ap_id)
  82. {:ok, _message} = AntiLinkSpamPolicy.filter(message)
  83. end
  84. test "it allows posts with links" do
  85. user = insert(:user, note_count: 1)
  86. assert user.note_count == 1
  87. message =
  88. @linkful_message
  89. |> Map.put("actor", user.ap_id)
  90. {:ok, _message} = AntiLinkSpamPolicy.filter(message)
  91. end
  92. end
  93. describe "with followed new user" do
  94. test "it allows posts without links" do
  95. user = insert(:user, follower_count: 1)
  96. assert user.follower_count == 1
  97. message =
  98. @linkless_message
  99. |> Map.put("actor", user.ap_id)
  100. {:ok, _message} = AntiLinkSpamPolicy.filter(message)
  101. end
  102. test "it allows posts with links" do
  103. user = insert(:user, follower_count: 1)
  104. assert user.follower_count == 1
  105. message =
  106. @linkful_message
  107. |> Map.put("actor", user.ap_id)
  108. {:ok, _message} = AntiLinkSpamPolicy.filter(message)
  109. end
  110. end
  111. describe "with unknown actors" do
  112. setup do
  113. Tesla.Mock.mock(fn
  114. %{method: :get, url: "http://invalid.actor"} ->
  115. %Tesla.Env{status: 500, body: ""}
  116. end)
  117. :ok
  118. end
  119. test "it rejects posts without links" do
  120. message =
  121. @linkless_message
  122. |> Map.put("actor", "http://invalid.actor")
  123. assert capture_log(fn ->
  124. {:reject, _} = AntiLinkSpamPolicy.filter(message)
  125. end) =~ "[error] Could not decode user at fetch http://invalid.actor"
  126. end
  127. test "it rejects posts with links" do
  128. message =
  129. @linkful_message
  130. |> Map.put("actor", "http://invalid.actor")
  131. assert capture_log(fn ->
  132. {:reject, _} = AntiLinkSpamPolicy.filter(message)
  133. end) =~ "[error] Could not decode user at fetch http://invalid.actor"
  134. end
  135. end
  136. describe "with contentless-objects" do
  137. test "it does not reject them or error out" do
  138. user = insert(:user, note_count: 1)
  139. message =
  140. @response_message
  141. |> Map.put("actor", user.ap_id)
  142. {:ok, _message} = AntiLinkSpamPolicy.filter(message)
  143. end
  144. end
  145. end