Skip to content

Commit

Permalink
fix: Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rijuma committed Sep 9, 2024
1 parent 2d06e09 commit 33627a4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
4 changes: 3 additions & 1 deletion edx_name_affirmation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
30 changes: 21 additions & 9 deletions edx_name_affirmation/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,38 @@
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
from edx_name_affirmation.statuses import VerifiedNameStatus

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."

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})

Check failure on line 36 in edx_name_affirmation/tests/test_models.py

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-20.04, 3.11, 10, django42)

Missing coverage

Missing coverage on line 36


class VerifiedNameModelTests(TestCase):
"""
Test suite for the VerifiedName models
Expand All @@ -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
Expand All @@ -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
20 changes: 20 additions & 0 deletions edx_name_affirmation/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
All tests for edx_name_affirmation views
"""
import json
from unittest.mock import PropertyMock, patch

import ddt

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 33627a4

Please sign in to comment.