| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2001-2006 Twisted Matrix Laboratories. | |
| 2 # See LICENSE for details. | |
| 3 | |
| 4 from twisted.trial import unittest | |
| 5 from twisted.words.im import basesupport | |
| 6 from twisted.internet import error, defer | |
| 7 | |
| 8 class DummyAccount(basesupport.AbstractAccount): | |
| 9 """ | |
| 10 An account object that will do nothing when asked to start to log on. | |
| 11 """ | |
| 12 | |
| 13 loginHasFailed = False | |
| 14 loginCallbackCalled = False | |
| 15 | |
| 16 def _startLogOn(self, *args): | |
| 17 """ | |
| 18 Set self.loginDeferred to the same as the deferred returned, allowing a | |
| 19 testcase to .callback or .errback. | |
| 20 | |
| 21 @return: A deferred. | |
| 22 """ | |
| 23 self.loginDeferred = defer.Deferred() | |
| 24 return self.loginDeferred | |
| 25 | |
| 26 def _loginFailed(self, result): | |
| 27 self.loginHasFailed = True | |
| 28 return basesupport.AbstractAccount._loginFailed(self, result) | |
| 29 | |
| 30 def _cb_logOn(self, result): | |
| 31 self.loginCallbackCalled = True | |
| 32 return basesupport.AbstractAccount._cb_logOn(self, result) | |
| 33 | |
| 34 class DummyUI(object): | |
| 35 """ | |
| 36 Provide just the interface required to be passed to AbstractAccount.logOn. | |
| 37 """ | |
| 38 clientRegistered = False | |
| 39 | |
| 40 def registerAccountClient(self, result): | |
| 41 self.clientRegistered = True | |
| 42 | |
| 43 class ClientMsgTests(unittest.TestCase): | |
| 44 def makeUI(self): | |
| 45 return DummyUI() | |
| 46 | |
| 47 def makeAccount(self): | |
| 48 return DummyAccount('la', False, 'la', None, 'localhost', 6667) | |
| 49 | |
| 50 def test_connect(self): | |
| 51 """ | |
| 52 Test that account.logOn works, and it calls the right callback when a | |
| 53 connection is established. | |
| 54 """ | |
| 55 account = self.makeAccount() | |
| 56 ui = self.makeUI() | |
| 57 d = account.logOn(ui) | |
| 58 account.loginDeferred.callback(None) | |
| 59 | |
| 60 def check(result): | |
| 61 self.assert_(not account.loginHasFailed, | |
| 62 "Login shouldn't have failed") | |
| 63 self.assert_(account.loginCallbackCalled, | |
| 64 "We should be logged in") | |
| 65 d.addCallback(check) | |
| 66 return d | |
| 67 | |
| 68 def test_failedConnect(self): | |
| 69 """ | |
| 70 Test that account.logOn works, and it calls the right callback when a | |
| 71 connection is established. | |
| 72 """ | |
| 73 account = self.makeAccount() | |
| 74 ui = self.makeUI() | |
| 75 d = account.logOn(ui) | |
| 76 account.loginDeferred.errback(Exception()) | |
| 77 | |
| 78 def err(reason): | |
| 79 self.assert_(account.loginHasFailed, "Login should have failed") | |
| 80 self.assert_(not account.loginCallbackCalled, | |
| 81 "We shouldn't be logged in") | |
| 82 self.assert_(not ui.clientRegistered, | |
| 83 "Client shouldn't be registered in the UI") | |
| 84 cb = lambda r: self.assert_(False, "Shouldn't get called back") | |
| 85 d.addCallbacks(cb, err) | |
| 86 return d | |
| 87 | |
| 88 def test_alreadyConnecting(self): | |
| 89 """ | |
| 90 Test that it can fail sensibly when someone tried to connect before | |
| 91 we did. | |
| 92 """ | |
| 93 account = self.makeAccount() | |
| 94 ui = self.makeUI() | |
| 95 account.logOn(ui) | |
| 96 self.assertRaises(error.ConnectError, account.logOn, ui) | |
| 97 | |
| OLD | NEW |