Пример #1
0
def test_get_items_by_url():
    os.environ["STAGE"] = "dev"
    with Session() as session:
        item = Item()
        item.content = "Test content"
        item.language = "de"
        item.status = "closed"
        item = item_handler.create_item(item, session)

        item2 = Item()
        item2.content = "Test content2"
        item2.language = "de"
        item2.status = "closed"
        item2 = item_handler.create_item(item2, session)

        url = URL()
        url.id = "url_id"
        url.url = "www.test.de/artikel1"

        item_url = ItemURL()
        item_url.id = "item_url_id"
        item_url.item_id = item.id
        item_url.url_id = url.id

        session.add_all([url, item_url])
        session.commit()

        # Testing get_items without url query param 204
        for event in [{}, {"queryStringParameters": None}]:
            response = get_closed_items(event, None)
            assert response['statusCode'] == 200
            body = json.loads(response['body'])
            assert len(body) == 2

        # Testing get_items with url query param 200
        for url in [url.url, url.url + '?queryparam=shouldbeignored']:
            response = get_closed_items(get_url_event(url), None)
            assert response['statusCode'] == 200
            body = json.loads(response['body'])
            assert len(body) == 1
            assert body[0]['id'] == item.id

        # Testing get_items with url query param 204 - no item with url
        for url in ['sinnloseUrl', 'www.test.de/artikel2', 'www.test.de']:
            response = get_closed_items(get_url_event(url), None)
            assert response['statusCode'] == 204

        # Testing get_items with url query param 204 - item not closed
        item.status = "open"
        session.merge(item)
        session.commit()
        response = get_closed_items(get_url_event("www.test.de/artikel1"),
                                    None)
        assert response['statusCode'] == 204
    def test_tags_and_urls(self):
        with Session() as session:

            session = setup_scenarios.create_levels_junior_and_senior_detectives(
                session)
            session = setup_scenarios.create_questions(session)

            junior_detective1 = user_handler.get_user_by_id("1", session)

            item1 = Item()
            item1.content = "Item 1"
            item1.status = "open"
            item1.item_type_id = "Type1"
            item1 = item_handler.create_item(item1, session)

            url = URL()
            url.id = str(uuid4())
            url.url = 'www.test.com'

            item_url = ItemURL()
            item_url.id = str(uuid4())
            item_url.item_id = item1.id
            item_url.url_id = url.id

            tag = Tag()
            tag.id = str(uuid4())
            tag.tag = "Test Tag"

            item_tag = ItemTag()
            item_tag.id = str(uuid4())
            item_tag.item_id = item1.id
            item_tag.tag_id = tag.id

            session.add_all([url, item_url, tag, item_tag])

            event = event_creator.get_create_review_event(
                junior_detective1.id, item1.id)

            response = get_open_items(event, None)
            assert response['statusCode'] == 200

            body = json.loads(response['body'])
            items = body['items']
            assert len(items) == 1
            urls = items[0]['urls']
            assert len(urls) == 1
            assert urls[0]['url'] == 'www.test.com'

            tags = items[0]['tags']
            assert len(tags) == 1
            assert tags[0] == "Test Tag"
Пример #3
0
def test_get_closed_items():

    # pre-stuff
    os.environ["STAGE"] = "dev"

    with Session() as session:

        context = None

        # create item
        item = Item()
        item.content = "Test content"
        item.language = "de"
        item.status = "closed"
        item = item_handler.create_item(item, session)

        # create events with tags
        event1 = {
            "pathParameters": {
                "item_id": item.id
            },
            "body": json.dumps({"tags": ['C', 'B', 'D']})
        }
        event2 = {
            "pathParameters": {
                "item_id": item.id
            },
            "body": json.dumps({"tags": ['B', 'C']})
        }
        event3 = {
            "pathParameters": {
                "item_id": item.id
            },
            "body": json.dumps({"tags": ['A', 'B']})
        }

        # post tags
        GetTags.post_tags_for_item(event1, context)
        GetTags.post_tags_for_item(event2, context)
        GetTags.post_tags_for_item(event3, context)

        # Check if tags are sorted by number of mentions
        response = get_closed_items(event1, context)
        body = response['body']
        tags = json.loads(body)[0]['tags']
        assert tags in [['B', 'C', 'A', 'D'], ['B', 'C', 'D', 'A']]
