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