def test_api_action_links(read_json, registry):
    transport = Payload(read_json('transport.json'))
    address = transport.get('meta/gateway')[1]
    service_name = 'foo'
    link_name = 'self'
    links_path = '|'.join([
        'links',
        address,
        nomap(service_name),
        nomap(link_name),
    ])
    action = Action(
        **{
            'action': 'bar',
            'params': [],
            'transport': transport,
            'component': None,
            'path': '/path/to/file.py',
            'name': service_name,
            'version': '1.0',
            'framework_version': '1.0.0',
        })

    # Clear transport links
    assert FIELD_MAPPINGS['links'] in transport
    del transport[FIELD_MAPPINGS['links']]
    assert FIELD_MAPPINGS['links'] not in transport
    assert not transport.path_exists(links_path, delimiter='|')

    # Set a link
    uri = 'http://api.example.com/v1/users/123'
    assert action.set_link(link_name, uri) == action
    assert transport.path_exists(links_path, delimiter='|')
    assert transport.get(links_path, delimiter='|') == uri
def test_api_action_transactions(read_json, registry):
    transport = Payload(read_json('transport.json'))
    service_name = 'foo'
    service_version = '1.0'
    service_action = 'foo'
    params = [Param('dummy', value=123)]
    action = Action(
        **{
            'action': service_action,
            'params': [],
            'transport': transport,
            'component': None,
            'path': '/path/to/file.py',
            'name': service_name,
            'version': service_version,
            'framework_version': '1.0.0',
        })

    # Clear transport transactions
    assert transport.path_exists('transactions')
    del transport[FIELD_MAPPINGS['transactions']]
    assert not transport.path_exists('transactions')

    tr_params = [{
        PARAM['name']: 'dummy',
        PARAM['value']: 123,
        PARAM['type']: TYPE_INTEGER
    }]
    actions = ('action-1', 'action-2')

    cases = {
        'commit': action.commit,
        'rollback': action.rollback,
        'complete': action.complete,
    }

    # Check all transaction types
    for type, register in cases.items():
        # Register 2 transaction actions for current type
        for name in actions:
            assert register(name, params=params) == action

        path = 'transactions/{}'.format(type)
        assert transport.path_exists(path)
        transactions = transport.get(path)
        assert isinstance(transactions, list)
        for tr in transactions:
            assert isinstance(tr, dict)
            assert get_path(tr, 'name', default='NO') == service_name
            assert get_path(tr, 'version', default='NO') == service_version
            assert get_path(tr, 'action', default='NO') in actions
            assert get_path(tr, 'caller',
                            default='NO') == action.get_action_name()
            assert get_path(tr, 'params', default='NO') == tr_params
def test_api_action_errors(read_json, registry):
    transport = Payload(read_json('transport.json'))
    address = transport.get('meta/gateway')[1]
    service_name = 'foo'
    service_version = '1.0'
    errors_path = '|'.join([
        'errors',
        address,
        nomap(service_name),
        service_version,
    ])
    action = Action(
        **{
            'action': 'bar',
            'params': [],
            'transport': transport,
            'component': None,
            'path': '/path/to/file.py',
            'name': service_name,
            'version': '1.0',
            'framework_version': '1.0.0',
        })

    # Clear transport errors
    assert FIELD_MAPPINGS['errors'] in transport
    del transport[FIELD_MAPPINGS['errors']]
    assert FIELD_MAPPINGS['errors'] not in transport
    assert not transport.path_exists(errors_path, delimiter='|')

    # Set an error
    msg = 'Error message'
    code = 99
    status = '500 Internal Server Error'
    assert action.error(msg, code=code, status=status) == action
    assert transport.path_exists(errors_path, delimiter='|')
    errors = transport.get(errors_path, delimiter='|')
    assert isinstance(errors, list)
    assert len(errors) == 1
    error = errors[0]
    assert isinstance(error, ErrorPayload)
    assert error.get('message') == msg
    assert error.get('code') == code
    assert error.get('status') == status

    # Add a second error
    assert action.error(msg, code=code, status=status) == action
    errors = transport.get(errors_path, delimiter='|')
    assert isinstance(errors, list)
    assert len(errors) == 2
    for error in errors:
        assert isinstance(error, ErrorPayload)
