Permalink
Browse files

Add some guards in login/logout and fix tests

  • Loading branch information...
1 parent aff69cf commit 1cda52300d9d7e92eebce62e28cf90add8393209 @myobie myobie committed Dec 13, 2011
Showing with 39 additions and 24 deletions.
  1. +6 −6 lib/gmail.rb
  2. +33 −18 test/test_gmail.rb
View
12 lib/gmail.rb
@@ -28,16 +28,16 @@ class << self
###########################
# READING EMAILS
- #
+ #
# gmail.inbox
# gmail.label('News')
- #
+ #
###########################
def inbox
in_label('inbox')
end
-
+
def create_label(name)
imap.create(name)
end
@@ -87,13 +87,13 @@ def deliver(mail=nil, &block)
mail.from = meta.username unless mail.from
mail.deliver!
end
-
+
###########################
# LOGIN
###########################
def login
res = @imap.login(meta.username, meta.password)
- @logged_in = true if res.name == 'OK'
+ @logged_in = true if res && res.name == 'OK'
end
def logged_in?
!!@logged_in
@@ -102,7 +102,7 @@ def logged_in?
def logout
if logged_in?
res = @imap.logout
- @logged_in = false if res.name == 'OK'
+ @logged_in = false if res && res.name == 'OK'
end
end
View
51 test/test_gmail.rb
@@ -6,67 +6,82 @@ def test_initialize
Net::IMAP.expects(:new).with('imap.gmail.com', 993, true, nil, false).returns(imap)
gmail = Gmail.new('test', 'password')
end
-
+
def test_imap_does_login
setup_mocks(:at_exit => true)
+ res = mock('res')
+ res.expects(:name).at_least(1).returns('OK')
- @imap.expects(:disconnected?).at_least_once.returns(true).then.returns(false)
- @imap.expects(:login).with('test@gmail.com', 'password')
+ # @imap.expects(:disconnected?).at_least_once.returns(true).then.returns(false)
+ # TODO: figure why this was here in the first place
+ @imap.expects(:login).with('test@gmail.com', 'password').returns(res)
@gmail.imap
end
def test_imap_does_login_only_once
setup_mocks(:at_exit => true)
+ res = mock('res')
+ res.expects(:name).at_least(1).returns('OK')
- @imap.expects(:disconnected?).at_least_once.returns(true).then.returns(false)
- @imap.expects(:login).with('test@gmail.com', 'password')
+ # @imap.expects(:disconnected?).at_least_once.returns(true).then.returns(false)
+ # TODO: figure why this was here in the first place
+ @imap.expects(:login).with('test@gmail.com', 'password').returns(res)
@gmail.imap
@gmail.imap
@gmail.imap
end
def test_imap_does_login_without_appending_gmail_domain
setup_mocks(:at_exit => true)
+ res = mock('res')
+ res.expects(:name).at_least(1).returns('OK')
- @imap.expects(:disconnected?).at_least_once.returns(true).then.returns(false)
- @imap.expects(:login).with('test@gmail.com', 'password')
+ # @imap.expects(:disconnected?).at_least_once.returns(true).then.returns(false)
+ # TODO: figure why this was here in the first place
+ @imap.expects(:login).with('test@gmail.com', 'password').returns(res)
@gmail.imap
end
-
+
def test_imap_logs_out
setup_mocks(:at_exit => true)
+ res = mock('res')
+ res.expects(:name).at_least(1).returns('OK')
- @imap.expects(:disconnected?).at_least_once.returns(true).then.returns(false)
- @imap.expects(:login).with('test@gmail.com', 'password')
+ # @imap.expects(:disconnected?).at_least_once.returns(true).then.returns(false)
+ # TODO: figure why this was here in the first place
+ @imap.expects(:login).with('test@gmail.com', 'password').returns(res)
@gmail.imap
- @imap.expects(:logout).returns(true)
+ @imap.expects(:logout).returns(res)
@gmail.logout
end
def test_imap_logout_does_nothing_if_not_logged_in
setup_mocks
- @imap.expects(:disconnected?).returns(true)
+ @gmail.expects(:logged_in?).returns(false)
@imap.expects(:logout).never
@gmail.logout
end
-
+
def test_imap_calls_create_label
setup_mocks(:at_exit => true)
- @imap.expects(:disconnected?).at_least_once.returns(true).then.returns(false)
- @imap.expects(:login).with('test@gmail.com', 'password')
+ res = mock('res')
+ res.expects(:name).at_least(1).returns('OK')
+ # @imap.expects(:disconnected?).at_least_once.returns(true).then.returns(false)
+ # TODO: figure out why this was here in the first place
+ @imap.expects(:login).with('test@gmail.com', 'password').returns(res)
@imap.expects(:create).with('foo')
@gmail.create_label('foo')
end
-
+
private
def setup_mocks(options = {})
options = {:at_exit => false}.merge(options)
@imap = mock('imap')
Net::IMAP.expects(:new).with('imap.gmail.com', 993, true, nil, false).returns(@imap)
@gmail = Gmail.new('test@gmail.com', 'password')
-
+
# need this for the at_exit block that auto-exits after this test method completes
@imap.expects(:logout).at_least(0) if options[:at_exit]
end
-end
+end

0 comments on commit 1cda523

Please sign in to comment.