diff options
-rw-r--r-- | app/models/outgoing_message.rb | 20 | ||||
-rw-r--r-- | spec/models/outgoing_message_spec.rb | 26 |
2 files changed, 37 insertions, 9 deletions
diff --git a/app/models/outgoing_message.rb b/app/models/outgoing_message.rb index 8240eb784..3448009bd 100644 --- a/app/models/outgoing_message.rb +++ b/app/models/outgoing_message.rb @@ -140,18 +140,28 @@ class OutgoingMessage < ActiveRecord::Base end end - def body + # Public: The body text of the OutgoingMessage. The text is cleaned and + # CensorRules are applied. + # + # options - Hash of options + # :censor_rules - Array of CensorRules to apply. Defaults to the + # applicable_censor_rules of the associated + # InfoRequest. (optional) + # + # Returns a String + def body(options = {}) text = raw_body.dup return text if text.nil? text = clean_text(text) - # Remove things from censor rules - unless info_request.nil? - self.info_request.apply_censor_rules_to_text!(text) + # Use the given censor_rules; otherwise fetch them from the associated + # info_request + censor_rules = options.fetch(:censor_rules) do + info_request.try(:applicable_censor_rules) or [] end - text + censor_rules.reduce(text) { |text, rule| rule.apply_to_text(text) } end def raw_body diff --git a/spec/models/outgoing_message_spec.rb b/spec/models/outgoing_message_spec.rb index cc7cd6bac..051f263d2 100644 --- a/spec/models/outgoing_message_spec.rb +++ b/spec/models/outgoing_message_spec.rb @@ -68,20 +68,37 @@ describe OutgoingMessage do expect(message.body).to eq("ab\n\nc") end - it 'applies censor rules to the text' do + it "applies the associated request's censor rules to the text" do + attrs = { :status => 'ready', + :message_type => 'initial_request', + :body => 'This sensitive text contains secret info!', + :what_doing => 'normal_sort' } + message = FactoryGirl.build(:outgoing_message, attrs) + rules = [FactoryGirl.build(:censor_rule, :text => 'secret'), FactoryGirl.build(:censor_rule, :text => 'sensitive')] InfoRequest.any_instance.stub(:censor_rules).and_return(rules) + expected = 'This [REDACTED] text contains [REDACTED] info!' + expect(message.body).to eq(expected) + end + + it "applies the given censor rules to the text" do attrs = { :status => 'ready', :message_type => 'initial_request', :body => 'This sensitive text contains secret info!', :what_doing => 'normal_sort' } - message = FactoryGirl.build(:outgoing_message, attrs) - expected = 'This [REDACTED] text contains [REDACTED] info!' - expect(message.body).to eq(expected) + request_rules = [FactoryGirl.build(:censor_rule, :text => 'secret'), + FactoryGirl.build(:censor_rule, :text => 'sensitive')] + InfoRequest.any_instance.stub(:censor_rules).and_return(request_rules) + + censor_rules = [FactoryGirl.build(:censor_rule, :text => 'text'), + FactoryGirl.build(:censor_rule, :text => 'contains')] + + expected = 'This sensitive [REDACTED] [REDACTED] secret info!' + expect(message.body(:censor_rules => censor_rules)).to eq(expected) end end @@ -127,6 +144,7 @@ describe OutgoingMessage, " when making an outgoing message" do info_request = mock_model(InfoRequest, :public_body => public_body, :url_title => 'a_test_title', :title => 'A test title', + :applicable_censor_rules => [], :apply_censor_rules_to_text! => nil, :is_batch_request_template? => false) outgoing_message = OutgoingMessage.new({ |