Skip to content
Browse files

* Imap setup mailer

  • Loading branch information...
1 parent 00711ca commit acb00c7079a778e3b40766052c50fab1964b1079 @hone committed Jan 11, 2009
View
27 app/models/imap.rb
@@ -0,0 +1,27 @@
+require 'net/imap'
+
+class Imap < RemoteMail
+ DEFAULT_PORT = 143
+ DEFAULT_SSL_PORT = 993
+
+ RemoteMailHelper::setup_columns( self )
+ column :old_ssl , :boolean
+
+ attr_reader :mailer
+
+ def setup_mailer
+ if self.ssl
+ self.port = DEFAULT_SSL_PORT if self.port.nil?
+ else
+ self.port = DEFAULT_PORT if self.port.nil?
+ end
+
+ if self.old_server != self.server or self.old_port != self.port or self.old_ssl != self.ssl
+ @mailer = Net::IMAP.new( self.server, self.port, self.ssl )
+ end
+
+ self.old_server = self.server
+ self.old_port = self.port
+ self.old_ssl = self.ssl
+ end
+end
View
7 spec/fixtures/imaps.yml
@@ -0,0 +1,7 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+# one:
+# column: value
+#
+# two:
+# column: value
View
127 spec/models/imap_spec.rb
@@ -0,0 +1,127 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+module ImapSpecHelper
+ def setup_imap( opts = {}, setup_mailer = true )
+ @valid_attributes = valid_imap_attributes.merge( opts )
+
+ @imap = Imap.new
+ @imap.attributes = @valid_attributes
+ @imap.setup_mailer if setup_mailer
+ end
+
+ def setup_mock_net_imap
+ @net_imap = mock( "Net::IMAP" )
+ # TODO stubbing this overrides should_receive
+# Net::IMAP.stub!(:new).and_return(@net_imap)
+ end
+end
+
+describe Imap, "setup mailer" do
+ include ImapSpecHelper
+
+ it "should setup mailer" do
+ setup_imap
+ @imap.mailer.class.should == Net::IMAP
+ end
+
+ it "should set default port if not set" do
+ setup_mock_net_imap
+ setup_imap( { :port => nil, :ssl => false } )
+
+ @imap.port.should == Imap::DEFAULT_PORT
+ end
+
+ it "should set default ssl port if not set" do
+ setup_mock_net_imap
+ Net::IMAP.should_receive(:new).once.and_return(@net_imap)
+ setup_imap( { :port => nil, :ssl => true } )
+
+ @imap.port.should == Imap::DEFAULT_SSL_PORT
+ end
+
+ it "should keep track of old server" do
+ setup_mock_net_imap
+ Net::IMAP.should_receive(:new).twice.and_return(@net_imap)
+ setup_imap( {}, false )
+ @imap.server.should == @valid_attributes[:server]
+ @imap.old_server.should be_nil
+ @imap.setup_mailer
+
+ new_server = "mail.wornpath.net"
+ @imap.server = new_server
+ @imap.server.should == new_server
+ @imap.old_server.should == @valid_attributes[:server]
+ @imap.setup_mailer
+
+ @imap.old_server.should == new_server
+ end
+
+ it "should keep track of old port" do
+ setup_mock_net_imap
+ Net::IMAP.should_receive(:new).twice.and_return(@net_imap)
+ setup_imap( {}, false )
+ @imap.port.should == @valid_attributes[:port]
+ @imap.old_port.should be_nil
+ @imap.setup_mailer
+
+ new_port = 992
+ @imap.port = new_port
+ @imap.port.should == new_port
+ @imap.old_port.should == @valid_attributes[:port]
+ @imap.setup_mailer
+
+ @imap.old_port.should == new_port
+ end
+
+ it "should keep track of old ssl" do
+ setup_mock_net_imap
+ Net::IMAP.should_receive(:new).twice.and_return(@net_imap)
+ setup_imap( {}, false )
+ @imap.ssl.should == @valid_attributes[:ssl]
+ @imap.old_ssl.should be_nil
+ @imap.setup_mailer
+
+ new_ssl = !@valid_attributes[:ssl]
+ @imap.ssl= new_ssl
+ @imap.ssl.should == new_ssl
+ @imap.old_ssl.should == @valid_attributes[:ssl]
+ @imap.setup_mailer
+
+ @imap.old_ssl.should == new_ssl
+ end
+
+ it "should not create a new Net::IMAP if server or port or ssl hasn't been changed" do
+ setup_mock_net_imap
+ Net::IMAP.should_receive(:new).once.and_return(@net_imap)
+
+ setup_imap
+ @imap.setup_mailer
+ end
+
+ it "should create a new Net::IMAP object upon server change" do
+ setup_mock_net_imap
+ Net::IMAP.should_receive(:new).twice.and_return(@net_imap)
+
+ setup_imap
+ @imap.server = "pop.dreamhost.com"
+ @imap.setup_mailer
+ end
+
+ it "should create a new Net::IMAP object upon port change" do
+ setup_mock_net_imap
+ Net::IMAP.should_receive(:new).twice.and_return(@net_imap)
+
+ setup_imap
+ @imap.port = 992
+ @imap.setup_mailer
+ end
+
+ it "should create a new Net::IMAP object upon ssl change" do
+ setup_mock_net_imap
+ Net::IMAP.should_receive(:new).twice.and_return(@net_imap)
+
+ setup_imap
+ @imap.ssl = !@valid_attributes[:ssl]
+ @imap.setup_mailer
+ end
+end
View
3 spec/models/pop3_spec.rb
@@ -2,7 +2,7 @@
module Pop3SpecHelper
def setup_pop3( opts = {}, setup_mailer = true )
- @valid_attributes = valid_attributes.merge( opts )
+ @valid_attributes = valid_pop3_attributes.merge( opts )
@pop3 = Pop3.new
@pop3.attributes = @valid_attributes
@@ -148,6 +148,7 @@ def setup_mock_time_pop3( opts = {}, setup_mailer = true )
result[:status].should == Pop3::TIMEOUT_ERROR_FLAG
end
+ # 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
View
2 spec/models/remote_mail_spec.rb
@@ -3,7 +3,7 @@
module RemoteMailSpecHelper
def setup_remote_mail( options = {} )
@remote_mail = RemoteMail.new
- @remote_mail.attributes = valid_attributes.merge( options )
+ @remote_mail.attributes = valid_pop3_attributes.merge( options )
end
def should_have_error_on_attribute( attribute, value = nil, error_num = 1 )
View
13 spec/spec_helper.rb
@@ -64,7 +64,7 @@ def setup_mock_time
@time.stub!(:to_s).and_return( 'Thu Jan 08 01:22:01 -0500 2009' )
end
-def valid_attributes
+def valid_pop3_attributes
@valid_attributes = {
:email_address => 'test.otherinbox@gmail.com',
:server => 'pop.gmail.com',
@@ -75,6 +75,17 @@ def valid_attributes
}
end
+def valid_imap_attributes
+ @valid_attributes = {
+ :email_address => 'otherinbox@hone.wornpath.net',
+ :server => 'mail.hone.wornpath.net',
+ :username => 'otherinbox@hone.wornpath.net',
+ :password => '0th3r1nb0x', # TODO need to encrypt this
+ :ssl => false,
+ :port => 143
+ }
+end
+
def setup_long_variable( value, length )
return_value = value
difference = length - value.size + 1
View
2 spec/views/backups/new.html.erb_spec.rb
@@ -3,7 +3,7 @@
describe "/backups/new" do
before(:each) do
@remote_mail = mock_model( RemoteMail )
- valid_attributes.each do |key, value|
+ valid_pop3_attributes.each do |key, value|
@remote_mail.stub!(key).and_return( value )
end
assigns[:remote_mail] = @remote_mail

0 comments on commit acb00c7

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