Merge branch 'stable' into stable-sync/2.1.1
[akkoma] / test / activity_expiration_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.ActivityExpirationTest do
6 use Pleroma.DataCase
7 alias Pleroma.ActivityExpiration
8 import Pleroma.Factory
9
10 setup do: clear_config([ActivityExpiration, :enabled])
11
12 test "finds activities due to be deleted only" do
13 activity = insert(:note_activity)
14
15 expiration_due =
16 insert(:expiration_in_the_past, %{activity_id: activity.id}) |> Repo.preload(:activity)
17
18 activity2 = insert(:note_activity)
19 insert(:expiration_in_the_future, %{activity_id: activity2.id})
20
21 expirations = ActivityExpiration.due_expirations()
22
23 assert length(expirations) == 1
24 assert hd(expirations) == expiration_due
25 end
26
27 test "denies expirations that don't live long enough" do
28 activity = insert(:note_activity)
29 now = NaiveDateTime.utc_now()
30 assert {:error, _} = ActivityExpiration.create(activity, now)
31 end
32
33 test "deletes an expiration activity" do
34 Pleroma.Config.put([ActivityExpiration, :enabled], true)
35 activity = insert(:note_activity)
36
37 naive_datetime =
38 NaiveDateTime.add(
39 NaiveDateTime.utc_now(),
40 -:timer.minutes(2),
41 :millisecond
42 )
43
44 expiration =
45 insert(
46 :expiration_in_the_past,
47 %{activity_id: activity.id, scheduled_at: naive_datetime}
48 )
49
50 Pleroma.Workers.Cron.PurgeExpiredActivitiesWorker.perform(%Oban.Job{})
51
52 refute Pleroma.Repo.get(Pleroma.Activity, activity.id)
53 refute Pleroma.Repo.get(Pleroma.ActivityExpiration, expiration.id)
54 end
55 end