aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/models/censor_rule.rb17
-rw-r--r--config/environments/development.rb6
-rw-r--r--spec/models/censor_rule_spec.rb36
3 files changed, 53 insertions, 6 deletions
diff --git a/app/models/censor_rule.rb b/app/models/censor_rule.rb
index 02b85dc70..3a15bf532 100644
--- a/app/models/censor_rule.rb
+++ b/app/models/censor_rule.rb
@@ -32,6 +32,7 @@ class CensorRule < ActiveRecord::Base
# a flag to allow the require_user_request_or_public_body validation to be skipped
attr_accessor :allow_global
validate :require_user_request_or_public_body, :unless => proc{ |rule| rule.allow_global == true }
+ validate :require_valid_regexp, :if => proc{ |rule| rule.regexp? == true }
named_scope :global, {:conditions => {:info_request_id => nil,
:user_id => nil,
@@ -43,11 +44,23 @@ class CensorRule < ActiveRecord::Base
end
end
+ def require_valid_regexp
+ begin
+ self.make_regexp()
+ rescue RegexpError => e
+ errors.add(:text, e.message)
+ end
+ end
+
+ def make_regexp
+ return Regexp.new(self.text, Regexp::MULTILINE)
+ end
+
def apply_to_text!(text)
if text.nil?
return nil
end
- to_replace = regexp? ? Regexp.new(self.text, Regexp::MULTILINE) : self.text
+ to_replace = regexp? ? self.make_regexp() : self.text
text.gsub!(to_replace, self.replacement)
end
@@ -55,7 +68,7 @@ class CensorRule < ActiveRecord::Base
if binary.nil?
return nil
end
- to_replace = regexp? ? Regexp.new(self.text, Regexp::MULTILINE) : self.text
+ to_replace = regexp? ? self.make_regexp() : self.text
binary.gsub!(to_replace){ |match| match.gsub(/./, 'x') }
end
diff --git a/config/environments/development.rb b/config/environments/development.rb
index f21f27ab6..f07fd31bf 100644
--- a/config/environments/development.rb
+++ b/config/environments/development.rb
@@ -1,6 +1,6 @@
# Settings specified here will take precedence over those in config/environment.rb
-config.log_level = :info
+config.log_level = :debug
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
@@ -17,8 +17,8 @@ config.action_view.debug_rjs = true
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false
-config.action_mailer.perform_deliveries = false
-config.action_mailer.delivery_method = :sendmail # so is queued, rather than giving immediate errors
+config.action_mailer.perform_deliveries = true
+config.action_mailer.delivery_method = :test # so is queued, rather than giving immediate errors
# Writes useful log files to debug memory leaks, of the sort where have
# unintentionally kept references to objects, especially strings.
diff --git a/spec/models/censor_rule_spec.rb b/spec/models/censor_rule_spec.rb
index 443bbe449..471a73fa2 100644
--- a/spec/models/censor_rule_spec.rb
+++ b/spec/models/censor_rule_spec.rb
@@ -73,6 +73,40 @@ end
describe 'when validating rules' do
+ describe 'when validating a regexp rule' do
+
+ before do
+ @censor_rule = CensorRule.new(:regexp => true,
+ :text => '*')
+ end
+
+ it 'should try to create a regexp from the text' do
+ Regexp.should_receive(:new).with('*', Regexp::MULTILINE)
+ @censor_rule.valid?
+ end
+
+ describe 'if a regexp error is produced' do
+
+ it 'should add an error message to the text field with the regexp error message' do
+ Regexp.stub!(:new).and_raise(RegexpError.new("very bad regexp"))
+ @censor_rule.valid?.should == false
+ @censor_rule.errors.on(:text).should == "very bad regexp"
+ end
+
+ end
+
+ describe 'if no regexp error is produced' do
+
+ it 'should not add any error message to the text field' do
+ Regexp.stub!(:new)
+ @censor_rule.valid?
+ @censor_rule.errors.on(:text).should == nil
+ end
+
+ end
+
+ end
+
describe 'when the allow_global flag has been set' do
before do
@@ -89,7 +123,7 @@ describe 'when validating rules' do
describe 'when the allow_global flag has not been set' do
before do
- @censor_rule = CensorRule.new
+ @censor_rule = CensorRule.new(:text => '/./')
end
it 'should not allow a global text censor rule (without user_id, request_id or public_body_id)' do