2019-04-02 00:31:01 +02:00
|
|
|
# Pleroma: A lightweight social networking server
|
2019-09-18 23:20:54 +02:00
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
2019-04-02 00:31:01 +02:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.ScheduledActivityTest do
|
|
|
|
use Pleroma.DataCase
|
|
|
|
alias Pleroma.DataCase
|
|
|
|
alias Pleroma.ScheduledActivity
|
|
|
|
import Pleroma.Factory
|
|
|
|
|
2019-11-27 07:26:37 +01:00
|
|
|
clear_config([ScheduledActivity, :enabled])
|
|
|
|
|
2019-04-02 00:31:01 +02:00
|
|
|
setup context do
|
|
|
|
DataCase.ensure_local_uploader(context)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "creation" do
|
2019-12-04 19:18:05 +01:00
|
|
|
test "scheduled activities with jobs when ScheduledActivity enabled" do
|
|
|
|
Pleroma.Config.put([ScheduledActivity, :enabled], true)
|
2019-04-02 00:31:01 +02:00
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
today =
|
|
|
|
NaiveDateTime.utc_now()
|
|
|
|
|> NaiveDateTime.add(:timer.minutes(6), :millisecond)
|
|
|
|
|> NaiveDateTime.to_iso8601()
|
|
|
|
|
|
|
|
attrs = %{params: %{}, scheduled_at: today}
|
2019-12-04 15:28:57 +01:00
|
|
|
{:ok, sa1} = ScheduledActivity.create(user, attrs)
|
|
|
|
{:ok, sa2} = ScheduledActivity.create(user, attrs)
|
2019-12-03 19:30:10 +01:00
|
|
|
|
2019-12-04 15:28:57 +01:00
|
|
|
jobs =
|
|
|
|
Repo.all(from(j in Oban.Job, where: j.queue == "scheduled_activities", select: j.args))
|
|
|
|
|
|
|
|
assert jobs == [%{"activity_id" => sa1.id}, %{"activity_id" => sa2.id}]
|
2019-12-04 19:18:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
test "scheduled activities without jobs when ScheduledActivity disabled" do
|
|
|
|
Pleroma.Config.put([ScheduledActivity, :enabled], false)
|
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
today =
|
|
|
|
NaiveDateTime.utc_now()
|
|
|
|
|> NaiveDateTime.add(:timer.minutes(6), :millisecond)
|
|
|
|
|> NaiveDateTime.to_iso8601()
|
|
|
|
|
|
|
|
attrs = %{params: %{}, scheduled_at: today}
|
|
|
|
{:ok, _sa1} = ScheduledActivity.create(user, attrs)
|
|
|
|
{:ok, _sa2} = ScheduledActivity.create(user, attrs)
|
|
|
|
|
|
|
|
jobs =
|
|
|
|
Repo.all(from(j in Oban.Job, where: j.queue == "scheduled_activities", select: j.args))
|
|
|
|
|
|
|
|
assert jobs == []
|
|
|
|
end
|
|
|
|
|
|
|
|
test "when daily user limit is exceeded" do
|
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
today =
|
|
|
|
NaiveDateTime.utc_now()
|
|
|
|
|> NaiveDateTime.add(:timer.minutes(6), :millisecond)
|
|
|
|
|> NaiveDateTime.to_iso8601()
|
|
|
|
|
|
|
|
attrs = %{params: %{}, scheduled_at: today}
|
|
|
|
{:ok, _} = ScheduledActivity.create(user, attrs)
|
|
|
|
{:ok, _} = ScheduledActivity.create(user, attrs)
|
|
|
|
|
2019-04-02 00:31:01 +02:00
|
|
|
{:error, changeset} = ScheduledActivity.create(user, attrs)
|
|
|
|
assert changeset.errors == [scheduled_at: {"daily limit exceeded", []}]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "when total user limit is exceeded" do
|
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
today =
|
|
|
|
NaiveDateTime.utc_now()
|
|
|
|
|> NaiveDateTime.add(:timer.minutes(6), :millisecond)
|
|
|
|
|> NaiveDateTime.to_iso8601()
|
|
|
|
|
|
|
|
tomorrow =
|
|
|
|
NaiveDateTime.utc_now()
|
2019-04-03 17:55:04 +02:00
|
|
|
|> NaiveDateTime.add(:timer.hours(36), :millisecond)
|
2019-04-02 00:31:01 +02:00
|
|
|
|> NaiveDateTime.to_iso8601()
|
|
|
|
|
|
|
|
{:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: today})
|
|
|
|
{:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: today})
|
|
|
|
{:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: tomorrow})
|
|
|
|
{:error, changeset} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: tomorrow})
|
|
|
|
assert changeset.errors == [scheduled_at: {"total limit exceeded", []}]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "when scheduled_at is earlier than 5 minute from now" do
|
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
scheduled_at =
|
|
|
|
NaiveDateTime.utc_now()
|
|
|
|
|> NaiveDateTime.add(:timer.minutes(4), :millisecond)
|
|
|
|
|> NaiveDateTime.to_iso8601()
|
|
|
|
|
|
|
|
attrs = %{params: %{}, scheduled_at: scheduled_at}
|
|
|
|
{:error, changeset} = ScheduledActivity.create(user, attrs)
|
|
|
|
assert changeset.errors == [scheduled_at: {"must be at least 5 minutes from now", []}]
|
|
|
|
end
|
|
|
|
end
|
2019-11-27 07:26:37 +01:00
|
|
|
|
|
|
|
test "creates a status from the scheduled activity" do
|
|
|
|
Pleroma.Config.put([ScheduledActivity, :enabled], true)
|
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
naive_datetime =
|
|
|
|
NaiveDateTime.add(
|
|
|
|
NaiveDateTime.utc_now(),
|
|
|
|
-:timer.minutes(2),
|
|
|
|
:millisecond
|
|
|
|
)
|
|
|
|
|
|
|
|
scheduled_activity =
|
|
|
|
insert(
|
|
|
|
:scheduled_activity,
|
|
|
|
scheduled_at: naive_datetime,
|
|
|
|
user: user,
|
|
|
|
params: %{status: "hi"}
|
|
|
|
)
|
|
|
|
|
2019-12-03 19:30:10 +01:00
|
|
|
Pleroma.Workers.ScheduledActivityWorker.perform(
|
|
|
|
%{"activity_id" => scheduled_activity.id},
|
|
|
|
:pid
|
|
|
|
)
|
2019-11-27 07:26:37 +01:00
|
|
|
|
|
|
|
refute Repo.get(ScheduledActivity, scheduled_activity.id)
|
|
|
|
activity = Repo.all(Pleroma.Activity) |> Enum.find(&(&1.actor == user.ap_id))
|
|
|
|
assert Pleroma.Object.normalize(activity).data["content"] == "hi"
|
|
|
|
end
|
2019-04-02 00:31:01 +02:00
|
|
|
end
|