From fc7abb2baf56f83a60134bb5d0069cbc1cf74e29 Mon Sep 17 00:00:00 2001 From: Andrew Gardener Date: Wed, 3 Jan 2018 11:52:13 -0800 Subject: [PATCH] Prevent students from performing extra comparisons Closes #658 --- compair/api/comparison.py | 7 +++++-- compair/tests/api/test_comparisons.py | 15 ++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/compair/api/comparison.py b/compair/api/comparison.py index ca1f2346a..3d6640981 100644 --- a/compair/api/comparison.py +++ b/compair/api/comparison.py @@ -52,12 +52,17 @@ def get(self, course_uuid, assignment_uuid): message="Comparisons can only be seen here by those enrolled in the course. Please double-check your enrollment in this course.") restrict_user = not allow(MANAGE, assignment) + comparison_count = assignment.completed_comparison_count_for_user(current_user.id) + if not assignment.compare_grace: abort(403, title="Comparisons Unavailable", message="Sorry, the comparison deadline has passed. No comparisons can be done after the deadline.") elif not restrict_user and not assignment.educators_can_compare: abort(403, title="Comparisons Unavailable", message="Only students can currently compare answers for this assignment. To change these settings to include instructors and teaching assistants, edit the assignment.") + elif restrict_user and comparison_count >= assignment.total_comparisons_required: + abort(400, title="Comparisons Completed", + message="More comparisons aren't available, since you've finished your comparisons for this assignment. Good job!") # check if user has a comparison they have not completed yet new_pair = False @@ -98,8 +103,6 @@ def get(self, course_uuid, assignment_uuid): except UnknownPairGeneratorException: abort(500, title="Comparisons Unavailable", message="Generating scored pairs failed, this really shouldn't happen.") - comparison_count = assignment.completed_comparison_count_for_user(current_user.id) - return { 'comparison': marshal(comparison, dataformat.get_comparison(restrict_user)), 'new_pair': new_pair, diff --git a/compair/tests/api/test_comparisons.py b/compair/tests/api/test_comparisons.py index 585b7b7f8..b07f1d85d 100644 --- a/compair/tests/api/test_comparisons.py +++ b/compair/tests/api/test_comparisons.py @@ -84,7 +84,7 @@ def test_submit_comparison_access_control(self): # expected_comparisons = rv.json comparison_submit = self._build_comparison_submit(WinningAnswer.answer1.value) - # test deny access to unenroled users + # test deny access to unenrolled users with self.login(self.data.get_unauthorized_student().username): rv = self.client.post( self.base_url, @@ -229,14 +229,17 @@ def test_get_and_submit_comparison(self, mocked_update_assignment_grades_run, mo users = [self.data.get_authorized_student(), self.data.get_authorized_instructor(), self.data.get_authorized_ta()] for user in users: - max_comparisons = 0 + max_comparisons = self.assignment.number_of_comparisons other_student_answers = 0 valid_answer_uuids = set() for answer in self.data.get_student_answers(): if answer.assignment.id == self.assignment.id and answer.user_id != user.id: other_student_answers += 1 valid_answer_uuids.add(answer.uuid) - max_comparisons = int(other_student_answers * (other_student_answers - 1) / 2) + + # instructors and tas can compare every possible pair + if user.id in [self.data.get_authorized_instructor().id, self.data.get_authorized_ta().id]: + max_comparisons = int(other_student_answers * (other_student_answers - 1) / 2) if user.id == self.data.get_authorized_student().id: for comparison_example in self.data.comparisons_examples: @@ -350,6 +353,12 @@ def test_get_and_submit_comparison(self, mocked_update_assignment_grades_run, mo # all answers has been compared by the user, errors out when trying to get another pair rv = self.client.get(self.base_url) self.assert400(rv) + if user.id == self.data.get_authorized_student().id: + self.assertEqual("Comparisons Completed", rv.json['title']) + self.assertEqual("More comparisons aren't available, since you've finished your comparisons for this assignment. Good job!", rv.json['message']) + else: + self.assertEqual("Comparisons Unavailable", rv.json['title']) + self.assertEqual("You have compared all the currently available answer pairs. Please check back later for more answers.", rv.json['message']) def _validate_comparison_submit(self, comparison_submit, actual_comparison, expected_comparison): self.assertEqual(