logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

scheduled_activity_test.exs (3600B)


  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. defmodule Pleroma.ScheduledActivityTest do
  5. use Pleroma.DataCase
  6. alias Pleroma.DataCase
  7. alias Pleroma.ScheduledActivity
  8. import Pleroma.Factory
  9. setup do: clear_config([ScheduledActivity, :enabled])
  10. setup context do
  11. DataCase.ensure_local_uploader(context)
  12. end
  13. describe "creation" do
  14. test "scheduled activities with jobs when ScheduledActivity enabled" do
  15. Pleroma.Config.put([ScheduledActivity, :enabled], true)
  16. user = insert(:user)
  17. today =
  18. NaiveDateTime.utc_now()
  19. |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
  20. |> NaiveDateTime.to_iso8601()
  21. attrs = %{params: %{}, scheduled_at: today}
  22. {:ok, sa1} = ScheduledActivity.create(user, attrs)
  23. {:ok, sa2} = ScheduledActivity.create(user, attrs)
  24. jobs =
  25. Repo.all(from(j in Oban.Job, where: j.queue == "scheduled_activities", select: j.args))
  26. assert jobs == [%{"activity_id" => sa1.id}, %{"activity_id" => sa2.id}]
  27. end
  28. test "scheduled activities without jobs when ScheduledActivity disabled" do
  29. Pleroma.Config.put([ScheduledActivity, :enabled], false)
  30. user = insert(:user)
  31. today =
  32. NaiveDateTime.utc_now()
  33. |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
  34. |> NaiveDateTime.to_iso8601()
  35. attrs = %{params: %{}, scheduled_at: today}
  36. {:ok, _sa1} = ScheduledActivity.create(user, attrs)
  37. {:ok, _sa2} = ScheduledActivity.create(user, attrs)
  38. jobs =
  39. Repo.all(from(j in Oban.Job, where: j.queue == "scheduled_activities", select: j.args))
  40. assert jobs == []
  41. end
  42. test "when daily user limit is exceeded" do
  43. user = insert(:user)
  44. today =
  45. NaiveDateTime.utc_now()
  46. |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
  47. |> NaiveDateTime.to_iso8601()
  48. attrs = %{params: %{}, scheduled_at: today}
  49. {:ok, _} = ScheduledActivity.create(user, attrs)
  50. {:ok, _} = ScheduledActivity.create(user, attrs)
  51. {:error, changeset} = ScheduledActivity.create(user, attrs)
  52. assert changeset.errors == [scheduled_at: {"daily limit exceeded", []}]
  53. end
  54. test "when total user limit is exceeded" do
  55. user = insert(:user)
  56. today =
  57. NaiveDateTime.utc_now()
  58. |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
  59. |> NaiveDateTime.to_iso8601()
  60. tomorrow =
  61. NaiveDateTime.utc_now()
  62. |> NaiveDateTime.add(:timer.hours(36), :millisecond)
  63. |> NaiveDateTime.to_iso8601()
  64. {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: today})
  65. {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: today})
  66. {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: tomorrow})
  67. {:error, changeset} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: tomorrow})
  68. assert changeset.errors == [scheduled_at: {"total limit exceeded", []}]
  69. end
  70. test "when scheduled_at is earlier than 5 minute from now" do
  71. user = insert(:user)
  72. scheduled_at =
  73. NaiveDateTime.utc_now()
  74. |> NaiveDateTime.add(:timer.minutes(4), :millisecond)
  75. |> NaiveDateTime.to_iso8601()
  76. attrs = %{params: %{}, scheduled_at: scheduled_at}
  77. {:error, changeset} = ScheduledActivity.create(user, attrs)
  78. assert changeset.errors == [scheduled_at: {"must be at least 5 minutes from now", []}]
  79. end
  80. end
  81. end