First
[anni] / test / pleroma / web / activity_pub / mrf / activity_expiration_policy_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicyTest do
6   use ExUnit.Case, async: true
7   alias Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicy
8
9   @id Pleroma.Web.Endpoint.url() <> "/activities/cofe"
10   @local_actor Pleroma.Web.Endpoint.url() <> "/users/cofe"
11
12   test "adds `expires_at` property" do
13     assert {:ok, %{"type" => "Create", "expires_at" => expires_at}} =
14              ActivityExpirationPolicy.filter(%{
15                "id" => @id,
16                "actor" => @local_actor,
17                "type" => "Create",
18                "object" => %{"type" => "Note"}
19              })
20
21     assert Timex.diff(expires_at, DateTime.utc_now(), :days) == 364
22   end
23
24   test "keeps existing `expires_at` if it less than the config setting" do
25     expires_at = DateTime.utc_now() |> Timex.shift(days: 1)
26
27     assert {:ok, %{"type" => "Create", "expires_at" => ^expires_at}} =
28              ActivityExpirationPolicy.filter(%{
29                "id" => @id,
30                "actor" => @local_actor,
31                "type" => "Create",
32                "expires_at" => expires_at,
33                "object" => %{"type" => "Note"}
34              })
35   end
36
37   test "overwrites existing `expires_at` if it greater than the config setting" do
38     too_distant_future = DateTime.utc_now() |> Timex.shift(years: 2)
39
40     assert {:ok, %{"type" => "Create", "expires_at" => expires_at}} =
41              ActivityExpirationPolicy.filter(%{
42                "id" => @id,
43                "actor" => @local_actor,
44                "type" => "Create",
45                "expires_at" => too_distant_future,
46                "object" => %{"type" => "Note"}
47              })
48
49     assert Timex.diff(expires_at, DateTime.utc_now(), :days) == 364
50   end
51
52   test "ignores remote activities" do
53     assert {:ok, activity} =
54              ActivityExpirationPolicy.filter(%{
55                "id" => "https://example.com/123",
56                "actor" => "https://example.com/users/cofe",
57                "type" => "Create",
58                "object" => %{"type" => "Note"}
59              })
60
61     refute Map.has_key?(activity, "expires_at")
62   end
63
64   test "ignores non-Create/Note activities" do
65     assert {:ok, activity} =
66              ActivityExpirationPolicy.filter(%{
67                "id" => "https://example.com/123",
68                "actor" => "https://example.com/users/cofe",
69                "type" => "Follow"
70              })
71
72     refute Map.has_key?(activity, "expires_at")
73
74     assert {:ok, activity} =
75              ActivityExpirationPolicy.filter(%{
76                "id" => "https://example.com/123",
77                "actor" => "https://example.com/users/cofe",
78                "type" => "Create",
79                "object" => %{"type" => "Cofe"}
80              })
81
82     refute Map.has_key?(activity, "expires_at")
83   end
84 end