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
6 use Pleroma.DataCase, async: true
8 alias Pleroma.ScheduledActivity
9 alias Pleroma.Test.StaticConfig
10 alias Pleroma.UnstubbedConfigMock, as: ConfigMock
13 import Pleroma.Factory
15 describe "creation" do
16 test "scheduled activities with jobs when ScheduledActivity enabled" do
19 [ScheduledActivity, :enabled] -> true
20 path -> StaticConfig.get(path)
26 NaiveDateTime.utc_now()
27 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
28 |> NaiveDateTime.to_iso8601()
30 attrs = %{params: %{}, scheduled_at: today}
31 {:ok, sa1} = ScheduledActivity.create(user, attrs)
32 {:ok, sa2} = ScheduledActivity.create(user, attrs)
35 Repo.all(from(j in Oban.Job, where: j.queue == "scheduled_activities", select: j.args))
37 assert jobs == [%{"activity_id" => sa1.id}, %{"activity_id" => sa2.id}]
40 test "scheduled activities without jobs when ScheduledActivity disabled" do
43 [ScheduledActivity, :enabled] -> false
44 path -> StaticConfig.get(path)
50 NaiveDateTime.utc_now()
51 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
52 |> NaiveDateTime.to_iso8601()
54 attrs = %{params: %{}, scheduled_at: today}
55 {:ok, _sa1} = ScheduledActivity.create(user, attrs)
56 {:ok, _sa2} = ScheduledActivity.create(user, attrs)
59 Repo.all(from(j in Oban.Job, where: j.queue == "scheduled_activities", select: j.args))
64 test "when daily user limit is exceeded" do
66 |> stub_with(StaticConfig)
71 NaiveDateTime.utc_now()
72 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
73 |> NaiveDateTime.to_iso8601()
75 attrs = %{params: %{}, scheduled_at: today}
76 {:ok, _} = ScheduledActivity.create(user, attrs)
77 {:ok, _} = ScheduledActivity.create(user, attrs)
79 {:error, changeset} = ScheduledActivity.create(user, attrs)
80 assert changeset.errors == [scheduled_at: {"daily limit exceeded", []}]
83 test "when total user limit is exceeded" do
85 |> stub_with(StaticConfig)
90 NaiveDateTime.utc_now()
91 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
92 |> NaiveDateTime.to_iso8601()
95 NaiveDateTime.utc_now()
96 |> NaiveDateTime.add(:timer.hours(36), :millisecond)
97 |> NaiveDateTime.to_iso8601()
99 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: today})
100 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: today})
101 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: tomorrow})
102 {:error, changeset} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: tomorrow})
103 assert changeset.errors == [scheduled_at: {"total limit exceeded", []}]
106 test "when scheduled_at is earlier than 5 minute from now" do
108 |> stub_with(StaticConfig)
113 NaiveDateTime.utc_now()
114 |> NaiveDateTime.add(:timer.minutes(4), :millisecond)
115 |> NaiveDateTime.to_iso8601()
117 attrs = %{params: %{}, scheduled_at: scheduled_at}
118 {:error, changeset} = ScheduledActivity.create(user, attrs)
119 assert changeset.errors == [scheduled_at: {"must be at least 5 minutes from now", []}]