logo

pleroma

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

anti_link_spam_policy_test.exs (3914B)


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