Fix MRF policies to also work with Update
[akkoma] / test / pleroma / web / activity_pub / mrf / activity_expiration_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.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
23 assert {:ok, %{"type" => "Update", "expires_at" => expires_at}} =
24 ActivityExpirationPolicy.filter(%{
25 "id" => @id,
26 "actor" => @local_actor,
27 "type" => "Update",
28 "object" => %{"type" => "Note"}
29 })
30
31 assert Timex.diff(expires_at, DateTime.utc_now(), :days) == 364
32 end
33
34 test "keeps existing `expires_at` if it less than the config setting" do
35 expires_at = DateTime.utc_now() |> Timex.shift(days: 1)
36
37 assert {:ok, %{"type" => "Create", "expires_at" => ^expires_at}} =
38 ActivityExpirationPolicy.filter(%{
39 "id" => @id,
40 "actor" => @local_actor,
41 "type" => "Create",
42 "expires_at" => expires_at,
43 "object" => %{"type" => "Note"}
44 })
45
46 assert {:ok, %{"type" => "Update", "expires_at" => ^expires_at}} =
47 ActivityExpirationPolicy.filter(%{
48 "id" => @id,
49 "actor" => @local_actor,
50 "type" => "Update",
51 "expires_at" => expires_at,
52 "object" => %{"type" => "Note"}
53 })
54 end
55
56 test "overwrites existing `expires_at` if it greater than the config setting" do
57 too_distant_future = DateTime.utc_now() |> Timex.shift(years: 2)
58
59 assert {:ok, %{"type" => "Create", "expires_at" => expires_at}} =
60 ActivityExpirationPolicy.filter(%{
61 "id" => @id,
62 "actor" => @local_actor,
63 "type" => "Create",
64 "expires_at" => too_distant_future,
65 "object" => %{"type" => "Note"}
66 })
67
68 assert Timex.diff(expires_at, DateTime.utc_now(), :days) == 364
69
70 assert {:ok, %{"type" => "Update", "expires_at" => expires_at}} =
71 ActivityExpirationPolicy.filter(%{
72 "id" => @id,
73 "actor" => @local_actor,
74 "type" => "Update",
75 "expires_at" => too_distant_future,
76 "object" => %{"type" => "Note"}
77 })
78
79 assert Timex.diff(expires_at, DateTime.utc_now(), :days) == 364
80 end
81
82 test "ignores remote activities" do
83 assert {:ok, activity} =
84 ActivityExpirationPolicy.filter(%{
85 "id" => "https://example.com/123",
86 "actor" => "https://example.com/users/cofe",
87 "type" => "Create",
88 "object" => %{"type" => "Note"}
89 })
90
91 refute Map.has_key?(activity, "expires_at")
92
93 assert {:ok, activity} =
94 ActivityExpirationPolicy.filter(%{
95 "id" => "https://example.com/123",
96 "actor" => "https://example.com/users/cofe",
97 "type" => "Update",
98 "object" => %{"type" => "Note"}
99 })
100
101 refute Map.has_key?(activity, "expires_at")
102 end
103
104 test "ignores non-Create/Update/Note activities" do
105 assert {:ok, activity} =
106 ActivityExpirationPolicy.filter(%{
107 "id" => "https://example.com/123",
108 "actor" => "https://example.com/users/cofe",
109 "type" => "Follow"
110 })
111
112 refute Map.has_key?(activity, "expires_at")
113
114 assert {:ok, activity} =
115 ActivityExpirationPolicy.filter(%{
116 "id" => "https://example.com/123",
117 "actor" => "https://example.com/users/cofe",
118 "type" => "Create",
119 "object" => %{"type" => "Cofe"}
120 })
121
122 refute Map.has_key?(activity, "expires_at")
123 end
124 end