0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-05 11:52:32 +08:00
discourse/spec/support/test_setup.rb
Alan Guo Xiang Tan 134ae87355
DEV: Reset test site settings without callbacks (#41960)
The test suite uses an in-memory site setting provider. `TestSetup`
previously cleared it with `SiteSetting.remove_override!`, the
production mutation API, which emits `site_setting_changed` and runs
application side effects for every override.

In the [failing core backend
job](https://github.com/discourse/discourse/actions/runs/29971702386/job/89094892866),
cleanup reset site metadata while Discourse ID credentials were still
present. This scheduled a metadata update and repeatedly failed in
deferred workers:

```text
WebMock::NetConnectNotAllowedError:
  Unregistered request: POST https://id.discourse.com/challenge
from config/initializers/014-track-setting-changes.rb:97
from lib/scheduler/defer.rb:116
```

Cleanup now clears the in-memory provider directly and refreshes the
site setting cache. Because this bypasses the `default_theme_id`
callback, it explicitly expires the theme cache when cleanup restores a
different default theme.
2026-07-23 14:10:50 +08:00

87 lines
3 KiB
Ruby
Vendored

# frozen_string_literal: true
# Per-test global-state reset, run before every example and every before_all
# block. `before_next_spec` queues one-shot cleanups that TestSetup drains here.
module TestSetup
# This is run before each test and before each before_all block
def self.test_setup(x = nil)
# This allows DB.transaction_open? to work in tests. See lib/mini_sql_multisite_connection.rb
DB.test_transaction = ActiveRecord::Base.connection.current_transaction
RateLimiter.disable
PostActionNotifier.disable
SearchIndexer.disable
UserActionManager.disable
NotificationEmailer.disable
SiteIconManager.disable
WordWatcher.disable_cache
UpcomingChanges.clear_caches!
previous_default_theme_id = SiteSetting.default_theme_id
SiteSetting.provider.clear
SiteSetting.refresh!
Theme.expire_site_cache! if SiteSetting.default_theme_id != previous_default_theme_id
# Set some standard overrides for tests. Some for performance, some to make the tests easier,
# and some because their default was changed, and we didn't want to refactor all the relevant specs.
{
s3_upload_bucket: "bucket",
min_post_length: 5,
min_first_post_length: 5,
min_personal_message_post_length: 10,
download_remote_images_to_local: false,
unique_posts_mins: 0,
max_consecutive_replies: 0,
allow_uncategorized_topics: true,
}.each { |k, v| SiteSetting.set(k, v) }
# very expensive IO operations
SiteSetting.automatically_download_gravatars = false
Discourse.clear_readonly!
Sidekiq::Worker.clear_all
I18n.locale = SiteSettings::DefaultsProvider::DEFAULT_LOCALE
# Database is rolled back between specs, but I18n override cache doesn't.
# Flush it if there were any TranslationOverrides created.
overrides_by_site = I18n.instance_variable_get(:@overrides_by_site) || {}
if overrides_by_site.values.flat_map(&:values).any?(&:any?)
I18n.reload!
ExtraLocalesController.clear_cache!
end
RspecErrorTracker.clear_exceptions
if $test_cleanup_callbacks
$test_cleanup_callbacks.reverse_each(&:call)
$test_cleanup_callbacks = nil
end
# in test this is very expensive, we explicitly enable when needed
Topic.update_featured_topics = false
# Running jobs are expensive and most of our tests are not concern with
# code that runs inside jobs. run_later! means they are put on the redis
# queue and never processed.
Jobs.run_later!
# Don't track ApplicationRequests in test mode unless opted in
ApplicationRequest.disable
# Don't queue badge grant in test mode
BadgeGranter.disable_queue
OmniAuth.config.test_mode = false
Middleware::AnonymousCache.disable_anon_cache
BlockRequestsMiddleware.allow_requests!
BlockRequestsMiddleware.current_example_location = nil
ApplicationSerializer.fragment_cache.clear
end
end
def before_next_spec(&callback)
($test_cleanup_callbacks ||= []) << callback
end