Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Make serialized watched word regex Javascript compatible #22010

Merged
merged 1 commit into from Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/serializers/watched_word_list_serializer.rb
Expand Up @@ -18,7 +18,7 @@ def words
def compiled_regular_expressions
expressions = {}
actions.each do |action|
expressions[action] = WordWatcher.serializable_word_matcher_regexp(action)
expressions[action] = WordWatcher.serializable_word_matcher_regexp(action, engine: :js)
end
expressions
end
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/watched_word_serializer.rb
Expand Up @@ -4,7 +4,7 @@ class WatchedWordSerializer < ApplicationSerializer
attributes :id, :word, :regexp, :replacement, :action, :case_sensitive

def regexp
WordWatcher.word_to_regexp(word)
WordWatcher.word_to_regexp(word, engine: :js)
end

def action
Expand Down
54 changes: 54 additions & 0 deletions spec/requests/admin/watched_words_controller_spec.rb
Expand Up @@ -6,6 +6,60 @@
fab!(:admin) { Fabricate(:admin) }
fab!(:user) { Fabricate(:user) }

describe "#index" do
context "when logged in as non-staff user" do
before { sign_in(user) }

it "does not return watched words" do
get "/admin/customize/watched_words.json"

expect(response.status).to eq(404)
end
end

context "when logged in as a staff user" do
fab!(:word1) { Fabricate(:watched_word, action: WatchedWord.actions[:block]) }
fab!(:word2) { Fabricate(:watched_word, action: WatchedWord.actions[:block]) }
fab!(:word3) { Fabricate(:watched_word, action: WatchedWord.actions[:censor]) }
fab!(:word4) { Fabricate(:watched_word, action: WatchedWord.actions[:censor]) }

before { sign_in(admin) }

it "returns all watched words" do
get "/admin/customize/watched_words.json"

expect(response.status).to eq(200)

watched_words = response.parsed_body

expect(watched_words["actions"]).to match_array(WatchedWord.actions.keys.map(&:to_s))
expect(watched_words["words"].length).to eq(4)
expect(watched_words["words"]).to include(
hash_including(
"id" => word1.id,
"word" => word1.word,
"regexp" => WordWatcher.word_to_regexp(word1.word, engine: :js),
"case_sensitive" => false,
"action" => "block",
),
hash_including(
"id" => word4.id,
"word" => word4.word,
"regexp" => WordWatcher.word_to_regexp(word4.word, engine: :js),
"case_sensitive" => false,
"action" => "censor",
),
)
expect(watched_words["compiled_regular_expressions"]["block"].first).to eq(
WordWatcher
.serializable_word_matcher_regexp(:block, engine: :js)
.first
.deep_stringify_keys,
)
end
end
end

describe "#destroy" do
fab!(:watched_word) { Fabricate(:watched_word) }

Expand Down