Exemple #4
0
def test_payload_mappings():
    # Check disabling mappings (by default mappings are enabled)
    assert not payload_module.DISABLE_FIELD_MAPPINGS
    # Create a payload that contains an unmapped name "abc"
    payload = Payload({'d': {'abc': True}})
    # Naturally "new_mapping" does not exists
    assert not payload.path_exists('data/new_mapping')
    # ... and the unmapped field does
    assert payload.path_exists('data/abc')

    # Add mappings for current fields
    payload.set_mappings({'data': 'd', 'new_mapping': 'abc'})
    # Now "abc" can be accessed using mapped or unmapped name
    assert payload.path_exists('data/new_mapping')
    assert payload.path_exists('data/abc')

    # Disable mappings
    payload_module.DISABLE_FIELD_MAPPINGS = True
    assert payload_module.DISABLE_FIELD_MAPPINGS

    # Create a payload wit mappings disabled
    payload = Payload({'d': {'abc': True}})
    payload.set_mappings({'data': 'd', 'new_mapping': 'abc'})
    assert not payload.path_exists('data/new_mapping')
    assert not payload.path_exists('data/abc')
    # Fields can only be traversed by real payload key name
    assert payload.path_exists('d/abc')
    # Restore flag to default value
    payload_module.DISABLE_FIELD_MAPPINGS = False
def test_api_action_return_value(read_json, registry):
    service_name = 'foo'
    service_version = '1.0'
    transport = Payload(read_json('transport.json'))
    return_value = Payload()
    action_args = {
        'action': 'foo',
        'params': [],
        'transport': transport,
        'component': None,
        'path': '/path/to/file.py',
        'name': service_name,
        'version': service_version,
        'framework_version': '1.0.0',
        'return_value': return_value,
    }

    # By default return value is set when no schema is available
    action = Action(**action_args)
    assert action.set_return(1) == action
    assert return_value.get('return') == 1

    # Check that registry does not have mappings
    assert not registry.has_mappings
    # Add an empty test action to mappings
    mappings = Payload(read_json('schema-service.json'))
    registry.update_registry({service_name: {service_version: mappings}})

    # Set return when mappings contain a return definition for the action
    action = Action(**action_args)
    assert action.set_return(1) == action
    assert action_args['return_value'].get('return') == 1

    # Set an invalid return value type
    with pytest.raises(ReturnTypeError):
        action.set_return('fail')

    # Set a return value when no return definition exists for the action
    assert mappings.path_exists('actions/foo/return')
    delete_path(mappings, 'actions/foo/return')
    assert not mappings.path_exists('actions/foo/return')
    action = Action(**action_args)
    with pytest.raises(UndefinedReturnValueError):
        action.set_return(1)
