Example #1
0
def test_verify_validation_conf_invalid_bad_preset():
    validation_conf = {
        'source': [
            {
                'source': 'http://example.com/file.csv',
                'preset': 'bad-preset'
            },
        ]
    }
    with pytest.raises(exceptions.InvalidValidationConfiguration):
        verify_validation_conf(validation_conf)
Example #2
0
def source_job_create(source_id, user):
    # It supports both:
    # - application/json
    #   - data == {'source': [{'source': 'url'}], 'settings': {}}
    # - multipart/form-data
    #   - form['data'] == {'source': [{'source': 'file1'}], 'settings': {}}
    #   - form['file1'] == <uploaded-file>

    # Get source
    source = Source.get(source_id)
    if not source:
        raise ApiError(404, 'Not Found')
    if source not in user.sources:
        raise ApiError(403, 'Forbidden')
    if source.integration_name != 'api':
        raise ApiError(403,
                       'Forbidden, you can only create jobs on API sources')

    # Get validation configuration
    try:
        validation_conf = request.get_json()
        if request.form:
            validation_conf = json.loads(request.form.get('data', ''))
    except Exception:
        raise ApiError(400, 'Malformed configuration')
    if not validation_conf:
        raise ApiError(400, 'Missing configuration')

    # Verify validation configuration
    try:
        verify_validation_conf(validation_conf)
    except exceptions.InvalidValidationConfiguration:
        raise ApiError(400, 'Invalid configuration')

    # Save uploaded files
    files = {}
    if request.files:
        if len(request.files) > settings.FLASK_MAX_CONTENT_FILES:
            raise ApiError(403, 'Forbidden, too many files')
        dirpath = tempfile.mkdtemp()
        for name, file in request.files.items():
            path = os.path.join(dirpath, secure_filename(file.filename))
            log.debug('Temporary file creation at "%s"' % path)
            files[name] = path
            file.save(path)

    # Create and run job
    job = Job.create(source=source)
    validate.delay(validation_conf, job_id=job.id, files=files)

    return jsonify({
        'job': job.to_api(),
    })
Example #3
0
def test_verify_validation_conf():
    validation_conf = {
        'source': [
            {
                'source': 'http://example.com/file.csv'
            },
            {
                'source': 'http://example.com/datapackage.json',
                'preset': 'datapackage'
            },
        ]
    }
    assert verify_validation_conf(validation_conf)
Example #4
0
def create_job():

    # Get validation configuration
    validation_conf = request.get_json()
    if not validation_conf:
        raise APIError(400, 'Missing configuration')

    # Validate validation configuration

    try:
        verify_validation_conf(validation_conf)
    except exceptions.InvalidValidationConfiguration:
        raise APIError(400, 'Invalid configuration')

    job_id = str(uuid.uuid4())

    # Write to database
    models.job.create({'id': job_id})

    # Create celery task
    validate.delay(validation_conf, job_id=job_id)

    return job_id
Example #5
0
def test_verify_validation_conf_invalid_empty_source():
    validation_conf = {'source': []}

    with pytest.raises(exceptions.InvalidValidationConfiguration):
        verify_validation_conf(validation_conf)