def test_api_action_files(read_json, registry): transport = Payload(read_json('transport.json')) service_name = 'users' service_version = '1.0.0' action_name = 'create' 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', }) assert action.has_file('avatar') # Get a file that is not available assert not action.has_file('missing') file = action.get_file('missing') assert isinstance(file, File) assert file.get_name() == 'missing' assert file.get_path() == '' assert not file.exists() # Get an existing file assert action.has_file('document') file = action.get_file('document') assert isinstance(file, File) assert file.get_name() == 'document' assert file.get_mime() == 'application/pdf' # Get all files files = action.get_files() assert isinstance(files, list) assert len(files) == 2 for file in files: assert file.get_name() in ('avatar', 'document') assert file.get_mime() in ('application/pdf', 'image/jpeg') # Clear all files and check result action._Action__files = {} files = action.get_files() assert isinstance(files, list) assert len(files) == 0 # Check file creation file = action.new_file('foo', path='/tmp/file.ext') assert isinstance(file, File) assert file.get_name() == 'foo'
def test_api_action_download(read_json, registry): service_name = 'dummy' service_version = '1.0' transport = Payload(read_json('transport.json')) 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', }) # Download accepts only a File instance with pytest.raises(TypeError): action.set_download('') # Create a new file and set is as download file = action.new_file('foo', path='/tmp/file.ext') transport.set('body', '') assert transport.get('body') == '' action.set_download(file) assert transport.get('body') == file_to_payload(file) # Clear download transport.set('body', '') assert transport.get('body') == '' # Check that registry does not have mappings assert not registry.has_mappings # Set file server mappings to False and try to set a download registry.update_registry( {service_name: { service_version: { 'files': False } }}) with pytest.raises(NoFileServerError): action.set_download(file)
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)