File: test_password.py

package info (click to toggle)
python-boto 2.34.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,584 kB
  • ctags: 10,521
  • sloc: python: 78,553; makefile: 123
file content (128 lines) | stat: -rw-r--r-- 4,429 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Copyright (c) 2010 Robert Mela
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, dis-
# tribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the fol-
# lowing conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
 

import unittest
import logging
import time

log= logging.getLogger('password_property_test')
log.setLevel(logging.DEBUG)

class PasswordPropertyTest(unittest.TestCase):
    """Test the PasswordProperty"""

    def tearDown(self):
        cls=self.test_model()
        for obj in cls.all(): obj.delete()

    def hmac_hashfunc(self):
        import hmac
        def hashfunc(msg):
            return hmac.new('mysecret', msg)
        return hashfunc

    def test_model(self,hashfunc=None):
        from boto.utils import Password
        from boto.sdb.db.model import Model
        from boto.sdb.db.property import PasswordProperty
        import hashlib
        class MyModel(Model):
            password=PasswordProperty(hashfunc=hashfunc)
        return MyModel

    def test_custom_password_class(self):
        from boto.utils import Password
        from boto.sdb.db.model import Model
        from boto.sdb.db.property import PasswordProperty
        import hmac, hashlib


        myhashfunc = hashlib.md5
	## Define a new Password class
        class MyPassword(Password):
            hashfunc = myhashfunc #hashlib.md5 #lambda cls,msg: hmac.new('mysecret',msg)

	## Define a custom password property using the new Password class

        class MyPasswordProperty(PasswordProperty):
            data_type=MyPassword
            type_name=MyPassword.__name__

	## Define a model using the new password property

        class MyModel(Model):
            password=MyPasswordProperty()#hashfunc=hashlib.md5)

        obj = MyModel()
        obj.password = 'bar'
        expected = myhashfunc('bar').hexdigest() #hmac.new('mysecret','bar').hexdigest()
        log.debug("\npassword=%s\nexpected=%s" % (obj.password, expected))
        self.assertTrue(obj.password == 'bar' )
        obj.save()
        id= obj.id
        time.sleep(5)
        obj = MyModel.get_by_id(id)
        self.assertEquals(obj.password, 'bar')
        self.assertEquals(str(obj.password), expected)
                          #hmac.new('mysecret','bar').hexdigest())
 
        
    def test_aaa_default_password_property(self):
        cls = self.test_model()
        obj = cls(id='passwordtest')
        obj.password = 'foo'
        self.assertEquals('foo', obj.password)
        obj.save()
        time.sleep(5)
        obj = cls.get_by_id('passwordtest')
        self.assertEquals('foo', obj.password)

    def test_password_constructor_hashfunc(self):
        import hmac
        myhashfunc=lambda msg: hmac.new('mysecret', msg)
        cls = self.test_model(hashfunc=myhashfunc)
        obj = cls()
        obj.password='hello'
        expected = myhashfunc('hello').hexdigest()
        self.assertEquals(obj.password, 'hello')
        self.assertEquals(str(obj.password), expected)
        obj.save()
        id = obj.id
        time.sleep(5)
        obj = cls.get_by_id(id)
        log.debug("\npassword=%s" % obj.password)
        self.assertTrue(obj.password == 'hello')

       
 
if __name__ == '__main__':
    import sys, os
    curdir = os.path.dirname( os.path.abspath(__file__) )
    srcroot = curdir + "/../.."
    sys.path = [ srcroot ] + sys.path
    logging.basicConfig()
    log.setLevel(logging.INFO)
    suite = unittest.TestLoader().loadTestsFromTestCase(PasswordPropertyTest)
    unittest.TextTestRunner(verbosity=2).run(suite)

    import boto