/
test_schematics_document.py
189 lines (147 loc) · 5.52 KB
/
test_schematics_document.py
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import unittest
import pytest
import couchdb
from couchdb.tests import testutil
from schematics.models import Model
from schematics.transforms import blacklist
from schematics.types import StringType, MD5Type
from couchdb_schematics.document import SchematicsDocument
from couchdb_schematics.document import EmbeddedDocType
class User1Model(Model):
name = StringType()
class User2Model(User1Model):
_password = MD5Type(serialized_name="password")
class User1(SchematicsDocument, User1Model):
pass
class User2(SchematicsDocument, User2Model):
def __init__(self, raw_data=None, **kwargs):
if raw_data is None:
raw_data = {}
password = raw_data.pop("password", None)
super(User2, self).__init__(raw_data=raw_data, **kwargs)
if password:
if '_rev' in raw_data:
self._password = password
else:
self.password = password
# password property
def _get_password(self):
return self._password
def _set_password(self,passwd):
self._password = self._salted_password(passwd)
password = property(_get_password, _set_password)
def challenge_password(self,passwd):
if self.password == self._salted_password(passwd): return True
return False
def _salted_password(self, passwd):
s = "salt+{}".format(passwd)
import hashlib
val = hashlib.md5(s).hexdigest()
print val
return val
class TestSchematicDocument(testutil.TempDatabaseMixin, unittest.TestCase):
def test_user1(self):
u = User1()
u.name = "Ryan"
u.store(self.db)
assert u.id is not None
assert u.rev is not None
fromdb = self.db.get(u.id)
assert u.name == fromdb['name']
u3 = User1(fromdb)
assert u.name == u3.name
u4 = User1.load(self.db, u.id)
assert u.name == u4.name
u4.delete_instance(self.db)
fromdb = self.db.get(u.id)
assert fromdb is None
# try deleting a document that does not exist
with pytest.raises(couchdb.ResourceNotFound):
del self.db[u.id]
with pytest.raises(couchdb.ResourceNotFound):
u4.delete_instance(self.db)
def test_model_with_serialized_name(self):
u = User2()
u.name = "Ryan"
u.password = "ChangeMe"
u.store(self.db)
print u.password
fromdb = self.db.get(u.id)
assert u.password == fromdb['password']
u3 = User2(fromdb)
assert u.password == u3.password
u4 = User2.load(self.db, u.id)
assert u.password == u4.password
assert u4.challenge_password("ChangeMe")
def test_EmbeddedDocType(self):
class SuperUser(SchematicsDocument):
user = EmbeddedDocType(User2)
u = User2(dict(name="Ryan", password="ChangeMe"))
u.store(self.db)
su = SuperUser()
su.user = u
assert isinstance(su.user, User2)
su_native = su.to_native()
assert 'user' in su_native
assert 'doc_id' in su_native['user']
assert '_id' not in su_native['user']
assert '_rev' not in su_native['user']
assert 'doc_type' not in su_native['user']
su.validate()
def test_EmbeddedDocType_overriding_Options(self):
class User3(User2):
class Options:
roles = {
"embedded": (blacklist("_password") +
SchematicsDocument.Options.roles['embedded']),
}
class SuperUser(SchematicsDocument):
user = EmbeddedDocType(User3)
u = User3(dict(name="Ryan", password="ChangeMe"))
assert 'id' not in u.serialize()
u.store(self.db)
su = SuperUser()
su.user = u
su.store(self.db)
assert isinstance(su.user, User3)
print su.user.Options.roles
assert '_password' in su.user.Options.roles['embedded']
su_native = su.to_native()
assert 'user' in su_native
assert 'doc_id' not in su_native
assert 'doc_id' in su_native['user']
assert '_id' not in su_native['user']
assert '_rev' not in su_native['user']
assert 'doc_type' not in su_native['user']
assert 'password' not in su_native['user']
fromdb = SuperUser.load(self.db, su.id)
su_native = fromdb.to_native()
assert 'user' in su_native
assert 'doc_id' not in su_native
assert 'doc_id' in su_native['user']
assert '_id' not in su_native['user']
assert '_rev' not in su_native['user']
assert 'doc_type' not in su_native['user']
assert 'password' not in su_native['user']
fromdb.user.reload(self.db)
assert fromdb.user.password == su.user.password
def test_hydrate(self):
class User3(User2):
class Options:
roles = {
"embedded": (blacklist("_password") +
SchematicsDocument.Options.roles['embedded']),
}
class SuperUser(SchematicsDocument):
user = EmbeddedDocType(User3)
u = User3(dict(name="Ryan", password="ChangeMe"))
assert 'id' not in u.serialize()
u.store(self.db)
su = SuperUser()
su.user = u
su.store(self.db)
assert isinstance(su.user, User3)
fromdb = SuperUser.load(self.db, su.id)
assert fromdb.user.password != su.user.password
fromdb.hydrate(self.db)
assert fromdb.user.password == su.user.password