1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.ScheduledActivityTest do
8 alias Pleroma.ScheduledActivity
11 clear_config([ScheduledActivity, :enabled])
14 DataCase.ensure_local_uploader(context)
17 describe "creation" do
18 test "when daily user limit is exceeded" do
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}]
34 {:error, changeset} = ScheduledActivity.create(user, attrs)
35 assert changeset.errors == [scheduled_at: {"daily limit exceeded", []}]
38 test "when total user limit is exceeded" do
42 NaiveDateTime.utc_now()
43 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
44 |> NaiveDateTime.to_iso8601()
47 NaiveDateTime.utc_now()
48 |> NaiveDateTime.add(:timer.hours(36), :millisecond)
49 |> NaiveDateTime.to_iso8601()
51 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: today})
52 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: today})
53 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: tomorrow})
54 {:error, changeset} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: tomorrow})
55 assert changeset.errors == [scheduled_at: {"total limit exceeded", []}]
58 test "when scheduled_at is earlier than 5 minute from now" do
62 NaiveDateTime.utc_now()
63 |> NaiveDateTime.add(:timer.minutes(4), :millisecond)
64 |> NaiveDateTime.to_iso8601()
66 attrs = %{params: %{}, scheduled_at: scheduled_at}
67 {:error, changeset} = ScheduledActivity.create(user, attrs)
68 assert changeset.errors == [scheduled_at: {"must be at least 5 minutes from now", []}]
72 test "creates a status from the scheduled activity" do
73 Pleroma.Config.put([ScheduledActivity, :enabled], true)
78 NaiveDateTime.utc_now(),
86 scheduled_at: naive_datetime,
88 params: %{status: "hi"}
91 Pleroma.Workers.ScheduledActivityWorker.perform(
92 %{"activity_id" => scheduled_activity.id},
96 refute Repo.get(ScheduledActivity, scheduled_activity.id)
97 activity = Repo.all(Pleroma.Activity) |> Enum.find(&(&1.actor == user.ap_id))
98 assert Pleroma.Object.normalize(activity).data["content"] == "hi"