Merge branch 'dkuku-develop-patch-66061' into 'develop'
[akkoma] / test / pleroma / web / activity_pub / mrf / object_age_policy_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.MRF.ObjectAgePolicyTest do
6 use Pleroma.DataCase
7 alias Pleroma.User
8 alias Pleroma.Web.ActivityPub.MRF.ObjectAgePolicy
9 alias Pleroma.Web.ActivityPub.Visibility
10
11 setup do:
12 clear_config(:mrf_object_age,
13 threshold: 172_800,
14 actions: [:delist, :strip_followers]
15 )
16
17 setup_all do
18 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
19 :ok
20 end
21
22 defp get_old_message do
23 File.read!("test/fixtures/mastodon-post-activity.json")
24 |> Jason.decode!()
25 |> Map.drop(["published"])
26 end
27
28 defp get_new_message do
29 old_message = get_old_message()
30
31 new_object =
32 old_message
33 |> Map.get("object")
34 |> Map.put("published", DateTime.utc_now() |> DateTime.to_iso8601())
35
36 old_message
37 |> Map.put("object", new_object)
38 end
39
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])
43
44 data =
45 get_old_message()
46 |> Map.put("cc", nil)
47 |> Map.put("to", nil)
48
49 assert match?({:reject, _}, ObjectAgePolicy.filter(data))
50 end
51
52 test "it rejects an old post" do
53 clear_config([:mrf_object_age, :actions], [:reject])
54
55 data = get_old_message()
56
57 assert match?({:reject, _}, ObjectAgePolicy.filter(data))
58 end
59
60 test "it allows a new post" do
61 clear_config([:mrf_object_age, :actions], [:reject])
62
63 data = get_new_message()
64
65 assert match?({:ok, _}, ObjectAgePolicy.filter(data))
66 end
67 end
68
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])
72
73 data =
74 get_old_message()
75 |> Map.put("cc", nil)
76 |> Map.put("to", nil)
77
78 {:ok, _u} = User.get_or_fetch_by_ap_id(data["actor"])
79
80 {:ok, data} = ObjectAgePolicy.filter(data)
81
82 assert Visibility.get_visibility(%{data: data}) == "unlisted"
83 end
84
85 test "it delists an old post" do
86 clear_config([:mrf_object_age, :actions], [:delist])
87
88 data = get_old_message()
89
90 {:ok, _u} = User.get_or_fetch_by_ap_id(data["actor"])
91
92 {:ok, data} = ObjectAgePolicy.filter(data)
93
94 assert Visibility.get_visibility(%{data: data}) == "unlisted"
95 end
96
97 test "it allows a new post" do
98 clear_config([:mrf_object_age, :actions], [:delist])
99
100 data = get_new_message()
101
102 {:ok, _user} = User.get_or_fetch_by_ap_id(data["actor"])
103
104 assert match?({:ok, ^data}, ObjectAgePolicy.filter(data))
105 end
106 end
107
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])
111
112 data =
113 get_old_message()
114 |> Map.put("cc", nil)
115 |> Map.put("to", nil)
116
117 {:ok, user} = User.get_or_fetch_by_ap_id(data["actor"])
118
119 {:ok, data} = ObjectAgePolicy.filter(data)
120
121 refute user.follower_address in data["to"]
122 refute user.follower_address in data["cc"]
123 end
124
125 test "it strips followers collections from an old post" do
126 clear_config([:mrf_object_age, :actions], [:strip_followers])
127
128 data = get_old_message()
129
130 {:ok, user} = User.get_or_fetch_by_ap_id(data["actor"])
131
132 {:ok, data} = ObjectAgePolicy.filter(data)
133
134 refute user.follower_address in data["to"]
135 refute user.follower_address in data["cc"]
136 end
137
138 test "it allows a new post" do
139 clear_config([:mrf_object_age, :actions], [:strip_followers])
140
141 data = get_new_message()
142
143 {:ok, _u} = User.get_or_fetch_by_ap_id(data["actor"])
144
145 assert match?({:ok, ^data}, ObjectAgePolicy.filter(data))
146 end
147 end
148 end