1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF.ObjectAgePolicyTest do
8 alias Pleroma.Web.ActivityPub.MRF.ObjectAgePolicy
9 alias Pleroma.Web.ActivityPub.Visibility
12 clear_config(:mrf_object_age,
14 actions: [:delist, :strip_followers]
18 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
22 defp get_old_message do
23 File.read!("test/fixtures/mastodon-post-activity.json")
25 |> Map.drop(["published"])
28 defp get_new_message do
29 old_message = get_old_message()
34 |> Map.put("published", DateTime.utc_now() |> DateTime.to_iso8601())
37 |> Map.put("object", new_object)
40 describe "with reject action" do
41 test "works with objects with empty to or cc fields" do
42 clear_config([:mrf_object_age, :actions], [:reject])
49 assert match?({:reject, _}, ObjectAgePolicy.filter(data))
52 test "it rejects an old post" do
53 clear_config([:mrf_object_age, :actions], [:reject])
55 data = get_old_message()
57 assert match?({:reject, _}, ObjectAgePolicy.filter(data))
60 test "it allows a new post" do
61 clear_config([:mrf_object_age, :actions], [:reject])
63 data = get_new_message()
65 assert match?({:ok, _}, ObjectAgePolicy.filter(data))
69 describe "with delist action" do
70 test "works with objects with empty to or cc fields" do
71 clear_config([:mrf_object_age, :actions], [:delist])
78 {:ok, _u} = User.get_or_fetch_by_ap_id(data["actor"])
80 {:ok, data} = ObjectAgePolicy.filter(data)
82 assert Visibility.get_visibility(%{data: data}) == "unlisted"
85 test "it delists an old post" do
86 clear_config([:mrf_object_age, :actions], [:delist])
88 data = get_old_message()
90 {:ok, _u} = User.get_or_fetch_by_ap_id(data["actor"])
92 {:ok, data} = ObjectAgePolicy.filter(data)
94 assert Visibility.get_visibility(%{data: data}) == "unlisted"
97 test "it allows a new post" do
98 clear_config([:mrf_object_age, :actions], [:delist])
100 data = get_new_message()
102 {:ok, _user} = User.get_or_fetch_by_ap_id(data["actor"])
104 assert match?({:ok, ^data}, ObjectAgePolicy.filter(data))
108 describe "with strip_followers action" do
109 test "works with objects with empty to or cc fields" do
110 clear_config([:mrf_object_age, :actions], [:strip_followers])
114 |> Map.put("cc", nil)
115 |> Map.put("to", nil)
117 {:ok, user} = User.get_or_fetch_by_ap_id(data["actor"])
119 {:ok, data} = ObjectAgePolicy.filter(data)
121 refute user.follower_address in data["to"]
122 refute user.follower_address in data["cc"]
125 test "it strips followers collections from an old post" do
126 clear_config([:mrf_object_age, :actions], [:strip_followers])
128 data = get_old_message()
130 {:ok, user} = User.get_or_fetch_by_ap_id(data["actor"])
132 {:ok, data} = ObjectAgePolicy.filter(data)
134 refute user.follower_address in data["to"]
135 refute user.follower_address in data["cc"]
138 test "it allows a new post" do
139 clear_config([:mrf_object_age, :actions], [:strip_followers])
141 data = get_new_message()
143 {:ok, _u} = User.get_or_fetch_by_ap_id(data["actor"])
145 assert match?({:ok, ^data}, ObjectAgePolicy.filter(data))