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, 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
import_config "test.secret.exs"

View File

@ -6,7 +6,6 @@ defmodule Pleroma.ScheduledActivity do
use Ecto.Schema
alias Ecto.Multi
alias Pleroma.Config
alias Pleroma.Repo
alias Pleroma.ScheduledActivity
alias Pleroma.User
@ -20,6 +19,8 @@ defmodule Pleroma.ScheduledActivity do
@min_offset :timer.minutes(5)
@config_impl Application.compile_env(:pleroma, [__MODULE__, :config_impl], Pleroma.Config)
schema "scheduled_activities" do
belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
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))
|> select([sa], count(sa.id))
|> Repo.one()
|> Kernel.>=(Config.get([ScheduledActivity, :daily_user_limit]))
|> Kernel.>=(@config_impl.get([ScheduledActivity, :daily_user_limit]))
end
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)
|> select([sa], count(sa.id))
|> Repo.one()
|> Kernel.>=(Config.get([ScheduledActivity, :total_user_limit]))
|> Kernel.>=(@config_impl.get([ScheduledActivity, :total_user_limit]))
end
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
Multi.new()
|> Multi.insert(:scheduled_activity, new(user, attrs))
|> maybe_add_jobs(Config.get([ScheduledActivity, :enabled]))
|> maybe_add_jobs(@config_impl.get([ScheduledActivity, :enabled]))
|> Repo.transaction()
|> transaction_response
end

View File

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