def get(self, request, course_id, checkpoint_name): """ Display the view for face photo submission""" # Check the in-course re-verification is enabled or not incourse_reverify_enabled = InCourseReverificationConfiguration.current().enabled if not incourse_reverify_enabled: raise Http404 user = request.user course_key = CourseKey.from_string(course_id) course = modulestore().get_course(course_key) if course is None: raise Http404 checkpoint = VerificationCheckpoint.get_verification_checkpoint(course_key, checkpoint_name) if checkpoint is None: raise Http404 init_verification = SoftwareSecurePhotoVerification.get_initial_verification(user) if not init_verification: return redirect(reverse('verify_student_verify_later', kwargs={'course_id': unicode(course_key)})) # emit the reverification event self._track_reverification_events( EVENT_NAME_USER_ENTERED_INCOURSE_REVERIFY_VIEW, user.id, course_id, checkpoint_name ) context = { 'course_key': unicode(course_key), 'course_name': course.display_name_with_default, 'checkpoint_name': checkpoint_name, 'platform_name': settings.PLATFORM_NAME, } return render_to_response("verify_student/incourse_reverify.html", context)
def get(self, request, course_id, checkpoint_name, usage_id): """ Display the view for face photo submission""" # Check the in-course re-verification is enabled or not incourse_reverify_enabled = InCourseReverificationConfiguration.current().enabled if not incourse_reverify_enabled: raise Http404 user = request.user course_key = CourseKey.from_string(course_id) course = modulestore().get_course(course_key) if course is None: raise Http404 checkpoint = VerificationCheckpoint.get_verification_checkpoint(course_key, checkpoint_name) if checkpoint is None: raise Http404 init_verification = SoftwareSecurePhotoVerification.get_initial_verification(user) if not init_verification: return redirect(reverse('verify_student_verify_later', kwargs={'course_id': unicode(course_key)})) # emit the reverification event self._track_reverification_events( EVENT_NAME_USER_ENTERED_INCOURSE_REVERIFY_VIEW, user.id, course_id, checkpoint_name ) context = { 'course_key': unicode(course_key), 'course_name': course.display_name_with_default, 'checkpoint_name': checkpoint_name, 'platform_name': settings.PLATFORM_NAME, 'usage_id': usage_id } return render_to_response("verify_student/incourse_reverify.html", context)
def post(self, request, course_id, checkpoint_name): """Submits the re-verification attempt to SoftwareSecure Args: request(HttpRequest): HttpRequest object course_id(str): Course Id checkpoint_name(str): Checkpoint name Returns: HttpResponse with status_code 400 if photo is missing or any error or redirect to verify_student_verify_later url if initial verification doesn't exist otherwise HttpsResponse with status code 200 """ # Check the in-course re-verification is enabled or not incourse_reverify_enabled = InCourseReverificationConfiguration.current().enabled if not incourse_reverify_enabled: raise Http404 user = request.user course_key = CourseKey.from_string(course_id) course = modulestore().get_course(course_key) checkpoint = VerificationCheckpoint.get_verification_checkpoint(course_key, checkpoint_name) if checkpoint is None: log.error("Checkpoint is not defined. Could not submit verification attempt for user %s", request.user.id) context = { 'course_key': unicode(course_key), 'course_name': course.display_name_with_default, 'checkpoint_name': checkpoint_name, 'error': True, 'errorMsg': _("No checkpoint found"), 'platform_name': settings.PLATFORM_NAME, } return render_to_response("verify_student/incourse_reverify.html", context) init_verification = SoftwareSecurePhotoVerification.get_initial_verification(user) if not init_verification: log.error("Could not submit verification attempt for user %s", request.user.id) return redirect(reverse('verify_student_verify_later', kwargs={'course_id': unicode(course_key)})) try: attempt = SoftwareSecurePhotoVerification.submit_faceimage( request.user, request.POST['face_image'], init_verification.photo_id_key ) checkpoint.add_verification_attempt(attempt) VerificationStatus.add_verification_status(checkpoint, user, "submitted") # emit the reverification event self._track_reverification_events( EVENT_NAME_USER_SUBMITTED_INCOURSE_REVERIFY, user.id, course_id, checkpoint_name ) return HttpResponse() except IndexError: log.exception("Invalid image data during photo verification.") return HttpResponseBadRequest(_("Invalid image data during photo verification.")) except Exception: # pylint: disable=broad-except log.exception("Could not submit verification attempt for user {}.").format(request.user.id) msg = _("Could not submit photos") return HttpResponseBadRequest(msg)
def test_get_or_create_verification_checkpoint_for_not_existing_values(self): # Retrieving a checkpoint that doesn't yet exist will create it location = u'i4x://edX/DemoX/edx-reverification-block/invalid_location' checkpoint = VerificationCheckpoint.get_or_create_verification_checkpoint(self.course.id, location) self.assertIsNot(checkpoint, None) self.assertEqual(checkpoint.course_id, self.course.id) self.assertEqual(checkpoint.checkpoint_location, location)
def test_get_verification_checkpoint_for_not_existing_values(self): """testing class method of VerificationCheckpoint. create the object and then uses the class method to get the verification check point. """ # create the VerificationCheckpoint checkpoint VerificationCheckpoint.objects.create(course_id=self.course.id, checkpoint_name=self.MIDTERM) # get verification for not existing checkpoint self.assertEqual(VerificationCheckpoint.get_verification_checkpoint(self.course.id, 'abc'), None)
def get(self, request, course_id, usage_id): """Display the view for face photo submission. Args: request(HttpRequest): HttpRequest object course_id(str): A string of course id usage_id(str): Location of Reverification XBlock in courseware Returns: HttpResponse """ # Check that in-course re-verification is enabled or not incourse_reverify_enabled = InCourseReverificationConfiguration.current().enabled if not incourse_reverify_enabled: log.error( u"In-course reverification is not enabled. " u"You can enable it in Django admin by setting " u"InCourseReverificationConfiguration to enabled." ) raise Http404 user = request.user course_key = CourseKey.from_string(course_id) course = modulestore().get_course(course_key) if course is None: log.error(u"Could not find course '%s' for in-course reverification.", course_key) raise Http404 checkpoint = VerificationCheckpoint.get_verification_checkpoint(course_key, usage_id) if checkpoint is None: log.error( u"No verification checkpoint exists for the " u"course '%s' and checkpoint location '%s'.", course_key, usage_id ) raise Http404 initial_verification = SoftwareSecurePhotoVerification.get_initial_verification(user) if not initial_verification: return self._redirect_no_initial_verification(user, course_key) # emit the reverification event self._track_reverification_events( EVENT_NAME_USER_ENTERED_INCOURSE_REVERIFY_VIEW, user.id, course_id, checkpoint.checkpoint_name ) context = { 'course_key': unicode(course_key), 'course_name': course.display_name_with_default, 'checkpoint_name': checkpoint.checkpoint_name, 'platform_name': settings.PLATFORM_NAME, 'usage_id': usage_id, 'capture_sound': staticfiles_storage.url("audio/camera_capture.wav"), } return render_to_response("verify_student/incourse_reverify.html", context)
def test_get_verification_checkpoint(self, check_point): """testing class method of VerificationCheckpoint. create the object and then uses the class method to get the verification check point. """ # create the VerificationCheckpoint checkpoint ver_check_point = VerificationCheckpoint.objects.create(course_id=self.course.id, checkpoint_name=check_point) self.assertEqual( VerificationCheckpoint.get_verification_checkpoint(self.course.id, check_point), ver_check_point )
def test_get_verification_checkpoint(self, check_point): """testing class method of VerificationCheckpoint. create the object and then uses the class method to get the verification check point. """ # create the VerificationCheckpoint checkpoint ver_check_point = VerificationCheckpoint.objects.create( course_id=self.course.id, checkpoint_name=check_point) self.assertEqual( VerificationCheckpoint.get_verification_checkpoint( self.course.id, check_point), ver_check_point)
def start_verification(self, course_id, related_assessment_location): """Create re-verification link against a verification checkpoint. Args: course_id(str): A string of course id related_assessment_location(str): Location of Reverification XBlock Returns: Re-verification link """ course_key = CourseKey.from_string(course_id) # Get-or-create the verification checkpoint VerificationCheckpoint.get_or_create_verification_checkpoint( course_key, related_assessment_location) re_verification_link = reverse( 'verify_student_incourse_reverify', args=(unicode(course_key), unicode(related_assessment_location))) return re_verification_link
def start_verification(self, course_id, related_assessment_location): """Create re-verification link against a verification checkpoint. Args: course_id(str): A string of course id related_assessment_location(str): Location of Reverification XBlock Returns: Re-verification link """ course_key = CourseKey.from_string(course_id) # Get-or-create the verification checkpoint VerificationCheckpoint.get_or_create_verification_checkpoint(course_key, related_assessment_location) re_verification_link = reverse( 'verify_student_incourse_reverify', args=( unicode(course_key), unicode(related_assessment_location) ) ) return re_verification_link
def test_get_verification_checkpoint(self, checkpoint): """ Test that a reverification checkpoint is created properly. """ checkpoint_location = u'i4x://{org}/{course}/edx-reverification-block/{checkpoint}'.format( org=self.course.id.org, course=self.course.id.course, checkpoint=checkpoint) # create the 'VerificationCheckpoint' checkpoint verification_checkpoint = VerificationCheckpoint.objects.create( course_id=self.course.id, checkpoint_location=checkpoint_location) self.assertEqual( VerificationCheckpoint.get_verification_checkpoint( self.course.id, checkpoint_location), verification_checkpoint)
def test_get_verification_checkpoint_for_not_existing_values(self): """Test that 'get_verification_checkpoint' method returns None if user tries to access a checkpoint with an invalid location. """ # create the VerificationCheckpoint checkpoint VerificationCheckpoint.objects.create(course_id=self.course.id, checkpoint_location=self.checkpoint_midterm) # get verification for a non existing checkpoint self.assertEqual( VerificationCheckpoint.get_verification_checkpoint( self.course.id, u'i4x://edX/DemoX/edx-reverification-block/invalid_location' ), None )
def test_get_or_create_verification_checkpoint(self, checkpoint): """ Test that a reverification checkpoint is created properly. """ checkpoint_location = u"i4x://{org}/{course}/edx-reverification-block/{checkpoint}".format( org=self.course.id.org, course=self.course.id.course, checkpoint=checkpoint ) # create the 'VerificationCheckpoint' checkpoint verification_checkpoint = VerificationCheckpoint.objects.create( course_id=self.course.id, checkpoint_location=checkpoint_location ) self.assertEqual( VerificationCheckpoint.get_or_create_verification_checkpoint(self.course.id, checkpoint_location), verification_checkpoint, )
def test_get_verification_checkpoint_for_not_existing_values(self): """Test that 'get_verification_checkpoint' method returns None if user tries to access a checkpoint with an invalid location. """ # create the VerificationCheckpoint checkpoint VerificationCheckpoint.objects.create( course_id=self.course.id, checkpoint_location=self.checkpoint_midterm) # get verification for a non existing checkpoint self.assertEqual( VerificationCheckpoint.get_verification_checkpoint( self.course.id, u'i4x://edX/DemoX/edx-reverification-block/invalid_location'), None)
def test_get_or_create_integrity_error(self): # Create the checkpoint VerificationCheckpoint.objects.create(course_id=self.course.id, checkpoint_location=self.checkpoint_midterm) # Simulate that the get-or-create operation raises an IntegrityError # This can happen when two processes both try to get-or-create at the same time # when the database is set to REPEATABLE READ. with patch.object(VerificationCheckpoint.objects, "get_or_create") as mock_get_or_create: mock_get_or_create.side_effect = IntegrityError checkpoint = VerificationCheckpoint.get_or_create_verification_checkpoint( self.course.id, self.checkpoint_midterm ) # The checkpoint should be retrieved without error self.assertEqual(checkpoint.course_id, self.course.id) self.assertEqual(checkpoint.checkpoint_location, self.checkpoint_midterm)
def test_get_or_create_integrity_error(self): # Create the checkpoint VerificationCheckpoint.objects.create( course_id=self.course.id, checkpoint_location=self.checkpoint_midterm, ) # Simulate that the get-or-create operation raises an IntegrityError # This can happen when two processes both try to get-or-create at the same time # when the database is set to REPEATABLE READ. with patch.object(VerificationCheckpoint.objects, "get_or_create") as mock_get_or_create: mock_get_or_create.side_effect = IntegrityError checkpoint = VerificationCheckpoint.get_or_create_verification_checkpoint( self.course.id, self.checkpoint_midterm ) # The checkpoint should be retrieved without error self.assertEqual(checkpoint.course_id, self.course.id) self.assertEqual(checkpoint.checkpoint_location, self.checkpoint_midterm)
def post(self, request, course_id, usage_id): """Submits the re-verification attempt to SoftwareSecure. Args: request(HttpRequest): HttpRequest object course_id(str): Course Id usage_id(str): Location of Reverification XBlock in courseware Returns: HttpResponse with status_code 400 if photo is missing or any error or redirect to the verification flow if initial verification doesn't exist otherwise HttpResponse with status code 200 """ # Check the in-course re-verification is enabled or not incourse_reverify_enabled = InCourseReverificationConfiguration.current().enabled if not incourse_reverify_enabled: raise Http404 user = request.user try: course_key = CourseKey.from_string(course_id) usage_key = UsageKey.from_string(usage_id).replace(course_key=course_key) except InvalidKeyError: raise Http404(u"Invalid course_key or usage_key") course = modulestore().get_course(course_key) if course is None: log.error(u"Invalid course id '%s'", course_id) return HttpResponseBadRequest(_("Invalid course location.")) checkpoint = VerificationCheckpoint.get_verification_checkpoint(course_key, usage_id) if checkpoint is None: log.error( u"Checkpoint is not defined. Could not submit verification attempt" u" for user '%s', course '%s' and checkpoint location '%s'.", request.user.id, course_key, usage_id ) return HttpResponseBadRequest(_("Invalid checkpoint location.")) init_verification = SoftwareSecurePhotoVerification.get_initial_verification(user) if not init_verification: return self._redirect_no_initial_verification(user, course_key) try: attempt = SoftwareSecurePhotoVerification.submit_faceimage( request.user, request.POST['face_image'], init_verification.photo_id_key ) checkpoint.add_verification_attempt(attempt) VerificationStatus.add_verification_status(checkpoint, user, "submitted") # emit the reverification event self._track_reverification_events( 'edx.bi.reverify.submitted', user.id, course_id, checkpoint.checkpoint_name ) redirect_url = get_redirect_url(course_key, usage_key) response = JsonResponse({'url': redirect_url}) except (ItemNotFoundError, NoPathToItem): log.warning(u"Could not find redirect URL for location %s in course %s", course_key, usage_key) redirect_url = reverse("courseware", args=(unicode(course_key),)) response = JsonResponse({'url': redirect_url}) except Http404 as expt: log.exception("Invalid location during photo verification.") response = HttpResponseBadRequest(expt.message) except IndexError: log.exception("Invalid image data during photo verification.") response = HttpResponseBadRequest(_("Invalid image data during photo verification.")) except Exception: # pylint: disable=broad-except log.exception("Could not submit verification attempt for user %s.", request.user.id) msg = _("Could not submit photos") response = HttpResponseBadRequest(msg) return response