Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

* 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
Terence Lee authored
5 app/models/pop3.rb
... ... @@ -1,5 +1,4 @@
1 1 require 'net/pop'
2   -require 'digest/sha1'
3 2
4 3 class Pop3 < RemoteMail
5 4 DEFAULT_PORT = 110
@@ -63,10 +62,6 @@ def download
63 62 }
64 63 end
65 64
66   - # generate hash based of e-mail address and current time
67   - def generate_mbox_name
68   - Digest::SHA1.hexdigest( "#{self.email_address}|#{Time.now.to_s}" )
69   - end
70 65
71 66 def write_mbox( name )
72 67 filename = "#{TMP_DIR}/#{name}"
9 app/models/remote_mail.rb
... ... @@ -1,5 +1,6 @@
1 1 require 'zip/zip'
2 2 require 'zip/zipfilesystem'
  3 +require 'digest/sha1'
3 4
4 5 module RemoteMailHelper
5 6 def self.setup_columns( klass )
@@ -47,8 +48,16 @@ class RemoteMail < ActiveRecord::BaseWithoutTable
47 48 def download
48 49 raise NotImplementedError.new
49 50 end
  51 +
  52 + # generate hash based of e-mail address and current time
  53 + def generate_mbox_name
  54 + Digest::SHA1.hexdigest( "#{self.email_address}|#{Time.now.to_s}" )
  55 + end
50 56
51 57 def zip( files, output_file )
  58 + if File.exist?( output_file )
  59 + FileUtils.rm( output_file )
  60 + end
52 61 Zip::ZipFile.open(output_file, Zip::ZipFile::CREATE) do |zipfile|
53 62 files.each do |file|
54 63 base_file = File.basename( file )
24 spec/models/pop3_spec.rb
@@ -10,20 +10,15 @@ def setup_pop3( opts = {}, setup_mailer = true )
10 10 end
11 11
12 12 def setup_mock_mailer_and_time_pop3( opts = {}, setup_mailer = true )
13   - @mailer = mock( "Net::POP3" )
14   - Net::POP3.should_receive(:new).once.and_return(@mailer)
15   - @mailer.should_receive(:enable_ssl).once
  13 + setup_mock_net_pop3
16 14 @time = mock("Time")
17   - Time.stub!(:now).and_return(@time)
18   - @time.stub!(:to_s).and_return( 'Thu Jan 08 01:22:01 -0500 2009' )
19 15 setup_pop3( opts, setup_mailer )
20 16 end
21 17
22   - def setup_mock_time_pop3( opts = {}, setup_mailer = true )
23   - @time = mock("Time")
24   - Time.should_receive(:now).once.and_return(@time)
25   - @time.should_receive(:to_s).once.and_return( 'Thu Jan 08 01:22:01 -0500 2009' )
26   - setup_pop3( opts, setup_mailer )
  18 + def setup_mock_net_pop3
  19 + @mailer = mock( "Net::POP3" )
  20 + Net::POP3.should_receive(:new).once.and_return(@mailer)
  21 + @mailer.should_receive(:enable_ssl).once
27 22 end
28 23 end
29 24
@@ -151,7 +146,9 @@ def setup_mock_time_pop3( opts = {}, setup_mailer = true )
151 146 # TODO not sure why this test fails, but gives tempfile error
152 147 it "should download mail" do
153 148 pending( "this test should be run explicitly" )
154   - setup_mock_time_pop3
  149 + setup_mock_time
  150 + Time.should_receive(:now).once.and_return(@time)
  151 + setup_pop3
155 152 puts "tmpdir: #{Dir::tmpdir}"
156 153 result = @pop3.download
157 154 result.should_not be_nil
@@ -174,11 +171,6 @@ def setup_mock_time_pop3( opts = {}, setup_mailer = true )
174 171 remove_file( TMP_MBOX_FILE )
175 172 end
176 173
177   - it "should generate mbox name" do
178   - setup_mock_time_pop3
179   -
180   - @pop3.generate_mbox_name.should == 'bd4937b271d8f20c3003489a231b3824943a163f'
181   - end
182 174
183 175 it "should generate mbox file" do
184 176 setup_pop3
26 spec/models/remote_mail_spec.rb
@@ -14,6 +14,17 @@ def should_have_error_on_attribute( attribute, value = nil, error_num = 1 )
14 14 end
15 15 end
16 16
  17 +describe RemoteMail do
  18 + include RemoteMailSpecHelper
  19 +
  20 + it "should generate mbox name" do
  21 + setup_mock_time
  22 + setup_remote_mail
  23 +
  24 + @remote_mail.generate_mbox_name.should == 'bd4937b271d8f20c3003489a231b3824943a163f'
  25 + end
  26 +end
  27 +
17 28 describe RemoteMail, "zip" do
18 29 include RemoteMailSpecHelper
19 30
@@ -23,18 +34,23 @@ def should_have_error_on_attribute( attribute, value = nil, error_num = 1 )
23 34
24 35 before(:each) do
25 36 setup_remote_mail
26   - remove_file( TMP_MBOX_FILE )
  37 + FileUtils.touch( TMP_MBOX_FILE )
27 38 end
28 39
29 40 it "should zip a single mbox" do
30   - FileUtils.touch( TMP_MBOX_FILE )
31   -
32   - @remote_mail.zip( TMP_MBOX_FILE, @zip_output )
  41 + @remote_mail.zip( [TMP_MBOX_FILE], @zip_output )
33 42 File.should be_exist( @zip_output )
34 43 File.should_not be_exist( TMP_MBOX_FILE )
35 44 end
36 45
37   - it "should overwrite existing zip file"
  46 + it "should overwrite existing zip file" do
  47 + remove_file( @zip_output )
  48 + File.open( @zip_output, 'w' ) {|file| file.puts "delete this" }
  49 +
  50 + @remote_mail.zip( [TMP_MBOX_FILE], @zip_output )
  51 + File.should be_exist( @zip_output )
  52 + File.open( @zip_output ) {|file| file.readlines.first.should_not match /delete this/ }
  53 + end
38 54
39 55 after(:all) do
40 56 remove_file( TMP_MBOX_FILE )
6 spec/spec_helper.rb
@@ -93,3 +93,9 @@ def setup_long_variable( value, length )
93 93
94 94 return_value
95 95 end
  96 +
  97 +def setup_mock_time
  98 + @time = mock("Time")
  99 + Time.should_receive(:now).once.and_return(@time)
  100 + @time.should_receive(:to_s).once.and_return( 'Thu Jan 08 01:22:01 -0500 2009' )
  101 +end

0 comments on commit 1586aec

Please sign in to comment.
Something went wrong with that request. Please try again.