Improved in-test config management functions.

This commit is contained in:
Ivan Tashkinov 2020-03-18 20:30:31 +03:00
parent 69341cbcba
commit ec3719f539
32 changed files with 89 additions and 155 deletions

View file

@ -10,9 +10,7 @@ defmodule Pleroma.Config.TransferTaskTest do
alias Pleroma.Config.TransferTask
alias Pleroma.ConfigDB
clear_config(:configurable_from_database) do
Pleroma.Config.put(:configurable_from_database, true)
end
clear_config(:configurable_from_database, true)
test "transfer config values from db to env" do
refute Application.get_env(:pleroma, :test_key)

View file

@ -11,9 +11,7 @@ defmodule Pleroma.ConversationTest do
import Pleroma.Factory
clear_config_all([:instance, :federating]) do
Pleroma.Config.put([:instance, :federating], true)
end
clear_config_all([:instance, :federating], true)
test "it goes through old direct conversations" do
user = insert(:user)

View file

@ -12,9 +12,7 @@ defmodule Pleroma.Web.RuntimeStaticPlugTest do
on_exit(fn -> File.rm_rf(@dir) end)
end
clear_config([:instance, :static_dir]) do
Pleroma.Config.put([:instance, :static_dir], @dir)
end
clear_config([:instance, :static_dir], @dir)
test "overrides index" do
bundled_index = get(build_conn(), "/")

View file

@ -9,9 +9,7 @@ defmodule Pleroma.Plugs.UserIsAdminPlugTest do
import Pleroma.Factory
describe "unless [:auth, :enforce_oauth_admin_scope_usage]," do
clear_config([:auth, :enforce_oauth_admin_scope_usage]) do
Pleroma.Config.put([:auth, :enforce_oauth_admin_scope_usage], false)
end
clear_config([:auth, :enforce_oauth_admin_scope_usage], false)
test "accepts a user that is an admin" do
user = insert(:user, is_admin: true)
@ -42,9 +40,7 @@ defmodule Pleroma.Plugs.UserIsAdminPlugTest do
end
describe "with [:auth, :enforce_oauth_admin_scope_usage]," do
clear_config([:auth, :enforce_oauth_admin_scope_usage]) do
Pleroma.Config.put([:auth, :enforce_oauth_admin_scope_usage], true)
end
clear_config([:auth, :enforce_oauth_admin_scope_usage], true)
setup do
admin_user = insert(:user, is_admin: true)

View file

@ -26,6 +26,25 @@ defmodule Pleroma.Tests.Helpers do
end
end
defmacro clear_config(config_path, temp_setting) do
quote do
clear_config(unquote(config_path)) do
Config.put(unquote(config_path), unquote(temp_setting))
end
end
end
@doc """
From _within a test case_, sets config to provided value and restores initial value on exit.
For multi-case setup use `clear_config/2` instead.
"""
def set_config(config_path, temp_setting) do
initial_setting = Config.get(config_path)
Config.put(config_path, temp_setting)
ExUnit.Callbacks.on_exit(fn -> Config.put(config_path, initial_setting) end)
end
@doc "Stores initial config value and restores it after *all* test examples are executed."
defmacro clear_config_all(config_path) do
quote do
@ -50,6 +69,14 @@ defmodule Pleroma.Tests.Helpers do
end
end
defmacro clear_config_all(config_path, temp_setting) do
quote do
clear_config_all(unquote(config_path)) do
Config.put(unquote(config_path), unquote(temp_setting))
end
end
end
defmacro __using__(_opts) do
quote do
import Pleroma.Tests.Helpers,
@ -57,7 +84,8 @@ defmodule Pleroma.Tests.Helpers do
clear_config: 1,
clear_config: 2,
clear_config_all: 1,
clear_config_all: 2
clear_config_all: 2,
set_config: 2
]
def to_datetime(naive_datetime) do

View file

@ -20,9 +20,7 @@ defmodule Mix.Tasks.Pleroma.ConfigTest do
:ok
end
clear_config_all(:configurable_from_database) do
Pleroma.Config.put(:configurable_from_database, true)
end
clear_config_all(:configurable_from_database, true)
test "error if file with custom settings doesn't exist" do
Mix.Tasks.Pleroma.Config.migrate_to_db("config/not_existance_config_file.exs")

View file

@ -250,9 +250,7 @@ defmodule Pleroma.UploadTest do
end
describe "Setting a custom base_url for uploaded media" do
clear_config([Pleroma.Upload, :base_url]) do
Pleroma.Config.put([Pleroma.Upload, :base_url], "https://cache.pleroma.social")
end
clear_config([Pleroma.Upload, :base_url], "https://cache.pleroma.social")
test "returns a media url with configured base_url" do
base_url = Pleroma.Config.get([Pleroma.Upload, :base_url])

