aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models
diff options
context:
space:
mode:
Diffstat (limited to 'spec/models')
-rw-r--r--spec/models/censor_rule_spec.rb36
-rw-r--r--spec/models/outgoing_message_spec.rb109
-rw-r--r--spec/models/post_redirect_spec.rb1
3 files changed, 124 insertions, 22 deletions
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
diff --git a/spec/models/outgoing_message_spec.rb b/spec/models/outgoing_message_spec.rb
index a3e2d1c68..051f263d2 100644
--- a/spec/models/outgoing_message_spec.rb
+++ b/spec/models/outgoing_message_spec.rb
@@ -18,6 +18,93 @@
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 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)
+
+ 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
+
+end
+
describe OutgoingMessage, " when making an outgoing message" do
before do
@@ -57,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({
@@ -155,27 +243,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
diff --git a/spec/models/post_redirect_spec.rb b/spec/models/post_redirect_spec.rb
index 70b221f10..750e47cc3 100644
--- a/spec/models/post_redirect_spec.rb
+++ b/spec/models/post_redirect_spec.rb
@@ -76,7 +76,6 @@ describe PostRedirect, " when accessing values" do
pr = PostRedirect.new
utf8_params = "--- \n:foo: !binary |\n 0KLQvtCz0LDRiCDR\n"
pr.reason_params_yaml = utf8_params
- puts pr.reason_params
pr.reason_params[:foo].encoding.to_s.should == 'UTF-8' if pr.reason_params[:foo].respond_to?(:encoding)
end
end