Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

* added download method to pop3 for accessing the mail from the server

  • Loading branch information...
commit fbbb3bc76ea5bcb8948ac3b55eee4c99156f3cce 1 parent b3f414c
@hone authored
Showing with 76 additions and 5 deletions.
  1. +24 −1 app/models/pop3.rb
  2. +52 −4 spec/models/pop3_spec.rb
View
25 app/models/pop3.rb
@@ -4,6 +4,10 @@ class Pop3 < ActiveRecord::BaseWithoutTable
DEFAULT_PORT = 110
DEFAULT_SSL_PORT = 992
+ OK_FLAG = :ok
+ AUTHENTICATION_ERROR_FLAG = :authentication_error
+ TIMEOUT_ERROR_FLAG = :timeout_error
+
column :email_address, :string
column :server , :string
column :old_server, :string
@@ -13,11 +17,12 @@ class Pop3 < ActiveRecord::BaseWithoutTable
column :port , :integer
column :old_port, :integer
- attr_reader :mailer
+ attr_reader :mailer, :mails
# callbacks don't seem to be working
# before_save :setup_mailer
+ # sets up the Net::POP3 object
def setup_mailer
if self.ssl
self.port = DEFAULT_SSL_PORT if self.port.nil?
@@ -39,4 +44,22 @@ def setup_mailer
self.old_server = self.server
self.old_port = self.port
end
+
+ def download
+ # reset mail info
+ @mails = nil
+
+ status = OK_FLAG
+ begin
+ @mailer.start( self.username, self.password )
+ @mails = @mailer.mails
+ @mailer.finish
+ rescue Net::POPAuthenticationError
+ status = AUTHENTICATION_ERROR_FLAG
+ rescue Timeout::Error
+ status = TIMEOUT_ERROR_FLAG
+ end
+
+ { :mails => @mails, :status => status }
+ end
end
View
56 spec/models/pop3_spec.rb
@@ -15,6 +15,12 @@ def setup_pop3( opts = {}, setup_mailer = true )
@pop3.attributes = @valid_attributes
@pop3.setup_mailer if setup_mailer
end
+
+ def setup_mock_pop3( opts = {}, setup_mailer = true )
+ Net::POP3.should_receive(:new).once.and_return(@mailer)
+ @mailer.should_receive(:enable_ssl).once
+ setup_pop3( opts, setup_mailer )
+ end
end
describe Pop3, "setup mailer" do
@@ -107,12 +113,54 @@ def setup_pop3( opts = {}, setup_mailer = true )
end
describe Pop3, "download mail" do
- it "should not connect"
+ include Pop3SpecHelper
+
+ before(:each) do
+ @mailer = mock( "Net::POP3" )
+ end
+
+ it "should not connect due to authentication problem after downloading mail" do
+ setup_pop3
+
+ result = @pop3.download
+ result[:mails].should_not be_nil
+ result[:status].should == Pop3::OK_FLAG
+
+ @pop3.mailer.should_receive(:start).once.and_raise(Net::POPAuthenticationError)
+ result = @pop3.download
+
+ result[:mails].should be_nil
+ result[:status].should == Pop3::AUTHENTICATION_ERROR_FLAG
+ end
+
+ it "should not connect due to authentication problem" do
+ setup_mock_pop3( :username => 'boo' )
+
+ @mailer.should_receive(:start).once.and_raise(Net::POPAuthenticationError)
+ result = @pop3.download
+
+ result[:mails].should be_nil
+ result[:status].should == Pop3::AUTHENTICATION_ERROR_FLAG
+ end
+
+ it "should not connect due to invalid server" do
+ setup_mock_pop3( :server => 'pop.wornpath.net' )
+
+ @mailer.should_receive(:start).once.and_raise(Timeout::Error)
+ result = @pop3.download
+
+ result[:mails].should be_nil
+ result[:status].should == Pop3::TIMEOUT_ERROR_FLAG
+ end
it "should download mail" do
- pending "get setup mailer first"
- @pop3.save
- @pop3.mailer.mail.size.should == 3
+ setup_pop3
+ result = @pop3.download
+ @pop3.mails.size.should == 3
+ result.should_not be_nil
+
+ result[:mails].should === @pop3.mails
+ result[:status].should === Pop3::OK_FLAG
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.