diff --git a/edx_name_affirmation/models.py b/edx_name_affirmation/models.py index 0dc2056..9f4849b 100644 --- a/edx_name_affirmation/models.py +++ b/edx_name_affirmation/models.py @@ -11,6 +11,11 @@ from edx_name_affirmation.statuses import VerifiedNameStatus +try: + from lms.djangoapps.verify_student.models import SoftwareSecurePhotoVerification +except ImportError: + SoftwareSecurePhotoVerification = None + User = get_user_model() @@ -47,14 +52,13 @@ class Meta: @property def verification_attempt_status(self): - if not self.verification_attempt_id: - return None + "Returns the status associated with the attempt verification_attempt_id if any." - try: - from lms.djangoapps.verify_student.models import SoftwareSecurePhotoVerification - except ImportError: + if not self.verification_attempt_id or not SoftwareSecurePhotoVerification: return None + # breakpoint() + verification = SoftwareSecurePhotoVerification.objects.get(id=self.verification_attempt_id) return verification.status if verification else None diff --git a/edx_name_affirmation/tests/test_models.py b/edx_name_affirmation/tests/test_models.py index 043c1bf..f60c613 100644 --- a/edx_name_affirmation/tests/test_models.py +++ b/edx_name_affirmation/tests/test_models.py @@ -4,18 +4,13 @@ from django.contrib.auth import get_user_model from django.test import TestCase +from unittest.mock import patch, MagicMock from edx_name_affirmation.models import VerifiedName from edx_name_affirmation.statuses import VerifiedNameStatus -try: - from lms.djangoapps.verify_student.models import SoftwareSecurePhotoVerification -except ImportError: - pass - User = get_user_model() - class VerifiedNameModelTests(TestCase): """ Test suite for the VerifiedName models @@ -30,15 +25,25 @@ def setUp(self): ) return super().setUp() - def test_histories(self): + @patch('edx_name_affirmation.models.SoftwareSecurePhotoVerification') + def test_histories(self, sspv_mock): """ Test the model history is recording records as expected """ + idv_attempt_id = 34455 + idv_attempt_status = 'submitted' + + obj = lambda dict: type('obj', (object,), dict) + + sspv_mock.objects.get = lambda id : obj({ 'status': idv_attempt_status }) if id == idv_attempt_id else obj({ 'status': None }) + verified_name_history = self.verified_name.history.all().order_by('history_date') assert len(verified_name_history) == 1 - idv_attempt_id = 34455 self.verified_name.status = VerifiedNameStatus.APPROVED self.verified_name.verification_attempt_id = idv_attempt_id + + + assert self.verified_name.verification_attempt_status is idv_attempt_status self.verified_name.save() verified_name_history = self.verified_name.history.all().order_by('history_date') assert len(verified_name_history) == 2