aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models
diff options
context:
space:
mode:
Diffstat (limited to 'spec/models')
-rw-r--r--spec/models/censor_rule_spec.rb25
-rw-r--r--spec/models/incoming_message_spec.rb94
-rw-r--r--spec/models/outgoing_message_spec.rb22
-rw-r--r--spec/models/profile_photo_spec.rb4
-rw-r--r--spec/models/user_spec.rb6
5 files changed, 137 insertions, 14 deletions
diff --git a/spec/models/censor_rule_spec.rb b/spec/models/censor_rule_spec.rb
new file mode 100644
index 000000000..802b342e3
--- /dev/null
+++ b/spec/models/censor_rule_spec.rb
@@ -0,0 +1,25 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+
+describe CensorRule, "substituting things" do
+ before do
+ @censor_rule = CensorRule.new
+ @censor_rule.text = "goodbye"
+ @censor_rule.replacement = "hello"
+ end
+
+ it 'should do basic text substitution' do
+ body = "I don't know why you say goodbye"
+ @censor_rule.apply_to_text!(body)
+ body.should == "I don't know why you say hello"
+ end
+
+ it 'should keep size same for binary substitution' do
+ body = "I don't know why you say goodbye"
+ orig_body = body.dup
+ @censor_rule.apply_to_binary!(body)
+ body.size.should == orig_body.size
+ body.should == "I don't know why you say xxxxxxx"
+ body.should_not == orig_body # be sure duplicated as expected
+ end
+end
+
diff --git a/spec/models/incoming_message_spec.rb b/spec/models/incoming_message_spec.rb
index 9fcaf42e7..df1b35c62 100644
--- a/spec/models/incoming_message_spec.rb
+++ b/spec/models/incoming_message_spec.rb
@@ -55,17 +55,17 @@ end
describe IncomingMessage, " folding quoted parts of emails" do
it "cope with [ in user names properly" do
- @user = mock_model(User)
- @user.stub!(:name).and_return("Sir [ Bobble")
- @info_request = mock_model(InfoRequest)
- @info_request.stub!(:user).and_return(@user)
+ @user = mock_model(User)
+ @user.stub!(:name).and_return("Sir [ Bobble")
+ @info_request = mock_model(InfoRequest)
+ @info_request.stub!(:user).and_return(@user)
- @incoming_message = IncomingMessage.new()
- @incoming_message.info_request = @info_request
+ @incoming_message = IncomingMessage.new()
+ @incoming_message.info_request = @info_request
- # this gives a warning if [ is in the name
- text = @incoming_message.remove_lotus_quoting("Sir [ Bobble \nSent by: \n")
- text.should == "\n\nFOLDED_QUOTED_SECTION"
+ # this gives a warning if [ is in the name
+ text = @incoming_message.remove_lotus_quoting("Sir [ Bobble \nSent by: \n")
+ text.should == "\n\nFOLDED_QUOTED_SECTION"
end
end
@@ -106,9 +106,85 @@ describe IncomingMessage, " checking validity to reply to" do
test_email("DoNotReply@tube.tfl.gov.uk", false)
end
+end
+
+describe IncomingMessage, " when censoring data" do
+ fixtures :incoming_messages, :raw_emails
+
+ before do
+ @test_data = "There was a mouse called Stilton, he wished that he was blue."
+
+ @im = incoming_messages(:useless_incoming_message)
+
+ @censor_rule_1 = CensorRule.new()
+ @censor_rule_1.text = "Stilton"
+ @censor_rule_1.replacement = "Jarlsberg"
+ @censor_rule_1.last_edit_editor = "unknown"
+ @censor_rule_1.last_edit_comment = "none"
+ @im.info_request.censor_rules << @censor_rule_1
+
+ @censor_rule_2 = CensorRule.new()
+ @censor_rule_2.text = "blue"
+ @censor_rule_2.replacement = "yellow"
+ @censor_rule_2.last_edit_editor = "unknown"
+ @censor_rule_2.last_edit_comment = "none"
+ @im.info_request.censor_rules << @censor_rule_2
+ end
+
+ it "should do nothing to a JPEG" do
+ data = @test_data.dup
+ @im.binary_mask_stuff!(data, "image/jpeg")
+ data.should == @test_data
+ end
+
+ it "should replace censor text in Word documents" do
+ data = @test_data.dup
+ @im.binary_mask_stuff!(data, "application/vnd.ms-word")
+ data.should == "There was a mouse called xxxxxxx, he wished that he was xxxx."
+ end
+
+ it "should replace ASCII email addresses in Word documents" do
+ orig_data = "His email was foo@bar.com"
+ data = orig_data.dup
+ @im.binary_mask_stuff!(data, "application/vnd.ms-word")
+ data.should == "His email was xxx@xxx.xxx"
+ end
+
+ it "should replace UCS-2 addresses in Word documents" do
+ orig_data = "His email was f\000o\000o\000@\000b\000a\000r\000.\000c\000o\000m\000, indeed"
+ data = orig_data.dup
+ @im.binary_mask_stuff!(data, "application/vnd.ms-word")
+ data.should == "His email was x\000x\000x\000@\000x\000x\000x\000.\000x\000x\000x\000, indeed"
+ end
+
+ it "should replace everything in PDF files" do
+ orig_pdf = load_file_fixture('tfl.pdf')
+ pdf = orig_pdf.dup
+
+ orig_text = IncomingMessage.get_attachment_text_internal_one_file('application/pdf', pdf)
+ orig_text.should match(/foi@tfl.gov.uk/)
+
+ @im.binary_mask_stuff!(pdf, "application/pdf")
+ masked_text = IncomingMessage.get_attachment_text_internal_one_file('application/pdf', pdf)
+ masked_text.should_not match(/foi@tfl.gov.uk/)
+ masked_text.should match(/xxx@xxx.xxx.xx/)
+ end
+ it "should apply censor rules to HTML files" do
+ data = @test_data.dup
+ data = @im.html_mask_stuff(data)
+ data.should == "There was a mouse called Jarlsberg, he wished that he was yellow."
+ end
+ it "should apply censor rules to From: addresses" do
+ mock_mail = mock('Email object')
+ mock_mail.stub!(:from_name_if_present).and_return("Stilton Mouse")
+ @im.stub!(:mail).and_return(mock_mail)
+
+ safe_mail_from = @im.safe_mail_from
+ safe_mail_from.should == "Jarlsberg Mouse"
+ end
end
diff --git a/spec/models/outgoing_message_spec.rb b/spec/models/outgoing_message_spec.rb
index 32705ce9d..969f77296 100644
--- a/spec/models/outgoing_message_spec.rb
+++ b/spec/models/outgoing_message_spec.rb
@@ -30,3 +30,25 @@ describe OutgoingMessage, " when making an outgoing message" do
end
+describe IncomingMessage, " when censoring data" do
+ fixtures :outgoing_messages
+
+ 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
+
+
diff --git a/spec/models/profile_photo_spec.rb b/spec/models/profile_photo_spec.rb
index 5b05c1205..51de45928 100644
--- a/spec/models/profile_photo_spec.rb
+++ b/spec/models/profile_photo_spec.rb
@@ -19,7 +19,7 @@ describe ProfilePhoto, "when constructing a new photo" do
end
it 'should accept and convert a PNG to right size' do
- data = load_image_fixture("parrot.png")
+ data = load_file_fixture("parrot.png")
profile_photo = ProfilePhoto.new(:data => data, :user => mock_model(User, :valid? => true))
profile_photo.valid?.should == true
profile_photo.image.format.should == 'PNG'
@@ -28,7 +28,7 @@ describe ProfilePhoto, "when constructing a new photo" do
end
it 'should accept and convert a JPEG to right format and size' do
- data = load_image_fixture("parrot.jpg")
+ data = load_file_fixture("parrot.jpg")
profile_photo = ProfilePhoto.new(:data => data, :user => mock_model(User, :valid? => true))
profile_photo.valid?.should == true
profile_photo.image.format.should == 'PNG'
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index 72e8ceb50..f4df22e9d 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -241,7 +241,7 @@ describe User, " when setting a profile photo" do
end
it "should attach it to the user" do
- data = load_image_fixture("parrot.png")
+ data = load_file_fixture("parrot.png")
profile_photo = ProfilePhoto.new(:data => data)
@user.set_profile_photo(profile_photo)
profile_photo.user.should == @user
@@ -250,9 +250,9 @@ describe User, " when setting a profile photo" do
# it "should destroy old photos being replaced" do
# ProfilePhoto.count.should == 0
#
-# data_1 = load_image_fixture("parrot.png")
+# data_1 = load_file_fixture("parrot.png")
# profile_photo_1 = ProfilePhoto.new(:data => data_1)
-# data_2 = load_image_fixture("parrot.jpg")
+# data_2 = load_file_fixture("parrot.jpg")
# profile_photo_2 = ProfilePhoto.new(:data => data_2)
#
# @user.set_profile_photo(profile_photo_1)