Example #1
0
def _to_gob_value(entity, field, spec, resolve_secure=False):
    """
    Transforms a entity field value into a GOB type value

    Attention:
    Resolve secure is normally False as this is all handled by the Authority classes
    For enhanced views however, this is not possible.
    These queries are based upon a view and cannot directly be related to a GOB Model
    If the field names of the view are properly named the decryption will be handled here

    :param entity:
    :param field:
    :param spec:
    :param resolve_secure:
    :return:
    """
    entity_value = getattr(entity, field, None)
    if isinstance(spec, dict):
        gob_type = get_gob_type_from_info(spec)
        if resolve_secure and Authority.is_secure_type(spec):
            # Transform the value into a secure type value
            secure_type = Authority.get_secure_type(gob_type, spec,
                                                    entity_value)
            # Return decrypted value
            return Authority.get_secured_value(secure_type)
        return gob_type.from_value(entity_value, **spec)
    else:
        gob_type = get_gob_type_from_sql_type(spec)
        return gob_type.from_value(entity_value)
Example #2
0
    def test_get_secure_type(self):
        mock_gob_type = mock.MagicMock()
        mock_gob_type.from_value_secure.return_value = "secure GOB type"

        result = Authority.get_secure_type(mock_gob_type, 'any spec',
                                           'any value')
        self.assertEqual(result, "secure GOB type")
        mock_gob_type.from_value_secure.assert_called_with(
            'any value', 'any spec')