test_simpleauth.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for basic constructs of L{twisted.cred.credentials}.
  5. """
  6. from __future__ import division, absolute_import
  7. from twisted.trial.unittest import TestCase
  8. from twisted.cred.credentials import UsernamePassword, IUsernamePassword
  9. from twisted.cred.credentials import UsernameHashedPassword
  10. from twisted.cred.credentials import IUsernameHashedPassword
  11. class UsernamePasswordTests(TestCase):
  12. """
  13. Tests for L{UsernamePassword}.
  14. """
  15. def test_initialisation(self):
  16. """
  17. The initialisation of L{UsernamePassword} will set C{username} and
  18. C{password} on it.
  19. """
  20. creds = UsernamePassword(b"foo", b"bar")
  21. self.assertEqual(creds.username, b"foo")
  22. self.assertEqual(creds.password, b"bar")
  23. def test_correctPassword(self):
  24. """
  25. Calling C{checkPassword} on a L{UsernamePassword} will return L{True}
  26. when the password given is the password on the object.
  27. """
  28. creds = UsernamePassword(b"user", b"pass")
  29. self.assertTrue(creds.checkPassword(b"pass"))
  30. def test_wrongPassword(self):
  31. """
  32. Calling C{checkPassword} on a L{UsernamePassword} will return L{False}
  33. when the password given is NOT the password on the object.
  34. """
  35. creds = UsernamePassword(b"user", b"pass")
  36. self.assertFalse(creds.checkPassword(b"someotherpass"))
  37. def test_interface(self):
  38. """
  39. L{UsernamePassword} implements L{IUsernamePassword}.
  40. """
  41. self.assertTrue(IUsernamePassword.implementedBy(UsernamePassword))
  42. class UsernameHashedPasswordTests(TestCase):
  43. """
  44. Tests for L{UsernameHashedPassword}.
  45. """
  46. def test_initialisation(self):
  47. """
  48. The initialisation of L{UsernameHashedPassword} will set C{username}
  49. and C{hashed} on it.
  50. """
  51. creds = UsernameHashedPassword(b"foo", b"bar")
  52. self.assertEqual(creds.username, b"foo")
  53. self.assertEqual(creds.hashed, b"bar")
  54. def test_correctPassword(self):
  55. """
  56. Calling C{checkPassword} on a L{UsernameHashedPassword} will return
  57. L{True} when the password given is the password on the object.
  58. """
  59. creds = UsernameHashedPassword(b"user", b"pass")
  60. self.assertTrue(creds.checkPassword(b"pass"))
  61. def test_wrongPassword(self):
  62. """
  63. Calling C{checkPassword} on a L{UsernameHashedPassword} will return
  64. L{False} when the password given is NOT the password on the object.
  65. """
  66. creds = UsernameHashedPassword(b"user", b"pass")
  67. self.assertFalse(creds.checkPassword(b"someotherpass"))
  68. def test_interface(self):
  69. """
  70. L{UsernameHashedPassword} implements L{IUsernameHashedPassword}.
  71. """
  72. self.assertTrue(
  73. IUsernameHashedPassword.implementedBy(UsernameHashedPassword))