Пример #4
0
def test_verification_process_best_case(monkeypatch):
    monkeypatch.setenv("STAGE", "dev")
    monkeypatch.setenv("MOTO", "1")
    monkeypatch.setenv("CORS_ALLOW_ORIGIN", "http://*****:*****@test.de'
        submission.status = 'confirmed'
        session.add(submission)
        session.commit()

        items = item_handler.get_all_items(session)
        assert len(items) == 1

        # Junior detectives accepting item
        jr1 = review_handler.create_review(junior_detective1, item, session)
        assert item.open_reviews_level_1 == 4
        assert item.in_progress_reviews_level_1 == 1

        jr2 = review_handler.create_review(junior_detective2, item, session)
        assert item.open_reviews_level_1 == 4
        assert item.in_progress_reviews_level_1 == 2

        jr3 = review_handler.create_review(junior_detective3, item, session)
        assert item.open_reviews_level_1 == 4
        assert item.in_progress_reviews_level_1 == 3

        jr4 = review_handler.create_review(junior_detective4, item, session)
        assert item.open_reviews_level_1 == 4
        assert item.in_progress_reviews_level_1 == 4

        with pytest.raises(Exception):
            review_handler.create_review(junior_detective5, item, session)

        # Senior detectives accepting item
        sr1 = review_handler.create_review(senior_detective1, item, session)
        assert item.open_reviews_level_2 == 4
        assert item.in_progress_reviews_level_2 == 1

        sr2 = review_handler.create_review(senior_detective2, item, session)
        assert item.open_reviews_level_2 == 4
        assert item.in_progress_reviews_level_2 == 2

        sr3 = review_handler.create_review(senior_detective3, item, session)
        assert item.open_reviews_level_2 == 4
        assert item.in_progress_reviews_level_2 == 3

        sr4 = review_handler.create_review(senior_detective4, item, session)
        assert item.open_reviews_level_2 == 4
        assert item.in_progress_reviews_level_2 == 4

        with pytest.raises(Exception):
            review_handler.create_review(senior_detective5, item, session)

        pairs = review_pair_handler.get_review_pairs_by_item(item.id, session)
        assert len(pairs) == 4

        # Detectives reviewing item
        reviews = [jr1, jr2, jr3, jr4, sr1, sr2, sr3, sr4]

        for review in reviews:
            event = event_creator.get_review_event(
                review, item.id, "in progress", review.user_id, 1)
            response = update_review(event, None)
            assert response['statusCode'] == 200
            event = event_creator.get_review_event(
                review, item.id, "closed", review.user_id, 1)
            response = update_review(event, None)
            assert response['statusCode'] == 200

        # reload object instead of refreshing the session
        item = item_handler.get_item_by_id(item.id, session)
        item = item_handler.update_item_warning_tags(item, session)
        assert item.status == 'closed'
        assert item.in_progress_reviews_level_1 == 0
        assert item.in_progress_reviews_level_2 == 0
        assert item.open_reviews_level_1 == 0
        assert item.open_reviews_level_2 == 0
        assert item.open_reviews == 0
        assert item.close_timestamp is not None
        assert item.warning_tags_calculated == True
        assert item.result_score == 1

        item_dict = item.to_dict(with_warnings=True)
        assert 'warning_tags' in item_dict
        assert len(item_dict['warning_tags']) > 0
        assert 'text' in item_dict['warning_tags'][0]
        assert 'icon' in item_dict['warning_tags'][0]
        assert item_dict['result_score'] == 0
        session.expire_all()
Пример #5
0
def test_verification_process_worst_case(monkeypatch):
    monkeypatch.setenv("CORS_ALLOW_ORIGIN", "http://localhost:4200")

    with Session() as session:
        session = setup_scenarios.create_levels_junior_and_senior_detectives(
            session)
        session = setup_scenarios.create_questions(session)

        junior_detective1 = user_handler.get_user_by_id("1", session)
        junior_detective2 = user_handler.get_user_by_id("2", session)
        junior_detective3 = user_handler.get_user_by_id("3", session)
        junior_detective4 = user_handler.get_user_by_id("4", session)
        junior_detective5 = user_handler.get_user_by_id("5", session)

        senior_detective1 = user_handler.get_user_by_id("11", session)
        senior_detective2 = user_handler.get_user_by_id("12", session)
        senior_detective3 = user_handler.get_user_by_id("13", session)
        senior_detective4 = user_handler.get_user_by_id("14", session)
        senior_detective5 = user_handler.get_user_by_id("15", session)

        users = user_handler.get_all_users(session)
        assert len(users) == 10

        # Creating an item
        item = Item()
        item.content = "This item needs to be checked"
        item.status = 'open'
        item.item_type_id = "Type1"
        item = item_handler.create_item(item, session)

        items = item_handler.get_all_items(session)
        assert len(items) == 1

        # Junior detectives accepting item
        jr1 = review_handler.create_review(junior_detective1, item, session)
        assert item.open_reviews_level_1 == 4
        assert item.in_progress_reviews_level_1 == 1

        jr2 = review_handler.create_review(junior_detective2, item, session)
        assert item.open_reviews_level_1 == 4
        assert item.in_progress_reviews_level_1 == 2

        jr3 = review_handler.create_review(junior_detective3, item, session)
        assert item.open_reviews_level_1 == 4
        assert item.in_progress_reviews_level_1 == 3

        jr4 = review_handler.create_review(junior_detective4, item, session)
        assert item.open_reviews_level_1 == 4
        assert item.in_progress_reviews_level_1 == 4

        with pytest.raises(Exception):
            review_handler.create_review(junior_detective5, item, session)

        # Senior detectives accepting item
        sr1 = review_handler.create_review(senior_detective1, item, session)
        assert item.open_reviews_level_2 == 4
        assert item.in_progress_reviews_level_2 == 1

        sr2 = review_handler.create_review(senior_detective2, item, session)
        assert item.open_reviews_level_2 == 4
        assert item.in_progress_reviews_level_2 == 2

        sr3 = review_handler.create_review(senior_detective3, item, session)
        assert item.open_reviews_level_2 == 4
        assert item.in_progress_reviews_level_2 == 3

        sr4 = review_handler.create_review(senior_detective4, item, session)
        assert item.open_reviews_level_2 == 4
        assert item.in_progress_reviews_level_2 == 4

        with pytest.raises(Exception):
            review_handler.create_review(senior_detective5, item, session)

        pairs = review_pair_handler.get_review_pairs_by_item(item.id, session)
        assert len(pairs) == 4

        # Detective without review in progress trying to get question
        junior_reviews = [jr1, jr2, jr3, jr4]
        senior_reviews = [sr1, sr2, sr3, sr4]
        for review in junior_reviews:
            event = event_creator.get_review_event(
                review, item.id, "in progress", review.user_id, 1)
            response = update_review(event, None)
            assert response['statusCode'] == 200
            event = event_creator.get_review_event(
                review, item.id, "closed", review.user_id, 1)
            response = update_review(event, None)
            assert response['statusCode'] == 200

        for review in senior_reviews:
            event = event_creator.get_review_event(
                review, item.id, "in progress", review.user_id, 4)
            response = update_review(event, None)
            assert response['statusCode'] == 200
            event = event_creator.get_review_event(
                review, item.id, "closed", review.user_id, 4)
            response = update_review(event, None)
            assert response['statusCode'] == 200

        # reload object instead of refreshing the session
        item = item_handler.get_item_by_id(item.id, session)
        assert item.status == 'open'
        assert item.in_progress_reviews_level_1 == 0
        assert item.in_progress_reviews_level_2 == 0
        assert item.open_reviews_level_1 == 4
        assert item.open_reviews_level_2 == 4
        assert item.open_reviews == 4

        session.expire_all()
Пример #6
0
def test_verification_process_best_case(monkeypatch):
    monkeypatch.setenv("STAGE", "dev")
    monkeypatch.setenv("DBNAME", "Test")
    conn = boto3.client("ses", region_name="eu-central-1")
    conn.verify_email_identity(EmailAddress="*****@*****.**")

    session = get_db_session(True, None)
    session = setup_scenarios.create_levels_junior_and_senior_detectives(
        session)
    session = setup_scenarios.create_questions(session)

    junior_detective1 = user_handler.get_user_by_id("1", True, session)
    junior_detective2 = user_handler.get_user_by_id("2", True, session)
    junior_detective3 = user_handler.get_user_by_id("3", True, session)
    junior_detective4 = user_handler.get_user_by_id("4", True, session)
    junior_detective5 = user_handler.get_user_by_id("5", True, session)

    senior_detective1 = user_handler.get_user_by_id("11", True, session)
    senior_detective2 = user_handler.get_user_by_id("12", True, session)
    senior_detective3 = user_handler.get_user_by_id("13", True, session)
    senior_detective4 = user_handler.get_user_by_id("14", True, session)
    senior_detective5 = user_handler.get_user_by_id("15", True, session)

    users = user_handler.get_all_users(True, session)
    assert len(users) == 10

    # Creating an item

    item = Item()
    item.content = "This item needs to be checked"
    item.item_type_id = "Type1"
    item = item_handler.create_item(item, True, session)
    assert item.in_progress_reviews_level_1 == 0
    assert item.open_reviews_level_1 == 4
    assert item.status == 'unconfirmed'

    item.status = 'open'
    session.merge(item)

    submission = Submission()
    submission.id = 'Submission 1'
    submission.item_id = item.id
    submission.mail = '*****@*****.**'
    submission.status = 'confirmed'
    session.add(submission)
    session.commit()

    items = item_handler.get_all_items(True, session)
    assert len(items) == 1

    # Junior detectives accepting item
    jr1 = review_handler.create_review(junior_detective1, item, True, session)
    assert item.open_reviews_level_1 == 4
    assert item.in_progress_reviews_level_1 == 1

    jr2 = review_handler.create_review(junior_detective2, item, True, session)
    assert item.open_reviews_level_1 == 4
    assert item.in_progress_reviews_level_1 == 2

    jr3 = review_handler.create_review(junior_detective3, item, True, session)
    assert item.open_reviews_level_1 == 4
    assert item.in_progress_reviews_level_1 == 3

    jr4 = review_handler.create_review(junior_detective4, item, True, session)
    assert item.open_reviews_level_1 == 4
    assert item.in_progress_reviews_level_1 == 4

    with pytest.raises(Exception):
        review_handler.create_review(junior_detective5, item, True, session)

    # Senior detectives accepting item
    sr1 = review_handler.create_review(senior_detective1, item, True, session)
    assert item.open_reviews_level_2 == 4
    assert item.in_progress_reviews_level_2 == 1

    sr2 = review_handler.create_review(senior_detective2, item, True, session)
    assert item.open_reviews_level_2 == 4
    assert item.in_progress_reviews_level_2 == 2

    sr3 = review_handler.create_review(senior_detective3, item, True, session)
    assert item.open_reviews_level_2 == 4
    assert item.in_progress_reviews_level_2 == 3

    sr4 = review_handler.create_review(senior_detective4, item, True, session)
    assert item.open_reviews_level_2 == 4
    assert item.in_progress_reviews_level_2 == 4

    with pytest.raises(Exception):
        review_handler.create_review(senior_detective5, item, True, session)

    pairs = review_pair_handler.get_review_pairs_by_item(
        item.id, True, session)
    assert len(pairs) == 4

    # Detectives reviewing item
    reviews = [jr1, jr2, jr3, jr4, sr1, sr2, sr3, sr4]
    for review in reviews:
        event = event_creator.get_review_event(review, item.id, "in progress",
                                               review.user_id, 1, session)
        response = update_review(event, None, True, session)
        assert response['statusCode'] == 200
        session.refresh(review)
        event = event_creator.get_review_event(review, item.id, "closed",
                                               review.user_id, 1, session)
        response = update_review(event, None, True, session)
        assert response['statusCode'] == 200

    assert item.status == 'closed'
    assert item.in_progress_reviews_level_1 == 0
    assert item.in_progress_reviews_level_2 == 0
    assert item.open_reviews_level_1 == 0
    assert item.open_reviews_level_2 == 0
    assert item.open_reviews == 0
    assert item.close_timestamp is not None

    send_quota = conn.get_send_quota()
    sent_count = int(send_quota["SentLast24Hours"])
    assert sent_count == 1
    session.refresh(submission)
    assert not submission.mail
Пример #7
0
    def test_get_open_items_for_user(self, monkeypatch):
        monkeypatch.setenv("DBNAME", "Test")

        session = get_db_session(True, None)

        session = setup_scenarios.create_levels_junior_and_senior_detectives(
            session)
        session = setup_scenarios.create_questions(session)

        junior_detective1 = user_handler.get_user_by_id("1", True, session)
        junior_detective2 = user_handler.get_user_by_id("2", True, session)
        junior_detective3 = user_handler.get_user_by_id("3", True, session)
        junior_detective4 = user_handler.get_user_by_id("4", True, session)
        junior_detective5 = user_handler.get_user_by_id("5", True, session)

        senior_detective1 = user_handler.get_user_by_id("11", True, session)

        # Creating 5 items

        item1 = Item()
        item1.content = "Item 1"
        item1.status = "open"
        item1.item_type_id = "Type1"
        item1 = item_handler.create_item(item1, True, session)

        item2 = Item()
        item2.content = "Item 2"
        item2.status = "open"
        item2.item_type_id = "Type1"
        item2 = item_handler.create_item(item2, True, session)

        item3 = Item()
        item3.content = "Item 3"
        item3.status = "open"
        item3.item_type_id = "Type1"
        item3 = item_handler.create_item(item3, True, session)

        item4 = Item()
        item4.content = "Item 4"
        item4.status = "open"
        item4.item_type_id = "Type1"
        item4 = item_handler.create_item(item4, True, session)

        item5 = Item()
        item5.content = "Item 5"
        item5.status = "open"
        item5.item_type_id = "Type1"
        item5 = item_handler.create_item(item5, True, session)

        items = item_handler.get_all_items(True, session)
        assert len(items) == 5

        open_items_for_senior = item_handler.get_open_items_for_user(
            senior_detective1, 5, True, session)['items']
        assert len(open_items_for_senior) == 5

        open_items_for_junior = item_handler.get_open_items_for_user(
            junior_detective1, 5, True, session)['items']
        assert len(open_items_for_junior) == 5

        # JuniorDetective 1 accepting item 1
        jr1 = review_handler.create_review(junior_detective1, item1, True,
                                           session)
        open_item_after_accept = item_handler.get_open_items_for_user(
            junior_detective1, 5, True, session)['items']
        assert len(open_item_after_accept) == 1

        item1 = item_handler.get_item_by_id(item1.id, True, session)
        assert item1.in_progress_reviews_level_1 == 1

        # Accepting event again should not create a new review
        review_handler.create_review(junior_detective1, item1, True, session)
        # app.create_review(accept_event, None, True, session)
        item1 = item_handler.get_item_by_id(item1.id, True, session)
        assert item1.in_progress_reviews_level_1 == 1

        # JuniorDetective 1 finishing review
        event = event_creator.get_review_event(jr1, item1.id, "in progress",
                                               jr1.user_id, 1, session)
        response = update_review(event, None, True, session)
        assert response['statusCode'] == 200
        event = event_creator.get_review_event(jr1, item1.id, "closed",
                                               jr1.user_id, 1, session)
        response = update_review(event, None, True, session)
        assert response['statusCode'] == 200

        # For JuniorDetective1 only 4 cases should be available
        open_items_after_submission = item_handler.get_open_items_for_user(
            junior_detective1, 5, True, session)['items']
        assert len(open_items_after_submission) == 4

        open_items_limit_3 = item_handler.get_open_items_for_user(
            junior_detective1, 3, True, session)['items']
        assert len(open_items_limit_3) == 3

        open_items_after_other_review = item_handler.get_open_items_for_user(
            junior_detective4, 5, True, session)['items']
        assert len(open_items_after_other_review) == 5

        # 4 Junior Detectives reviewing Item 2
        jr1 = review_handler.create_review(junior_detective1, item2, True,
                                           session)
        jr2 = review_handler.create_review(junior_detective2, item2, True,
                                           session)
        jr3 = review_handler.create_review(junior_detective3, item2, True,
                                           session)
        jr4 = review_handler.create_review(junior_detective4, item2, True,
                                           session)

        reviews = [jr1, jr2, jr3, jr4]
        for review in reviews:
            event = event_creator.get_review_event(review, item2.id,
                                                   "in progress",
                                                   review.user_id, 1, session)
            response = update_review(event, None, True, session)
            assert response['statusCode'] == 200
            event = event_creator.get_review_event(review, item2.id, "closed",
                                                   review.user_id, 1, session)
            response = update_review(event, None, True, session)
            assert response['statusCode'] == 200

        # 4 Cases should be available for Detective 5

        open_items_after_other_review = item_handler.get_open_items_for_user(
            junior_detective5, 5, True, session)['items']
        assert len(open_items_after_other_review) == 4

        # 5 cases should be available for senior
        open_items_for_senior = item_handler.get_open_items_for_user(
            senior_detective1, 5, True, session)['items']
        assert len(open_items_for_senior) == 5

        # Senior detective accepting item 1
        sr1 = review_handler.create_review(senior_detective1, item1, True,
                                           session)

        open_item_after_accept = item_handler.get_open_items_for_user(
            senior_detective1, 5, True, session)['items']
        assert len(open_item_after_accept) == 1

        # Senior detective finishing review
        event = event_creator.get_review_event(sr1, item1.id, "in progress",
                                               sr1.user_id, 1, session)
        response = update_review(event, None, True, session)
        assert response['statusCode'] == 200
        event = event_creator.get_review_event(sr1, item1.id, "closed",
                                               sr1.user_id, 1, session)
        response = update_review(event, None, True, session)
        assert response['statusCode'] == 200

        # For SeniorDetective1 only 4 cases should be available
        open_items_after_submission = item_handler.get_open_items_for_user(
            senior_detective1, 5, True, session)['items']
        assert len(open_items_after_submission) == 4

        # SeniorDetective 1 accepting item 3
        sr1 = review_handler.create_review(senior_detective1, item3, True,
                                           session)
        open_item_after_accept = item_handler.get_open_items_for_user(
            senior_detective1, 5, True, session)['items']
        assert len(open_item_after_accept) == 1

        item3 = item_handler.get_item_by_id(item3.id, True, session)
        assert item3.in_progress_reviews_level_2 == 1

        # Accepting event again should not create a new review
        review_handler.create_review(senior_detective1, item3, True, session)
        item3 = item_handler.get_item_by_id(item3.id, True, session)
        assert item3.in_progress_reviews_level_2 == 1

        # SeniorDetective 1 finishing review
        event = event_creator.get_review_event(sr1, item3.id, "in progress",
                                               sr1.user_id, 1, session)
        response = update_review(event, None, True, session)
        assert response['statusCode'] == 200
        event = event_creator.get_review_event(sr1, item3.id, "closed",
                                               sr1.user_id, 1, session)
        response = update_review(event, None, True, session)
        assert response['statusCode'] == 200

        open_items_for_senior = item_handler.get_open_items_for_user(
            senior_detective1, 5, True, session)['items']
        assert len(open_items_for_senior) == 3

        event = {
            "requestContext": {
                "identity": {
                    "cognitoAuthenticationProvider":
                    "...CognitoSignIn:{}".format(senior_detective1.id)
                }
            }
        }
        response = get_open_items(event, None, True, session)
        assert 'is_open_review' in response['headers']
        assert response['headers']['is_open_review'] == "False"
        body = json.loads(response['body'])
        assert len(body) == 3
    def test_open_items_sorting(self):

        with Session() as session:

            session = setup_scenarios.create_levels_junior_and_senior_detectives(
                session)
            session = setup_scenarios.create_questions(session)

            junior_detective1 = user_handler.get_user_by_id("1", session)

            senior_detective1 = user_handler.get_user_by_id("11", session)

            # Creating 5 items

            item1 = Item()
            item1.content = "Item 1"
            item1.status = "open"
            item1.item_type_id = "Type1"
            item1.open_timestamp = datetime.now() + timedelta(seconds=1)
            item1 = item_handler.create_item(item1, session)

            item2 = Item()
            item2.content = "Item 2"
            item2.status = "open"
            item2.item_type_id = "Type1"
            item2.open_timestamp = datetime.now() + timedelta(seconds=2)
            item2 = item_handler.create_item(item2, session)

            item3 = Item()
            item3.content = "Item 3"
            item3.status = "open"
            item3.item_type_id = "Type1"
            item3.open_timestamp = datetime.now() + timedelta(seconds=3)
            item3 = item_handler.create_item(item3, session)

            item4 = Item()
            item4.content = "Item 4"
            item4.status = "open"
            item4.item_type_id = "Type1"
            item4.open_timestamp = datetime.now() + timedelta(seconds=4)
            item4 = item_handler.create_item(item4, session)

            item5 = Item()
            item5.content = "Item 5"
            item5.status = "open"
            item5.item_type_id = "Type1"
            item5.open_timestamp = datetime.now() + timedelta(seconds=5)
            item5 = item_handler.create_item(item5, session)

            item6 = Item()
            item6.content = "Item 6"
            item6.status = "open"
            item6.item_type_id = "Type1"
            item6.open_timestamp = datetime.now() + timedelta(seconds=6)
            item6 = item_handler.create_item(item6, session)

            items = item_handler.get_all_items(session)
            assert len(items) == 6

            open_items_for_senior = item_handler.get_open_items_for_user(
                senior_detective1, 5, session)['items']
            assert len(open_items_for_senior) == 5
            assert all(x in open_items_for_senior
                       for x in [item1, item2, item4, item5, item6])

            open_items_for_junior = item_handler.get_open_items_for_user(
                junior_detective1, 5, session)['items']
            assert all(x in open_items_for_junior
                       for x in [item1, item2, item4, item5, item6])

            # When item3 needs less reviewed it should be prioritized over item4
            item3.open_reviews_level_1 = 3
            item3.open_reviews_level_2 = 3
            session.merge(item3)
            session.commit

            open_items_for_senior = item_handler.get_open_items_for_user(
                senior_detective1, 5, session)['items']
            assert len(open_items_for_senior) == 5
            assert all(x in open_items_for_senior
                       for x in [item1, item2, item3, item5, item6])

            open_items_for_junior = item_handler.get_open_items_for_user(
                junior_detective1, 5, session)['items']
            assert all(x in open_items_for_junior
                       for x in [item1, item2, item3, item5, item6])
    def test_get_open_items_for_user(self, monkeypatch):
        monkeypatch.setenv("CORS_ALLOW_ORIGIN", "http://localhost:4200")

        with Session() as session:

            session = setup_scenarios.create_levels_junior_and_senior_detectives(
                session)
            session = setup_scenarios.create_questions(session)

            junior_detective1 = user_handler.get_user_by_id("1", session)
            junior_detective2 = user_handler.get_user_by_id("2", session)
            junior_detective3 = user_handler.get_user_by_id("3", session)
            junior_detective4 = user_handler.get_user_by_id("4", session)
            junior_detective5 = user_handler.get_user_by_id("5", session)

            senior_detective1 = user_handler.get_user_by_id("11", session)

            # Creating 5 items

            item1 = Item()
            item1.content = "Item 1"
            item1.status = "open"
            item1.item_type_id = "Type1"
            item1.open_timestamp = datetime.now() + timedelta(seconds=1)
            item1 = item_handler.create_item(item1, session)

            url = URL()
            url.id = str(uuid4())
            url.url = 'www.test.com'

            item_url = ItemURL()
            item_url.id = str(uuid4())
            item_url.item_id = item1.id
            item_url.url_id = url.id

            session.add_all([url, item_url])

            item2 = Item()
            item2.content = "Item 2"
            item2.status = "open"
            item2.item_type_id = "Type1"
            item2.open_timestamp = datetime.now() + timedelta(seconds=2)
            item2 = item_handler.create_item(item2, session)

            item3 = Item()
            item3.content = "Item 3"
            item3.status = "open"
            item3.item_type_id = "Type1"
            item3.open_timestamp = datetime.now() + timedelta(seconds=3)
            item3 = item_handler.create_item(item3, session)

            item4 = Item()
            item4.content = "Item 4"
            item4.status = "open"
            item4.item_type_id = "Type1"
            item4.open_timestamp = datetime.now() + timedelta(seconds=4)
            item4 = item_handler.create_item(item4, session)

            item5 = Item()
            item5.content = "Item 5"
            item5.status = "open"
            item5.item_type_id = "Type1"
            item5.open_timestamp = datetime.now() + timedelta(seconds=5)
            item5 = item_handler.create_item(item5, session)

            items = item_handler.get_all_items(session)
            assert len(items) == 5

            open_items_for_senior = item_handler.get_open_items_for_user(
                senior_detective1, 5, session)['items']
            assert len(open_items_for_senior) == 5

            open_items_for_junior = item_handler.get_open_items_for_user(
                junior_detective1, 5, session)['items']
            assert len(open_items_for_junior) == 5

            # JuniorDetective 1 accepting item 1
            jr1 = review_handler.create_review(junior_detective1, item1,
                                               session)
            open_item_after_accept = item_handler.get_open_items_for_user(
                junior_detective1, 5, session)['items']
            assert len(open_item_after_accept) == 1

            item1 = item_handler.get_item_by_id(item1.id, session)
            assert item1.in_progress_reviews_level_1 == 1

            # Accepting event again should not create a new review
            review_handler.create_review(junior_detective1, item1, session)
            # app.create_review(accept_event, None, True, session)
            item1 = item_handler.get_item_by_id(item1.id, session)
            assert item1.in_progress_reviews_level_1 == 1

            # JuniorDetective 1 finishing review
            event = event_creator.get_review_event(jr1, item1.id,
                                                   "in progress", jr1.user_id,
                                                   1)
            response = update_review(event, None)
            assert response['statusCode'] == 200
            event = event_creator.get_review_event(jr1, item1.id, "closed",
                                                   jr1.user_id, 1)
            response = update_review(event, None)
            assert response['statusCode'] == 200

            # For JuniorDetective1 only 4 cases should be available
            open_items_after_submission = item_handler.get_open_items_for_user(
                junior_detective1, 5, session)['items']
            assert len(open_items_after_submission) == 4

            open_items_limit_3 = item_handler.get_open_items_for_user(
                junior_detective1, 3, session)['items']
            assert len(open_items_limit_3) == 3

            open_items_after_other_review = item_handler.get_open_items_for_user(
                junior_detective4, 5, session)['items']
            assert len(open_items_after_other_review) == 5
            # 4 Junior Detectives reviewing Item 2
            item2 = item_handler.get_item_by_id(item2.id, session)
            jr1 = review_handler.create_review(junior_detective1, item2,
                                               session)
            jr2 = review_handler.create_review(junior_detective2, item2,
                                               session)
            jr3 = review_handler.create_review(junior_detective3, item2,
                                               session)
            jr4 = review_handler.create_review(junior_detective4, item2,
                                               session)

            reviews = [jr1, jr2, jr3, jr4]
            for review in reviews:
                event = event_creator.get_review_event(review, item2.id,
                                                       "in progress",
                                                       review.user_id, 1)
                response = update_review(event, None)
                assert response['statusCode'] == 200
                event = event_creator.get_review_event(review, item2.id,
                                                       "closed",
                                                       review.user_id, 1)
                response = update_review(event, None)
                assert response['statusCode'] == 200

            # 4 Cases should be available for Detective 5
            open_items_after_other_review = item_handler.get_open_items_for_user(
                junior_detective5, 5, session)['items']
            assert len(open_items_after_other_review) == 4

            # 5 cases should be available for senior
            open_items_for_senior = item_handler.get_open_items_for_user(
                senior_detective1, 5, session)['items']
            assert len(open_items_for_senior) == 5

            # Senior detective accepting item 1
            sr1 = review_handler.create_review(senior_detective1, item1,
                                               session)

            open_item_after_accept = item_handler.get_open_items_for_user(
                senior_detective1, 5, session)['items']
            assert len(open_item_after_accept) == 1

            # Senior detective finishing review
            event = event_creator.get_review_event(sr1, item1.id,
                                                   "in progress", sr1.user_id,
                                                   1)
            response = update_review(event, None)
            assert response['statusCode'] == 200
            event = event_creator.get_review_event(sr1, item1.id, "closed",
                                                   sr1.user_id, 1)
            response = update_review(event, None)
            assert response['statusCode'] == 200

            # For SeniorDetective1 only 4 cases should be available
            open_items_after_submission = item_handler.get_open_items_for_user(
                senior_detective1, 5, session)['items']
            assert len(open_items_after_submission) == 4

            # SeniorDetective 1 accepting item 3
            item3 = item_handler.get_item_by_id(item3.id, session)
            sr1 = review_handler.create_review(senior_detective1, item3,
                                               session)
            open_item_after_accept = item_handler.get_open_items_for_user(
                senior_detective1, 5, session)['items']
            assert len(open_item_after_accept) == 1

            item3 = item_handler.get_item_by_id(item3.id, session)
            assert item3.in_progress_reviews_level_2 == 1

            # Accepting event again should not create a new review
            review_handler.create_review(senior_detective1, item3, session)
            item3 = item_handler.get_item_by_id(item3.id, session)
            assert item3.in_progress_reviews_level_2 == 1

            # SeniorDetective 1 finishing review
            event = event_creator.get_review_event(sr1, item3.id,
                                                   "in progress", sr1.user_id,
                                                   1)
            response = update_review(event, None)
            assert response['statusCode'] == 200
            event = event_creator.get_review_event(sr1, item3.id, "closed",
                                                   sr1.user_id, 1)
            response = update_review(event, None)
            assert response['statusCode'] == 200

            # Accepting closed item again should not create a new review
            event = event_creator.get_create_review_event(
                senior_detective1.id, item3.id)
            response = create_review.create_review(event, None)
            assert response['statusCode'] == 500

            open_items_for_senior = item_handler.get_open_items_for_user(
                senior_detective1, 5, session)['items']
            assert len(open_items_for_senior) == 3

            event = {
                "requestContext": {
                    "identity": {
                        "cognitoAuthenticationProvider":
                        "...CognitoSignIn:{}".format(senior_detective1.id)
                    }
                }
            }
            response = get_open_items(event, None)
            # assert 'is_open_review' in response['headers']
            # assert response['headers']['is_open_review'] == "False"
            body = json.loads(response['body'])
            assert 'is_open_review' in body
            assert body['is_open_review'] is False
            assert len(body['items']) == 3