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