1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.ScheduledActivityTest do
9 alias Pleroma.ScheduledActivity
10 alias Pleroma.Web.ActivityPub.ActivityPub
11 import Pleroma.Factory
14 Config.put([ScheduledActivity, :daily_user_limit], 2)
15 Config.put([ScheduledActivity, :total_user_limit], 3)
16 DataCase.ensure_local_uploader(context)
19 describe "creation" do
20 test "when daily user limit is exceeded" do
24 NaiveDateTime.utc_now()
25 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
26 |> NaiveDateTime.to_iso8601()
28 attrs = %{params: %{}, scheduled_at: today}
29 {:ok, _} = ScheduledActivity.create(user, attrs)
30 {:ok, _} = ScheduledActivity.create(user, attrs)
31 {:error, changeset} = ScheduledActivity.create(user, attrs)
32 assert changeset.errors == [scheduled_at: {"daily limit exceeded", []}]
35 test "when total user limit is exceeded" do
39 NaiveDateTime.utc_now()
40 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
41 |> NaiveDateTime.to_iso8601()
44 NaiveDateTime.utc_now()
45 |> NaiveDateTime.add(:timer.hours(24), :millisecond)
46 |> NaiveDateTime.to_iso8601()
48 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: today})
49 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: today})
50 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: tomorrow})
51 {:error, changeset} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: tomorrow})
52 assert changeset.errors == [scheduled_at: {"total limit exceeded", []}]
55 test "when scheduled_at is earlier than 5 minute from now" do
59 NaiveDateTime.utc_now()
60 |> NaiveDateTime.add(:timer.minutes(4), :millisecond)
61 |> NaiveDateTime.to_iso8601()
63 attrs = %{params: %{}, scheduled_at: scheduled_at}
64 {:error, changeset} = ScheduledActivity.create(user, attrs)
65 assert changeset.errors == [scheduled_at: {"must be at least 5 minutes from now", []}]
68 test "excludes attachments belonging to another user" do
70 another_user = insert(:user)
73 NaiveDateTime.utc_now()
74 |> NaiveDateTime.add(:timer.minutes(10), :millisecond)
75 |> NaiveDateTime.to_iso8601()
78 content_type: "image/jpg",
79 path: Path.absname("test/fixtures/image.jpg"),
80 filename: "an_image.jpg"
83 {:ok, user_upload} = ActivityPub.upload(file, actor: user.ap_id)
84 {:ok, another_user_upload} = ActivityPub.upload(file, actor: another_user.ap_id)
86 media_ids = [user_upload.id, another_user_upload.id]
87 attrs = %{params: %{"media_ids" => media_ids}, scheduled_at: scheduled_at}
88 {:ok, scheduled_activity} = ScheduledActivity.create(user, attrs)
89 assert to_string(user_upload.id) in scheduled_activity.params["media_ids"]
90 refute to_string(another_user_upload.id) in scheduled_activity.params["media_ids"]