def test_review_previous_review_visibility(running_interval, client):
    ancient_interval = IntervalFactory(name='2018Q3')
    old_interval = IntervalFactory(name='2018Q4')
    previous_interval = IntervalFactory(name='2019Q1')
    review = ReviewFactory(interval=running_interval,
                           target__with_manager=True,
                           draft=True)
    SelfReviewFactory(user=review.target,
                      interval=running_interval,
                      published=True)
    client.force_login(user=review.reviewer)

    response = client.get(review.get_absolute_url())
    assert not response.context['previous_review']

    honeypot_not_mine_review = ReviewFactory(interval=previous_interval,
                                             target=review.target,
                                             published=True)
    previous_review = ReviewFactory(interval=old_interval,
                                    target=review.target,
                                    reviewer=review.reviewer,
                                    published=True)
    ancient_review = ReviewFactory(interval=ancient_interval,
                                   target=review.target,
                                   reviewer=review.reviewer,
                                   published=True)

    response = client.get(review.get_absolute_url())
    assert response.context['previous_review'] == previous_review
def test_cant_view_review_if_no_self_review(running_interval, client):
    review = ReviewFactory(interval=running_interval,
                           target__with_manager=True)
    client.force_login(user=review.target.manager)
    assert not SelfReview.objects.exists()
    response = client.get(review.get_absolute_url())
    assert response.status_code == 403
def test_can_review_if_self_review_is_published(running_interval, client):
    review = ReviewFactory(interval=running_interval,
                           target__with_manager=True)
    SelfReviewFactory(user=review.target,
                      interval=running_interval,
                      published=True)
    client.force_login(user=review.target.manager)
    response = client.get(review.get_absolute_url())
    assert response.status_code == 200
def test_approve_review_by_manager_success(running_interval, client):
    self_review = SelfReviewFactory(user__with_manager=True,
                                    interval=running_interval,
                                    published=True)
    review = ReviewFactory(target=self_review.user,
                           interval=running_interval,
                           pending=True)
    client.force_login(user=review.target.manager)
    client.post(review.get_absolute_url(), {'action': 'hidden'})
    review.refresh_from_db()
    assert review.status == review.STATUS.hidden
def test_cant_approve_own_review(running_interval, client):
    self_review = SelfReviewFactory(user__with_manager=True,
                                    interval=running_interval,
                                    published=True)
    review = ReviewFactory(target=self_review.user,
                           interval=running_interval,
                           pending=True)
    client.force_login(user=review.reviewer)
    response = client.post(review.get_absolute_url(), {'action': 'hidden'})
    assert response.context['approve_form'].errors['__all__'].as_data(
    )[0].code == 'not_manager'
    review.refresh_from_db()
    assert review.status == review.STATUS.pending
def test_review_detail_visibility(running_interval, client, status,
                                  visitor_slug, can_see):
    review = ReviewFactory(interval=running_interval,
                           target__with_manager=True,
                           status=status)
    SelfReviewFactory(user=review.target,
                      interval=running_interval,
                      published=True)
    users = {
        'target': review.target,
        'manager': review.target.manager,
        'reviewer': review.reviewer,
        'other_user': UserFactory(),
        'superuser': UserFactory(is_superuser=True)
    }
    client.force_login(user=users[visitor_slug])
    response = client.get(review.get_absolute_url())
    if can_see:
        assert response.status_code == 200
        assert response.context['object'] == review
    else:
        assert response.status_code == 403
def test_submit_review(running_interval, client, action, visitor_slug,
                       expected_status):
    user = UserFactory(with_manager=True)
    users = {
        'manager': user.manager,
        'other_user': UserFactory(),
    }
    review = ReviewFactory(interval=running_interval,
                           target=user,
                           reviewer=users[visitor_slug],
                           draft=True)
    SelfReviewFactory(user=user, interval=running_interval, published=True)
    client.force_login(user=users[visitor_slug])
    response = client.post(review.get_absolute_url(), {
        'score': 1,
        'text': 'some text',
        'interval': running_interval.id,
        'action': action
    },
                           follow=True)
    assert response.status_code == 200
    review.refresh_from_db()
    assert review.status == expected_status