ScheduledActivity: Use config mocking

This commit is contained in:
Lain Soykaf 2023-12-12 12:48:55 +04:00
parent 8da1fd329d
commit 22c4d89dbb
3 changed files with 31 additions and 11 deletions

View File

@ -150,6 +150,7 @@
config :pleroma, Pleroma.Uploaders.S3, ex_aws_impl: Pleroma.Uploaders.S3.ExAwsMock config :pleroma, Pleroma.Uploaders.S3, ex_aws_impl: Pleroma.Uploaders.S3.ExAwsMock
config :pleroma, Pleroma.Uploaders.S3, config_impl: Pleroma.UnstubbedConfigMock config :pleroma, Pleroma.Uploaders.S3, config_impl: Pleroma.UnstubbedConfigMock
config :pleroma, Pleroma.Upload, config_impl: Pleroma.UnstubbedConfigMock config :pleroma, Pleroma.Upload, config_impl: Pleroma.UnstubbedConfigMock
config :pleroma, Pleroma.ScheduledActivity, config_impl: Pleroma.UnstubbedConfigMock
if File.exists?("./config/test.secret.exs") do if File.exists?("./config/test.secret.exs") do
import_config "test.secret.exs" import_config "test.secret.exs"

View File

@ -6,7 +6,6 @@ defmodule Pleroma.ScheduledActivity do
use Ecto.Schema use Ecto.Schema
alias Ecto.Multi alias Ecto.Multi
alias Pleroma.Config
alias Pleroma.Repo alias Pleroma.Repo
alias Pleroma.ScheduledActivity alias Pleroma.ScheduledActivity
alias Pleroma.User alias Pleroma.User
@ -20,6 +19,8 @@ defmodule Pleroma.ScheduledActivity do
@min_offset :timer.minutes(5) @min_offset :timer.minutes(5)
@config_impl Application.compile_env(:pleroma, [__MODULE__, :config_impl], Pleroma.Config)
schema "scheduled_activities" do schema "scheduled_activities" do
belongs_to(:user, User, type: FlakeId.Ecto.CompatType) belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
field(:scheduled_at, :naive_datetime) field(:scheduled_at, :naive_datetime)
@ -87,7 +88,7 @@ def exceeds_daily_user_limit?(user_id, scheduled_at) do
|> where([sa], type(sa.scheduled_at, :date) == type(^scheduled_at, :date)) |> where([sa], type(sa.scheduled_at, :date) == type(^scheduled_at, :date))
|> select([sa], count(sa.id)) |> select([sa], count(sa.id))
|> Repo.one() |> Repo.one()
|> Kernel.>=(Config.get([ScheduledActivity, :daily_user_limit])) |> Kernel.>=(@config_impl.get([ScheduledActivity, :daily_user_limit]))
end end
def exceeds_total_user_limit?(user_id) do def exceeds_total_user_limit?(user_id) do
@ -95,7 +96,7 @@ def exceeds_total_user_limit?(user_id) do
|> where(user_id: ^user_id) |> where(user_id: ^user_id)
|> select([sa], count(sa.id)) |> select([sa], count(sa.id))
|> Repo.one() |> Repo.one()
|> Kernel.>=(Config.get([ScheduledActivity, :total_user_limit])) |> Kernel.>=(@config_impl.get([ScheduledActivity, :total_user_limit]))
end end
def far_enough?(scheduled_at) when is_binary(scheduled_at) do def far_enough?(scheduled_at) when is_binary(scheduled_at) do
@ -123,7 +124,7 @@ def new(%User{} = user, attrs) do
def create(%User{} = user, attrs) do def create(%User{} = user, attrs) do
Multi.new() Multi.new()
|> Multi.insert(:scheduled_activity, new(user, attrs)) |> Multi.insert(:scheduled_activity, new(user, attrs))
|> maybe_add_jobs(Config.get([ScheduledActivity, :enabled])) |> maybe_add_jobs(@config_impl.get([ScheduledActivity, :enabled]))
|> Repo.transaction() |> Repo.transaction()
|> transaction_response |> transaction_response
end end

View File

@ -3,19 +3,23 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.ScheduledActivityTest do defmodule Pleroma.ScheduledActivityTest do
use Pleroma.DataCase use Pleroma.DataCase, async: true
alias Pleroma.ScheduledActivity alias Pleroma.ScheduledActivity
alias Pleroma.UnstubbedConfigMock, as: ConfigMock
alias Pleroma.Test.StaticConfig
import Mox
import Pleroma.Factory import Pleroma.Factory
setup do: clear_config([ScheduledActivity, :enabled])
setup [:ensure_local_uploader]
describe "creation" do describe "creation" do
test "scheduled activities with jobs when ScheduledActivity enabled" do test "scheduled activities with jobs when ScheduledActivity enabled" do
clear_config([ScheduledActivity, :enabled], true) ConfigMock
|> stub(:get, fn
[ScheduledActivity, :enabled] -> true
path -> StaticConfig.get(path)
end)
user = insert(:user) user = insert(:user)
today = today =
@ -34,7 +38,12 @@ test "scheduled activities with jobs when ScheduledActivity enabled" do
end end
test "scheduled activities without jobs when ScheduledActivity disabled" do test "scheduled activities without jobs when ScheduledActivity disabled" do
clear_config([ScheduledActivity, :enabled], false) ConfigMock
|> stub(:get, fn
[ScheduledActivity, :enabled] -> false
path -> StaticConfig.get(path)
end)
user = insert(:user) user = insert(:user)
today = today =
@ -53,6 +62,9 @@ test "scheduled activities without jobs when ScheduledActivity disabled" do
end end
test "when daily user limit is exceeded" do test "when daily user limit is exceeded" do
ConfigMock
|> stub_with(StaticConfig)
user = insert(:user) user = insert(:user)
today = today =
@ -69,6 +81,9 @@ test "when daily user limit is exceeded" do
end end
test "when total user limit is exceeded" do test "when total user limit is exceeded" do
ConfigMock
|> stub_with(StaticConfig)
user = insert(:user) user = insert(:user)
today = today =
@ -89,6 +104,9 @@ test "when total user limit is exceeded" do
end end
test "when scheduled_at is earlier than 5 minute from now" do test "when scheduled_at is earlier than 5 minute from now" do
ConfigMock
|> stub_with(StaticConfig)
user = insert(:user) user = insert(:user)
scheduled_at = scheduled_at =