logo

pleroma

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

object_age_policy_test.exs (3919B)


  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.ObjectAgePolicyTest do
  5. use Pleroma.DataCase
  6. alias Pleroma.Config
  7. alias Pleroma.User
  8. alias Pleroma.Web.ActivityPub.MRF.ObjectAgePolicy
  9. alias Pleroma.Web.ActivityPub.Visibility
  10. setup do:
  11. clear_config(:mrf_object_age,
  12. threshold: 172_800,
  13. actions: [:delist, :strip_followers]
  14. )
  15. setup_all do
  16. Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
  17. :ok
  18. end
  19. defp get_old_message do
  20. File.read!("test/fixtures/mastodon-post-activity.json")
  21. |> Poison.decode!()
  22. end
  23. defp get_new_message do
  24. old_message = get_old_message()
  25. new_object =
  26. old_message
  27. |> Map.get("object")
  28. |> Map.put("published", DateTime.utc_now() |> DateTime.to_iso8601())
  29. old_message
  30. |> Map.put("object", new_object)
  31. end
  32. describe "with reject action" do
  33. test "works with objects with empty to or cc fields" do
  34. Config.put([:mrf_object_age, :actions], [:reject])
  35. data =
  36. get_old_message()
  37. |> Map.put("cc", nil)
  38. |> Map.put("to", nil)
  39. assert match?({:reject, _}, ObjectAgePolicy.filter(data))
  40. end
  41. test "it rejects an old post" do
  42. Config.put([:mrf_object_age, :actions], [:reject])
  43. data = get_old_message()
  44. assert match?({:reject, _}, ObjectAgePolicy.filter(data))
  45. end
  46. test "it allows a new post" do
  47. Config.put([:mrf_object_age, :actions], [:reject])
  48. data = get_new_message()
  49. assert match?({:ok, _}, ObjectAgePolicy.filter(data))
  50. end
  51. end
  52. describe "with delist action" do
  53. test "works with objects with empty to or cc fields" do
  54. Config.put([:mrf_object_age, :actions], [:delist])
  55. data =
  56. get_old_message()
  57. |> Map.put("cc", nil)
  58. |> Map.put("to", nil)
  59. {:ok, _u} = User.get_or_fetch_by_ap_id(data["actor"])
  60. {:ok, data} = ObjectAgePolicy.filter(data)
  61. assert Visibility.get_visibility(%{data: data}) == "unlisted"
  62. end
  63. test "it delists an old post" do
  64. Config.put([:mrf_object_age, :actions], [:delist])
  65. data = get_old_message()
  66. {:ok, _u} = User.get_or_fetch_by_ap_id(data["actor"])
  67. {:ok, data} = ObjectAgePolicy.filter(data)
  68. assert Visibility.get_visibility(%{data: data}) == "unlisted"
  69. end
  70. test "it allows a new post" do
  71. Config.put([:mrf_object_age, :actions], [:delist])
  72. data = get_new_message()
  73. {:ok, _user} = User.get_or_fetch_by_ap_id(data["actor"])
  74. assert match?({:ok, ^data}, ObjectAgePolicy.filter(data))
  75. end
  76. end
  77. describe "with strip_followers action" do
  78. test "works with objects with empty to or cc fields" do
  79. Config.put([:mrf_object_age, :actions], [:strip_followers])
  80. data =
  81. get_old_message()
  82. |> Map.put("cc", nil)
  83. |> Map.put("to", nil)
  84. {:ok, user} = User.get_or_fetch_by_ap_id(data["actor"])
  85. {:ok, data} = ObjectAgePolicy.filter(data)
  86. refute user.follower_address in data["to"]
  87. refute user.follower_address in data["cc"]
  88. end
  89. test "it strips followers collections from an old post" do
  90. Config.put([:mrf_object_age, :actions], [:strip_followers])
  91. data = get_old_message()
  92. {:ok, user} = User.get_or_fetch_by_ap_id(data["actor"])
  93. {:ok, data} = ObjectAgePolicy.filter(data)
  94. refute user.follower_address in data["to"]
  95. refute user.follower_address in data["cc"]
  96. end
  97. test "it allows a new post" do
  98. Config.put([:mrf_object_age, :actions], [:strip_followers])
  99. data = get_new_message()
  100. {:ok, _u} = User.get_or_fetch_by_ap_id(data["actor"])
  101. assert match?({:ok, ^data}, ObjectAgePolicy.filter(data))
  102. end
  103. end
  104. end