Skip to content

Commit

Permalink
fix: digital credentials helper (#2341)
Browse files Browse the repository at this point in the history
* fix: add role to credential data with tests

Signed-off-by: Akiff Manji <amanji@petridish.dev>

* feat: digital credentials decorator (#2337)

* feat: adds check for digital business card feature

Signed-off-by: Akiff Manji <amanji@petridish.dev>

* refactor: better function naming

Signed-off-by: Akiff Manji <amanji@petridish.dev>

* feat: decorator pto protect digital credentials endpoints

Signed-off-by: Akiff Manji <amanji@petridish.dev>

* chore: address code review comments

Signed-off-by: Akiff Manji <amanji@petridish.dev>

---------

Signed-off-by: Akiff Manji <amanji@petridish.dev>

* fix: test failures

Signed-off-by: Akiff Manji <amanji@petridish.dev>

---------

Signed-off-by: Akiff Manji <amanji@petridish.dev>
  • Loading branch information
amanji authored Dec 1, 2023
1 parent 6869daa commit f6c20d9
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 4 deletions.
8 changes: 7 additions & 1 deletion legal-api/src/legal_api/services/digital_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,12 @@ def get_digital_credential_data(business: Business, user: User, credential_type:

given_names = ' '.join([x.strip() for x in [user.firstname, user.middlename] if x and x.strip()]).upper()

# For an SP there is only one role. This will need to be updated
# when the entity model changes and we need to support multiple roles.
role = (
business.party_roles[0].role if (business.party_roles and len(business.party_roles.all())) else ''
).replace('_', ' ').title()

return [
{
'name': 'credential_id',
Expand Down Expand Up @@ -331,7 +337,7 @@ def get_digital_credential_data(business: Business, user: User, credential_type:
},
{
'name': 'role',
'value': ''
'value': role or ''
}
]

Expand Down
105 changes: 102 additions & 3 deletions legal-api/tests/unit/services/test_digital_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
"""
from unittest.mock import MagicMock

from legal_api.models import DCDefinition
import pytest
from legal_api.models import DCDefinition, DCIssuedBusinessUserCredential, PartyRole
from legal_api.services import digital_credentials
from legal_api.services.digital_credentials import DigitalCredentialsService
from legal_api.services.digital_credentials import DigitalCredentialsHelpers, DigitalCredentialsService
from tests.unit.models import factory_business, factory_user

schema_id = 'test_schema_id'
cred_def_id = 'test_credential_definition_id'

def test_init_app(session, app): # pylint:disable=unused-argument
def test_init_app(app, session):
"""Assert that the init app register schema and credential definition."""
DigitalCredentialsService._fetch_schema = MagicMock(return_value=schema_id)
DigitalCredentialsService._fetch_credential_definition = MagicMock(return_value=cred_def_id)
Expand All @@ -36,3 +38,100 @@ def test_init_app(session, app): # pylint:disable=unused-argument
assert definition.schema_version == digital_credentials.business_schema_version
assert definition.credential_definition_id == cred_def_id
assert not definition.is_deleted


@pytest.mark.parametrize('test_data', [{
'business': {
'identifier': 'FM1234567',
'entity_type': 'SP',
'founding_date': '2010-01-01',
'state': 'ACTIVE',
},
'business_extra': {
'legal_name': 'Test Business',
'tax_id': '000000000000001',
},
'party_roles': [{
'role': 'proprietor'
}],
'user': {
'username': 'test',
'lastname': 'Last',
'firstname': 'First',
},
'user_extra': {
'middlename': 'Middle',
},
'expected': [
{'name': 'credential_id', 'value': ''},
{'name': 'identifier', 'value': 'FM1234567'},
{'name': 'business_name', 'value': 'Test Business'},
{'name': 'business_type', 'value': 'BC Sole Proprietorship'},
{'name': 'cra_business_number', 'value': '000000000000001'},
{'name': 'registered_on_dateint', 'value': '20100101'},
{'name': 'company_status', 'value': 'ACTIVE'},
{'name': 'family_name', 'value': 'LAST'},
{'name': 'given_names', 'value': 'FIRST MIDDLE'},
{'name': 'role', 'value': 'Proprietor'}
]
}, {
'business': {
'identifier': 'FM1234567'
},
'business_extra': {
'legal_name': '',
'tax_id': '',
},
'party_roles': [{
'role': ''
}],
'user': {
'username': 'test'
},
'user_extra': {
'middlename': '',
},
'expected': [
{'name': 'credential_id', 'value': ''},
{'name': 'identifier', 'value': 'FM1234567'},
{'name': 'business_name', 'value': ''},
{'name': 'business_type', 'value': 'BC Cooperative Association'},
{'name': 'cra_business_number', 'value': ''},
{'name': 'registered_on_dateint', 'value': '19700101'},
{'name': 'company_status', 'value': 'ACTIVE'},
{'name': 'family_name', 'value': ''},
{'name': 'given_names', 'value': ''},
{'name': 'role', 'value': ''}
]
}])
def test_data_helper(app, session, test_data):
"""Assert that the data helper returns the correct data."""
# Arrange
credential_type = DCDefinition.CredentialType.business

user = factory_user(**test_data['user'])
user.middlename = test_data['user_extra']['middlename']
user.save()

business = factory_business(**test_data['business'])
business.legal_name = test_data['business_extra']['legal_name']
business.tax_id = test_data['business_extra']['tax_id']
business.save()

for party_role in test_data['party_roles']:
_party_role = PartyRole(**party_role)
_party_role.business_id = business.id
_party_role.save()

issued_business_user_credential = DCIssuedBusinessUserCredential(business_id=business.id, user_id=user.id)
issued_business_user_credential.save()

# Act
credential_data = DigitalCredentialsHelpers.get_digital_credential_data(business, user, credential_type)

# Assert
for item in credential_data:
if item['name'] == 'credential_id':
assert item['value'] == f'{issued_business_user_credential.id:08}'
else:
assert item in test_data['expected']

0 comments on commit f6c20d9

Please sign in to comment.