-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathtest_models.py
203 lines (174 loc) · 7.16 KB
/
test_models.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"""Tests for our data models."""
from flask_stormpath.models import User
from stormpath.resources.account import Account
from .helpers import StormpathTestCase
class TestUser(StormpathTestCase):
"""Our User test suite."""
def test_subclass(self):
with self.app.app_context():
user = User.create(
email = 'r@rdegges.com',
password = 'woot1LoveCookies!',
given_name = 'Randall',
surname = 'Degges',
)
# Ensure that our lazy construction of the subclass works as
# expected for users (a `User` should be a valid Stormpath
# `Account`.
self.assertTrue(user.writable_attrs)
self.assertIsInstance(user, Account)
self.assertIsInstance(user, User)
def test_repr(self):
with self.app.app_context():
# Ensure `email` is shown in the output if no `username` is
# specified.
user = User.create(
email = 'r@rdegges.com',
password = 'woot1LoveCookies!',
given_name = 'Randall',
surname = 'Degges',
)
self.assertTrue(user.email in user.__repr__())
# Delete this user.
user.delete()
# Ensure `username` is shown in the output if specified.
user = User.create(
username = 'omgrandall',
email = 'r@rdegges.com',
password = 'woot1LoveCookies!',
given_name = 'Randall',
surname = 'Degges',
)
self.assertTrue(user.username in user.__repr__())
# Ensure Stormpath `href` is shown in the output.
self.assertTrue(user.href in user.__repr__())
def test_get_id(self):
with self.app.app_context():
user = User.create(
email = 'r@rdegges.com',
password = 'woot1LoveCookies!',
given_name = 'Randall',
surname = 'Degges',
)
self.assertEqual(user.get_id(), user.href)
def test_is_active(self):
with self.app.app_context():
# Ensure users are active by default.
user = User.create(
email = 'r@rdegges.com',
password = 'woot1LoveCookies!',
given_name = 'Randall',
surname = 'Degges',
)
self.assertEqual(user.is_active, True)
# Ensure users who have their accounts explicitly disabled actually
# return a proper status when `is_active` is called.
user.status = User.STATUS_DISABLED
self.assertEqual(user.is_active, False)
# Ensure users who have not verified their accounts return a proper
# status when `is_active` is called.
user.status = User.STATUS_UNVERIFIED
self.assertEqual(user.is_active, False)
def test_is_anonymous(self):
with self.app.app_context():
# There is no way we can be anonymous, as Stormpath doesn't support
# anonymous users (that is a job better suited for a cache or
# something).
user = User.create(
email = 'r@rdegges.com',
password = 'woot1LoveCookies!',
given_name = 'Randall',
surname = 'Degges',
)
self.assertEqual(user.is_anonymous, False)
def test_is_authenticated(self):
with self.app.app_context():
# This should always return true. If a user account can be
# fetched, that means it must be authenticated.
user = User.create(
email = 'r@rdegges.com',
password = 'woot1LoveCookies!',
given_name = 'Randall',
surname = 'Degges',
)
self.assertEqual(user.is_authenticated, True)
def test_create(self):
with self.app.app_context():
# Ensure all requied fields are properly set.
user = User.create(
email = 'r@rdegges.com',
password = 'woot1LoveCookies!',
given_name = 'Randall',
surname = 'Degges',
)
self.assertEqual(user.email, 'r@rdegges.com')
self.assertEqual(user.given_name, 'Randall')
self.assertEqual(user.surname, 'Degges')
self.assertEqual(user.username, 'r@rdegges.com')
self.assertEqual(user.middle_name, None)
self.assertEqual(
dict(user.custom_data),
{
'created_at': user.custom_data.created_at,
'modified_at': user.custom_data.modified_at,
})
# Delete this user.
user.delete()
# Ensure all optional parameters are properly set.
user = User.create(
email = 'r@rdegges.com',
password = 'woot1LoveCookies!',
given_name = 'Randall',
surname = 'Degges',
username = 'rdegges',
middle_name = 'Clark',
custom_data = {
'favorite_shows': ['Code Monkeys', 'The IT Crowd'],
'friends': ['Sami', 'Alven'],
'favorite_place': {
'city': 'Santa Cruz',
'state': 'California',
'reason': 'Beautiful landscape.',
'amount_of_likage': 99.9999,
},
},
)
self.assertEqual(user.username, 'rdegges')
self.assertEqual(user.middle_name, 'Clark')
self.assertEqual(dict(user.custom_data), {
'favorite_shows': ['Code Monkeys', 'The IT Crowd'],
'friends': ['Sami', 'Alven'],
'favorite_place': {
'city': 'Santa Cruz',
'state': 'California',
'reason': 'Beautiful landscape.',
'amount_of_likage': 99.9999,
},
'created_at': user.custom_data.created_at,
'modified_at': user.custom_data.modified_at,
})
def test_from_login(self):
with self.app.app_context():
# First we'll create a user.
user = User.create(
email = 'r@rdegges.com',
password = 'woot1LoveCookies!',
given_name = 'Randall',
surname = 'Degges',
username = 'rdegges',
)
original_href = user.href
# Now we'll try to retrieve that user by specifing the user's
# `email` and `password`.
user = User.from_login(
'r@rdegges.com',
'woot1LoveCookies!',
)
self.assertEqual(user.href, original_href)
# Now we'll try to retrieve that user by specifying the user's
# `username` and `password`.
user = User.from_login(
'rdegges',
'woot1LoveCookies!',
)
self.assertEqual(user.href, original_href)