def add_coverage_data(coverage_id, coverage, environment_type): if coverage is None: raise ObjectNotFound("Coverage {} not found.".format(coverage_id)) if environment_type not in coverage.environments: raise ObjectNotFound("Environment{}' not found.".format(environment_type)) if not request.files: raise InvalidArguments('No file provided.') if request.files and 'file' not in request.files: raise InvalidArguments('File provided with bad param ("file" param expected).') content = request.files['file'] logger = logging.getLogger(__name__) logger.info('content received: %s', content) with tempfile.TemporaryDirectory() as tmpdirname: tmp_file = os.path.join(tmpdirname, content.filename) content.save(tmp_file) #TODO: improve this function so we don't have to write the file localy first file_type, file_name = data_handler.type_of_data(tmp_file) if file_type in [None, "tmp"]: logger.warning('invalid file provided: %s', content.filename) raise InvalidArguments('Invalid file provided: {}.'.format(content.filename)) with open(tmp_file, 'rb') as file: if file_type == 'fusio': #ntfs is called fusio in type_of_data coverage.save_ntfs(environment_type, file) tasks.send_ntfs_to_tyr.delay(coverage_id, environment_type) else: #we need to temporary save the file before sending it file_id = models.save_file_in_gridfs(file, filename=content.filename) tasks.send_file_to_tyr_and_discard.delay(coverage_id, environment_type, file_id) return {'message': 'Valid {} file provided : {}'.format(file_type, file_name)}, 200
def test_upload_file_ok(coverage_obj, fixture_dir): path = os.path.join(fixture_dir, 'geo_data/empty_pbf.osm.pbf') with open(path, 'rb') as f: file_id = models.save_file_in_gridfs(f, filename='test.osm.pbf') with requests_mock.Mocker() as m: m.post('http://tyr.prod/v0/instances/test', text='ok') send_file_to_tyr_and_discard(coverage_obj.id, 'production', file_id) assert m.called
def test_upload_file_ok(coverage_obj, fixture_dir): path = os.path.join(fixture_dir, "geo_data/empty_pbf.osm.pbf") with open(path, "rb") as f: file_id = models.save_file_in_gridfs(f, filename="test.osm.pbf") with requests_mock.Mocker() as m: m.post("http://tyr.prod/v0/instances/test", text="ok") send_file_to_tyr_and_discard(coverage_obj.id, "production", file_id) assert m.called