-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
287 lines (245 loc) · 13.8 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import datetime
import mock
from django.forms import ModelForm
from django.test import TestCase
from django.utils import timezone
import cryptography.fernet
import encrypted_model_fields.fields
from . import models
class TestModelTestCase(TestCase):
def test_value(self):
test_date_today = datetime.date.today()
test_date = datetime.date(2011, 1, 1)
test_datetime = datetime.datetime(2011, 1, 1, 1, tzinfo=timezone.utc)
inst = models.TestModel()
inst.enc_char_field = 'This is a test string!'
inst.enc_text_field = 'This is a test string2!'
inst.enc_date_field = test_date
inst.enc_datetime_field = test_datetime
inst.enc_boolean_field = True
inst.enc_integer_field = 123456789
inst.enc_positive_integer_field = 123456789
inst.enc_small_integer_field = 123456789
inst.enc_positive_small_integer_field = 123456789
inst.enc_big_integer_field = 9223372036854775807
inst.save()
inst = models.TestModel.objects.get()
self.assertEqual(inst.enc_char_field, 'This is a test string!')
self.assertEqual(inst.enc_text_field, 'This is a test string2!')
self.assertEqual(inst.enc_date_field, test_date)
self.assertEqual(inst.enc_date_now_field, test_date_today)
self.assertEqual(inst.enc_date_now_add_field, test_date_today)
self.assertEqual(inst.enc_datetime_field, test_datetime)
self.assertEqual(inst.enc_boolean_field, True)
self.assertEqual(inst.enc_integer_field, 123456789)
self.assertEqual(inst.enc_positive_integer_field, 123456789)
self.assertEqual(inst.enc_small_integer_field, 123456789)
self.assertEqual(inst.enc_positive_small_integer_field, 123456789)
self.assertEqual(inst.enc_big_integer_field, 9223372036854775807)
test_date = datetime.date(2012, 2, 1)
test_datetime = datetime.datetime(2012, 1, 1, 2, tzinfo=timezone.utc)
inst.enc_char_field = 'This is another test string!'
inst.enc_text_field = 'This is another test string2!'
inst.enc_date_field = test_date
inst.enc_datetime_field = test_datetime
inst.enc_boolean_field = False
inst.enc_integer_field = -123456789
inst.enc_positive_integer_field = 0
inst.enc_small_integer_field = -123456789
inst.enc_positive_small_integer_field = 0
inst.enc_big_integer_field = -9223372036854775806
inst.save()
inst = models.TestModel.objects.get()
self.assertEqual(inst.enc_char_field, 'This is another test string!')
self.assertEqual(inst.enc_text_field, 'This is another test string2!')
self.assertEqual(inst.enc_date_field, test_date)
self.assertEqual(inst.enc_date_now_field, datetime.date.today())
self.assertEqual(inst.enc_date_now_add_field, datetime.date.today())
# be careful about sqlite testing, which doesn't support native dates
if timezone.is_naive(inst.enc_datetime_field):
inst.enc_datetime_field = timezone.make_aware(inst.enc_datetime_field, timezone.utc)
self.assertEqual(inst.enc_datetime_field, test_datetime)
self.assertEqual(inst.enc_boolean_field, False)
self.assertEqual(inst.enc_integer_field, -123456789)
self.assertEqual(inst.enc_positive_integer_field, 0)
self.assertEqual(inst.enc_small_integer_field, -123456789)
self.assertEqual(inst.enc_positive_small_integer_field, 0)
self.assertEqual(inst.enc_big_integer_field, -9223372036854775806)
inst.save()
inst = models.TestModel.objects.get()
def test_unicode_value(self):
inst = models.TestModel()
inst.enc_char_field = u'\xa2\u221e\xa7\xb6\u2022\xaa'
inst.enc_text_field = u'\xa2\u221e\xa7\xb6\u2022\xa2'
inst.save()
inst2 = models.TestModel.objects.get()
self.assertEqual(inst2.enc_char_field, u'\xa2\u221e\xa7\xb6\u2022\xaa')
self.assertEqual(inst2.enc_text_field, u'\xa2\u221e\xa7\xb6\u2022\xa2')
@mock.patch('django.db.models.sql.compiler.SQLCompiler.get_converters')
def test_raw_value(self, get_converters_method):
get_converters_method.return_value = []
inst = models.TestModel()
inst.enc_char_field = 'This is a test string!'
inst.enc_text_field = 'This is a test string2!'
inst.enc_date_field = datetime.date(2011, 1, 1)
inst.enc_datetime_field = datetime.datetime(2012, 2, 1, 1, tzinfo=timezone.utc)
inst.enc_boolean_field = True
inst.enc_integer_field = 123456789
inst.enc_positive_integer_field = 123456789
inst.enc_small_integer_field = 123456789
inst.enc_positive_small_integer_field = 123456789
inst.enc_big_integer_field = 9223372036854775807
inst.save()
d = models.TestModel.objects.values()[0]
for key, value in d.items():
if key == 'id':
continue
self.assertEqual(value[:7], 'gAAAAAB', f'{key} failed: {value}')
inst.save()
d = models.TestModel.objects.values()[0]
def test_get_internal_type(self):
enc_char_field = models.TestModel._meta.fields[1]
enc_text_field = models.TestModel._meta.fields[2]
enc_date_field = models.TestModel._meta.fields[3]
enc_date_now_field = models.TestModel._meta.fields[4]
enc_boolean_field = models.TestModel._meta.fields[7]
enc_integer_field = models.TestModel._meta.fields[8]
enc_positive_integer_field = models.TestModel._meta.fields[9]
enc_small_integer_field = models.TestModel._meta.fields[10]
enc_positive_small_integer_field = models.TestModel._meta.fields[11]
enc_big_integer_field = models.TestModel._meta.fields[12]
self.assertEqual(enc_char_field.get_internal_type(), 'TextField')
self.assertEqual(enc_text_field.get_internal_type(), 'TextField')
self.assertEqual(enc_date_field.get_internal_type(), 'TextField')
self.assertEqual(enc_date_now_field.get_internal_type(), 'TextField')
self.assertEqual(enc_boolean_field.get_internal_type(), 'TextField')
self.assertEqual(enc_integer_field.get_internal_type(), 'TextField')
self.assertEqual(enc_positive_integer_field.get_internal_type(), 'TextField')
self.assertEqual(enc_small_integer_field.get_internal_type(), 'TextField')
self.assertEqual(enc_positive_small_integer_field.get_internal_type(), 'TextField')
self.assertEqual(enc_big_integer_field.get_internal_type(), 'TextField')
def test_auto_date(self):
enc_date_now_field = models.TestModel._meta.fields[4]
self.assertEqual(enc_date_now_field.name, 'enc_date_now_field')
self.assertTrue(enc_date_now_field.auto_now)
enc_date_now_add_field = models.TestModel._meta.fields[5]
self.assertEqual(enc_date_now_add_field.name, 'enc_date_now_add_field')
self.assertFalse(enc_date_now_add_field.auto_now)
self.assertFalse(enc_date_now_field.auto_now_add)
self.assertTrue(enc_date_now_add_field.auto_now_add)
def test_max_length_validation(self):
class TestModelForm(ModelForm):
class Meta:
model = models.TestModel
fields = ('enc_char_field', )
f = TestModelForm(data={'enc_char_field': 'a' * 200})
self.assertFalse(f.is_valid())
f = TestModelForm(data={'enc_char_field': 'a' * 99})
self.assertTrue(f.is_valid())
def test_rotating_keys(self):
key1 = cryptography.fernet.Fernet.generate_key()
key2 = cryptography.fernet.Fernet.generate_key()
with self.settings(FIELD_ENCRYPTION_KEY=key1):
# make sure we update the crypter with the new key
encrypted_model_fields.fields.CRYPTER = encrypted_model_fields.fields.get_crypter()
test_date_today = datetime.date.today()
test_date = datetime.date(2011, 1, 1)
test_datetime = datetime.datetime(2011, 1, 1, 1, tzinfo=timezone.utc)
inst = models.TestModel()
inst.enc_char_field = 'This is a test string!'
inst.enc_text_field = 'This is a test string2!'
inst.enc_date_field = test_date
inst.enc_datetime_field = test_datetime
inst.enc_boolean_field = True
inst.enc_integer_field = 123456789
inst.enc_positive_integer_field = 123456789
inst.enc_small_integer_field = 123456789
inst.enc_positive_small_integer_field = 123456789
inst.enc_big_integer_field = 9223372036854775807
inst.save()
# test that loading the instance from the database results in usable data
# (since it uses the older key that's still configured)
with self.settings(FIELD_ENCRYPTION_KEY=[key2, key1]):
# make sure we update the crypter with the new key
encrypted_model_fields.fields.CRYPTER = encrypted_model_fields.fields.get_crypter()
inst = models.TestModel.objects.get()
self.assertEqual(inst.enc_char_field, 'This is a test string!')
self.assertEqual(inst.enc_text_field, 'This is a test string2!')
self.assertEqual(inst.enc_date_field, test_date)
self.assertEqual(inst.enc_date_now_field, test_date_today)
self.assertEqual(inst.enc_date_now_add_field, test_date_today)
# be careful about sqlite testing, which doesn't support native dates
if timezone.is_naive(inst.enc_datetime_field):
inst.enc_datetime_field = timezone.make_aware(inst.enc_datetime_field, timezone.utc)
self.assertEqual(inst.enc_datetime_field, test_datetime)
self.assertEqual(inst.enc_boolean_field, True)
self.assertEqual(inst.enc_integer_field, 123456789)
self.assertEqual(inst.enc_positive_integer_field, 123456789)
self.assertEqual(inst.enc_small_integer_field, 123456789)
self.assertEqual(inst.enc_positive_small_integer_field, 123456789)
self.assertEqual(inst.enc_big_integer_field, 9223372036854775807)
# save the instance to rotate the key
inst.save()
# test that saving the instance results in key rotation to the correct key
with self.settings(FIELD_ENCRYPTION_KEY=[key2, ]):
# make sure we update the crypter with the new key
encrypted_model_fields.fields.CRYPTER = encrypted_model_fields.fields.get_crypter()
# test that loading the instance from the database results in usable data
# (since it uses the older key that's still configured)
inst = models.TestModel.objects.get()
self.assertEqual(inst.enc_char_field, 'This is a test string!')
self.assertEqual(inst.enc_text_field, 'This is a test string2!')
self.assertEqual(inst.enc_date_field, test_date)
self.assertEqual(inst.enc_date_now_field, test_date_today)
self.assertEqual(inst.enc_date_now_add_field, test_date_today)
# be careful about sqlite testing, which doesn't support native dates
if timezone.is_naive(inst.enc_datetime_field):
inst.enc_datetime_field = timezone.make_aware(inst.enc_datetime_field, timezone.utc)
self.assertEqual(inst.enc_datetime_field, test_datetime)
self.assertEqual(inst.enc_boolean_field, True)
self.assertEqual(inst.enc_integer_field, 123456789)
self.assertEqual(inst.enc_positive_integer_field, 123456789)
self.assertEqual(inst.enc_small_integer_field, 123456789)
self.assertEqual(inst.enc_positive_small_integer_field, 123456789)
self.assertEqual(inst.enc_big_integer_field, 9223372036854775807)
# test that the instance with rotated key is no longer readable using the old key
with self.settings(FIELD_ENCRYPTION_KEY=[key1, ]):
# make sure we update the crypter with the new key
encrypted_model_fields.fields.CRYPTER = encrypted_model_fields.fields.get_crypter()
# test that loading the instance from the database results in usable data
# (since it uses the older key that's still configured)
# Note we need to only load the enc_char_field because loading date field types
# results in conversion to python dates, which will be raise a ValidationError when
# the field can't be properly decoded
inst = models.TestModel.objects.only('enc_char_field').get()
self.assertNotEqual(inst.enc_char_field, 'This is a test string!')
self.assertEqual(inst.enc_char_field[:5], 'gAAAA')
# reset the CRYPTER since we screwed with the default configuration with this test
encrypted_model_fields.fields.CRYPTER = encrypted_model_fields.fields.get_crypter()
@mock.patch('django.db.connection.ops.integer_field_range')
def test_integer_field_validators(self, integer_field_range):
def side_effect(arg):
# throw error as mysql does in this case
if arg == 'TextField':
raise KeyError(arg)
# benign return value
return (None, None)
integer_field_range.side_effect = side_effect
class TestModelForm(ModelForm):
class Meta:
model = models.TestModel
fields = ('enc_integer_field', )
f = TestModelForm(data={'enc_integer_field': 99})
self.assertTrue(f.is_valid())
inst = models.TestModel()
# Should be safe to call
super(
encrypted_model_fields.fields.EncryptedIntegerField,
inst._meta.get_field('enc_integer_field')
).validators
# should fail due to error
with self.assertRaises(Exception):
super(
encrypted_model_fields.fields.EncryptedNumberMixin,
inst._meta.get_field('enc_integer_field')
).validators