forked from dwaiter/django-bcrypt
/
tests.py
219 lines (184 loc) · 8.04 KB
/
tests.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# -*- coding: utf-8 -*-
from __future__ import with_statement
from contextlib import contextmanager
import bcrypt
from django import conf
from django.contrib.auth.models import User, UNUSABLE_PASSWORD
from django.test import TestCase
from django.utils.encoding import smart_str
from django.utils.functional import LazyObject
from django_bcrypt.models import (bcrypt_check_password, bcrypt_set_password,
_check_password, _set_password,
get_rounds, is_enabled, migrate_to_bcrypt)
class CheckPasswordTest(TestCase):
def test_bcrypt_password(self):
user = User()
with settings():
bcrypt_set_password(user, 'password')
self.assertTrue(bcrypt_check_password(user, 'password'))
self.assertFalse(bcrypt_check_password(user, 'invalid'))
def test_unicode_password(self):
user = User()
with settings():
bcrypt_set_password(user, u"aáåäeéêëoôö")
self.assertTrue(bcrypt_check_password(user, u"aáåäeéêëoôö"))
self.assertFalse(bcrypt_check_password(user, u"aaaaeeeeooo"))
self.assertFalse(bcrypt_check_password(user, 'invalid'))
def test_sha1_password(self):
user = User()
_set_password(user, 'password')
self.assertTrue(bcrypt_check_password(user, 'password'))
self.assertFalse(bcrypt_check_password(user, 'invalid'))
def test_change_rounds(self):
user = User()
# Hash with 5 rounds
with settings(BCRYPT_ROUNDS=5):
bcrypt_set_password(user, 'password')
password_5 = user.password
self.assertTrue(bcrypt_check_password(user, 'password'))
# Hash with 12 rounds
with settings(BCRYPT_ROUNDS=12):
bcrypt_set_password(user, 'password')
password_12 = user.password
self.assertTrue(bcrypt_check_password(user, 'password'))
class SetPasswordTest(TestCase):
def assertBcrypt(self, hashed, password):
self.assertEqual(hashed[:3], 'bc$')
self.assertEqual(hashed[3:], bcrypt.hashpw(smart_str(password), hashed[3:]))
def test_set_password(self):
user = User()
with settings():
bcrypt_set_password(user, 'password')
self.assertBcrypt(user.password, 'password')
def test_disabled(self):
user = User()
with settings(BCRYPT_ENABLED=False):
bcrypt_set_password(user, 'password')
self.assertFalse(user.password.startswith('bc$'), user.password)
def test_set_unusable_password(self):
user = User()
with settings():
bcrypt_set_password(user, None)
self.assertEqual(user.password, UNUSABLE_PASSWORD)
def test_change_rounds(self):
user = User()
with settings(BCRYPT_ROUNDS=0):
settings.BCRYPT_ROUNDS = 0
bcrypt_set_password(user, 'password')
self.assertBcrypt(user.password, 'password')
class MigratePasswordTest(TestCase):
def assertBcrypt(self, hashed, password):
self.assertEqual(hashed[:3], 'bc$')
self.assertEqual(hashed[3:], bcrypt.hashpw(smart_str(password), hashed[3:]))
def assertSha1(self, hashed, password):
self.assertEqual(hashed[:5], 'sha1$')
def test_migrate_sha1_to_bcrypt(self):
user = User(username='username')
with settings(BCRYPT_MIGRATE=True, BCRYPT_ENABLED_UNDER_TEST=True):
_set_password(user, 'password')
self.assertSha1(user.password, 'password')
self.assertTrue(bcrypt_check_password(user, 'password'))
self.assertBcrypt(user.password, 'password')
self.assertEqual(User.objects.get(username='username').password,
user.password)
def test_migate_unicode(self):
user = User(username='username')
pw = u'aáåäeéêëoôö'
with settings(BCRYPT_MIGRATE=True, BCRYPT_ENABLED_UNDER_TEST=True):
_set_password(user, pw)
self.assertSha1(user.password, pw)
self.assertTrue(bcrypt_check_password(user, pw))
self.assertBcrypt(user.password, pw)
self.assertEqual(User.objects.get(username='username').password,
user.password)
def test_migrate_bcrypt_to_bcrypt(self):
user = User(username='username')
with settings(BCRYPT_MIGRATE=True,
BCRYPT_ROUNDS=10,
BCRYPT_ENABLED_UNDER_TEST=True):
user.set_password('password')
with settings(BCRYPT_MIGRATE=True,
BCRYPT_ROUNDS=12,
BCRYPT_ENABLED_UNDER_TEST=True):
user.check_password('password')
salt_and_hash = user.password[3:]
self.assertEqual(salt_and_hash.split('$')[2], '12')
self.assertEqual(User.objects.get(username='username').password,
user.password)
def test_no_bcrypt_to_bcrypt(self):
user = User(username='username')
with settings(BCRYPT_MIGRATE=True,
BCRYPT_ROUNDS=10,
BCRYPT_ENABLED_UNDER_TEST=True):
user.set_password('password')
old_password = user.password
user.check_password('password')
self.assertEqual(old_password, user.password)
def test_no_migrate_password(self):
user = User()
with settings(BCRYPT_MIGRATE=False, BCRYPT_ENABLED_UNDER_TEST=True):
_set_password(user, 'password')
self.assertSha1(user.password, 'password')
self.assertTrue(bcrypt_check_password(user, 'password'))
self.assertSha1(user.password, 'password')
class SettingsTest(TestCase):
def test_rounds(self):
with settings(BCRYPT_ROUNDS=0):
self.assertEqual(get_rounds(), 0)
with settings(BCRYPT_ROUNDS=5):
self.assertEqual(get_rounds(), 5)
with settings(BCRYPT_ROUNDS=NotImplemented):
self.assertEqual(get_rounds(), 12)
def test_enabled(self):
with settings(BCRYPT_ENABLED=False):
self.assertFalse(is_enabled())
with settings(BCRYPT_ENABLED=True):
self.assertTrue(is_enabled())
with settings(BCRYPT_ENABLED=NotImplemented):
self.assertTrue(is_enabled())
def test_enabled_under_test(self):
with settings(BCRYPT_ENABLED_UNDER_TEST=True):
self.assertTrue(is_enabled())
with settings(BCRYPT_ENABLED_UNDER_TEST=False):
self.assertFalse(is_enabled())
with settings(BCRYPT_ENABLED_UNDER_TEST=NotImplemented):
self.assertFalse(is_enabled())
def test_migrate_to_bcrypt(self):
with settings(BCRYPT_MIGRATE=False):
self.assertEqual(migrate_to_bcrypt(), False)
with settings(BCRYPT_MIGRATE=True):
self.assertEqual(migrate_to_bcrypt(), True)
with settings(BCRYPT_MIGRATE=NotImplemented):
self.assertEqual(migrate_to_bcrypt(), False)
def settings(**kwargs):
kwargs = dict({'BCRYPT_ENABLED': True,
'BCRYPT_ENABLED_UNDER_TEST': True},
**kwargs)
return patch(conf.settings, **kwargs)
@contextmanager
def patch(namespace, **values):
"""Patches `namespace`.`name` with `value` for (name, value) in values"""
originals = {}
if isinstance(namespace, LazyObject):
if namespace._wrapped is None:
namespace._setup()
namespace = namespace._wrapped
for (name, value) in values.iteritems():
try:
originals[name] = getattr(namespace, name)
except AttributeError:
originals[name] = NotImplemented
if value is NotImplemented:
if originals[name] is not NotImplemented:
delattr(namespace, name)
else:
setattr(namespace, name, value)
try:
yield
finally:
for (name, original_value) in originals.iteritems():
if original_value is NotImplemented:
if values[name] is not NotImplemented:
delattr(namespace, name)
else:
setattr(namespace, name, original_value)