2
# -*- coding: utf-8 -*-
3
# Copyright (c) 2009-2011, Nicolas Clairon
5
# Redistribution and use in source and binary forms, with or without
6
# modification, are permitted provided that the following conditions are met:
8
# * Redistributions of source code must retain the above copyright
9
# notice, this list of conditions and the following disclaimer.
10
# * Redistributions in binary form must reproduce the above copyright
11
# notice, this list of conditions and the following disclaimer in the
12
# documentation and/or other materials provided with the distribution.
13
# * Neither the name of the University of California, Berkeley nor the
14
# names of its contributors may be used to endorse or promote products
15
# derived from this software without specific prior written permission.
17
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
18
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
# DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
21
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
from mongokit import *
31
from mongokit.auth import User
32
from pymongo.objectid import ObjectId
37
class AuthTestCase(unittest.TestCase):
39
self.connection = Connection()
40
self.col = self.connection['test']['mongokit']
43
self.connection['test'].drop_collection('mongokit')
44
self.connection['test'].drop_collection('versionned_mongokit')
46
def test_password_validation(self):
47
class SimpleUser(User): pass
48
self.connection.register([SimpleUser])
50
user = self.col.SimpleUser()
52
self.assertRaises(RequireFieldError, user.validate)
53
user.password = "myp4$$ord"
55
assert user.verify_password("bla") == False
56
assert user.verify_password("myp4$$ord") == True
57
assert len(user.password) == len(user['user']['password']) == 80
60
assert user.password is None
61
assert user['user']['password'] is None
63
def test_create_user(self):
64
class SimpleUser(User): pass
65
self.connection.register([SimpleUser])
67
user = self.col.SimpleUser()
69
user.email = u"user@foo.bar"
70
user.password = u"u$ser_p4$$w0rd"
71
print "°°°°°°°°°", user
74
saved_user = self.col.SimpleUser.get_from_id('user')
75
assert saved_user.verify_password("bad") == False
76
assert saved_user.verify_password(u"u$ser_p4$$w0rd") == True
78
assert user.login == u"user"
79
assert user['_id'] == u'user'
80
assert user['user']['login'] == u'user'
82
assert user['_id'] is None
83
assert user['user']['login'] is None
84
assert user.login is None
86
assert user.email == user['user']['email'] == u'user@foo.bar'
88
assert user['user']['email'] is None
89
assert user.email is None
91
def test_overload_user(self):
92
class SimpleUser(User):
101
self.connection.register([SimpleUser])
103
user = self.col.SimpleUser()
105
user.email = u"user@foo.bar"
106
user.password = "u$ser_p4$$w0rd"
109
saved_user = self.col.SimpleUser.get_from_id('user')
110
assert saved_user.verify_password("bad") == False
111
assert saved_user.verify_password("u$ser_p4$$w0rd") == True