View file

@ -11,12 +11,10 @@ defmodule Pleroma.Uploaders.S3Test do
import Mock
import ExUnit.CaptureLog
clear_config([Pleroma.Uploaders.S3]) do
Config.put([Pleroma.Uploaders.S3],
bucket: "test_bucket",
public_endpoint: "https://s3.amazonaws.com"
)
end
clear_config(Pleroma.Uploaders.S3,
bucket: "test_bucket",
public_endpoint: "https://s3.amazonaws.com"
)
describe "get_file/1" do
test "it returns path to local folder for files" do

View file

@ -476,9 +476,7 @@ defmodule Pleroma.UserTest do
email: "email@example.com"
}
clear_config([:instance, :account_activation_required]) do
Pleroma.Config.put([:instance, :account_activation_required], true)
end
clear_config([:instance, :account_activation_required], true)
test "it creates unconfirmed user" do
changeset = User.register_changeset(%User{}, @full_user_data)

View file

@ -26,9 +26,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do
:ok
end
clear_config([:instance, :federating]) do
Config.put([:instance, :federating], true)
end
clear_config([:instance, :federating], true)
describe "/relay" do
clear_config([:instance, :allow_relay])

View file

@ -9,12 +9,10 @@ defmodule Pleroma.Web.ActivityPub.MRF.ObjectAgePolicyTest do
alias Pleroma.Web.ActivityPub.MRF.ObjectAgePolicy
alias Pleroma.Web.ActivityPub.Visibility
clear_config([:mrf_object_age]) do
Config.put(:mrf_object_age,
threshold: 172_800,
actions: [:delist, :strip_followers]
)
end
clear_config(:mrf_object_age,
threshold: 172_800,
actions: [:delist, :strip_followers]
)
setup_all do
Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)

View file

@ -8,18 +8,16 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do
alias Pleroma.Config
alias Pleroma.Web.ActivityPub.MRF.SimplePolicy
clear_config([:mrf_simple]) do
Config.put(:mrf_simple,
media_removal: [],
media_nsfw: [],
federated_timeline_removal: [],
report_removal: [],
reject: [],
accept: [],
avatar_removal: [],
banner_removal: []
)
end
clear_config(:mrf_simple,
media_removal: [],
media_nsfw: [],
federated_timeline_removal: [],
report_removal: [],
reject: [],
accept: [],
avatar_removal: [],
banner_removal: []
)
describe "when :media_removal" do
test "is empty" do

View file

@ -23,9 +23,7 @@ defmodule Pleroma.Web.ActivityPub.PublisherTest do
:ok
end
clear_config_all([:instance, :federating]) do
Pleroma.Config.put([:instance, :federating], true)
end
clear_config_all([:instance, :federating], true)
describe "gather_webfinger_links/1" do
test "it returns links" do

View file

@ -1351,9 +1351,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
end
describe "`handle_incoming/2`, Mastodon format `replies` handling" do
clear_config([:activitypub, :note_replies_output_limit]) do
Pleroma.Config.put([:activitypub, :note_replies_output_limit], 5)
end
clear_config([:activitypub, :note_replies_output_limit], 5)
clear_config([:instance, :federation_incoming_replies_max_depth])
@ -1394,9 +1392,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
end
describe "`handle_incoming/2`, Pleroma format `replies` handling" do
clear_config([:activitypub, :note_replies_output_limit]) do
Pleroma.Config.put([:activitypub, :note_replies_output_limit], 5)
end
clear_config([:activitypub, :note_replies_output_limit], 5)
clear_config([:instance, :federation_incoming_replies_max_depth])
@ -2145,9 +2141,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
end
describe "set_replies/1" do
clear_config([:activitypub, :note_replies_output_limit]) do
Pleroma.Config.put([:activitypub, :note_replies_output_limit], 2)
end
clear_config([:activitypub, :note_replies_output_limit], 2)
test "returns unmodified object if activity doesn't have self-replies" do
data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))

View file

@ -37,9 +37,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectViewTest do
end
describe "note activity's `replies` collection rendering" do
clear_config([:activitypub, :note_replies_output_limit]) do
Pleroma.Config.put([:activitypub, :note_replies_output_limit], 5)
end
clear_config([:activitypub, :note_replies_output_limit], 5)
test "renders `replies` collection for a note activity" do
user = insert(:user)

View file

