Example #1
0
def secret_model():
    """
    A context manager that yields a Secret model defined at runtime.

    All EncryptedField init logic occurs at model class definition time, not at
    object instantiation time. This means that in order to test different keys
    and modes, we must generate a new class definition at runtime, after
    establishing the correct KeyCzar settings. This context manager handles
    that process.

    See http://dynamic-models.readthedocs.org/en/latest/ and
    https://docs.djangoproject.com/en/dev/topics/db/models/
        #differences-between-proxy-inheritance-and-unmanaged-models
    """

    try:
        # Create a new class that shadows tests.models.Secret.
        attrs = {
            'name':
            EncryptedCharField(
                "Name", max_length=Secret._meta.get_field('name').max_length),
            'text':
            EncryptedTextField("Text"),
            '__module__':
            'tests.testapp.models',
            'Meta':
            type('Meta', (object, ), {
                'managed': False,
                'db_table': Secret._meta.db_table
            })
        }
        yield type('Secret', (models.Model, ), attrs)

    except:
        raise  # Reraise any exceptions.
Example #2
0
class Secret(models.Model):
    name = EncryptedCharField("Name", blank=True, max_length=255)
    text = EncryptedTextField("Text", blank=True)
    def test_should_return_TextField_as_internal_type(self):
        with override_settings(ENCRYPTED_FIELD_KEYS_DIR=self.tmpdir):
            internal_type = EncryptedTextField(
                max_length=50).get_internal_type()

        self.assertEqual(internal_type, 'TextField')
    def test_should_return_formfield_with_Textarea_widget(self):
        with override_settings(ENCRYPTED_FIELD_KEYS_DIR=self.tmpdir):
            formfield = EncryptedTextField(max_length=50).formfield()

        self.assertTrue(isinstance(formfield.widget, Textarea))