aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/foi_attachment_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/models/foi_attachment_spec.rb')
-rw-r--r--spec/models/foi_attachment_spec.rb151
1 files changed, 120 insertions, 31 deletions
diff --git a/spec/models/foi_attachment_spec.rb b/spec/models/foi_attachment_spec.rb
index 882723d1e..b383e5d09 100644
--- a/spec/models/foi_attachment_spec.rb
+++ b/spec/models/foi_attachment_spec.rb
@@ -1,3 +1,4 @@
+# -*- encoding : utf-8 -*-
# == Schema Information
#
# Table name: foi_attachments
@@ -17,45 +18,133 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe FoiAttachment do
- before(:each) do
- load_raw_emails_data
+ describe :body= do
+
+ it "sets the body" do
+ attachment = FoiAttachment.new
+ attachment.body = "baz"
+ attachment.body.should == "baz"
+ end
+
+ it "sets the size" do
+ attachment = FoiAttachment.new
+ attachment.body = "baz"
+ attachment.body.should == "baz"
+ attachment.display_size.should == "0K"
+ end
+
+ it "reparses the body if it disappears" do
+ load_raw_emails_data
+ im = incoming_messages(:useless_incoming_message)
+ im.extract_attachments!
+ main = im.get_main_body_text_part
+ orig_body = main.body
+ main.delete_cached_file!
+ lambda {
+ im.get_main_body_text_part.body
+ }.should_not raise_error(Errno::ENOENT)
+ main.delete_cached_file!
+ main = im.get_main_body_text_part
+ main.body.should == orig_body
+ end
+
end
- it "sets the body" do
- attachment = FoiAttachment.new
- attachment.body = "baz"
- attachment.body.should == "baz"
+ describe :body do
+
+ it 'returns a binary encoded string when newly created' do
+ foi_attachment = FactoryGirl.create(:body_text)
+ if String.method_defined?(:encode)
+ expect(foi_attachment.body.encoding.to_s).to eq('ASCII-8BIT')
+ end
+ end
+
+
+ it 'returns a binary encoded string when saved' do
+ foi_attachment = FactoryGirl.create(:body_text)
+ foi_attachment = FoiAttachment.find(foi_attachment)
+ if String.method_defined?(:encode)
+ expect(foi_attachment.body.encoding.to_s).to eq('ASCII-8BIT')
+ end
+ end
+
+ end
+
+ describe :body_as_text do
+
+ it 'has a valid UTF-8 string when newly created' do
+ foi_attachment = FactoryGirl.create(:body_text)
+ if String.method_defined?(:encode)
+ expect(foi_attachment.body_as_text.string.encoding.to_s).to eq('UTF-8')
+ expect(foi_attachment.body_as_text.string.valid_encoding?).to be_true
+ end
+ end
+
+ it 'has a valid UTF-8 string when saved' do
+ foi_attachment = FactoryGirl.create(:body_text)
+ foi_attachment = FoiAttachment.find(foi_attachment)
+ if String.method_defined?(:encode)
+ expect(foi_attachment.body_as_text.string.encoding.to_s).to eq('UTF-8')
+ expect(foi_attachment.body_as_text.string.valid_encoding?).to be_true
+ end
+ end
+
+
+ it 'has a true scrubbed? value if the body has been coerced to valid UTF-8' do
+ foi_attachment = FactoryGirl.create(:body_text)
+ foi_attachment.body = "\x0FX\x1C\x8F\xA4\xCF\xF6\x8C\x9D\xA7\x06\xD9\xF7\x90lo"
+ expect(foi_attachment.body_as_text.scrubbed?).to be_true
+ end
+
+ it 'has a false scrubbed? value if the body has not been coerced to valid UTF-8' do
+ foi_attachment = FactoryGirl.create(:body_text)
+ foi_attachment.body = "κόσμε"
+ expect(foi_attachment.body_as_text.scrubbed?).to be_false
+ end
+
end
- it "sets the size" do
- attachment = FoiAttachment.new
- attachment.body = "baz"
- attachment.body.should == "baz"
- attachment.update_display_size!
- attachment.display_size.should == "0K"
+
+ describe :default_body do
+
+ it 'returns valid UTF-8 for a text attachment' do
+ foi_attachment = FactoryGirl.create(:body_text)
+ if String.method_defined?(:encode)
+ expect(foi_attachment.default_body.encoding.to_s).to eq('UTF-8')
+ expect(foi_attachment.default_body.valid_encoding?).to be_true
+ end
+ end
+
+ it 'returns binary for a PDF attachment' do
+ foi_attachment = FactoryGirl.create(:pdf_attachment)
+ if String.method_defined?(:encode)
+ expect(foi_attachment.default_body.encoding.to_s).to eq('ASCII-8BIT')
+ end
+ end
+
end
- it "reparses the body if it disappears" do
- im = incoming_messages(:useless_incoming_message)
- im.extract_attachments!
- main = im.get_main_body_text_part
- orig_body = main.body
- main.delete_cached_file!
- lambda {
- im.get_main_body_text_part.body
- }.should_not raise_error(Errno::ENOENT)
- main.delete_cached_file!
- main = im.get_main_body_text_part
- main.body.should == orig_body
+
+
+ describe :ensure_filename! do
+
+ it 'should create a filename for an instance with a blank filename' do
+ attachment = FoiAttachment.new
+ attachment.filename = ''
+ attachment.ensure_filename!
+ attachment.filename.should == 'attachment.bin'
+ end
end
-end
-describe FoiAttachment, "when ensuring a filename is present" do
+ describe :has_body_as_html? do
+
+ it 'should be true for a pdf attachment' do
+ FactoryGirl.build(:pdf_attachment).has_body_as_html?.should be_true
+ end
+
+ it 'should be false for an html attachment' do
+ FactoryGirl.build(:html_attachment).has_body_as_html?.should be_false
+ end
- it 'should create a filename for an instance with a blank filename' do
- attachment = FoiAttachment.new
- attachment.filename = ''
- attachment.ensure_filename!
- attachment.filename.should == 'attachment.bin'
end
end