test_basesupport.py 3.0 KB

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