summaryrefslogtreecommitdiff
path: root/test/web/activity_pub/mrf/object_age_policy_test.exs
blob: 4815edd04da29a061294343b34cb4791d670ec67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Pleroma: A lightweight social networking server
# Copyright © 2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.ActivityPub.MRF.ObjectAgePolicyTest do
  use Pleroma.DataCase
  alias Pleroma.Config
  alias Pleroma.User
  alias Pleroma.Web.ActivityPub.MRF.ObjectAgePolicy
  alias Pleroma.Web.ActivityPub.Visibility

  clear_config([:mrf_object_age]) do
    Config.put(:mrf_object_age,
      threshold: 172_800,
      actions: [:delist, :strip_followers]
    )
  end

  setup_all do
    Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
    :ok
  end

  defp get_old_message do
    File.read!("test/fixtures/mastodon-post-activity.json")
    |> Poison.decode!()
  end

  defp get_new_message do
    old_message = get_old_message()

    new_object =
      old_message
      |> Map.get("object")
      |> Map.put("published", DateTime.utc_now() |> DateTime.to_iso8601())

    old_message
    |> Map.put("object", new_object)
  end

  describe "with reject action" do
    test "it rejects an old post" do
      Config.put([:mrf_object_age, :actions], [:reject])

      data = get_old_message()

      assert match?({:reject, _}, ObjectAgePolicy.filter(data))
    end

    test "it allows a new post" do
      Config.put([:mrf_object_age, :actions], [:reject])

      data = get_new_message()

      assert match?({:ok, _}, ObjectAgePolicy.filter(data))
    end
  end

  describe "with delist action" do
    test "it delists an old post" do
      Config.put([:mrf_object_age, :actions], [:delist])

      data = get_old_message()

      {:ok, _u} = User.get_or_fetch_by_ap_id(data["actor"])

      {:ok, data} = ObjectAgePolicy.filter(data)

      assert Visibility.get_visibility(%{data: data}) == "unlisted"
    end

    test "it allows a new post" do
      Config.put([:mrf_object_age, :actions], [:delist])

      data = get_new_message()

      {:ok, _user} = User.get_or_fetch_by_ap_id(data["actor"])

      assert match?({:ok, ^data}, ObjectAgePolicy.filter(data))
    end
  end

  describe "with strip_followers action" do
    test "it strips followers collections from an old post" do
      Config.put([:mrf_object_age, :actions], [:strip_followers])

      data = get_old_message()

      {:ok, user} = User.get_or_fetch_by_ap_id(data["actor"])

      {:ok, data} = ObjectAgePolicy.filter(data)

      refute user.follower_address in data["to"]
      refute user.follower_address in data["cc"]
    end

    test "it allows a new post" do
      Config.put([:mrf_object_age, :actions], [:strip_followers])

      data = get_new_message()

      {:ok, _u} = User.get_or_fetch_by_ap_id(data["actor"])

      assert match?({:ok, ^data}, ObjectAgePolicy.filter(data))
    end
  end
end