Esempio n. 1
0
def local_client():
    if os.path.isdir(Config.STATIC_STORAGE_DIR):
        shutil.rmtree(Config.STATIC_STORAGE_DIR)
    app = create_app(TEST_MODE)
    with app.app_context():
        db.reflect()
        db.drop_all()
        db.create_all()

        user = User(username='******',
                    email='email@test_upload.com',
                    privileges=PrivilegesEnum.ADMIN)
        user.set_password('TestTest1')
        db.session.add(user)
        db.session.commit()

        project = Project(project_name='test/test', created_by=user.id)
        db.session.add(project)
        db.session.commit()

        permission = ProjectPermission(access_type=AccessTypeEnum.READ_WRITE)
        user.project_permissions.append(permission)
        project.permissions.append(permission)

        user = User(username='******',
                    email='email2@test_upload.com')
        user.set_password('TestTest1')
        permission = ProjectPermission(access_type=AccessTypeEnum.READ_WRITE)
        user.project_permissions.append(permission)
        project.permissions.append(permission)
        db.session.add(user)
        db.session.commit()

    client = app.test_client()
    yield client
def test_user_and_project(client):
    user = User()
    project = Project()
    project_permission = ProjectPermission()

    user.project_permissions.append(project_permission)
    project_permission.project = project

    assert len(user.project_permissions) == 1
    assert len(project.permissions) == 1
    assert project_permission == user.project_permissions[0]
    assert project_permission == project.permissions[0]
    assert user == project_permission.user
    assert project == project_permission.project
Esempio n. 3
0
def test_project_permissions_repr(client):
    project_permissions = ProjectPermission(
        access_type=AccessTypeEnum.READ_ONLY)
    assert str(
        project_permissions
    ) == '<Access type of permission: %r>' % AccessTypeEnum.READ_ONLY
Esempio n. 4
0
def local_client():
    if os.path.isdir(Config.STATIC_STORAGE_DIR):
        shutil.rmtree(Config.STATIC_STORAGE_DIR)
    app = create_app(TEST_MODE)
    with app.app_context():
        db.reflect()
        db.drop_all()
        db.create_all()

        rw_permission = ProjectPermission(
            access_type=AccessTypeEnum.READ_WRITE)
        r_permission = ProjectPermission(access_type=AccessTypeEnum.READ_ONLY)

        admin = User(username='******',
                     email='email@test_load.com',
                     privileges=PrivilegesEnum.ADMIN)
        admin.set_password('TestTest1')
        admin.project_permissions.append(rw_permission)
        db.session.add(admin)
        db.session.commit()

        annotator = User(username='******',
                         email='email2@test_load.com',
                         privileges=PrivilegesEnum.ANNOTATOR)
        annotator.set_password('TestTest2')
        annotator.project_permissions.append(r_permission)
        db.session.add(annotator)
        db.session.commit()

        annotator_2 = User(username='******',
                           email='email3@test_load.com')
        annotator_2.set_password('TestTest3')
        db.session.add(annotator_2)
        db.session.commit()

        project = Project(project_name='TestProject', created_by=admin.id)
        db.session.add(project)
        db.session.commit()

        project.permissions.append(rw_permission)
        project.permissions.append(r_permission)

        test_image = Image(image_name='cat.jpg',
                           image_url='test_dir/test_dir_2/cat.jpg',
                           project_id=project.id)
        db.session.add(test_image)
        db.session.commit()

        test_image_2 = Image(image_name='dog.png',
                             image_url='test_dir/test_dir_2/dog.png',
                             project_id=project.id)
        db.session.add(test_image_2)
        db.session.commit()

        test_image_3 = Image(image_name='cat.jpg',
                             image_url='tests/cat.jpg',
                             image_storage_path='tests/cat.jpg',
                             project_id=project.id)
        db.session.add(test_image_3)
        db.session.commit()

    client = app.test_client()
    yield client
Esempio n. 5
0
def update_user_permission(project_id):
    """ Grant a user with specified privilege to project

        Grant a user with specified privilege (READ_WRITE or READ_ONLY) to
        project
        upon receiving a `PUT`request to the `/project/<project_id>/permissions`
        entry point. User must be signed in as an ADMIN and have READ_WRITE
        permission to the project. User must provide a `username` to specify the
        user that will be granted permissions, and `access_type` to specify the
        type of privilege to grant.

        `access_type` takes a string of two values: `r` or `rw`, where `r` is
        `READ_ONLY` and `rw` is `READ_WRITE`

        Only user with `ADMIN` privilege can have `READ_WRITE` access to
        projects.
        That is, the user indicated by `username` should have an `ADMIN`
        privilege
        (as opposed to `ANNOTATOR` privilege)

        Must supply a jwt-token to verify user status and extract `user_id`.

        Raises:
            400 Error if username or access_type is not specified
            400 Error if access_type is not 'r' or 'rw'
            401 Error if not logged in
            401 Error if user is not an ADMIN
            401 Error if user does not have privilege to update permission
            403 Error if user indicated by `username` has only ANNOTATOR
            privilege
                but `access_type` is 'rw'
            404 Error if user with user_name does not exist

        Returns:
            201 if success. Will also return `project_id`.
        """

    project = maybe_get_project(project_id)  # check privilege and get project

    req = request.get_json()
    check_json(req, ('username', 'access_type'))  # check missing keys

    username = req['username']
    user = User.query.filter_by(username=username).first()
    if user is None:
        abort(404, 'User with username=%s not found' % username)

    map_code_to_access_type = {
        'r': AccessTypeEnum.READ_ONLY,
        'rw': AccessTypeEnum.READ_WRITE,
    }
    if req['access_type'] not in map_code_to_access_type:
        abort(400, 'Not able to interpret access_type.')
    access_type = map_code_to_access_type[req['access_type']]

    # Check if user has already had some sort of permission to the project
    permission = ProjectPermission.query.filter(
        (ProjectPermission.user_id == user.id)
        & (ProjectPermission.project_id == project_id)).first()
    if permission is not None:
        permission.access_type = access_type
        db.session.commit()
        return construct_msg('Permission updated successfully'), 200

    new_permission = ProjectPermission(access_type=access_type)
    project.permissions.append(new_permission)
    user.project_permissions.append(new_permission)
    try:
        db.session.add(new_permission)
        db.session.commit()
    except IntegrityError:
        db.session.rollback()
        abort(400, 'Update permission failed.')

    return construct_msg('Permission added successfully'), 201
