1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.ScheduledActivityTest do
8 alias Pleroma.ScheduledActivity
11 setup do: clear_config([ScheduledActivity, :enabled])
14 DataCase.ensure_local_uploader(context)
17 describe "creation" do
18 test "scheduled activities with jobs when ScheduledActivity enabled" do
19 Pleroma.Config.put([ScheduledActivity, :enabled], true)
23 NaiveDateTime.utc_now()
24 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
25 |> NaiveDateTime.to_iso8601()
27 attrs = %{params: %{}, scheduled_at: today}
28 {:ok, sa1} = ScheduledActivity.create(user, attrs)
29 {:ok, sa2} = ScheduledActivity.create(user, attrs)
32 Repo.all(from(j in Oban.Job, where: j.queue == "scheduled_activities", select: j.args))
34 assert jobs == [%{"activity_id" => sa1.id}, %{"activity_id" => sa2.id}]
37 test "scheduled activities without jobs when ScheduledActivity disabled" do
38 Pleroma.Config.put([ScheduledActivity, :enabled], false)
42 NaiveDateTime.utc_now()
43 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
44 |> NaiveDateTime.to_iso8601()
46 attrs = %{params: %{}, scheduled_at: today}
47 {:ok, _sa1} = ScheduledActivity.create(user, attrs)
48 {:ok, _sa2} = ScheduledActivity.create(user, attrs)
51 Repo.all(from(j in Oban.Job, where: j.queue == "scheduled_activities", select: j.args))
56 test "when daily user limit is exceeded" do
60 NaiveDateTime.utc_now()
61 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
62 |> NaiveDateTime.to_iso8601()
64 attrs = %{params: %{}, scheduled_at: today}
65 {:ok, _} = ScheduledActivity.create(user, attrs)
66 {:ok, _} = ScheduledActivity.create(user, attrs)
68 {:error, changeset} = ScheduledActivity.create(user, attrs)
69 assert changeset.errors == [scheduled_at: {"daily limit exceeded", []}]
72 test "when total user limit is exceeded" do
76 NaiveDateTime.utc_now()
77 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
78 |> NaiveDateTime.to_iso8601()
81 NaiveDateTime.utc_now()
82 |> NaiveDateTime.add(:timer.hours(36), :millisecond)
83 |> NaiveDateTime.to_iso8601()
85 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: today})
86 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: today})
87 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: tomorrow})
88 {:error, changeset} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: tomorrow})
89 assert changeset.errors == [scheduled_at: {"total limit exceeded", []}]
92 test "when scheduled_at is earlier than 5 minute from now" do
96 NaiveDateTime.utc_now()
97 |> NaiveDateTime.add(:timer.minutes(4), :millisecond)
98 |> NaiveDateTime.to_iso8601()
100 attrs = %{params: %{}, scheduled_at: scheduled_at}
101 {:error, changeset} = ScheduledActivity.create(user, attrs)
102 assert changeset.errors == [scheduled_at: {"must be at least 5 minutes from now", []}]