~clint-fewbar/+junk/python-pymongo-packaging

« back to all changes in this revision

Viewing changes to tests/test_auth.py

  • Committer: Clint Byrum
  • Date: 2012-01-25 21:53:57 UTC
  • Revision ID: clint@ubuntu.com-20120125215357-j603u9d5alv1bt9v
Tags: upstream-0.7.2
Import upstream version 0.7.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: utf-8 -*-
 
3
# Copyright (c) 2009-2011, Nicolas Clairon
 
4
# All rights reserved.
 
5
# Redistribution and use in source and binary forms, with or without
 
6
# modification, are permitted provided that the following conditions are met:
 
7
#
 
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.
 
16
#
 
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.
 
27
 
 
28
import unittest
 
29
 
 
30
from mongokit import *
 
31
from mongokit.auth import User
 
32
from pymongo.objectid import ObjectId
 
33
 
 
34
import logging
 
35
logging.basicConfig()
 
36
 
 
37
class AuthTestCase(unittest.TestCase):
 
38
    def setUp(self):
 
39
        self.connection = Connection()
 
40
        self.col = self.connection['test']['mongokit']
 
41
        
 
42
    def tearDown(self):
 
43
        self.connection['test'].drop_collection('mongokit')
 
44
        self.connection['test'].drop_collection('versionned_mongokit')
 
45
 
 
46
    def test_password_validation(self):
 
47
        class SimpleUser(User): pass
 
48
        self.connection.register([SimpleUser])
 
49
 
 
50
        user = self.col.SimpleUser()
 
51
        user.login = u"user"
 
52
        self.assertRaises(RequireFieldError, user.validate)
 
53
        user.password = "myp4$$ord"
 
54
 
 
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
 
58
        
 
59
        del user.password
 
60
        assert user.password is None
 
61
        assert user['user']['password'] is None
 
62
    
 
63
    def test_create_user(self):
 
64
        class SimpleUser(User): pass
 
65
        self.connection.register([SimpleUser])
 
66
 
 
67
        user = self.col.SimpleUser()
 
68
        user.login = u"user"
 
69
        user.email = u"user@foo.bar"
 
70
        user.password = u"u$ser_p4$$w0rd"
 
71
        print "°°°°°°°°°", user
 
72
        user.save()
 
73
 
 
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
 
77
 
 
78
        assert user.login == u"user"
 
79
        assert user['_id'] == u'user'
 
80
        assert user['user']['login'] == u'user'
 
81
        del user.login
 
82
        assert user['_id'] is None
 
83
        assert user['user']['login'] is None
 
84
        assert user.login is None
 
85
 
 
86
        assert user.email == user['user']['email'] == u'user@foo.bar'
 
87
        del user.email
 
88
        assert user['user']['email'] is None
 
89
        assert user.email is None
 
90
 
 
91
    def test_overload_user(self):
 
92
        class SimpleUser(User):
 
93
            structure = {
 
94
                "auth":{
 
95
                    "session_id":unicode,
 
96
                },
 
97
                "profil":{
 
98
                    "name":unicode,
 
99
                }
 
100
            }
 
101
        self.connection.register([SimpleUser])
 
102
 
 
103
        user = self.col.SimpleUser()
 
104
        user.login = u"user"
 
105
        user.email = u"user@foo.bar"
 
106
        user.password = "u$ser_p4$$w0rd"
 
107
        user.save()
 
108
 
 
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
 
112
 
 
113