<s> import tempfile <EOL> from contextlib import contextmanager <EOL> import django <EOL> import pytest <EOL> from django . conf import settings <EOL> from django . db import connection , models <EOL> from django . test import TestCase <EOL> from . testapp . models import Secret <EOL> try : <EOL> from django_extensions . db . fields . encrypted import EncryptedTextField , EncryptedCharField <EOL> from keyczar import keyczar , keyczart , keyinfo <EOL> keyczar_active = True <EOL> except ImportError : <EOL> keyczar_active = False <EOL> KEY_LOCS = { } <EOL> @ pytest . fixture ( scope = "<STR_LIT:class>" ) <EOL> def keyczar_keys ( request ) : <EOL> if keyczar_active : <EOL> keys_dir = tempfile . mkdtemp ( "<STR_LIT>" ) <EOL> keyczart . Create ( keys_dir , "<STR_LIT:test>" , keyinfo . DECRYPT_AND_ENCRYPT , asymmetric = True ) <EOL> keyczart . AddKey ( keys_dir , "<STR_LIT>" , size = <NUM_LIT> ) <EOL> KEY_LOCS [ '<STR_LIT>' ] = keys_dir <EOL> pub_dir = tempfile . mkdtemp ( "<STR_LIT>" ) <EOL> keyczart . PubKey ( keys_dir , pub_dir ) <EOL> KEY_LOCS [ '<STR_LIT>' ] = pub_dir <EOL> def cleanup ( ) : <EOL> import shutil <EOL> for name , path in KEY_LOCS . items ( ) : <EOL> shutil . rmtree ( path ) <EOL> request . addfinalizer ( cleanup ) <EOL> @ contextmanager <EOL> def keys ( purpose , mode = None ) : <EOL> """<STR_LIT>""" <EOL> orig_setting_dir = getattr ( settings , '<STR_LIT>' , None ) <EOL> orig_setting_mode = getattr ( settings , '<STR_LIT>' , None ) <EOL> try : <EOL> if mode : <EOL> settings . ENCRYPTED_FIELD_MODE = mode <EOL> if purpose == keyinfo . DECRYPT_AND_ENCRYPT : <EOL> settings . ENCRYPTED_FIELD_KEYS_DIR = KEY_LOCS [ '<STR_LIT>' ] <EOL> yield keyczar . Crypter . Read ( settings . ENCRYPTED_FIELD_KEYS_DIR ) <EOL> else : <EOL> settings . ENCRYPTED_FIELD_KEYS_DIR = KEY_LOCS [ '<STR_LIT>' ] <EOL> yield keyczar . Encrypter . Read ( settings . ENCRYPTED_FIELD_KEYS_DIR ) <EOL> except : <EOL> raise <EOL> finally : <EOL> settings . ENCRYPTED_FIELD_KEYS_DIR = orig_setting_dir <EOL> if mode : <EOL> if orig_setting_mode : <EOL> settings . ENCRYPTED_FIELD_MODE = orig_setting_mode <EOL> else : <EOL> del settings . ENCRYPTED_FIELD_MODE <EOL> @ contextmanager <EOL> def secret_model ( ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> attrs = { <EOL> '<STR_LIT:name>' : EncryptedCharField ( "<STR_LIT:Name>" , max_length = Secret . _meta . get_field ( '<STR_LIT:name>' ) . max_length ) , <EOL> '<STR_LIT:text>' : EncryptedTextField ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:Meta>' : type ( '<STR_LIT:Meta>' , ( object , ) , { <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : Secret . _meta . db_table <EOL> } ) <EOL> } <EOL> yield type ( '<STR_LIT>' , ( models . Model , ) , attrs ) <EOL> except : <EOL> raise <EOL> @ pytest . mark . skipif ( keyczar_active is False or django . VERSION < ( <NUM_LIT:1> , <NUM_LIT:7> ) , <EOL> reason = "<STR_LIT>" ) <EOL> @ pytest . mark . usefixtures ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> class EncryptedFieldsTestCase ( TestCase ) : <EOL> def test_char_field_create ( self ) : <EOL> """<STR_LIT>""" <EOL> with keys ( keyinfo . DECRYPT_AND_ENCRYPT ) as crypt : <EOL> with secret_model ( ) as model : <EOL> test_val = "<STR_LIT>" <EOL> secret = model . objects . create ( name = test_val ) <EOL> cursor = connection . cursor ( ) <EOL> query = "<STR_LIT>" % ( model . _meta . db_table , secret . id ) <EOL> cursor . execute ( query ) <EOL> db_val , = cursor . fetchone ( ) <EOL> decrypted_val = crypt . Decrypt ( db_val [ len ( EncryptedCharField . prefix ) : ] ) <EOL> self . assertEqual ( test_val , decrypted_val ) <EOL> def test_char_field_read ( self ) : <EOL> """<STR_LIT>""" <EOL> with keys ( keyinfo . DECRYPT_AND_ENCRYPT ) : <EOL> with secret_model ( ) as model : <EOL> test_val = "<STR_LIT>" <EOL> secret = model . objects . create ( name = test_val ) <EOL> retrieved_secret = model . objects . get ( id = secret . id ) <EOL> self . assertEqual ( test_val , retrieved_secret . name ) <EOL> def test_text_field_create ( self ) : <EOL> """<STR_LIT>""" <EOL> with keys ( keyinfo . DECRYPT_AND_ENCRYPT ) as crypt : <EOL> with secret_model ( ) as model : <EOL> test_val = "<STR_LIT>" <EOL> secret = model . objects . create ( text = test_val ) <EOL> cursor = connection . cursor ( ) <EOL> query = "<STR_LIT>" % ( model . _meta . db_table , secret . id ) <EOL> cursor . execute ( query ) <EOL> db_val , = cursor . fetchone ( ) <EOL> decrypted_val = crypt . Decrypt ( db_val [ len ( EncryptedCharField . prefix ) : ] ) <EOL> self . assertEqual ( test_val , decrypted_val ) <EOL> def test_text_field_read ( self ) : <EOL> """<STR_LIT>""" <EOL> with keys ( keyinfo . DECRYPT_AND_ENCRYPT ) : <EOL> with secret_model ( ) as model : <EOL> test_val = "<STR_LIT>" <EOL> secret = model . objects . create ( text = test_val ) <EOL> retrieved_secret = model . objects . get ( id = secret . id ) <EOL> self . assertEqual ( test_val , retrieved_secret . text ) <EOL> def test_cannot_decrypt ( self ) : <EOL> """<STR_LIT>""" <EOL> with keys ( keyinfo . ENCRYPT , mode = keyinfo . ENCRYPT . name ) : <EOL> with secret_model ( ) as model : <EOL> test_val = "<STR_LIT>" <EOL> secret = model . objects . create ( name = test_val ) <EOL> retrieved_secret = model . objects . get ( id = secret . id ) <EOL> self . assertNotEqual ( test_val , retrieved_secret . name ) <EOL> self . assertTrue ( retrieved_secret . name . startswith ( EncryptedCharField . prefix ) ) <EOL> def test_unacceptable_purpose ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertRaises ( keyczar . errors . KeyczarError ) : <EOL> with keys ( keyinfo . ENCRYPT ) : <EOL> with secret_model ( ) : <EOL> pass <EOL> def test_decryption_forbidden ( self ) : <EOL> """<STR_LIT>""" <EOL> with keys ( keyinfo . DECRYPT_AND_ENCRYPT , mode = keyinfo . ENCRYPT . name ) : <EOL> with secret_model ( ) as model : <EOL> test_val = "<STR_LIT>" <EOL> secret = model . objects . create ( name = test_val ) <EOL> retrieved_secret = model . objects . get ( id = secret . id ) <EOL> self . assertNotEqual ( test_val , retrieved_secret . name ) <EOL> self . assertTrue ( retrieved_secret . name . startswith ( EncryptedCharField . prefix ) ) <EOL> def test_encrypt_public_decrypt_private ( self ) : <EOL> """<STR_LIT>""" <EOL> test_val = "<STR_LIT>" <EOL> with keys ( keyinfo . ENCRYPT , mode = keyinfo . ENCRYPT . name ) : <EOL> with secret_model ( ) as model : <EOL> secret = model . objects . create ( name = test_val ) <EOL> enc_retrieved_secret = model . objects . get ( id = secret . id ) <EOL> self . assertNotEqual ( test_val , enc_retrieved_secret . name ) <EOL> self . assertTrue ( enc_retrieved_secret . name . startswith ( EncryptedCharField . prefix ) ) <EOL> with keys ( keyinfo . DECRYPT_AND_ENCRYPT ) : <EOL> with secret_model ( ) as model : <EOL> retrieved_secret = model . objects . get ( id = secret . id ) <EOL> self . assertEqual ( test_val , retrieved_secret . name ) </s>