Exemple #6
0
def test_api_schema_action(read_json):
    # Get schema info for 'foo' action
    payload = Payload(read_json('schema-service'))
    assert payload.path_exists('actions/foo')
    payload = payload.get('actions/foo')
    assert isinstance(payload, dict)
    payload = Payload(payload)

    action = ActionSchema('foo', payload)

    assert action.get_name() == 'foo'
    assert action.is_deprecated()
    assert action.is_collection()
    assert action.get_entity_path() == payload.get('entity_path')
    assert action.get_path_delimiter() == payload.get('path_delimiter')
    assert action.get_primary_key() == payload.get('primary_key')
    assert action.resolve_entity({'foo': {'bar': 'OK'}}) == 'OK'
    # Resolve an invalid entity
    with pytest.raises(ActionSchemaError):
        action.resolve_entity({'foo': {'MISSING': 'OK'}})

    # Check entity schema related methods
    assert action.has_entity_definition()
    entity = action.get_entity()
    assert isinstance(entity, dict)
    assert len(entity) == 3
    assert sorted(entity.keys()) == ['field', 'fields', 'validate']

    # Check return value
    assert action.has_return()
    assert action.get_return_type() == payload.get('return/type')

    # Check tags
    tags = action.get_tags()
    assert len(tags) == 2
    for tag in ['foo', 'bar']:
        assert tag in tags

    # Check relations
    assert action.has_relations()
    assert sorted(action.get_relations()) == [
        ['many', 'posts'],
        ['one', 'accounts'],
    ]

    # Check runtime calls
    assert action.has_calls()
    assert sorted(action.get_calls()) == [
        ['bar', '1.1', 'dummy'],
        ['foo', '1.0', 'dummy'],
    ]
    assert action.has_call('foo', '1.0', 'dummy')
    # Check invalid local call arguments
    assert not action.has_call('MISSING')
    assert not action.has_call('foo', 'MISSING')
    assert not action.has_call('foo', '1.0', 'MISSING')

    # Check deferred calls
    assert action.has_defer_calls()
    assert sorted(action.get_defer_calls()) == [
        ['bar', '1.1', 'dummy'],
        ['foo', '1.0', 'dummy'],
    ]
    assert action.has_defer_call('foo', '1.0', 'dummy')
    # Check invalid local call arguments
    assert not action.has_defer_call('MISSING')
    assert not action.has_defer_call('foo', 'MISSING')
    assert not action.has_defer_call('foo', '1.0', 'MISSING')

    # Check files
    assert action.has_file('upload')
    assert action.get_files() == ['upload']

    # Check params
    assert action.has_param('value')
    assert action.get_params() == ['value']

    # Check remote calls
    assert action.has_remote_calls()
    remote = 'ktp://87.65.43.21:4321'
    remote_calls = [[remote, 'foo', '1.0', 'dummy']]
    assert sorted(action.get_remote_calls()) == remote_calls
    assert action.has_remote_call(*remote_calls[0])
    # Check invalid remote call arguments
    assert not action.has_remote_call('MISSING')
    assert not action.has_remote_call(remote, 'MISSING')
    assert not action.has_remote_call(remote, 'foo', 'MISSING')
    assert not action.has_remote_call(remote, 'foo', '1.0', 'MISSING')

    # Check HTTP schema
    http_schema = action.get_http_schema()
    assert isinstance(http_schema, HttpActionSchema)
    assert not http_schema.is_accessible()
    assert http_schema.get_method() == payload.get('http/method')
    assert http_schema.get_path() == payload.get('http/path')
    assert http_schema.get_input() == payload.get('http/input')
    assert http_schema.get_body().split(',') == payload.get('http/body')

    # Check file schema
    file_schema = action.get_file_schema('upload')
    assert isinstance(file_schema, FileSchema)

    # Check param schema
    param_schema = action.get_param_schema('value')
    assert isinstance(param_schema, ParamSchema)
