logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

purge_expired_activities_worker_test.exs (2656B)


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