@ -43,9 +43,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
end
describe "with [:auth, :enforce_oauth_admin_scope_usage]," do
clear_config([:auth, :enforce_oauth_admin_scope_usage]) do
Config.put([:auth, :enforce_oauth_admin_scope_usage], true)
end
clear_config([:auth, :enforce_oauth_admin_scope_usage], true)
test "GET /api/pleroma/admin/users/:nickname requires admin:read:accounts or broader scope",
%{admin: admin} do
@ -93,9 +91,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
end
describe "unless [:auth, :enforce_oauth_admin_scope_usage]," do
clear_config([:auth, :enforce_oauth_admin_scope_usage]) do
Config.put([:auth, :enforce_oauth_admin_scope_usage], false)
end
clear_config([:auth, :enforce_oauth_admin_scope_usage], false)
test "GET /api/pleroma/admin/users/:nickname requires " <>
"read:accounts or admin:read:accounts or broader scope",
@ -581,13 +577,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
end
describe "POST /api/pleroma/admin/email_invite, with valid config" do
clear_config([:instance, :registrations_open]) do
Config.put([:instance, :registrations_open], false)
end
clear_config([:instance, :invites_enabled]) do
Config.put([:instance, :invites_enabled], true)
end
clear_config([:instance, :registrations_open], false)
clear_config([:instance, :invites_enabled], true)
test "sends invitation and returns 204", %{admin: admin, conn: conn} do
recipient_email = "foo@bar.com"
@ -1888,9 +1879,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
end
describe "GET /api/pleroma/admin/config" do
clear_config(:configurable_from_database) do
Config.put(:configurable_from_database, true)
end
clear_config(:configurable_from_database, true)
test "when configuration from database is off", %{conn: conn} do
Config.put(:configurable_from_database, false)
@ -2041,9 +2030,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
end)
end
clear_config(:configurable_from_database) do
Config.put(:configurable_from_database, true)
end
clear_config(:configurable_from_database, true)
@tag capture_log: true
test "create new config setting in db", %{conn: conn} do
@ -3052,9 +3039,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
end
describe "GET /api/pleroma/admin/restart" do
clear_config(:configurable_from_database) do
Config.put(:configurable_from_database, true)
end
clear_config(:configurable_from_database, true)
test "pleroma restarts", %{conn: conn} do
capture_log(fn ->

View file

@ -21,9 +21,7 @@ defmodule Pleroma.Web.FederatorTest do
:ok
end
clear_config_all([:instance, :federating]) do
Pleroma.Config.put([:instance, :federating], true)
end
clear_config_all([:instance, :federating], true)
clear_config([:instance, :allow_relay])
clear_config([:instance, :rewrite_policy])

View file

@ -12,9 +12,7 @@ defmodule Pleroma.Web.Feed.UserControllerTest do
alias Pleroma.Object
alias Pleroma.User
clear_config([:instance, :federating]) do
Config.put([:instance, :federating], true)
end
clear_config([:instance, :federating], true)
describe "feed" do
clear_config([:feed])

View file

@ -10,9 +10,7 @@ defmodule Pleroma.Instances.InstanceTest do
import Pleroma.Factory
clear_config_all([:instance, :federation_reachability_timeout_days]) do
Pleroma.Config.put([:instance, :federation_reachability_timeout_days], 1)
end
clear_config_all([:instance, :federation_reachability_timeout_days], 1)
describe "set_reachable/1" do
test "clears `unreachable_since` of existing matching Instance record having non-nil `unreachable_since`" do

View file

@ -7,9 +7,7 @@ defmodule Pleroma.InstancesTest do
use Pleroma.DataCase
clear_config_all([:instance, :federation_reachability_timeout_days]) do
Pleroma.Config.put([:instance, :federation_reachability_timeout_days], 1)
end
clear_config_all([:instance, :federation_reachability_timeout_days], 1)
describe "reachable?/1" do
test "returns `true` for host / url with unknown reachability status" do

View file

@ -756,9 +756,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
end
describe "create account by app / rate limit" do
clear_config([:rate_limit, :app_account_creation]) do
Pleroma.Config.put([:rate_limit, :app_account_creation], {10_000, 2})
end
clear_config([:rate_limit, :app_account_creation], {10_000, 2})
test "respects rate limit setting", %{conn: conn} do
app_token = insert(:oauth_token, user: nil)

View file

@ -739,9 +739,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do
%{activity: activity}
end
clear_config([:instance, :max_pinned_statuses]) do
Config.put([:instance, :max_pinned_statuses], 1)
end
clear_config([:instance, :max_pinned_statuses], 1)
test "pin status", %{conn: conn, user: user, activity: activity} do
id_str = to_string(activity.id)

View file

@ -12,13 +12,9 @@ defmodule Pleroma.Web.OAuth.LDAPAuthorizationTest do
@skip if !Code.ensure_loaded?(:eldap), do: :skip
clear_config_all([:ldap, :enabled]) do
Pleroma.Config.put([:ldap, :enabled], true)
end
clear_config_all([:ldap, :enabled], true)
clear_config_all(Pleroma.Web.Auth.Authenticator) do
Pleroma.Config.put(Pleroma.Web.Auth.Authenticator, Pleroma.Web.Auth.LDAPAuthenticator)
end
clear_config_all(Pleroma.Web.Auth.Authenticator, Pleroma.Web.Auth.LDAPAuthenticator)
@tag @skip
test "authorizes the existing user using LDAP credentials" do

View file

@ -31,12 +31,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do
]
end
clear_config([:auth, :oauth_consumer_strategies]) do
Pleroma.Config.put(
[:auth, :oauth_consumer_strategies],
~w(twitter facebook)
)
end
clear_config([:auth, :oauth_consumer_strategies], ~w(twitter facebook))
test "GET /oauth/authorize renders auth forms, including OAuth consumer form", %{
app: app,

View file

@ -17,9 +17,7 @@ defmodule Pleroma.Web.OStatus.OStatusControllerTest do
:ok
end
clear_config([:instance, :federating]) do
Config.put([:instance, :federating], true)
end
clear_config([:instance, :federating], true)
# Note: see ActivityPubControllerTest for JSON format tests
describe "GET /objects/:uuid (text/html)" do

View file

@ -27,9 +27,7 @@ defmodule Pleroma.Web.PleromaAPI.AccountControllerTest do
[user: user]
end
clear_config([:instance, :account_activation_required]) do
Config.put([:instance, :account_activation_required], true)
end
clear_config([:instance, :account_activation_required], true)
test "resend account confirmation email", %{conn: conn, user: user} do
conn

View file

@ -13,9 +13,7 @@ defmodule Pleroma.Web.PleromaAPI.EmojiAPIControllerTest do
"emoji"
)
clear_config([:auth, :enforce_oauth_admin_scope_usage]) do
Pleroma.Config.put([:auth, :enforce_oauth_admin_scope_usage], false)
end
clear_config([:auth, :enforce_oauth_admin_scope_usage], false)
test "shared & non-shared pack information in list_packs is ok" do
conn = build_conn()

