def create_item(id, item_type_id) -> Item: item = Item() item.id = id item.item_type_id = item_type_id return item
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
def test_item_model_to_dict_with_reviews(item_id, review_id, review_answer_id, user_id, comment_id): 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='testuser') level = Level(id=1, description='beginner') comment = Comment(id=comment_id, comment='testcomment', is_review_comment=True, user_id=user_id, review_id=review_id) session.add_all([ item, review, review_question, review_answer, user, level, comment ]) session.commit() assert len(item.reviews) == 1 assert item.reviews[0].id == review_id item_dict = item.to_dict(with_reviews=True, with_comments=True) assert item_dict['reviews'] assert len(item_dict['reviews']) == 1 assert item_dict['reviews'][0]['id'] == review_id assert item_dict['reviews'][0]['user'] == user.name assert len(item_dict['reviews'][0]['questions']) == 1 assert 'review_comments' in item_dict assert len(item_dict['review_comments']) == 1 assert item_dict['review_comments'][0]['comment'] == 'testcomment' assert item_dict['review_comments'][0]['user'] == user.name assert len(item_dict['users']) == 1 assert item_dict['users'][0]['username'] == 'testuser' assert item_dict['users'][0]['level_description'] == 'beginner' session.delete(user) session.expire_all() item_dict = item.to_dict(with_reviews=True, with_comments=True) assert item_dict['review_comments'][0]['user'] == None assert item_dict['reviews'][0]['user'] == None
def test_result_score_computation(input: float, expected: int): item = Item() item.result_score = input item_dict = item.to_dict() assert item_dict['result_score'] == expected
def item(): item = Item() item.id = str(uuid4()) item.in_progress_reviews_level_1 = 1 item.in_progress_reviews_level_2 = 1 return item
def item(): item = Item() item.id = str(uuid4()) return item
def item(): item = Item() item.id = str(uuid4()) item.content = "Test" item.result_score = 1.000 return item
def item2(): item = Item() item.id = str(uuid4()) item.status = 'open' return item
def get_item(): item = Item() item.id = str(uuid4()) item.content = "Item Content" item.language = "de" 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 item1(test_item_type: ItemType): item = Item() item.id = str(uuid4()) 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