"""Test SecureDocument.""" from dynamodb_encryption_sdk.identifiers import CryptoAction import pytest from payee.connectors.secure_data.document import SecureDocument from payee.connectors.secure_data.exceptions import SecureDocumentException from payee.connectors.secure_data.fields.text_field import TextField def test_document_populate_fields(test_document_class): """Test document field populating.""" test_document = test_document_class() test_document.values = {'id': 'one', 'name': 'test_name'} assert test_document.values == {'id': 'one', 'name': '***************'} assert test_document.original_values == {'id': 'one', 'name': 'test_name'} assert test_document.get_values(True) == {'id': 'one', 'name': '***************'} assert test_document.get_values(False) == {'id': 'one', 'name': 'test_name'} def test_document_fields_property(test_document_class): """Test setting the fields on a document.""" assert 'id' in test_document_class._fields assert 'name' in test_document_class._fields def test_document_field_actions(): """Test getting field actions.""" class TestDocument(SecureDocument): """Test document class.""" _document_type = 'test' _fields = {'id': TextField('id', encrypt=False), 'foo': TextField('foo')} test_document = TestDocument() assert test_document.get_field_actions() == {'id': CryptoAction.DO_NOTHING} def test_document_document_type(test_document_class): """Test getting the document type.""" test_document = test_document_class() assert test_document.document_type() == 'test' def test_document_invalid_values(): """Test invalid field values.""" class TestDocument(SecureDocument): """Test document class.""" _document_type = 'test' _fields = { 'id': TextField('id', required=True), 'foo': TextField('foo', required=True), } test_document = TestDocument() with pytest.raises(SecureDocumentException): test_document.values = {} def test_document_meta_values(test_document_class): """Test dict representation with meta fields.""" test_document = test_document_class() test_document.values = {'id': 'one'} test_document.created_at = '2020-02-02 02:20:02+0000' assert test_document.values_with_meta == { 'id': 'one', 'name': None, 'created_at': '2020-02-02 02:20:02+0000', 'closed_at': None, } @pytest.mark.parametrize( 'name,expected_has_obscured', (('test_name', False), ('***************', True)) ) def test_document_has_obscured_values(test_document_class, name, expected_has_obscured): """Test document field populating.""" test_document = test_document_class() test_document.values = {'id': 'one', 'name': name} assert test_document.has_obscured_values is expected_has_obscured @pytest.mark.parametrize( 'asterisk_count,original_left,original_right,name,expected_has_obscured', ( (3, 2, None, 'test_name', False), (3, 2, None, 'te***', True), (4, None, 1, '****e', True), (4, None, 1, '****me', False), ), ) def test_document_has_obscured_values_advanced( asterisk_count, original_left, original_right, name, expected_has_obscured ): """Test invalid field values.""" class TestDocument(SecureDocument): """Test document class.""" _document_type = 'test' _fields = { 'id': TextField('id'), 'name': TextField( 'name', obscure_in_response=True, asterisk_count=asterisk_count, original_symbols_count_left=original_left, original_symbols_count_right=original_right, ), } test_document = TestDocument(dict(id=1, name=name)) assert test_document.has_obscured_values is expected_has_obscured @pytest.mark.parametrize( 'id_value,name_value,has_id,has_name', ( ('one', 'test_name', True, True), (None, 'test_name', False, True), ('one', None, True, False), (None, None, False, False), ), ) def test_document_populate_fields( test_document_class, id_value, name_value, has_id, has_name ): """Test document field populating.""" test_document = test_document_class() test_document.values = {'id': id_value, 'name': name_value} assert test_document.field_with_value_exists('id') is has_id assert test_document.field_with_value_exists('name') is has_name