Exemplo n.º 1
0
def test_get_predefined_brush_label_elements(prepare_environment: Any) -> None:
    """Test for fetching Predefined Brush Label Elements."""
    # Step 1. Prepare a structure for the test
    dataset = DatasetsRepository.add_new_dataset('KIDNEYS', 'Kidneys')
    task = TasksRepository.add_task('MARK_KIDNEYS', 'Mark Kidneys',
                                    'path/to/image', ['KIDNEYS'], [])
    label_tag = LabelTagsRepository.add_new_tag('EXAMPLE_TAG', 'Example Tag',
                                                [LabelTool.RECTANGLE], task.id)
    scan = ScansRepository.add_new_scan(dataset, 3)
    user_id = UsersRepository.add_new_user(
        User('user@medtagger', 'HASH', 'Admin', 'Admin'))
    user = UsersRepository.get_user_by_id(user_id)

    # Step 2. Add Label with Brush Elements
    label = LabelsRepository.add_new_label(scan.id,
                                           task.key,
                                           user,
                                           LabelingTime(0),
                                           is_predefined=True)
    LabelsRepository.add_new_brush_label_element(label.id, 0, 0, 0, b'',
                                                 label_tag)
    LabelsRepository.add_new_brush_label_element(label.id, 1, 0, 0, b'',
                                                 label_tag)
    LabelsRepository.add_new_brush_label_element(label.id, 2, 0, 0, b'',
                                                 label_tag)

    # Step 3. Check if there is is Predefined Label
    brush_label_elements = LabelsRepository.get_predefined_brush_label_elements(
        scan.id, task.id, 0, 3)
    assert len(brush_label_elements) == 3
    brush_label_elements = LabelsRepository.get_predefined_brush_label_elements(
        scan.id, task.id, 0, 1)
    assert len(brush_label_elements) == 1
Exemplo n.º 2
0
def test_get_predefined_label_for_scan_in_task__predefined_label(
        prepare_environment: Any) -> None:
    """Test for fetching Predefined Label that exists."""
    # Step 1. Prepare a structure for the test
    dataset = DatasetsRepository.add_new_dataset('KIDNEYS', 'Kidneys')
    task = TasksRepository.add_task('MARK_KIDNEYS', 'Mark Kidneys',
                                    'path/to/image', ['KIDNEYS'], [])
    LabelTagsRepository.add_new_tag('EXAMPLE_TAG', 'Example Tag',
                                    [LabelTool.RECTANGLE], task.id)
    scan = ScansRepository.add_new_scan(dataset, 0)
    user_id = UsersRepository.add_new_user(
        User('user@medtagger', 'HASH', 'Admin', 'Admin'))
    user = UsersRepository.get_user_by_id(user_id)

    # Step 2. Add Label which is predefined
    label = LabelsRepository.add_new_label(scan.id,
                                           task.key,
                                           user,
                                           LabelingTime(0),
                                           is_predefined=True)

    # Step 3. Check if there is is Predefined Label
    predefined_label = LabelsRepository.get_predefined_label_for_scan_in_task(
        scan, task)
    assert predefined_label
    assert predefined_label.id == label.id
Exemplo n.º 3
0
def get_user_by_email_success_fixture(mocker: Any) -> None:
    """Return fixture for get user by email method used in create user business method."""
    mocked_user_repository = mocker.patch.object(UsersRepository,
                                                 'get_user_by_email')
    mocked_user_repository.return_value = User('*****@*****.**', 'medtagger',
                                               'First', 'Last')
    return mocked_user_repository
def insert_admin_account() -> None:
    """Insert default admin account."""
    user_email = '*****@*****.**'
    password = '******'
    password_hash = hash_password(password)

    with db_session() as session:
        user_exists = session.query(
            exists().where(User.email == user_email)).scalar()
        if user_exists:
            logger.warning('Admin user already exists with email "%s"',
                           user_email)
            return

        role = Role.query.filter_by(name='admin').first()
        if not role:
            logger.error(
                'Role not found! You have probably forgot to apply fixtures.')
            return

        user = User(user_email, password_hash, 'Admin', 'Medtagger')
        user.roles.append(role)
        session.add(user)
    logger.info('User added with email "%s" and password "%s"', user_email,
                password)
