def test_create_review(item_id, junior_user_id, senior_user_id):

    with Session() as session:

        session = setup_scenarios.create_questions(session)
        session = setup_scenarios.create_levels_junior_and_senior_detectives(session)
        item = Item(id=item_id, item_type_id="Type1")
        session.add(item)
        session.commit()        

        # Create junior review
        event = event_creator.get_create_review_event(junior_user_id, item_id)
        response = create_review.create_review(event, None)
        assert response['statusCode'] == 201
        reviews = session.query(Review).all()
        assert len(reviews) == 1
        review = Review()
        review = reviews[0]
        assert review.user_id == junior_user_id
        assert review.item_id == item_id
        question_ids = []
        for answer in review.review_answers:
            question_ids.append(answer.review_question_id)

        assert "other_type" not in question_ids
        parent_question_counter = 0
        for answer in review.review_answers:
            if answer.review_question.max_children > 0:
                child_questions = session.query(ReviewQuestion).filter(
                    ReviewQuestion.parent_question_id == answer.review_question_id).all()
                for child_question in child_questions:
                    assert child_question.id in question_ids
            if answer.review_question.parent_question_id is None:
                parent_question_counter += 1

        assert parent_question_counter == 6

        event = event_creator.get_create_review_event(senior_user_id, item_id)
        response = create_review.create_review(event, None)
        assert response['statusCode'] == 201
        reviews = session.query(Review).all()
        assert len(reviews) == 2
        reviews = session.query(Review).filter(
            Review.user_id == senior_user_id).all()
        assert len(reviews) == 1
        review = reviews[0]
        assert review.user_id == senior_user_id
        assert review.item_id == item_id
        for answer in review.review_answers:
            assert answer.review_question_id in question_ids
def test_create_review(session, item_id, junior_user_id, senior_user_id):
    # Create junior review
    event = event_creator.get_create_review_event(junior_user_id, item_id)
    response = create_review.create_review(event, None, True, session)
    assert response['statusCode'] == 201
    reviews = session.query(Review).all()
    assert len(reviews) == 1
    review = Review()
    review = reviews[0]
    assert review.user_id == junior_user_id
    assert review.item_id == item_id
    question_ids = []
    for answer in review.review_answers:
        question_ids.append(answer.review_question_id)

    assert "other_type" not in question_ids
    parent_question_counter = 0
    for answer in review.review_answers:
        if answer.review_question.max_children > 0:
            child_questions = session.query(ReviewQuestion).filter(
                ReviewQuestion.parent_question_id ==
                answer.review_question_id).all()
            for child_question in child_questions:
                assert child_question.id in question_ids
        if answer.review_question.parent_question_id is None:
            parent_question_counter += 1

    assert parent_question_counter == 6

    event = event_creator.get_create_review_event(senior_user_id, item_id)
    response = create_review.create_review(event, None, True, session)
    assert response['statusCode'] == 201
    reviews = session.query(Review).all()
    assert len(reviews) == 2
    reviews = session.query(Review).filter(
        Review.user_id == senior_user_id).all()
    assert len(reviews) == 1
    review = reviews[0]
    assert review.user_id == senior_user_id
    assert review.item_id == item_id
    for answer in review.review_answers:
        assert answer.review_question_id in question_ids
示例#3
0
def test_update_review(session, item_id, junior_user_id, senior_user_id):
    # Create junior review
    event = event_creator.get_create_review_event(junior_user_id, item_id)
    response = create_review.create_review(event, None, True, session)
    assert response['statusCode'] == 201
    reviews = session.query(Review).all()
    assert len(reviews) == 1
    review = Review()
    review = reviews[0]

    # Test 403
    event = event_creator.get_review_event(review, item_id, "in progress",
                                           senior_user_id, 1, session)
    response = update_review.update_review(event, None, True, session)
    assert response['statusCode'] == 403

    event = event_creator.get_review_event(review, item_id, "in progress",
                                           junior_user_id, 1, session)

    response = update_review.update_review(event, None, True, session)
    assert response['statusCode'] == 200
