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