• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
22
23import unittest
24import logging
25import time
26
27log= logging.getLogger('password_property_test')
28log.setLevel(logging.DEBUG)
29
30class PasswordPropertyTest(unittest.TestCase):
31    """Test the PasswordProperty"""
32
33    def tearDown(self):
34        cls=self.test_model()
35        for obj in cls.all(): obj.delete()
36
37    def hmac_hashfunc(self):
38        import hmac
39        def hashfunc(msg):
40            return hmac.new('mysecret', msg)
41        return hashfunc
42
43    def test_model(self,hashfunc=None):
44        from boto.utils import Password
45        from boto.sdb.db.model import Model
46        from boto.sdb.db.property import PasswordProperty
47        import hashlib
48        class MyModel(Model):
49            password=PasswordProperty(hashfunc=hashfunc)
50        return MyModel
51
52    def test_custom_password_class(self):
53        from boto.utils import Password
54        from boto.sdb.db.model import Model
55        from boto.sdb.db.property import PasswordProperty
56        import hmac, hashlib
57
58
59        myhashfunc = hashlib.md5
60	## Define a new Password class
61        class MyPassword(Password):
62            hashfunc = myhashfunc #hashlib.md5 #lambda cls,msg: hmac.new('mysecret',msg)
63
64	## Define a custom password property using the new Password class
65
66        class MyPasswordProperty(PasswordProperty):
67            data_type=MyPassword
68            type_name=MyPassword.__name__
69
70	## Define a model using the new password property
71
72        class MyModel(Model):
73            password=MyPasswordProperty()#hashfunc=hashlib.md5)
74
75        obj = MyModel()
76        obj.password = 'bar'
77        expected = myhashfunc('bar').hexdigest() #hmac.new('mysecret','bar').hexdigest()
78        log.debug("\npassword=%s\nexpected=%s" % (obj.password, expected))
79        self.assertTrue(obj.password == 'bar' )
80        obj.save()
81        id= obj.id
82        time.sleep(5)
83        obj = MyModel.get_by_id(id)
84        self.assertEquals(obj.password, 'bar')
85        self.assertEquals(str(obj.password), expected)
86                          #hmac.new('mysecret','bar').hexdigest())
87
88
89    def test_aaa_default_password_property(self):
90        cls = self.test_model()
91        obj = cls(id='passwordtest')
92        obj.password = 'foo'
93        self.assertEquals('foo', obj.password)
94        obj.save()
95        time.sleep(5)
96        obj = cls.get_by_id('passwordtest')
97        self.assertEquals('foo', obj.password)
98
99    def test_password_constructor_hashfunc(self):
100        import hmac
101        myhashfunc=lambda msg: hmac.new('mysecret', msg)
102        cls = self.test_model(hashfunc=myhashfunc)
103        obj = cls()
104        obj.password='hello'
105        expected = myhashfunc('hello').hexdigest()
106        self.assertEquals(obj.password, 'hello')
107        self.assertEquals(str(obj.password), expected)
108        obj.save()
109        id = obj.id
110        time.sleep(5)
111        obj = cls.get_by_id(id)
112        log.debug("\npassword=%s" % obj.password)
113        self.assertTrue(obj.password == 'hello')
114
115
116
117if __name__ == '__main__':
118    import sys, os
119    curdir = os.path.dirname( os.path.abspath(__file__) )
120    srcroot = curdir + "/../.."
121    sys.path = [ srcroot ] + sys.path
122    logging.basicConfig()
123    log.setLevel(logging.INFO)
124    suite = unittest.TestLoader().loadTestsFromTestCase(PasswordPropertyTest)
125    unittest.TextTestRunner(verbosity=2).run(suite)
126
127    import boto
128
129