Exemplo n.º 5
0
def create_user(email: str, password: str, first_name: str,
                last_name: str) -> int:
    """Create user with the given user information. Password is being hashed.

    :param email: user email in string format
    :param password: user password in string format
    :param first_name: user first name in string format
    :param last_name: user last name in string format

    :return: id of the new user
    """
    user = UsersRepository.get_user_by_email(email)
    if user:
        raise InvalidArgumentsException('User with this email already exist')
    password_hash = hash_password(password)
    new_user = User(email, password_hash, first_name, last_name)
    role = RolesRepository.get_role_with_name('volunteer')
    if not role:
        raise InvalidArgumentsException('Role does not exist.')
    new_user.roles.append(role)
    return UsersRepository.add_new_user(new_user)
def test_get_predefined_label_for_scan_in_task__predefined_label_for_given_task(
        prepare_environment: Any) -> None:
    """Test for fetching Predefined Label only for specific Task."""
    # Step 1. Prepare a structure for the test
    dataset = DatasetsRepository.add_new_dataset('KIDNEYS', 'Kidneys')
    task_left = TasksRepository.add_task('MARK_LEFT', 'Mark Left',
                                         'path/to/image', ['KIDNEYS'], '', [],
                                         [])
    task_right = TasksRepository.add_task('MARK_RIGHT', 'Mark Right',
                                          'path/to/image', ['KIDNEYS'], '', [],
                                          [])
    LabelTagsRepository.add_new_tag('EXAMPLE_TAG', 'Example Tag',
                                    [LabelTool.RECTANGLE], task_left.id)
    scan = ScansRepository.add_new_scan(dataset, 0)
    user_id = UsersRepository.add_new_user(
        User('user@medtagger', 'HASH', 'Admin', 'Admin'))
    user = UsersRepository.get_user_by_id(user_id)

    # Step 2. Add Labels for each Task
    label_left = LabelsRepository.add_new_label(scan.id,
                                                task_left.key,
                                                user,
                                                LabelingTime(0),
                                                is_predefined=True)
    label_right = LabelsRepository.add_new_label(scan.id,
                                                 task_right.key,
                                                 user,
                                                 LabelingTime(0),
                                                 is_predefined=True)

    # Step 3. Check if there are these Predefined Labels
    predefined_label = LabelsRepository.get_predefined_label_for_scan_in_task(
        scan, task_left)
    assert predefined_label
    assert predefined_label.id == label_left.id
    predefined_label = LabelsRepository.get_predefined_label_for_scan_in_task(
        scan, task_right)
    assert predefined_label
    assert predefined_label.id == label_right.id
Exemplo n.º 7
0
def create_user(email: str, password: str, first_name: str,
                last_name: str) -> Tuple[int, str]:
    """Create user with the given user information. Password is being hashed.

    :param email: user email in string format
    :param password: user password in string format
    :param first_name: user first name in string format
    :param last_name: user last name in string format

    :return: tuple with user id and authentication token
    """
    user = UsersRepository.get_user_by_email(email)
    if user:
        raise InvalidArgumentsException('User with this email already exists')
    password_hash = hash_password(password)
    new_user = User(email, password_hash, first_name, last_name)
    role = RolesRepository.get_role_with_name('volunteer')
    if not role:
        raise InvalidArgumentsException('Role does not exist.')
    new_user.roles.append(role)
    user_id = UsersRepository.add_new_user(new_user)
    user_token = generate_auth_token(new_user)
    return user_id, user_token
Exemplo n.º 8
0
def set_user_info(user: User, first_name: str, last_name: str) -> None:
    """Set user's info."""
    with db_transaction_session() as session:
        user.first_name = first_name
        user.last_name = last_name
        session.add(user)