From beacf3c93a9291f40710a0872457b16e9750fee3 Mon Sep 17 00:00:00 2001 From: Bill DeRusha Date: Tue, 23 Feb 2016 10:16:51 -0500 Subject: [PATCH] Only return unexpired initial verifications. --- lms/djangoapps/verify_student/models.py | 10 +++++-- .../verify_student/tests/test_models.py | 29 +++++++++++++++---- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/lms/djangoapps/verify_student/models.py b/lms/djangoapps/verify_student/models.py index fbc9cdc015..c761835db8 100644 --- a/lms/djangoapps/verify_student/models.py +++ b/lms/djangoapps/verify_student/models.py @@ -593,18 +593,22 @@ class SoftwareSecurePhotoVerification(PhotoVerification): copy_id_photo_from = models.ForeignKey("self", null=True, blank=True) @classmethod - def get_initial_verification(cls, user): + def get_initial_verification(cls, user, earliest_allowed_date=None): """Get initial verification for a user with the 'photo_id_key'. Arguments: user(User): user object + earliest_allowed_date(datetime): override expiration date for initial verification Return: - SoftwareSecurePhotoVerification (object) + SoftwareSecurePhotoVerification (object) or None """ init_verification = cls.objects.filter( user=user, - status__in=["submitted", "approved"] + status__in=["submitted", "approved"], + created_at__gte=( + earliest_allowed_date or cls._earliest_allowed_date() + ) ).exclude(photo_id_key='') return init_verification.latest('created_at') if init_verification.exists() else None diff --git a/lms/djangoapps/verify_student/tests/test_models.py b/lms/djangoapps/verify_student/tests/test_models.py index 07fa37548f..349cf7d144 100644 --- a/lms/djangoapps/verify_student/tests/test_models.py +++ b/lms/djangoapps/verify_student/tests/test_models.py @@ -1,16 +1,17 @@ # -*- coding: utf-8 -*- from datetime import timedelta, datetime -import ddt import json -import mock -import requests.exceptions -import pytz +import ddt from django.conf import settings from django.db import IntegrityError from django.test import TestCase +from freezegun import freeze_time +import mock from mock import patch from nose.tools import assert_is_none, assert_equals, assert_raises, assert_true, assert_false # pylint: disable=no-name-in-module +import pytz +import requests.exceptions from student.tests.factories import UserFactory from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase @@ -42,8 +43,9 @@ iwIDAQAB "API_URL": "http://localhost/verify_student/fake_endpoint", "AWS_ACCESS_KEY": "FAKEACCESSKEY", "AWS_SECRET_KEY": "FAKESECRETKEY", - "S3_BUCKET": "fake-bucket" - } + "S3_BUCKET": "fake-bucket", + }, + "DAYS_GOOD_FOR": 10, } @@ -524,6 +526,21 @@ class TestPhotoVerification(ModuleStoreTestCase): self.assertIsNotNone(second_result) self.assertEqual(second_result, first_result) + # Test method 'get_initial_verification' returns None after expiration + expired_future = datetime.utcnow() + timedelta(days=(FAKE_SETTINGS['DAYS_GOOD_FOR'] + 1)) + with freeze_time(expired_future): + third_result = SoftwareSecurePhotoVerification.get_initial_verification(user) + self.assertIsNone(third_result) + + # Test method 'get_initial_verification' returns correct attempt after system expiration, + # but within earliest allowed override. + expired_future = datetime.utcnow() + timedelta(days=(FAKE_SETTINGS['DAYS_GOOD_FOR'] + 1)) + earliest_allowed = datetime.utcnow() - timedelta(days=1) + with freeze_time(expired_future): + fourth_result = SoftwareSecurePhotoVerification.get_initial_verification(user, earliest_allowed) + self.assertIsNotNone(fourth_result) + self.assertEqual(fourth_result, first_result) + @ddt.ddt class VerificationCheckpointTest(ModuleStoreTestCase):