示例#4
0
def test_update_review(item_id, junior_user_id, senior_user_id, monkeypatch):

    monkeypatch.setenv("CORS_ALLOW_ORIGIN", "http://localhost:4200")
    with Session() as session:

        session = setup_scenarios.create_questions(session)
        session = setup_scenarios.create_levels_junior_and_senior_detectives(
            session)
        item = Item(id=item_id, item_type_id="Type1")
        session.add(item)
        session.commit()

        # Create junior review
        event = event_creator.get_create_review_event(junior_user_id, item_id)
        response = create_review.create_review(event, None)
        assert response['statusCode'] == 201
        reviews = session.query(Review).all()
        assert len(reviews) == 1
        review = Review()
        review = reviews[0]

        # Test 403
        event = event_creator.get_review_event(
            review, item_id, "in progress", senior_user_id, 1)

        response = update_review.update_review(event, None)
        assert response['statusCode'] == 403

        event = event_creator.get_review_event(
            review, item_id, "in progress", junior_user_id, 1, tags=['test', 'tag'])

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

        # Test comments
        comments = session.query(Comment).all()[0]
        assert comments.comment == "Test comment"
        assert comments.review_id == review.id
        assert comments.user_id == junior_user_id
        assert comments.status == "published"
        assert comments.is_review_comment == True

        # Test tags
        item_tags = session.query(ItemTag).all()
        assert len(item_tags) == 2
        for item_tag in item_tags:
            assert item_tag.item_id == item.id
            assert item_tag.review_id == review.id
            assert item_tag.tag.tag in ['test', 'tag']

        event = event_creator.get_review_event(
            review, item_id, "in progress", junior_user_id, 1, tags=['test'])
        response = update_review.update_review(event, None)
        assert response['statusCode'] == 200
        item_tags = session.query(ItemTag).all()
        assert len(item_tags) == 1
        assert item_tags[0].item_id == item.id
        assert item_tags[0].review_id == review.id
        assert item_tags[0].tag.tag == 'test'
        response_body = json.loads(response['body'])
        assert 'tags' in response_body
        assert len(response_body['tags']) == 1
        assert response_body['tags'][0] == 'test'

        event = event_creator.get_review_event(
            review, item_id, "in progress", junior_user_id, 1, tags=[])
        response = update_review.update_review(event, None)
        assert response['statusCode'] == 200
        response_body = json.loads(response['body'])
        assert 'tags' in response_body
        assert len(response_body['tags']) == 0

        event = event_creator.get_review_event(
            review, item_id, "in progress", junior_user_id, 1, tags=None)
        response = update_review.update_review(event, None)
        assert response['statusCode'] == 200
        response_body = json.loads(response['body'])
        assert 'tags' in response_body
        assert len(response_body['tags']) == 0

        event = event_creator.get_review_event(
            review, item_id, "closed", junior_user_id, 1, tags=None)
        response = update_review.update_review(event, None)
        assert response['statusCode'] == 200

        # Test not existing review
        fake_review = review
        fake_review.id = "fake"
        event = event_creator.get_review_event(
            fake_review, item_id, "in progress", junior_user_id, 1)

        response = update_review.update_review(event, None)
        assert response['statusCode'] == 404
def test_no_item_found(junior_user_id):
    with Session() as session:
        event = event_creator.get_create_review_event(junior_user_id, "wrong_item")
        response = create_review.create_review(event, None)
        assert response['statusCode'] == 404
def test_bad_request():
    with Session() as session:
        event = {}
        response = create_review.create_review(event, None)
        assert response['statusCode'] == 400
def test_no_item_found(session, item_id, junior_user_id):
    event = event_creator.get_create_review_event(junior_user_id, "wrong_item")
    response = create_review.create_review(event, None, True, session)
    assert response['statusCode'] == 404
def test_bad_request():
    session = get_db_session(True, None)
    event = {}
    response = create_review.create_review(event, None, True, session)
    assert response['statusCode'] == 400
    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