Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

* finished zip pending example for overwriting zip output file

* moved generate_mbox_name from pop3 to remote_mail
  • Loading branch information...
commit 1586aec68798748a7323d40b5590c1eaff1becfd 1 parent acb00c7
@hone authored
View
5 app/models/pop3.rb
@@ -1,5 +1,4 @@
require 'net/pop'
-require 'digest/sha1'
class Pop3 < RemoteMail
DEFAULT_PORT = 110
@@ -63,10 +62,6 @@ def download
}
end
- # generate hash based of e-mail address and current time
- def generate_mbox_name
- Digest::SHA1.hexdigest( "#{self.email_address}|#{Time.now.to_s}" )
- end
def write_mbox( name )
filename = "#{TMP_DIR}/#{name}"
View
9 app/models/remote_mail.rb
@@ -1,5 +1,6 @@
require 'zip/zip'
require 'zip/zipfilesystem'
+require 'digest/sha1'
module RemoteMailHelper
def self.setup_columns( klass )
@@ -47,8 +48,16 @@ class RemoteMail < ActiveRecord::BaseWithoutTable
def download
raise NotImplementedError.new
end
+
+ # generate hash based of e-mail address and current time
+ def generate_mbox_name
+ Digest::SHA1.hexdigest( "#{self.email_address}|#{Time.now.to_s}" )
+ end
def zip( files, output_file )
+ if File.exist?( output_file )
+ FileUtils.rm( output_file )
+ end
Zip::ZipFile.open(output_file, Zip::ZipFile::CREATE) do |zipfile|
files.each do |file|
base_file = File.basename( file )
View
24 spec/models/pop3_spec.rb
@@ -10,20 +10,15 @@ def setup_pop3( opts = {}, setup_mailer = true )
end
def setup_mock_mailer_and_time_pop3( opts = {}, setup_mailer = true )
- @mailer = mock( "Net::POP3" )
- Net::POP3.should_receive(:new).once.and_return(@mailer)
- @mailer.should_receive(:enable_ssl).once
+ setup_mock_net_pop3
@time = mock("Time")
- Time.stub!(:now).and_return(@time)
- @time.stub!(:to_s).and_return( 'Thu Jan 08 01:22:01 -0500 2009' )
setup_pop3( opts, setup_mailer )
end
- def setup_mock_time_pop3( opts = {}, setup_mailer = true )
- @time = mock("Time")
- Time.should_receive(:now).once.and_return(@time)
- @time.should_receive(:to_s).once.and_return( 'Thu Jan 08 01:22:01 -0500 2009' )
- setup_pop3( opts, setup_mailer )
+ def setup_mock_net_pop3
+ @mailer = mock( "Net::POP3" )
+ Net::POP3.should_receive(:new).once.and_return(@mailer)
+ @mailer.should_receive(:enable_ssl).once
end
end
@@ -151,7 +146,9 @@ def setup_mock_time_pop3( opts = {}, setup_mailer = true )
# TODO not sure why this test fails, but gives tempfile error
it "should download mail" do
pending( "this test should be run explicitly" )
- setup_mock_time_pop3
+ setup_mock_time
+ Time.should_receive(:now).once.and_return(@time)
+ setup_pop3
puts "tmpdir: #{Dir::tmpdir}"
result = @pop3.download
result.should_not be_nil
@@ -174,11 +171,6 @@ def setup_mock_time_pop3( opts = {}, setup_mailer = true )
remove_file( TMP_MBOX_FILE )
end
- it "should generate mbox name" do
- setup_mock_time_pop3
-
- @pop3.generate_mbox_name.should == 'bd4937b271d8f20c3003489a231b3824943a163f'
- end
it "should generate mbox file" do
setup_pop3
View
26 spec/models/remote_mail_spec.rb
@@ -14,6 +14,17 @@ def should_have_error_on_attribute( attribute, value = nil, error_num = 1 )
end
end
+describe RemoteMail do
+ include RemoteMailSpecHelper
+
+ it "should generate mbox name" do
+ setup_mock_time
+ setup_remote_mail
+
+ @remote_mail.generate_mbox_name.should == 'bd4937b271d8f20c3003489a231b3824943a163f'
+ end
+end
+
describe RemoteMail, "zip" do
include RemoteMailSpecHelper
@@ -23,18 +34,23 @@ def should_have_error_on_attribute( attribute, value = nil, error_num = 1 )
before(:each) do
setup_remote_mail
- remove_file( TMP_MBOX_FILE )
+ FileUtils.touch( TMP_MBOX_FILE )
end
it "should zip a single mbox" do
- FileUtils.touch( TMP_MBOX_FILE )
-
- @remote_mail.zip( TMP_MBOX_FILE, @zip_output )
+ @remote_mail.zip( [TMP_MBOX_FILE], @zip_output )
File.should be_exist( @zip_output )
File.should_not be_exist( TMP_MBOX_FILE )
end
- it "should overwrite existing zip file"
+ it "should overwrite existing zip file" do
+ remove_file( @zip_output )
+ File.open( @zip_output, 'w' ) {|file| file.puts "delete this" }
+
+ @remote_mail.zip( [TMP_MBOX_FILE], @zip_output )
+ File.should be_exist( @zip_output )
+ File.open( @zip_output ) {|file| file.readlines.first.should_not match /delete this/ }
+ end
after(:all) do
remove_file( TMP_MBOX_FILE )
View
6 spec/spec_helper.rb
@@ -93,3 +93,9 @@ def setup_long_variable( value, length )
return_value
end
+
+def setup_mock_time
+ @time = mock("Time")
+ Time.should_receive(:now).once.and_return(@time)
+ @time.should_receive(:to_s).once.and_return( 'Thu Jan 08 01:22:01 -0500 2009' )
+end
Please sign in to comment.
Something went wrong with that request. Please try again.