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