Пример #1
0
def update_service(service_id):
    try:
        service = get_model_services(service_id=service_id)
    except DataError:
        return jsonify(result="error", message="Invalid service id"), 400
    except NoResultFound:
        return jsonify(result="error", message="Service not found"), 404
    if request.method == 'DELETE':
        status_code = 202
        delete_model_service(service)
    else:
        status_code = 200
        # TODO there has got to be a better way to do the next three lines
        upd_serv, errors = service_schema.load(request.get_json())
        if errors:
            return jsonify(result="error", message=errors), 400
        update_dict, errors = service_schema.dump(upd_serv)
        # TODO FIX ME
        # Remove update_service model which is added to db.session
        db.session.rollback()
        try:
            save_model_service(service, update_dict=update_dict)
        except DAOException as e:
            return jsonify(result="error", message=str(e)), 400
    return jsonify(data=service_schema.dump(service).data), status_code
Пример #2
0
def update_service(service_id):
    try:
        service = get_model_services(service_id=service_id)
    except DataError:
        return jsonify(result="error", message="Invalid service id"), 400
    except NoResultFound:
        return jsonify(result="error", message="Service not found"), 404
    if request.method == 'DELETE':
        status_code = 202
        delete_model_service(service)
    else:
        status_code = 200
        # TODO there has got to be a better way to do the next three lines
        upd_serv, errors = service_schema.load(request.get_json())
        if errors:
            return jsonify(result="error", message=errors), 400
        update_dict, errors = service_schema.dump(upd_serv)
        # TODO FIX ME
        # Remove update_service model which is added to db.session
        db.session.rollback()
        try:
            save_model_service(service, update_dict=update_dict)
        except DAOException as e:
            return jsonify(result="error", message=str(e)), 400
    return jsonify(data=service_schema.dump(service).data), status_code
Пример #3
0
def create_service():
    # TODO what exceptions get passed from schema parsing?
    service, errors = service_schema.load(request.get_json())
    if errors:
        return jsonify(result="error", message=errors), 400
    # I believe service is already added to the session but just needs a
    # db.session.commit
    try:
        save_model_service(service)
    except DAOException as e:
        return jsonify(result="error", message=str(e)), 400
    return jsonify(data=service_schema.dump(service).data), 201
Пример #4
0
def create_service():
    # TODO what exceptions get passed from schema parsing?
    service, errors = service_schema.load(request.get_json())
    if errors:
        return jsonify(result="error", message=errors), 400
    # I believe service is already added to the session but just needs a
    # db.session.commit
    try:
        save_model_service(service)
    except DAOException as e:
        return jsonify(result="error", message=str(e)), 400
    return jsonify(data=service_schema.dump(service).data), 201
def test_create_service(notify_api, notify_db, notify_db_session, sample_user):
    assert Service.query.count() == 0
    service_name = 'Sample Service'
    data = {
        'name': service_name,
        'users': [sample_user],
        'limit': 1000,
        'active': False,
        'restricted': False}
    service = Service(**data)
    save_model_service(service)
    assert Service.query.count() == 1
    assert Service.query.first().name == service_name
    assert Service.query.first().id == service.id
def test_create_service(notify_api, notify_db, notify_db_session, sample_user):
    assert Service.query.count() == 0
    service_name = 'Sample Service'
    data = {
        'name': service_name,
        'users': [sample_user],
        'limit': 1000,
        'active': False,
        'restricted': False
    }
    service = Service(**data)
    save_model_service(service)
    assert Service.query.count() == 1
    assert Service.query.first().name == service_name
    assert Service.query.first().id == service.id
def test_missing_user_attribute(notify_api, notify_db, notify_db_session):
    assert Service.query.count() == 0
    try:
        service_name = 'Sample Service'
        data = {
            'name': service_name,
            'limit': 1000,
            'active': False,
            'restricted': False}

        service = Service(**data)
        save_model_service(service)
        pytest.fail("DAOException not thrown")
    except DAOException as e:
        assert "Missing data for required attribute" in str(e)