Esempio n. 6
0
def create_project():
    """ Create a new project by the user currently signed in

    Create a new project entry in the database and a new folder in the
    filesystem
    to hold image dataset upon receiving a `POST`request to the `/project` entry
    point. User must be signed in as an ADMIN and must provide a `project name`
    to create a new project.

    Must supply a jwt-token to verify user status and extract `user_id`.
    `project name` must be unique.

    Raises:
        400 Error if project name is not specified
        401 Error if not logged in
        401 Error if user is not an ADMIN
        400 Error if project name already exists in the database


    Returns:
        201 if success. Will also return `project_id`.
    """

    req = request.get_json()
    check_json(req, ['project_name'])

    # check user identity and privilege
    identity = get_jwt_identity()
    user = check_user_admin_privilege(identity)

    project_name = req['project_name']
    user_id = user.id
    project = Project(project_name=project_name, created_by=user_id)

    try:
        db.session.add(project)
        db.session.flush()  # To make sure no duplicates exist in db
    except IntegrityError:
        db.session.rollback()
        abort(
            400, 'Duplicate project name. '
            'Please provide a different project name.')
    else:
        # if able to add project to db, try add project_permission
        # creator is granted with READ_WRITE privilege
        project_permission = ProjectPermission(
            access_type=AccessTypeEnum.READ_WRITE,
            user_id=user_id,
            project_id=project.id)
        user.project_permissions.append(project_permission)
        project.permissions.append(project_permission)
        try:
            db.session.add(project_permission)
        except DatabaseError:
            db.session.rollback()
            abort(400, 'Cannot update project permission for user.')

    db.session.commit()

    # create project directory
    project_dir = os.path.join(app.config['STATIC_STORAGE_DIR'],
                               str(project.id))
    if not os.path.exists(project_dir):
        os.makedirs(project_dir)

    return jsonify({
        'msg': 'project added successfully',
        'project_id': project.id
    }), 201
Esempio n. 7
0
def get_project_fixture():
    app = create_app(TEST_MODE)
    with app.app_context():
        db.reflect()
        db.drop_all()
        db.create_all()

        user = User(username=ADMIN_USERNAME,
                    email='success@test_project.com',
                    privileges=PrivilegesEnum.ADMIN)
        user.set_password(ADMIN_PWD)
        db.session.add(user)
        db.session.commit()

        project = Project(project_name='test1', created_by=user.id)
        db.session.add(project)
        db.session.commit()

        image1 = Image(project_id=1, image_name='cracked_building.png')
        db.session.add(image1)
        db.session.commit()

        label1 = Label(label_name='asdf1', project_id=project.id)
        db.session.add(label1)
        label2 = Label(label_name='asdf2', project_id=project.id)
        db.session.add(label2)
        db.session.commit()

        annotation1 = Annotation(image_id=1,
                                 project_id=1,
                                 creator_id=1,
                                 label_id=1,
                                 data=b'1234',
                                 vector=b'1234')
        db.session.add(annotation1)
        annotation2 = Annotation(image_id=1,
                                 project_id=1,
                                 creator_id=1,
                                 label_id=2,
                                 data=b'1234',
                                 vector=b'1234')
        db.session.add(annotation2)
        db.session.commit()

        # Create project directory
        if not os.path.isdir('static-dir/1'):
            os.makedirs('static-dir/1')

        permission = ProjectPermission(access_type=AccessTypeEnum.READ_WRITE)
        user.project_permissions.append(permission)
        project.permissions.append(permission)

        user = User(username=ANNOTATOR_USERNAME,
                    email='no_privilege@test_project.com',
                    privileges=PrivilegesEnum.ANNOTATOR)
        user.set_password(ANNOTATOR_PWD)

        db.session.add(user)
        db.session.commit()

        permission = ProjectPermission(access_type=AccessTypeEnum.READ_ONLY)
        user.project_permissions.append(permission)
        project.permissions.append(permission)

        db.session.commit()

    client = app.test_client()
    yield client