c49c65c0acb31c329aa109d9d960c67c4c77feec
[akkoma] / test / scheduled_activity_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 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.Config
8 alias Pleroma.DataCase
9 alias Pleroma.ScheduledActivity
10 alias Pleroma.Web.ActivityPub.ActivityPub
11 import Pleroma.Factory
12
13 setup context do
14 Config.put([ScheduledActivity, :daily_user_limit], 2)
15 Config.put([ScheduledActivity, :total_user_limit], 3)
16 DataCase.ensure_local_uploader(context)
17 end
18
19 describe "creation" do
20 test "when daily user limit is exceeded" do
21 user = insert(:user)
22
23 today =
24 NaiveDateTime.utc_now()
25 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
26 |> NaiveDateTime.to_iso8601()
27
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", []}]
33 end
34
35 test "when total user limit is exceeded" do
36 user = insert(:user)
37
38 today =
39 NaiveDateTime.utc_now()
40 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
41 |> NaiveDateTime.to_iso8601()
42
43 tomorrow =
44 NaiveDateTime.utc_now()
45 |> NaiveDateTime.add(:timer.hours(24), :millisecond)
46 |> NaiveDateTime.to_iso8601()
47
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", []}]
53 end
54
55 test "when scheduled_at is earlier than 5 minute from now" do
56 user = insert(:user)
57
58 scheduled_at =
59 NaiveDateTime.utc_now()
60 |> NaiveDateTime.add(:timer.minutes(4), :millisecond)
61 |> NaiveDateTime.to_iso8601()
62
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", []}]
66 end
67
68 test "excludes attachments belonging to another user" do
69 user = insert(:user)
70 another_user = insert(:user)
71
72 scheduled_at =
73 NaiveDateTime.utc_now()
74 |> NaiveDateTime.add(:timer.minutes(10), :millisecond)
75 |> NaiveDateTime.to_iso8601()
76
77 file = %Plug.Upload{
78 content_type: "image/jpg",
79 path: Path.absname("test/fixtures/image.jpg"),
80 filename: "an_image.jpg"
81 }
82
83 {:ok, user_upload} = ActivityPub.upload(file, actor: user.ap_id)
84 {:ok, another_user_upload} = ActivityPub.upload(file, actor: another_user.ap_id)
85
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"]
91 end
92 end
93 end