def test_run_server_certification_exception(app, mocker):
    with app.app_context():
        server = get_db().servers.find_one()

    def new_main(*args, **kwargs):
        raise Exception('error')

    mocker.patch('certification_service.tasks.main', new=new_main)

    store = mocker.patch('certification_service.tasks.store_run_results')

    tasks.run_server_certification(server['url'], 'CDAT', 'TOKEN')

    with app.app_context():
        store.assert_called_with(get_db(), server['url'], 'error', state='fail')
def test_run_server_certification(app, mocker):
    with app.app_context():
        server = get_db().servers.find_one()

    def new_main(*args, **kwargs):
        with open(args[-1], 'w') as outfile:
            outfile.write('hello')

    mocker.patch('certification_service.tasks.main', new=new_main)

    store = mocker.patch('certification_service.tasks.store_run_results')

    tasks.run_server_certification(server['url'], 'CDAT', 'TOKEN')

    with app.app_context():
        store.assert_called_with(get_db(), server['url'], 'hello', state='success')
    def post(self):
        if g.user is None:
            api.abort(403)

        db = get_db()

        args = parser.parse_args()

        now = datetime.now().isoformat()

        entry = {
            'url': args['url'],
            'module': args['module'],
            'token': args['token'],
            'date_added': now,
            'date_updated': now,

        }

        try:
            db.servers.insert_one(entry)
        except DuplicateKeyError:
            api.abort(400)

        return entry, 201
    def put(self, server_id):
        if g.user is None:
            api.abort(403)

        db = get_db()

        args = parser.parse_args()

        filter = {'_id': bson.ObjectId(server_id)}

        update_values = {
            'url': args['url'],
            'date_updated': datetime.now().isoformat(),
        }

        if 'module' in args:
            update_values['module'] = args['module']

        if 'token' in args:
            update_values['token'] = args['token']

        update = {'$set': update_values}

        entry = db.servers.find_one_and_update(filter, update, return_document=True)

        return entry
Esempio n. 5
0
def test_server_delete(app, client):
    with app.app_context():
        db = get_db()

        id = str(db.servers.find_one()['_id'])

    assert client.delete('/servers/{!s}'.format(id)).status_code == 200

    with app.app_context():
        db = get_db()

        assert db.servers.count_documents({'_id': {'$eq': id}}) == 0

        assert db.metrics.count_documents({'server_id': {'$eq': id}}) == 0

        assert db.runs.count_documents({'server_id': {'$eq': id}}) == 0
def test_run_certification_exception(app, mocker):
    with app.app_context():
        server = get_db().servers.find_one()

    certification = mocker.patch('certification_service.tasks.run_server_certification.apply_async')

    def apply_async(*args, **kwargs):
        if args[0][0] == server['url']:
            raise Exception('error')

    certification.side_effect = apply_async

    store = mocker.patch('certification_service.tasks.store_run_results')

    tasks.run_certification()

    with app.app_context():
        store.assert_called_with(get_db(), server['url'], 'error', state='fail')
def test_pull_metrics_exception(app, mocker):
    with app.app_context():
        server = get_db().servers.find().next()

    apply_async = mocker.patch('certification_service.tasks.pull_server_metrics.apply_async')

    def apply_async_ret(*args, **kwargs):
        if args[0][0] == server['url']:
            raise Exception()

    apply_async.side_effect = apply_async_ret

    store = mocker.patch('certification_service.tasks.store_metrics_results')

    tasks.pull_metrics()

    with app.app_context():
        store.assert_called_with(get_db(), server['url'], '', state='fail')
Esempio n. 8
0
    def lookup_current_user():
        g.user = None

        if 'openid' in session:
            openid = session['openid']

            db = get_db()

            g.user = db.users.find_one({'openid': openid})
def app():
    app = create_app()

    with app.app_context():
        get_client().drop_database('certification')

        init_db()

        load_fake_data(get_db())

    yield app
Esempio n. 10
0
    def delete(self, server_id):
        if g.user is None:
            api.abort(403)

        db = get_db()

        db.servers.delete_one({'_id': bson.ObjectId(server_id)})

        db.metrics.delete_many({'server_id': bson.ObjectId(server_id)})

        db.runs.delete_many({'server_id': bson.ObjectId(server_id)})
Esempio n. 11
0
        def get(self):
            openid = session['openid'] = request.args['openid.identity']

            db = get_db()

            user = db.users.find_one({'openid': openid})

            if user is not None:
                g.user = user
            else:
                return {'message': 'User has not been whitelisted'}, 200

            return redirect(oid.get_next_url() + '?openid_complete=true')
Esempio n. 12
0
def test_runs_get(app, client):
    with app.app_context():
        db = get_db()

        id = str(db.servers.find_one()['_id'])

    res = client.get('/runs/{!s}'.format(id))

    assert res.status_code == 200

    data = res.get_json()

    assert len(data) == 2

    assert data[0]['run']['state'] in ('success', 'fail')
    assert 'data' in data[0]['run']
def test_store_run_results(app, mocker):
    with app.app_context():
        db = get_db()

        server = db.servers.find_one()

        db.runs = mock.MagicMock()

        tasks.store_run_results(db, server['url'], 'data', state='success')

        db.runs.insert_one.assert_called_with({
            'data': 'data',
            'server_id': server['_id'],
            'date_added': db.runs.insert_one.call_args[0][0]['date_added'],
            'state': 'success'
        })
def test_pull_server_metrics_exception(app, mocker):
    with app.app_context():
        server = get_db().servers.find_one()

    module = 'CDAT'
    token = 'TOKEN'

    client = mocker.patch('certification_service.tasks.cwt.WPSClient')

    client.return_value.execute.side_effect = Exception('error')

    store = mocker.patch('certification_service.tasks.store_metrics_results')

    db = mocker.patch('certification_service.tasks.get_db')

    tasks.pull_server_metrics(server['url'], module, token)

    db_call = db.return_value.__enter__.return_value

    store.assert_called_with(db_call, server['url'], str(Exception('error')), state='fail')
def test_pull_server_metrics(app, mocker):
    with app.app_context():
        server = get_db().servers.find_one()

    module = 'CDAT'
    token = 'TOKEN'

    client = mocker.patch('certification_service.tasks.cwt.WPSClient')

    output = {'url': 'https://examplesite.com/api/'}

    client.return_value.process_by_name.return_value.output = output

    store = mocker.patch('certification_service.tasks.store_metrics_results')

    db = mocker.patch('certification_service.tasks.get_db')

    tasks.pull_server_metrics(server['url'], module, token)

    db_call = db.return_value.__enter__.return_value

    store.assert_called_with(db_call, server['url'], json.dumps(output), state='success')
Esempio n. 16
0
    def get(self, server_id):
        db = get_db()

        entries = [{'id': str(x['_id']), 'run': x} for x in db.runs.find({'server_id': bson.ObjectId(server_id)})]

        return entries, 200
Esempio n. 17
0
    def get(self):
        db = get_db()

        entries = [{'id': str(x['_id']), 'server': x} for x in db.servers.find()]

        return entries, 200
Esempio n. 18
0
    def whitelist_user(openid):
        db = get_db()

        db.users.insert_one({'openid': openid})