diff --git a/edx_name_affirmation/models.py b/edx_name_affirmation/models.py index 9a4c3a0..bd61db7 100644 --- a/edx_name_affirmation/models.py +++ b/edx_name_affirmation/models.py @@ -7,6 +7,7 @@ from simple_history.models import HistoricalRecords from django.contrib.auth import get_user_model +from django.core.exceptions import ObjectDoesNotExist from django.db import models from edx_name_affirmation.statuses import VerifiedNameStatus @@ -61,9 +62,10 @@ def verification_attempt_status(self): verification = SoftwareSecurePhotoVerification.objects.get(id=self.verification_attempt_id) return verification.status if verification else None - except SoftwareSecurePhotoVerification.DoesNotExist: + except ObjectDoesNotExist: return None + class VerifiedNameConfig(ConfigurationModel): """ This model provides various configuration fields for users regarding their diff --git a/edx_name_affirmation/tests/test_models.py b/edx_name_affirmation/tests/test_models.py index 5325a9c..5a030f1 100644 --- a/edx_name_affirmation/tests/test_models.py +++ b/edx_name_affirmation/tests/test_models.py @@ -4,6 +4,7 @@ from unittest.mock import patch from django.contrib.auth import get_user_model +from django.core.exceptions import ObjectDoesNotExist from django.test import TestCase from edx_name_affirmation.models import VerifiedName @@ -11,6 +12,12 @@ User = get_user_model() +idv_attempt_id = 34455 +idv_attempt_status = 'submitted' + +idv_attempt_id_notfound = 404 +idv_attempt_id_notfound_status = None + def _obj(dictionary): "Helper method to turn a dict into an object. Used to mock below." @@ -18,6 +25,17 @@ def _obj(dictionary): return type('obj', (object,), dictionary) +def _mocked_model_get(id): # pylint: disable=redefined-builtin + "Helper method to mock the behavior of SoftwareSecurePhotoVerification model. Used to mock below." + if id == idv_attempt_id_notfound: + raise ObjectDoesNotExist + + if id == idv_attempt_id: + return _obj({'status': idv_attempt_status}) + + return _obj({'status': None}) + + class VerifiedNameModelTests(TestCase): """ Test suite for the VerifiedName models @@ -36,7 +54,6 @@ def test_histories(self): """ Test the model history is recording records as expected """ - idv_attempt_id = 34455 verified_name_history = self.verified_name.history.all().order_by('history_date') assert len(verified_name_history) == 1 @@ -59,15 +76,10 @@ def test_verification_status(self, sspv_mock): """ Test the model history is recording records as expected """ - idv_attempt_id = 34455 - idv_attempt_status = 'submitted' - - sspv_mock.objects.get = lambda id: \ - _obj({'status': idv_attempt_status}) if id == idv_attempt_id \ - else _obj({'status': None}) + sspv_mock.objects.get = _mocked_model_get - self.verified_name.verification_attempt_id = 12345 - assert self.verified_name.verification_attempt_status is None + self.verified_name.verification_attempt_id = idv_attempt_id_notfound + assert self.verified_name.verification_attempt_status is idv_attempt_id_notfound_status self.verified_name.verification_attempt_id = idv_attempt_id assert self.verified_name.verification_attempt_status is idv_attempt_status diff --git a/edx_name_affirmation/tests/test_views.py b/edx_name_affirmation/tests/test_views.py index c4dd425..eb1eb86 100644 --- a/edx_name_affirmation/tests/test_views.py +++ b/edx_name_affirmation/tests/test_views.py @@ -2,6 +2,7 @@ All tests for edx_name_affirmation views """ import json +from unittest.mock import PropertyMock, patch import ddt @@ -380,6 +381,25 @@ def test_get(self): data = json.loads(response.content.decode('utf-8')) self.assertEqual(data, expected_response) + @patch('edx_name_affirmation.api.VerifiedName.verification_attempt_status', new_callable=PropertyMock) + def test_get_with_idv_status(self, verification_attempt_status_mock): + mocked_idv_status = 'approved' + + verification_attempt_status_mock.return_value = mocked_idv_status + verified_name_history = self._create_verified_name_history(self.user) + expected_response = self._get_expected_response(self.user, verified_name_history) + + # replacing the expected response results with the mocked status + for row in expected_response['results']: + row['verification_attempt_status'] = mocked_idv_status + + response = self.client.get(reverse('edx_name_affirmation:verified_name_history')) + + self.assertEqual(response.status_code, 200) + data = json.loads(response.content.decode('utf-8')) + + self.assertEqual(data, expected_response) + def test_get_bools(self): verified_name_history = self._create_verified_name_history(self.user) expected_response = self._get_expected_response(