DEV: Don't cache watched words in test env (#16731)

The cache was causing state to leak between tests since the `WatchedWord` record in the DB would have been rolled back but `WordWatcher` still had the word in the cache.
This commit is contained in:
Alan Guo Xiang Tan 2022-05-12 14:45:05 +08:00 committed by GitHub
parent 8e9164fb60
commit fd1dc91eed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 3 deletions

View File

@ -7,6 +7,19 @@ class WordWatcher
@raw = raw
end
def self.disable_cache
@disabled = true
end
def self.enable_cache
@disabled = false
end
# Don't cache in tests mode
def self.cache_disabled?
@disabled
end
def self.words_for_action(action)
words = WatchedWord
.where(action: WatchedWord.actions[action.to_sym])
@ -24,8 +37,12 @@ class WordWatcher
end
def self.get_cached_words(action)
Discourse.cache.fetch(word_matcher_regexp_key(action), expires_in: 1.day) do
if cache_disabled?
words_for_action(action).presence
else
Discourse.cache.fetch(word_matcher_regexp_key(action), expires_in: 1.day) do
words_for_action(action).presence
end
end
end

View File

@ -520,11 +520,12 @@ describe Report do
before do
freeze_time
PostActionCreator.new(flagger, post, PostActionType.types[:spam], message: 'bad').perform
end
it "returns a report with data" do
result = PostActionCreator.new(flagger, post, PostActionType.types[:spam], message: 'bad').perform
expect(result.success).to eq(true)
expect(report.data).to be_present
row = report.data[0]

View File

@ -105,6 +105,7 @@ module TestSetup
UserActionManager.disable
NotificationEmailer.disable
SiteIconManager.disable
WordWatcher.disable_cache
SiteSetting.provider.all.each do |setting|
SiteSetting.remove_override!(setting.name)