From 3f45c033cb46b24f69b88e6705557e4c077ecd9b Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Fri, 22 May 2015 15:46:43 +0100 Subject: Remove obsolete test We don't write the data for a raw email to the database anymore. It's written to a file. --- spec/models/raw_email_spec.rb | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) (limited to 'spec/models/raw_email_spec.rb') diff --git a/spec/models/raw_email_spec.rb b/spec/models/raw_email_spec.rb index aa82b0bc3..e1533b981 100644 --- a/spec/models/raw_email_spec.rb +++ b/spec/models/raw_email_spec.rb @@ -7,7 +7,8 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') -describe User, "manipulating a raw email" do +describe User, "manipulating a raw email" do + before do @raw_email = RawEmail.new incoming_message = mock_model(IncomingMessage) @@ -16,28 +17,12 @@ describe User, "manipulating a raw email" do @raw_email.stub!(:incoming_message).and_return(incoming_message) end - it 'putting data in comes back out' do + it 'putting data in comes back out' do @raw_email.data = "Hello, world!" @raw_email.save! @raw_email.reload @raw_email.data.should == "Hello, world!" end - # TODO: this test fails, hopefully will be fixed in later Rails. - # Doesn't matter too much for us for storing raw_emails, it would seem, - # but keep an eye out. - - # This is testing a bug in Rails PostgreSQL code - # http://blog.aradine.com/2009/09/rubys-marshal-and-activerecord-and.html - # https://rails.lighthouseapp.com/projects/8994/tickets/1063-binary-data-broken-with-postgresql-adapter -# it 'putting data in comes back out even if it has a backslash in it' do -# @raw_email.data = "This \\ that" -# @raw_email.save! -# @raw_email.reload -# $stderr.puts @raw_email.data -# $stderr.puts "This \\ that" -# @raw_email.data.should == "This \\ that" -# end - end - + -- cgit v1.2.3 From 0c915e5e1a982513f9ded38b11f8b7b570c518e8 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Fri, 22 May 2015 15:54:40 +0100 Subject: Cleanup spec. Move setup to spec - not in common with other specs. --- spec/models/raw_email_spec.rb | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'spec/models/raw_email_spec.rb') diff --git a/spec/models/raw_email_spec.rb b/spec/models/raw_email_spec.rb index e1533b981..cbd7d71a5 100644 --- a/spec/models/raw_email_spec.rb +++ b/spec/models/raw_email_spec.rb @@ -7,21 +7,22 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') -describe User, "manipulating a raw email" do +describe RawEmail do - before do - @raw_email = RawEmail.new - incoming_message = mock_model(IncomingMessage) - info_request = mock_model(InfoRequest) - incoming_message.stub!(:info_request).and_return(info_request) - @raw_email.stub!(:incoming_message).and_return(incoming_message) - end + describe :data do + + it 'roundtrips data unchanged' do + raw_email = RawEmail.new + incoming_message = mock_model(IncomingMessage) + info_request = mock_model(InfoRequest) + incoming_message.stub!(:info_request).and_return(info_request) + raw_email.stub!(:incoming_message).and_return(incoming_message) + raw_email.data = "Hello, world!" + raw_email.save! + raw_email.reload + raw_email.data.should == "Hello, world!" + end - it 'putting data in comes back out' do - @raw_email.data = "Hello, world!" - @raw_email.save! - @raw_email.reload - @raw_email.data.should == "Hello, world!" end end -- cgit v1.2.3 From c0a76dd0b1816f9a324e6187795c18abf36eeed5 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Fri, 22 May 2015 15:29:34 +0100 Subject: Handle unparsed email contents as binary. I think I was wrong in a83b379fd2d676172855825d0592937b234371e2 in assuming that all email gets properly encoded for transfer. Looking at the mail gem load method https://github.com/mikel/mail/blob/b159e0a542962fdd5e292a48cfffa560d7cf412e/lib/mail/mail.rb#L175a, it reads raw email content from a file in binary mode. So this commit makes both reading and writing the raw_email a binary mode operation and adds a data_as_text method for displaying the data in the admin interface that coerces it to valid utf-8. --- spec/models/raw_email_spec.rb | 47 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) (limited to 'spec/models/raw_email_spec.rb') diff --git a/spec/models/raw_email_spec.rb b/spec/models/raw_email_spec.rb index cbd7d71a5..8e0d3b457 100644 --- a/spec/models/raw_email_spec.rb +++ b/spec/models/raw_email_spec.rb @@ -1,3 +1,4 @@ +# -*- encoding : utf-8 -*- # == Schema Information # # Table name: raw_emails @@ -9,18 +10,46 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe RawEmail do + def roundtrip_data(raw_email, data) + raw_email.data = data + raw_email.save! + raw_email.reload + raw_email.data + end + describe :data do it 'roundtrips data unchanged' do - raw_email = RawEmail.new - incoming_message = mock_model(IncomingMessage) - info_request = mock_model(InfoRequest) - incoming_message.stub!(:info_request).and_return(info_request) - raw_email.stub!(:incoming_message).and_return(incoming_message) - raw_email.data = "Hello, world!" - raw_email.save! - raw_email.reload - raw_email.data.should == "Hello, world!" + raw_email = FactoryGirl.create(:incoming_message).raw_email + data = roundtrip_data(raw_email, "Hello, world!") + data.should == "Hello, world!" + end + + it 'returns an unchanged binary string with a valid encoding if the data is non-ascii and non-utf-8' do + raw_email = FactoryGirl.create(:incoming_message).raw_email + data = roundtrip_data(raw_email, "\xA0") + + if data.respond_to?(:encoding) + data.encoding.to_s.should == 'ASCII-8BIT' + data.valid_encoding?.should be_true + data = data.force_encoding('UTF-8') + end + data.should == "\xA0" + end + + end + + describe :data_as_text do + + it 'returns a utf-8 string with a valid encoding if the data is non-ascii and non-utf8' do + raw_email = FactoryGirl.create(:incoming_message).raw_email + roundtrip_data(raw_email, "\xA0ccc") + data_as_text = raw_email.data_as_text + data_as_text.should == "ccc" + if data_as_text.respond_to?(:encoding) + data_as_text.encoding.to_s.should == 'UTF-8' + data_as_text.valid_encoding?.should be_true + end end end -- cgit v1.2.3