View file

@ -8,13 +8,9 @@ defmodule Pleroma.Web.StaticFE.StaticFEControllerTest do
import Pleroma.Factory
clear_config_all([:static_fe, :enabled]) do
Config.put([:static_fe, :enabled], true)
end
clear_config_all([:static_fe, :enabled], true)
clear_config([:instance, :federating]) do
Config.put([:instance, :federating], true)
end
clear_config([:instance, :federating], true)
setup %{conn: conn} do
conn = put_req_header(conn, "accept", "text/html")

View file

@ -17,9 +17,7 @@ defmodule Pleroma.Web.TwitterAPI.RemoteFollowControllerTest do
:ok
end
clear_config_all([:instance, :federating]) do
Config.put([:instance, :federating], true)
end
clear_config_all([:instance, :federating], true)
clear_config([:instance])
clear_config([:frontend_configurations, :pleroma_fe])

View file

@ -117,9 +117,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
end
describe "register with one time token" do
clear_config([:instance, :registrations_open]) do
Pleroma.Config.put([:instance, :registrations_open], false)
end
clear_config([:instance, :registrations_open], false)
test "returns user on success" do
{:ok, invite} = UserInviteToken.create_invite()
@ -184,9 +182,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
end
describe "registers with date limited token" do
clear_config([:instance, :registrations_open]) do
Pleroma.Config.put([:instance, :registrations_open], false)
end
clear_config([:instance, :registrations_open], false)
setup do
data = %{
@ -246,9 +242,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
end
describe "registers with reusable token" do
clear_config([:instance, :registrations_open]) do
Pleroma.Config.put([:instance, :registrations_open], false)
end
clear_config([:instance, :registrations_open], false)
test "returns user on success, after him registration fails" do
{:ok, invite} = UserInviteToken.create_invite(%{max_use: 100})
@ -292,9 +286,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
end
describe "registers with reusable date limited token" do
clear_config([:instance, :registrations_open]) do
Pleroma.Config.put([:instance, :registrations_open], false)
end
clear_config([:instance, :registrations_open], false)
test "returns user on success" do
{:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})

View file

@ -427,9 +427,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
end
describe "POST /main/ostatus - remote_subscribe/2" do
clear_config([:instance, :federating]) do
Config.put([:instance, :federating], true)
end
clear_config([:instance, :federating], true)
test "renders subscribe form", %{conn: conn} do
user = insert(:user)

View file

@ -14,9 +14,7 @@ defmodule Pleroma.Web.WebFinger.WebFingerControllerTest do
:ok
end
clear_config_all([:instance, :federating]) do
Pleroma.Config.put([:instance, :federating], true)
end
clear_config_all([:instance, :federating], true)
test "GET host-meta" do
response =