|
1 | 1 | import time |
2 | 2 | import string |
| 3 | + |
3 | 4 | from Crypto.Random import random |
4 | 5 | from Crypto.Cipher import Blowfish |
| 6 | + |
5 | 7 | from django import forms |
6 | 8 | from django.conf import settings |
7 | 9 | from django.core.cache import cache |
|
15 | 17 | from django.utils.translation import ugettext as _ |
16 | 18 | from django.utils.safestring import mark_safe |
17 | 19 |
|
| 20 | + |
18 | 21 | # Chars that are safe to use in field names. |
19 | 22 | SAFE_CHARS = string.ascii_letters + string.digits |
20 | 23 |
|
@@ -43,6 +46,14 @@ def random_name(choices=SAFE_CHARS, length=16): |
43 | 46 | return ''.join(random.sample(choices, length)) |
44 | 47 |
|
45 | 48 |
|
| 49 | +def testing(): |
| 50 | + """ |
| 51 | + Detects if we are running under Django unit tests. If so, security is |
| 52 | + disabled. |
| 53 | + """ |
| 54 | + return getattr(settings, 'TESTING', False) |
| 55 | + |
| 56 | + |
46 | 57 | class SecureFormException(Exception): |
47 | 58 | 'Base exception for security faults.' |
48 | 59 |
|
@@ -174,11 +185,12 @@ def __iter__(self): |
174 | 185 |
|
175 | 186 | def __getitem__(self, name): |
176 | 187 | 'Returns a SecureBoundField with the given name.' |
177 | | - try: |
178 | | - field = self.fields[name] |
179 | | - except KeyError: |
180 | | - raise KeyError('Key %r not found in Form' % name) |
181 | | - return SecureBoundField(self, field, name) |
| 188 | + if not testing(): |
| 189 | + try: |
| 190 | + return SecureBoundField(self, self.fields[name], name) |
| 191 | + except KeyError: |
| 192 | + raise KeyError('Key %r not found in Form' % name) |
| 193 | + return super(SecureFormBase, self).__getitem__(name) |
182 | 194 |
|
183 | 195 | def _script(self): |
184 | 196 | '''Generates the JavaScript necessary for hiding the honeypots or an empty string |
@@ -211,6 +223,8 @@ def decode_data(self): |
211 | 223 | and while the honeypots are checked to ensure they are empty.''' |
212 | 224 | if not self.is_bound: |
213 | 225 | return |
| 226 | + if testing(): |
| 227 | + return |
214 | 228 | cleaned_data = {} |
215 | 229 | secure = self.data[self._meta.secure_field_name] |
216 | 230 | secure = self.crypt.decrypt(secure.decode('hex')).rstrip() |
@@ -266,6 +280,8 @@ def full_clean(self): |
266 | 280 |
|
267 | 281 | def secure_data(self): |
268 | 282 | 'Prepares the secure data before the form is rendered.' |
| 283 | + if testing(): |
| 284 | + return |
269 | 285 | # Empty out the previous map, we will generate a new one. |
270 | 286 | self._secure_field_map = {} |
271 | 287 | labels = [] |
|
0 commit comments