1
# Copyright (c) 2001-2006 Twisted Matrix Laboratories.
2
# See LICENSE for details.
4
from twisted.trial import unittest
5
from twisted.words.im import basesupport
6
from twisted.internet import error, defer
8
class DummyAccount(basesupport.AbstractAccount):
10
An account object that will do nothing when asked to start to log on.
13
loginHasFailed = False
14
loginCallbackCalled = False
16
def _startLogOn(self, *args):
18
Set self.loginDeferred to the same as the deferred returned, allowing a
19
testcase to .callback or .errback.
23
self.loginDeferred = defer.Deferred()
24
return self.loginDeferred
26
def _loginFailed(self, result):
27
self.loginHasFailed = True
28
return basesupport.AbstractAccount._loginFailed(self, result)
30
def _cb_logOn(self, result):
31
self.loginCallbackCalled = True
32
return basesupport.AbstractAccount._cb_logOn(self, result)
34
class DummyUI(object):
36
Provide just the interface required to be passed to AbstractAccount.logOn.
38
clientRegistered = False
40
def registerAccountClient(self, result):
41
self.clientRegistered = True
43
class ClientMsgTests(unittest.TestCase):
47
def makeAccount(self):
48
return DummyAccount('la', False, 'la', None, 'localhost', 6667)
50
def test_connect(self):
52
Test that account.logOn works, and it calls the right callback when a
53
connection is established.
55
account = self.makeAccount()
58
account.loginDeferred.callback(None)
61
self.assert_(not account.loginHasFailed,
62
"Login shouldn't have failed")
63
self.assert_(account.loginCallbackCalled,
64
"We should be logged in")
68
def test_failedConnect(self):
70
Test that account.logOn works, and it calls the right callback when a
71
connection is established.
73
account = self.makeAccount()
76
account.loginDeferred.errback(Exception())
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)
88
def test_alreadyConnecting(self):
90
Test that it can fail sensibly when someone tried to connect before
93
account = self.makeAccount()
96
self.assertRaises(error.ConnectError, account.logOn, ui)