django-extensions/django-extensions — pull request #363 by heston (2013-08-30 23:17:54+00:00)
Title: Bug fix for public keys with encrypted fields; More robust testing.

During the course of using encrypted fields with asymmetric public/private keys, we were seeing an exception being raised in a particular scenario.

In `BaseEncryptedField.to_python`, when a private key is provided but `settings.ENCRYPTED_FIELD_MODE` is set to `ENCRYPT`, `to_python` tries to call `Decrypt` when it shouldn't. In this scenario a KeyCzar `Encrypter` object is used, which doesn't have a `Decrypt` method. That method is only available on `Crypter` objects. We were seeing an `AttributeError`, when actually the encrypted string should have just been returned.

This pull request fixes that bug, so that `to_python` can recover from this scenario. The fix is wholly contained in `django_extensions/db/fields/encrypted.py`. It's pretty simple.

However, when I tried to write tests for this case, I realized that the current test structure for encrypted fields would not work. In order to test both private-key scenarios, and my new public-key scenarios, I needed two sets of keys, and I needed to be able to switch which set was being used on a per-test basis. The current test runner sets up the keys once, and doesn't provide a clear way to change them during the test run.

So, most of this pull request is actually reworking the test environment to allow for easily switching key sets during testing.

All the existing test cases have been maintained, and I've added several new ones to test public-key encryption scenarios.

Admittedly, this pull request makes the encrypted field test module more complex, but it also allows for much more robust testing of encrypted field functionality.