1
1
# -*- coding: utf-8 -*-
2
2
3
3
import re
4
+ import unittest
4
5
6
+ import django
7
+ from django .conf import settings
5
8
from django .db import models , connection
6
9
from django .test import TestCase
7
10
from django .utils import timezone
19
22
20
23
from keyczar import keyczar , readers
21
24
25
+
22
26
# Test class that encapsulates some Keyczar functions.
23
27
# Requirements are to implement __init__, decrypt(), encrypt()
24
28
class TestCrypter (object ):
@@ -32,24 +36,28 @@ def encrypt(self, cleartext):
32
36
def decrypt (self , ciphertext ):
33
37
return self .crypter .Decrypt (ciphertext )
34
38
39
+
35
40
class TestModel (models .Model ):
36
- char = EncryptedCharField (max_length = 255 , null = True )
37
- prefix_char = EncryptedCharField (max_length = 255 , prefix = 'ENCRYPTED:::' )
38
- decrypt_only = EncryptedCharField (max_length = 255 , decrypt_only = True )
39
- short_char = EncryptedCharField (max_length = 50 , null = True , enforce_max_length = True )
41
+ char = EncryptedCharField (max_length = 255 , null = True , blank = True )
42
+ prefix_char = EncryptedCharField (max_length = 255 , prefix = 'ENCRYPTED:::' , blank = True )
43
+ decrypt_only = EncryptedCharField (max_length = 255 , decrypt_only = True , blank = True )
44
+ short_char = EncryptedCharField (
45
+ max_length = 50 , null = True , enforce_max_length = True , blank = True )
40
46
41
- text = EncryptedTextField (null = True )
42
- datetime = EncryptedDateTimeField (null = True )
43
- integer = EncryptedIntegerField (null = True )
44
- date = EncryptedDateField (null = True )
45
- floating = EncryptedFloatField (null = True )
46
- email = EncryptedEmailField (null = True )
47
- boolean = EncryptedBooleanField (default = False )
47
+ text = EncryptedTextField (null = True , blank = True )
48
+ datetime = EncryptedDateTimeField (null = True , blank = True )
49
+ integer = EncryptedIntegerField (null = True , blank = True )
50
+ date = EncryptedDateField (null = True , blank = True )
51
+ floating = EncryptedFloatField (null = True , blank = True )
52
+ email = EncryptedEmailField (null = True , blank = True )
53
+ boolean = EncryptedBooleanField (default = False , blank = True )
48
54
49
- char_custom_crypter = EncryptedCharField (max_length = 255 , null = True , crypter_klass = TestCrypter )
55
+ char_custom_crypter = EncryptedCharField (
56
+ max_length = 255 , null = True ,crypter_klass = TestCrypter , blank = True )
50
57
51
58
52
59
class FieldTest (TestCase ):
60
+ IS_POSTGRES = settings .DATABASES ['default' ]['ENGINE' ] == 'django.db.backends.postgresql_psycopg2'
53
61
54
62
def get_db_value (self , field , model_id ):
55
63
cursor = connection .cursor ()
@@ -222,7 +230,7 @@ def test_float_field_encrypted(self):
222
230
self .assertEqual (fresh_model .floating , plaintext )
223
231
224
232
def test_email_field_encrypted (self ):
225
- plaintext = '[email protected] ' # my email address, btw
233
+ plaintext = '[email protected] ' # my email address, btw
226
234
227
235
model = TestModel ()
228
236
model .email = plaintext
@@ -255,3 +263,23 @@ def test_boolean_field_encrypted(self):
255
263
256
264
fresh_model = TestModel .objects .get (id = model .id )
257
265
self .assertEqual (fresh_model .boolean , plaintext )
266
+
267
+ @unittest .skipIf (django .VERSION < (1 , 7 ), "Issue exists in django 1.7+" )
268
+ @unittest .skipIf (not IS_POSTGRES , "Issue exists for postgresql" )
269
+ def test_integerfield_validation_in_django_1_7_passes_successfully (self ):
270
+ plainint = 1111
271
+
272
+ obj = TestModel ()
273
+ obj .integer = plainint
274
+
275
+ # see https://github.com/defrex/django-encrypted-fields/issues/7
276
+ obj .full_clean ()
277
+ obj .save ()
278
+
279
+ ciphertext = self .get_db_value ('integer' , obj .id )
280
+
281
+ self .assertNotEqual (plainint , ciphertext )
282
+ self .assertNotEqual (plainint , str (ciphertext ))
283
+
284
+ fresh_model = TestModel .objects .get (id = obj .id )
285
+ self .assertEqual (fresh_model .integer , plainint )
0 commit comments