def test_missing_user_attribute(notify_api, notify_db, notify_db_session):
    assert Service.query.count() == 0
    try:
        service_name = 'Sample Service'
        data = {
            'name': service_name,
            'limit': 1000,
            'active': False,
            'restricted': False
        }

        service = Service(**data)
        save_model_service(service)
        pytest.fail("DAOException not thrown")
    except DAOException as e:
        assert "Missing data for required attribute" in str(e)
Пример #9
0
def sample_service(notify_db,
                   notify_db_session,
                   service_name="Sample service",
                   user=None):
    if user is None:
        user = sample_user(notify_db, notify_db_session)
    data = {
        'name': service_name,
        'users': [user],
        'limit': 1000,
        'active': False,
        'restricted': False}
    service = Service.query.filter_by(name=service_name).first()
    if not service:
        service = Service(**data)
        save_model_service(service)
    return service
Пример #10
0
def test_put_service_remove_user(notify_api, notify_db, notify_db_session,
                                 sample_service, sample_admin_service_id):
    """
    Tests PUT endpoint '/<service_id>' add user to the service.
    """
    with notify_api.test_request_context():
        with notify_api.test_client() as client:
            sample_user = sample_service.users[0]
            another_user = create_sample_user(
                notify_db, notify_db_session,
                "*****@*****.**")
            data = {
                'name': sample_service.name,
                'users': [sample_user.id, another_user.id],
                'limit': sample_service.limit,
                'restricted': sample_service.restricted,
                'active': sample_service.active
            }
            save_model_service(sample_service, update_dict=data)
            assert Service.query.count() == 2
            data['users'] = [another_user.id]

            auth_header = create_authorization_header(
                service_id=sample_admin_service_id,
                path=url_for('service.update_service',
                             service_id=sample_service.id),
                method='PUT',
                request_body=json.dumps(data))
            headers = [('Content-Type', 'application/json'), auth_header]
            resp = client.put(url_for('service.update_service',
                                      service_id=sample_service.id),
                              data=json.dumps(data),
                              headers=headers)
            assert Service.query.count() == 2
            assert resp.status_code == 200
            updated_service = Service.query.get(sample_service.id)
            json_resp = json.loads(resp.get_data(as_text=True))
            assert len(json_resp['data']['users']) == 1
            assert sample_user.id not in json_resp['data']['users']
            assert another_user.id in json_resp['data']['users']
            assert sample_user not in updated_service.users
            assert another_user in updated_service.users
Пример #11
0
def test_put_service_remove_user(notify_api, notify_db, notify_db_session, sample_service, sample_admin_service_id):
    """
    Tests PUT endpoint '/<service_id>' add user to the service.
    """
    with notify_api.test_request_context():
        with notify_api.test_client() as client:
            sample_user = sample_service.users[0]
            another_user = create_sample_user(
                notify_db,
                notify_db_session,
                "*****@*****.**")
            data = {
                'name': sample_service.name,
                'users': [sample_user.id, another_user.id],
                'limit': sample_service.limit,
                'restricted': sample_service.restricted,
                'active': sample_service.active}
            save_model_service(sample_service, update_dict=data)
            assert Service.query.count() == 2
            data['users'] = [another_user.id]

            auth_header = create_authorization_header(service_id=sample_admin_service_id,
                                                      path=url_for('service.update_service',
                                                                   service_id=sample_service.id),
                                                      method='PUT',
                                                      request_body=json.dumps(data))
            headers = [('Content-Type', 'application/json'), auth_header]
            resp = client.put(
                url_for('service.update_service', service_id=sample_service.id),
                data=json.dumps(data),
                headers=headers)
            assert Service.query.count() == 2
            assert resp.status_code == 200
            updated_service = Service.query.get(sample_service.id)
            json_resp = json.loads(resp.get_data(as_text=True))
            assert len(json_resp['data']['users']) == 1
            assert sample_user.id not in json_resp['data']['users']
            assert another_user.id in json_resp['data']['users']
            assert sample_user not in updated_service.users
            assert another_user in updated_service.users