From 88abaa3e40cb8293baf0c74e006e9d64d12a6ff3 Mon Sep 17 00:00:00 2001 From: Selase Krakani Date: Thu, 8 Jun 2023 17:33:46 +0000 Subject: [PATCH] FIX: Make serialized watched word regex Javascript compatible This change ensures Javascript compatible regex is serialized instead of the default ruby based one. --- .../watched_word_list_serializer.rb | 2 +- app/serializers/watched_word_serializer.rb | 2 +- .../admin/watched_words_controller_spec.rb | 54 +++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/app/serializers/watched_word_list_serializer.rb b/app/serializers/watched_word_list_serializer.rb index 4b47ce16f78e8c..9d0929a9fa98e8 100644 --- a/app/serializers/watched_word_list_serializer.rb +++ b/app/serializers/watched_word_list_serializer.rb @@ -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 diff --git a/app/serializers/watched_word_serializer.rb b/app/serializers/watched_word_serializer.rb index 135aca292b30c8..0ba4f7cc5daf1e 100644 --- a/app/serializers/watched_word_serializer.rb +++ b/app/serializers/watched_word_serializer.rb @@ -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 diff --git a/spec/requests/admin/watched_words_controller_spec.rb b/spec/requests/admin/watched_words_controller_spec.rb index ef140bbdcf3db4..12677fbc655235 100644 --- a/spec/requests/admin/watched_words_controller_spec.rb +++ b/spec/requests/admin/watched_words_controller_spec.rb @@ -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) }