def test_get_closed_items(item_id, review_id, review_answer_id, user_id,
                          comment_id):
    os.environ["STAGE"] = "dev"

    with Session() as session:

        item = Item(id=item_id)
        review = Review(id=review_id, item_id=item_id, user_id=user_id)
        review_question = ReviewQuestion(id='Question1')
        review_answer = ReviewAnswer(id=review_answer_id,
                                     review_id=review_id,
                                     review_question_id=review_question.id)
        user = User(id=user_id, name='User')
        level = Level(id=1, description='beginner')
        comment = Comment(id=comment_id,
                          comment='testcomment',
                          is_review_comment=True,
                          user_id=user_id,
                          item_id=item_id)
        session.add_all([
            item, review, review_question, review_answer, user, level, comment
        ])
        session.commit()

        # Test no query param
        event = {}
        response = get_item.get_item(event, None)
        assert response['statusCode'] == 400

        # Test invalid item id
        event = get_event('Quatsch_mit_Soße')
        response = get_item.get_item(event, None)
        assert response['statusCode'] == 404

        event = get_event(item_id)
        response = get_item.get_item(event, None)
        assert response['statusCode'] == 403

        item.status = 'closed'
        session.merge(item)
        session.commit()
        response = get_item.get_item(event, None)
        assert response['statusCode'] == 200
        body = json.loads(response['body'])
        assert body['id'] == item.id

        session.delete(user)
        session.expire_all()
        response = get_item.get_item(event, None)
        assert response['statusCode'] == 200
        body = json.loads(response['body'])
        assert body['users'][0] is None
Exemple #2
0
def item2():
    item = Item()
    item.id = str(uuid4())
    item.status = 'open'
    return item
def item_with_type(test_item_type: ItemType):
    item = Item()
    item.id = str(uuid4())
    item.status = 'open'
    item.item_type_id = test_item_type.id
    return item
def prepare_and_store_urls(item: Item, urls: [], session):
    """ prepares the urls (extract the claimant and perform threat check) and stores urls in the item

    Parameters
    ----------
    item:
        Item, required

    urls:
        string [], required

    session:
        Session, required

    Returns
    ------
        item:
            Item

    """
    unsafe_urls = False
    # Store all urls referenced in the item
    for str_url in urls:
        # do not accept urls referencing localhost
        try:
            if str_url == "" or re.search('127\.', str_url) or re.search(
                    'localhost', str_url, re.IGNORECASE):
                continue
        except (AttributeError, TypeError):
            continue

        # store claimant derived from url
        domain = urlparse(str_url).hostname
        claimant = Claimant()
        # claimant already exists?
        try:
            claimant = claimant_handler.get_claimant_by_name(domain, session)
        except Exception:
            # store claimant in database
            claimant.id = str(uuid4())
            claimant.claimant = domain
            try:
                update_object(claimant, session)
            except Exception as e:
                logger.error("Could not store claimant. Exception: %s",
                             e,
                             exc_info=True)
                raise

        url = URL()
        # get or create URL item
        try:
            url = get_url_by_content(str_url, session)
        except Exception:
            # create and store url in database
            url.id = str(uuid4())
            url.url = str_url
            url.claimant_id = claimant.id
            ## check the URL for malware etc.
            url.unsafe = url_threatcheck.threatcheck(str_url)
            if (url.unsafe != None):
                unsafe_urls = True

            try:
                update_object(url, session)
            except Exception as e:
                logger.error("Could not store urls. Exception: %s",
                             e,
                             exc_info=True)
                raise
        itemurl = ItemURL()
        # get or create itemUrl
        try:
            itemurl = get_itemurl_by_url_and_item_id(url.id, item.id, session)
        except Exception:
            # store itemurl in database
            itemurl.id = str(uuid4())
            itemurl.item_id = item.id
            itemurl.url_id = url.id
            try:
                update_object(itemurl, session)
            except Exception as e:
                logger.error("Could not store itemurls. Exception: %s",
                             e,
                             exc_info=True)
                raise

    if unsafe_urls is True:
        item.status = 'Unsafe'
        update_object(item, session)

    return item