Merge remote-tracking branch 'origin/develop' into global-status-expiration
[akkoma] / test / workers / cron / purge_expired_activities_worker_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.Workers.Cron.PurgeExpiredActivitiesWorkerTest do
6 use Pleroma.DataCase
7
8 alias Pleroma.ActivityExpiration
9 alias Pleroma.Workers.Cron.PurgeExpiredActivitiesWorker
10
11 import Pleroma.Factory
12 import ExUnit.CaptureLog
13
14 clear_config([ActivityExpiration, :enabled])
15 clear_config([:instance, :rewrite_policy])
16
17 test "deletes an expiration activity" do
18 Pleroma.Config.put([ActivityExpiration, :enabled], true)
19 activity = insert(:note_activity)
20
21 naive_datetime =
22 NaiveDateTime.add(
23 NaiveDateTime.utc_now(),
24 -:timer.minutes(2),
25 :millisecond
26 )
27
28 expiration =
29 insert(
30 :expiration_in_the_past,
31 %{activity_id: activity.id, scheduled_at: naive_datetime}
32 )
33
34 Pleroma.Workers.Cron.PurgeExpiredActivitiesWorker.perform(:ops, :pid)
35
36 refute Pleroma.Repo.get(Pleroma.Activity, activity.id)
37 refute Pleroma.Repo.get(Pleroma.ActivityExpiration, expiration.id)
38 end
39
40 test "works with ActivityExpirationPolicy" do
41 Pleroma.Config.put([ActivityExpiration, :enabled], true)
42
43 Pleroma.Config.put(
44 [:instance, :rewrite_policy],
45 Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicy
46 )
47
48 user = insert(:user)
49
50 days = Pleroma.Config.get([:mrf_activity_expiration, :days], 365)
51
52 {:ok, %{id: id} = activity} = Pleroma.Web.CommonAPI.post(user, %{"status" => "cofe"})
53
54 past_date =
55 NaiveDateTime.utc_now() |> Timex.shift(days: -days) |> NaiveDateTime.truncate(:second)
56
57 activity
58 |> Repo.preload(:expiration)
59 |> Map.get(:expiration)
60 |> Ecto.Changeset.change(%{scheduled_at: past_date})
61 |> Repo.update!()
62
63 Pleroma.Workers.Cron.PurgeExpiredActivitiesWorker.perform(:ops, :pid)
64
65 assert [%{data: %{"type" => "Delete", "deleted_activity_id" => ^id}}] =
66 Pleroma.Repo.all(Pleroma.Activity)
67 end
68
69 describe "delete_activity/1" do
70 test "adds log message if activity isn't find" do
71 assert capture_log([level: :error], fn ->
72 PurgeExpiredActivitiesWorker.delete_activity(%ActivityExpiration{
73 activity_id: "test-activity"
74 })
75 end) =~ "Couldn't delete expired activity: not found activity"
76 end
77
78 test "adds log message if actor isn't find" do
79 assert capture_log([level: :error], fn ->
80 PurgeExpiredActivitiesWorker.delete_activity(%ActivityExpiration{
81 activity_id: "test-activity"
82 })
83 end) =~ "Couldn't delete expired activity: not found activity"
84 end
85 end
86 end