tests: Replace 2 second sleep with offseting updated_at
[akkoma] / test / scheduled_activity_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.ScheduledActivityTest do
6 use Pleroma.DataCase
7 alias Pleroma.DataCase
8 alias Pleroma.ScheduledActivity
9 import Pleroma.Factory
10
11 setup context do
12 DataCase.ensure_local_uploader(context)
13 end
14
15 describe "creation" do
16 test "when daily user limit is exceeded" do
17 user = insert(:user)
18
19 today =
20 NaiveDateTime.utc_now()
21 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
22 |> NaiveDateTime.to_iso8601()
23
24 attrs = %{params: %{}, scheduled_at: today}
25 {:ok, _} = ScheduledActivity.create(user, attrs)
26 {:ok, _} = ScheduledActivity.create(user, attrs)
27 {:error, changeset} = ScheduledActivity.create(user, attrs)
28 assert changeset.errors == [scheduled_at: {"daily limit exceeded", []}]
29 end
30
31 test "when total user limit is exceeded" do
32 user = insert(:user)
33
34 today =
35 NaiveDateTime.utc_now()
36 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
37 |> NaiveDateTime.to_iso8601()
38
39 tomorrow =
40 NaiveDateTime.utc_now()
41 |> NaiveDateTime.add(:timer.hours(36), :millisecond)
42 |> NaiveDateTime.to_iso8601()
43
44 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: today})
45 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: today})
46 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: tomorrow})
47 {:error, changeset} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: tomorrow})
48 assert changeset.errors == [scheduled_at: {"total limit exceeded", []}]
49 end
50
51 test "when scheduled_at is earlier than 5 minute from now" do
52 user = insert(:user)
53
54 scheduled_at =
55 NaiveDateTime.utc_now()
56 |> NaiveDateTime.add(:timer.minutes(4), :millisecond)
57 |> NaiveDateTime.to_iso8601()
58
59 attrs = %{params: %{}, scheduled_at: scheduled_at}
60 {:error, changeset} = ScheduledActivity.create(user, attrs)
61 assert changeset.errors == [scheduled_at: {"must be at least 5 minutes from now", []}]
62 end
63 end
64 end