aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/models/info_request.rb19
-rw-r--r--spec/models/info_request_spec.rb83
2 files changed, 70 insertions, 32 deletions
diff --git a/app/models/info_request.rb b/app/models/info_request.rb
index 83456b54f..dfaa524b2 100644
--- a/app/models/info_request.rb
+++ b/app/models/info_request.rb
@@ -995,27 +995,28 @@ public
return ret.reverse
end
- # Call groups of censor rules
- def apply_censor_rules_to_text!(text)
+ # Get the list of censor rules that apply to this request
+ def applicable_censor_rules
applicable_rules = [self.censor_rules, self.public_body.censor_rules, CensorRule.global.all]
if self.user && !self.user.censor_rules.empty?
applicable_rules << self.user.censor_rules
end
- applicable_rules.flatten.each do |censor_rule|
+ return applicable_rules.flatten
+ end
+
+ # Call groups of censor rules
+ def apply_censor_rules_to_text!(text)
+ self.applicable_censor_rules.each do |censor_rule|
censor_rule.apply_to_text!(text)
end
return text
end
def apply_censor_rules_to_binary!(binary)
- for censor_rule in self.censor_rules
+ self.applicable_censor_rules.each do |censor_rule|
censor_rule.apply_to_binary!(binary)
end
- if self.user # requests during construction have no user
- for censor_rule in self.user.censor_rules
- censor_rule.apply_to_binary!(binary)
- end
- end
+ return binary
end
def is_owning_user?(user)
diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb
index b1d10a51d..2a738fa4c 100644
--- a/spec/models/info_request_spec.rb
+++ b/spec/models/info_request_spec.rb
@@ -401,13 +401,17 @@ describe InfoRequest do
end
- context "when applying censor rules" do
+ describe 'when applying censor rules' do
before do
- @global_rule = mock_model(CensorRule, :apply_to_text! => nil)
- @user_rule = mock_model(CensorRule, :apply_to_text! => nil)
- @request_rule = mock_model(CensorRule, :apply_to_text! => nil)
- @body_rule = mock_model(CensorRule, :apply_to_text! => nil)
+ @global_rule = mock_model(CensorRule, :apply_to_text! => nil,
+ :apply_to_binary! => nil)
+ @user_rule = mock_model(CensorRule, :apply_to_text! => nil,
+ :apply_to_binary! => nil)
+ @request_rule = mock_model(CensorRule, :apply_to_text! => nil,
+ :apply_to_binary! => nil)
+ @body_rule = mock_model(CensorRule, :apply_to_text! => nil,
+ :apply_to_binary! => nil)
@user = mock_model(User, :censor_rules => [@user_rule])
@body = mock_model(PublicBody, :censor_rules => [@body_rule])
@info_request = InfoRequest.new(:prominence => 'normal',
@@ -420,29 +424,62 @@ describe InfoRequest do
CensorRule.stub!(:global).and_return(mock('global context', :all => [@global_rule]))
end
- it "should apply a global censor rule" do
- @global_rule.should_receive(:apply_to_text!).with(@text)
- @info_request.apply_censor_rules_to_text!(@text)
- end
+ context "when applying censor rules to text" do
- it 'should apply a user rule' do
- @user_rule.should_receive(:apply_to_text!).with(@text)
- @info_request.apply_censor_rules_to_text!(@text)
- end
+ it "should apply a global censor rule" do
+ @global_rule.should_receive(:apply_to_text!).with(@text)
+ @info_request.apply_censor_rules_to_text!(@text)
+ end
- it 'should not raise an error if there is no user' do
- @info_request.user_id = nil
- lambda{ @info_request.apply_censor_rules_to_text!(@text) }.should_not raise_error
- end
+ it 'should apply a user rule' do
+ @user_rule.should_receive(:apply_to_text!).with(@text)
+ @info_request.apply_censor_rules_to_text!(@text)
+ end
+
+ it 'should not raise an error if there is no user' do
+ @info_request.user_id = nil
+ lambda{ @info_request.apply_censor_rules_to_text!(@text) }.should_not raise_error
+ end
+
+ it 'should apply a rule from the body associated with the request' do
+ @body_rule.should_receive(:apply_to_text!).with(@text)
+ @info_request.apply_censor_rules_to_text!(@text)
+ end
+
+ it 'should apply a request rule' do
+ @request_rule.should_receive(:apply_to_text!).with(@text)
+ @info_request.apply_censor_rules_to_text!(@text)
+ end
- it 'should apply a rule from the body associated with the request' do
- @body_rule.should_receive(:apply_to_text!).with(@text)
- @info_request.apply_censor_rules_to_text!(@text)
end
- it 'should apply a request rule' do
- @request_rule.should_receive(:apply_to_text!).with(@text)
- @info_request.apply_censor_rules_to_text!(@text)
+ context 'when applying censor rules to binary files' do
+
+ it "should apply a global censor rule" do
+ @global_rule.should_receive(:apply_to_binary!).with(@text)
+ @info_request.apply_censor_rules_to_binary!(@text)
+ end
+
+ it 'should apply a user rule' do
+ @user_rule.should_receive(:apply_to_binary!).with(@text)
+ @info_request.apply_censor_rules_to_binary!(@text)
+ end
+
+ it 'should not raise an error if there is no user' do
+ @info_request.user_id = nil
+ lambda{ @info_request.apply_censor_rules_to_binary!(@text) }.should_not raise_error
+ end
+
+ it 'should apply a rule from the body associated with the request' do
+ @body_rule.should_receive(:apply_to_binary!).with(@text)
+ @info_request.apply_censor_rules_to_binary!(@text)
+ end
+
+ it 'should apply a request rule' do
+ @request_rule.should_receive(:apply_to_binary!).with(@text)
+ @info_request.apply_censor_rules_to_binary!(@text)
+ end
+
end
end