PageRenderTime 49ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/boto-2.5.2/tests/db/test_password.py

#
Python | 128 lines | 80 code | 23 blank | 25 comment | 2 complexity | 8c22c5da8aa4bb6363c077250afefc44 MD5 | raw file
  1. # Copyright (c) 2010 Robert Mela
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a
  4. # copy of this software and associated documentation files (the
  5. # "Software"), to deal in the Software without restriction, including
  6. # without limitation the rights to use, copy, modify, merge, publish, dis-
  7. # tribute, sublicense, and/or sell copies of the Software, and to permit
  8. # persons to whom the Software is furnished to do so, subject to the fol-
  9. # lowing conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included
  12. # in all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
  16. # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  17. # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. # IN THE SOFTWARE.
  21. import unittest
  22. import logging
  23. import time
  24. log= logging.getLogger('password_property_test')
  25. log.setLevel(logging.DEBUG)
  26. class PasswordPropertyTest(unittest.TestCase):
  27. """Test the PasswordProperty"""
  28. def tearDown(self):
  29. cls=self.test_model()
  30. for obj in cls.all(): obj.delete()
  31. def hmac_hashfunc(self):
  32. import hmac
  33. def hashfunc(msg):
  34. return hmac.new('mysecret', msg)
  35. return hashfunc
  36. def test_model(self,hashfunc=None):
  37. from boto.utils import Password
  38. from boto.sdb.db.model import Model
  39. from boto.sdb.db.property import PasswordProperty
  40. import hashlib
  41. class MyModel(Model):
  42. password=PasswordProperty(hashfunc=hashfunc)
  43. return MyModel
  44. def test_custom_password_class(self):
  45. from boto.utils import Password
  46. from boto.sdb.db.model import Model
  47. from boto.sdb.db.property import PasswordProperty
  48. import hmac, hashlib
  49. myhashfunc = hashlib.md5
  50. ## Define a new Password class
  51. class MyPassword(Password):
  52. hashfunc = myhashfunc #hashlib.md5 #lambda cls,msg: hmac.new('mysecret',msg)
  53. ## Define a custom password property using the new Password class
  54. class MyPasswordProperty(PasswordProperty):
  55. data_type=MyPassword
  56. type_name=MyPassword.__name__
  57. ## Define a model using the new password property
  58. class MyModel(Model):
  59. password=MyPasswordProperty()#hashfunc=hashlib.md5)
  60. obj = MyModel()
  61. obj.password = 'bar'
  62. expected = myhashfunc('bar').hexdigest() #hmac.new('mysecret','bar').hexdigest()
  63. log.debug("\npassword=%s\nexpected=%s" % (obj.password, expected))
  64. self.assertTrue(obj.password == 'bar' )
  65. obj.save()
  66. id= obj.id
  67. time.sleep(5)
  68. obj = MyModel.get_by_id(id)
  69. self.assertEquals(obj.password, 'bar')
  70. self.assertEquals(str(obj.password), expected)
  71. #hmac.new('mysecret','bar').hexdigest())
  72. def test_aaa_default_password_property(self):
  73. cls = self.test_model()
  74. obj = cls(id='passwordtest')
  75. obj.password = 'foo'
  76. self.assertEquals('foo', obj.password)
  77. obj.save()
  78. time.sleep(5)
  79. obj = cls.get_by_id('passwordtest')
  80. self.assertEquals('foo', obj.password)
  81. def test_password_constructor_hashfunc(self):
  82. import hmac
  83. myhashfunc=lambda msg: hmac.new('mysecret', msg)
  84. cls = self.test_model(hashfunc=myhashfunc)
  85. obj = cls()
  86. obj.password='hello'
  87. expected = myhashfunc('hello').hexdigest()
  88. self.assertEquals(obj.password, 'hello')
  89. self.assertEquals(str(obj.password), expected)
  90. obj.save()
  91. id = obj.id
  92. time.sleep(5)
  93. obj = cls.get_by_id(id)
  94. log.debug("\npassword=%s" % obj.password)
  95. self.assertTrue(obj.password == 'hello')
  96. if __name__ == '__main__':
  97. import sys, os
  98. curdir = os.path.dirname( os.path.abspath(__file__) )
  99. srcroot = curdir + "/../.."
  100. sys.path = [ srcroot ] + sys.path
  101. logging.basicConfig()
  102. log.setLevel(logging.INFO)
  103. suite = unittest.TestLoader().loadTestsFromTestCase(PasswordPropertyTest)
  104. unittest.TextTestRunner(verbosity=2).run(suite)
  105. import boto