Merge pull request 'feature/elasticsearch' (#1) from feature/elasticsearch into develop
[akkoma] / test / pleroma / scheduled_activity_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.ScheduledActivityTest do
6 use Pleroma.DataCase
7
8 alias Pleroma.ScheduledActivity
9
10 import Pleroma.Factory
11
12 setup do: clear_config([ScheduledActivity, :enabled])
13
14 setup [:ensure_local_uploader]
15
16 describe "creation" do
17 test "scheduled activities with jobs when ScheduledActivity enabled" do
18 clear_config([ScheduledActivity, :enabled], true)
19 user = insert(:user)
20
21 today =
22 NaiveDateTime.utc_now()
23 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
24 |> NaiveDateTime.to_iso8601()
25
26 attrs = %{params: %{}, scheduled_at: today}
27 {:ok, sa1} = ScheduledActivity.create(user, attrs)
28 {:ok, sa2} = ScheduledActivity.create(user, attrs)
29
30 jobs =
31 Repo.all(from(j in Oban.Job, where: j.queue == "scheduled_activities", select: j.args))
32
33 assert jobs == [%{"activity_id" => sa1.id}, %{"activity_id" => sa2.id}]
34 end
35
36 test "scheduled activities without jobs when ScheduledActivity disabled" do
37 clear_config([ScheduledActivity, :enabled], false)
38 user = insert(:user)
39
40 today =
41 NaiveDateTime.utc_now()
42 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
43 |> NaiveDateTime.to_iso8601()
44
45 attrs = %{params: %{}, scheduled_at: today}
46 {:ok, _sa1} = ScheduledActivity.create(user, attrs)
47 {:ok, _sa2} = ScheduledActivity.create(user, attrs)
48
49 jobs =
50 Repo.all(from(j in Oban.Job, where: j.queue == "scheduled_activities", select: j.args))
51
52 assert jobs == []
53 end
54
55 test "when daily user limit is exceeded" do
56 user = insert(:user)
57
58 today =
59 NaiveDateTime.utc_now()
60 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
61 |> NaiveDateTime.to_iso8601()
62
63 attrs = %{params: %{}, scheduled_at: today}
64 {:ok, _} = ScheduledActivity.create(user, attrs)
65 {:ok, _} = ScheduledActivity.create(user, attrs)
66
67 {:error, changeset} = ScheduledActivity.create(user, attrs)
68 assert changeset.errors == [scheduled_at: {"daily limit exceeded", []}]
69 end
70
71 test "when total user limit is exceeded" do
72 user = insert(:user)
73
74 today =
75 NaiveDateTime.utc_now()
76 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
77 |> NaiveDateTime.to_iso8601()
78
79 tomorrow =
80 NaiveDateTime.utc_now()
81 |> NaiveDateTime.add(:timer.hours(36), :millisecond)
82 |> NaiveDateTime.to_iso8601()
83
84 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: today})
85 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: today})
86 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: tomorrow})
87 {:error, changeset} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: tomorrow})
88 assert changeset.errors == [scheduled_at: {"total limit exceeded", []}]
89 end
90
91 test "when scheduled_at is earlier than 5 minute from now" do
92 user = insert(:user)
93
94 scheduled_at =
95 NaiveDateTime.utc_now()
96 |> NaiveDateTime.add(:timer.minutes(4), :millisecond)
97 |> NaiveDateTime.to_iso8601()
98
99 attrs = %{params: %{}, scheduled_at: scheduled_at}
100 {:error, changeset} = ScheduledActivity.create(user, attrs)
101 assert changeset.errors == [scheduled_at: {"must be at least 5 minutes from now", []}]
102 end
103 end
104 end