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