From 6e3d6c35d1aab075096e2c2191e8941b4625ca04 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Wed, 29 Apr 2015 12:12:53 +0100 Subject: Improve OutgoingMessage#body specs --- spec/models/outgoing_message_spec.rb | 91 +++++++++++++++++++++++++++--------- 1 file changed, 70 insertions(+), 21 deletions(-) (limited to 'spec/models') diff --git a/spec/models/outgoing_message_spec.rb b/spec/models/outgoing_message_spec.rb index a3e2d1c68..cc7cd6bac 100644 --- a/spec/models/outgoing_message_spec.rb +++ b/spec/models/outgoing_message_spec.rb @@ -18,6 +18,76 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +describe OutgoingMessage do + + describe :initialize do + + it 'does not censor the #body' do + attrs = { :status => 'ready', + :message_type => 'initial_request', + :body => 'abc', + :what_doing => 'normal_sort' } + + message = FactoryGirl.create(:outgoing_message, attrs) + + OutgoingMessage.any_instance.should_not_receive(:body).and_call_original + OutgoingMessage.find(message.id) + end + + end + + describe :body do + + it 'returns the body attribute' do + attrs = { :status => 'ready', + :message_type => 'initial_request', + :body => 'abc', + :what_doing => 'normal_sort' } + + message = FactoryGirl.build(:outgoing_message, attrs) + expect(message.body).to eq('abc') + end + + it 'strips the body of leading and trailing whitespace' do + attrs = { :status => 'ready', + :message_type => 'initial_request', + :body => ' abc ', + :what_doing => 'normal_sort' } + + message = FactoryGirl.build(:outgoing_message, attrs) + expect(message.body).to eq('abc') + end + + it 'removes excess linebreaks that unnecessarily space it out' do + attrs = { :status => 'ready', + :message_type => 'initial_request', + :body => "ab\n\nc\n\n", + :what_doing => 'normal_sort' } + + message = FactoryGirl.build(:outgoing_message, attrs) + expect(message.body).to eq("ab\n\nc") + end + + it 'applies censor rules to the text' do + rules = [FactoryGirl.build(:censor_rule, :text => 'secret'), + FactoryGirl.build(:censor_rule, :text => 'sensitive')] + InfoRequest.any_instance.stub(:censor_rules).and_return(rules) + + 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) + end + + end + +end + describe OutgoingMessage, " when making an outgoing message" do before do @@ -155,27 +225,6 @@ describe OutgoingMessage, " when making an outgoing message" do end end - -describe OutgoingMessage, " when censoring data" do - - before do - @om = outgoing_messages(:useless_outgoing_message) - - @censor_rule = CensorRule.new() - @censor_rule.text = "dog" - @censor_rule.replacement = "cat" - @censor_rule.last_edit_editor = "unknown" - @censor_rule.last_edit_comment = "none" - - @om.info_request.censor_rules << @censor_rule - end - - it "should apply censor rules to outgoing messages" do - @om.read_attribute(:body).should match(/fancy dog/) - @om.body.should match(/fancy cat/) - end -end - describe OutgoingMessage, "when validating the format of the message body" do it 'should handle a salutation with a bracket in it' do -- cgit v1.2.3 From 4e568aab076f818ea5d011d05779edfa2bca68ec Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Wed, 29 Apr 2015 12:48:05 +0100 Subject: Add a non-mutating #apply_to_text Still need to `return nil if text_to_censor.nil?` because `gsub!` returns `nil` rather than the original text if it does not make a substitution. --- spec/models/censor_rule_spec.rb | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'spec/models') diff --git a/spec/models/censor_rule_spec.rb b/spec/models/censor_rule_spec.rb index 4ecd2d3e1..77b8cd07a 100644 --- a/spec/models/censor_rule_spec.rb +++ b/spec/models/censor_rule_spec.rb @@ -17,6 +17,42 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +describe CensorRule do + + describe :apply_to_text do + + it 'applies the rule to the text' do + rule = FactoryGirl.build(:censor_rule, :text => 'secret') + text = 'Some secret text' + expect(rule.apply_to_text(text)).to eq('Some [REDACTED] text') + end + + it 'does not mutate the input' do + rule = FactoryGirl.build(:censor_rule, :text => 'secret') + text = 'Some secret text' + rule.apply_to_text(text) + expect(text).to eq('Some secret text') + end + + it 'returns the text if the rule is unmatched' do + rule = FactoryGirl.build(:censor_rule, :text => 'secret') + text = 'Some text' + expect(rule.apply_to_text(text)).to eq('Some text') + end + end + + describe :apply_to_text! do + + it 'mutates the input' do + rule = FactoryGirl.build(:censor_rule, :text => 'secret') + text = 'Some secret text' + rule.apply_to_text!(text) + expect(text).to eq('Some [REDACTED] text') + end + + end +end + describe CensorRule, "substituting things" do describe 'when using a text rule' do -- cgit v1.2.3 From dd00661123379ccb508a7422b152c17769f7501d Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Wed, 29 Apr 2015 13:44:51 +0100 Subject: Allow optional censor rules to be passed to #body MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If we’re in the context of an InfoRequest and want the OutgoingMessage#body, we can pass the rules in explicitly to avoid looking up the info request again. Otherwise it falls back to the existing behaviour. --- spec/models/outgoing_message_spec.rb | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'spec/models') 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({ -- cgit v1.2.3