def test_api_action_call_remote(read_json, registry):
    service_name = 'foo'
    service_version = '1.0'

    # Check that registry does not have mappings
    assert not registry.has_mappings
    # Add an empty test action to mappings
    registry.update_registry({
        service_name: {
            service_version: {
                FIELD_MAPPINGS['files']: True,
                FIELD_MAPPINGS['actions']: {
                    'test': {}
                },
            },
        },
    })

    transport = Payload(read_json('transport.json'))
    calls_path = 'calls/{}/{}'.format(nomap(service_name), service_version)
    action = Action(
        **{
            'action': 'test',
            'params': [],
            'transport': transport,
            'component': None,
            'path': '/path/to/file.py',
            'name': service_name,
            'version': service_version,
            'framework_version': '1.0.0',
        })

    # Clear transport calls
    assert transport.path_exists('calls')
    del transport[FIELD_MAPPINGS['calls']]
    assert not transport.path_exists('calls')

    # Prepare call arguments
    params = [Param('dummy', value=123)]
    c_addr = '87.65.43.21:4321'
    c_name = 'foo'
    c_version = '1.1'
    c_action = 'bar'
    c_params = [{
        PARAM['name']: 'dummy',
        PARAM['value']: 123,
        PARAM['type']: TYPE_INTEGER
    }]

    # Make a remotr call
    kwargs = {
        'address': c_addr,
        'service': c_name,
        'version': c_version,
        'action': c_action,
        'params': params,
        'timeout': 2.0,
    }
    assert action.remote_call(**kwargs) == action
    assert transport.path_exists(calls_path)
    calls = transport.get(calls_path)
    assert isinstance(calls, list)
    assert len(calls) == 1
    call = calls[0]
    assert isinstance(call, dict)
    assert get_path(call, 'gateway', default='NO') == 'ktp://{}'.format(c_addr)
    assert get_path(call, 'name', default='NO') == c_name
    assert get_path(call, 'version', default='NO') == c_version
    assert get_path(call, 'action', default='NO') == c_action
    assert get_path(call, 'params', default='NO') == c_params

    # Make a call and add files
    files_path = '|'.join([
        'files',
        transport.get('meta/gateway')[1],
        nomap(c_name),
        c_version,
        nomap(c_action),
    ])
    kwargs['files'] = [action.new_file('download', '/tmp/file.ext')]
    assert action.remote_call(**kwargs) == action
    tr_files = transport.get(files_path, delimiter='|')
    assert isinstance(tr_files, list)
    assert len(tr_files) == 1
    assert tr_files[0] == {
        FIELD_MAPPINGS['name']: 'download',
        FIELD_MAPPINGS['token']: '',
        FIELD_MAPPINGS['filename']: 'file.ext',
        FIELD_MAPPINGS['size']: 0,
        FIELD_MAPPINGS['mime']: 'text/plain',
        FIELD_MAPPINGS['path']: 'file:///tmp/file.ext',
    }

    # Set file server mappings to False and try to call with local files
    registry.update_registry({
        service_name: {
            service_version: {
                FIELD_MAPPINGS['files']: False,
                FIELD_MAPPINGS['actions']: {
                    'test': {}
                },
            },
        },
    })

    # TODO: Figure out why existing action does not see new mappungs.
    #       Action should read the mapping values from previous statement.
    action = Action(
        **{
            'action': 'test',
            'params': [],
            'transport': transport,
            'component': None,
            'path': '/path/to/file.py',
            'name': service_name,
            'version': service_version,
            'framework_version': '1.0.0',
        })

    with pytest.raises(NoFileServerError):
        action.remote_call(**kwargs)
def test_api_action_data(read_json, registry):
    transport = Payload(read_json('transport.json'))
    address = transport.get('meta/gateway')[1]
    service_name = 'users'
    service_version = '1.0.0'
    action_name = 'create'
    data_path = '|'.join([
        'data',
        address,
        nomap(service_name),
        service_version,
        nomap(action_name),
    ])
    action = Action(
        **{
            'action': action_name,
            'params': [],
            'transport': transport,
            'component': None,
            'path': '/path/to/file.py',
            'name': service_name,
            'version': service_version,
            'framework_version': '1.0.0',
        })

    # Clear transport data
    assert FIELD_MAPPINGS['data'] in transport
    del transport[FIELD_MAPPINGS['data']]
    assert FIELD_MAPPINGS['data'] not in transport
    assert not transport.path_exists(data_path, delimiter='|')

    # Set a transport entity
    entity = {'foo': 'bar'}
    assert action.set_entity(entity) == action
    assert transport.path_exists(data_path, delimiter='|')
    assert transport.get(data_path, delimiter='|') == [entity]
    # Set another entity
    assert action.set_entity(entity) == action
    assert transport.get(data_path, delimiter='|') == [entity, entity]

    # Check that entity can only be a dictionary
    with pytest.raises(TypeError):
        action.set_entity(1)

    # Clear transport data
    assert FIELD_MAPPINGS['data'] in transport
    del transport[FIELD_MAPPINGS['data']]
    assert FIELD_MAPPINGS['data'] not in transport
    assert not transport.path_exists(data_path, delimiter='|')

    # Set a transport collection
    collection = [{'foo': 1}, {'bar': 2}]
    assert action.set_collection(collection) == action
    assert transport.path_exists(data_path, delimiter='|')
    assert transport.get(data_path, delimiter='|') == [collection]
    # Set another collection
    assert action.set_collection(collection) == action
    assert transport.get(data_path, delimiter='|') == [collection, collection]

    # Check that collection can only be list
    with pytest.raises(TypeError):
        action.set_collection(1)

    # Items in a collection can only be dict
    with pytest.raises(TypeError):
        action.set_collection([1])