1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.ScheduledActivityTest do
8 alias Pleroma.ScheduledActivity
10 import Pleroma.Factory
12 setup do: clear_config([ScheduledActivity, :enabled])
14 setup [:ensure_local_uploader]
16 describe "creation" do
17 test "scheduled activities with jobs when ScheduledActivity enabled" do
18 clear_config([ScheduledActivity, :enabled], true)
22 NaiveDateTime.utc_now()
23 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
24 |> NaiveDateTime.to_iso8601()
26 attrs = %{params: %{}, scheduled_at: today}
27 {:ok, sa1} = ScheduledActivity.create(user, attrs)
28 {:ok, sa2} = ScheduledActivity.create(user, attrs)
31 Repo.all(from(j in Oban.Job, where: j.queue == "scheduled_activities", select: j.args))
33 assert jobs == [%{"activity_id" => sa1.id}, %{"activity_id" => sa2.id}]
36 test "scheduled activities without jobs when ScheduledActivity disabled" do
37 clear_config([ScheduledActivity, :enabled], false)
41 NaiveDateTime.utc_now()
42 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
43 |> NaiveDateTime.to_iso8601()
45 attrs = %{params: %{}, scheduled_at: today}
46 {:ok, _sa1} = ScheduledActivity.create(user, attrs)
47 {:ok, _sa2} = ScheduledActivity.create(user, attrs)
50 Repo.all(from(j in Oban.Job, where: j.queue == "scheduled_activities", select: j.args))
55 test "when daily user limit is exceeded" do
59 NaiveDateTime.utc_now()
60 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
61 |> NaiveDateTime.to_iso8601()
63 attrs = %{params: %{}, scheduled_at: today}
64 {:ok, _} = ScheduledActivity.create(user, attrs)
65 {:ok, _} = ScheduledActivity.create(user, attrs)
67 {:error, changeset} = ScheduledActivity.create(user, attrs)
68 assert changeset.errors == [scheduled_at: {"daily limit exceeded", []}]
71 test "when total user limit is exceeded" do
75 NaiveDateTime.utc_now()
76 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
77 |> NaiveDateTime.to_iso8601()
80 NaiveDateTime.utc_now()
81 |> NaiveDateTime.add(:timer.hours(36), :millisecond)
82 |> NaiveDateTime.to_iso8601()
84 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: today})
85 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: today})
86 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: tomorrow})
87 {:error, changeset} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: tomorrow})
88 assert changeset.errors == [scheduled_at: {"total limit exceeded", []}]
91 test "when scheduled_at is earlier than 5 minute from now" do
95 NaiveDateTime.utc_now()
96 |> NaiveDateTime.add(:timer.minutes(4), :millisecond)
97 |> NaiveDateTime.to_iso8601()
99 attrs = %{params: %{}, scheduled_at: scheduled_at}
100 {:error, changeset} = ScheduledActivity.create(user, attrs)
101 assert changeset.errors == [scheduled_at: {"must be at least 5 minutes from now", []}]