Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / test / web / activity_pub / mrf / activity_expiration_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.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
11 test "adds `expires_at` property" do
12 assert {:ok, %{"type" => "Create", "expires_at" => expires_at}} =
13 ActivityExpirationPolicy.filter(%{
14 "id" => @id,
15 "type" => "Create",
16 "object" => %{"type" => "Note"}
17 })
18
19 assert Timex.diff(expires_at, NaiveDateTime.utc_now(), :days) == 364
20 end
21
22 test "keeps existing `expires_at` if it less than the config setting" do
23 expires_at = NaiveDateTime.utc_now() |> Timex.shift(days: 1)
24
25 assert {:ok, %{"type" => "Create", "expires_at" => ^expires_at}} =
26 ActivityExpirationPolicy.filter(%{
27 "id" => @id,
28 "type" => "Create",
29 "expires_at" => expires_at,
30 "object" => %{"type" => "Note"}
31 })
32 end
33
34 test "overwrites existing `expires_at` if it greater than the config setting" do
35 too_distant_future = NaiveDateTime.utc_now() |> Timex.shift(years: 2)
36
37 assert {:ok, %{"type" => "Create", "expires_at" => expires_at}} =
38 ActivityExpirationPolicy.filter(%{
39 "id" => @id,
40 "type" => "Create",
41 "expires_at" => too_distant_future,
42 "object" => %{"type" => "Note"}
43 })
44
45 assert Timex.diff(expires_at, NaiveDateTime.utc_now(), :days) == 364
46 end
47
48 test "ignores remote activities" do
49 assert {:ok, activity} =
50 ActivityExpirationPolicy.filter(%{
51 "id" => "https://example.com/123",
52 "type" => "Create",
53 "object" => %{"type" => "Note"}
54 })
55
56 refute Map.has_key?(activity, "expires_at")
57 end
58
59 test "ignores non-Create/Note activities" do
60 assert {:ok, activity} =
61 ActivityExpirationPolicy.filter(%{
62 "id" => "https://example.com/123",
63 "type" => "Follow"
64 })
65
66 refute Map.has_key?(activity, "expires_at")
67
68 assert {:ok, activity} =
69 ActivityExpirationPolicy.filter(%{
70 "id" => "https://example.com/123",
71 "type" => "Create",
72 "object" => %{"type" => "Cofe"}
73 })
74
75 refute Map.has_key?(activity, "expires_at")
76 end
77 end