Skip to content

Commit

Permalink
Incorporate Oleg Pesok's cached validator crash
Browse files Browse the repository at this point in the history
  • Loading branch information
lanshark committed Aug 26, 2021
1 parent 61de933 commit f9200e2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ django-encrypted-model-field Changelog
* Move travis-ci testing to gitlab
* Remove Support for EncryptedNullBooleanField (deprecated by Django 4.0)
* Update Test App to Django 2.2 standard
* Include 2 of Oleg Pesok's fixes: for Timezone-aware datetimes, and the
cached validator crash

- 0.5.8 - Move to GITLAB repository

Expand Down
6 changes: 4 additions & 2 deletions encrypted_model_fields/fields.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import itertools

import django.db
import django.db.models
from django.conf import settings
Expand Down Expand Up @@ -74,7 +76,7 @@ def to_python(self, value):

return super(EncryptedMixin, self).to_python(value)

def from_db_value(self, value, expression, connection):
def from_db_value(self, value, *args, **kwargs):
return self.to_python(value)

def get_db_prep_save(self, value, connection):
Expand Down Expand Up @@ -152,7 +154,7 @@ def validators(self):
range_validators.append(validators.MinValueValidator(min_value))
if max_value is not None:
range_validators.append(validators.MaxValueValidator(max_value))
return super(EncryptedNumberMixin, self).validators + range_validators
return list(itertools.chain(self.default_validators, self._validators, range_validators))


class EncryptedIntegerField(EncryptedNumberMixin, django.db.models.IntegerField):
Expand Down
33 changes: 33 additions & 0 deletions testapp/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,36 @@ def test_rotating_keys(self):

# 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

0 comments on commit f9200e2

Please sign in to comment.