def test_update_entity_existing_success(session, monkeypatch): # pylint:disable=unused-argument """Assert that an Entity can be updated from a dictionary.""" entity = EntityService.save_entity({ 'businessIdentifier': TestEntityInfo.bc_entity_passcode3['businessIdentifier'], 'businessNumber': TestEntityInfo.bc_entity_passcode3['businessNumber'], 'passCode': TestEntityInfo.bc_entity_passcode3['passCode'], 'name': TestEntityInfo.bc_entity_passcode3['name'], 'corpTypeCode': TestEntityInfo.bc_entity_passcode3['corpTypeCode'] }) assert entity assert entity.as_dict()['corp_type']['code'] == 'BC' updated_entity_info = { 'businessIdentifier': TestEntityInfo.bc_entity_passcode4['businessIdentifier'], 'businessNumber': TestEntityInfo.bc_entity_passcode4['businessNumber'], 'name': TestEntityInfo.bc_entity_passcode4['name'], 'corpTypeCode': TestEntityInfo.bc_entity_passcode4['corpTypeCode'] } user_with_token = TestUserInfo.user_test user_with_token['keycloak_guid'] = TestJwtClaims.public_user_role['sub'] patch_token_info( { 'loginSource': '', 'realm_access': { 'roles': ['system'] }, 'corp_type': 'BC' }, monkeypatch) updated_entity = EntityService.update_entity( entity.as_dict().get('business_identifier'), updated_entity_info) assert updated_entity assert updated_entity.as_dict()['name'] == updated_entity_info['name'] assert updated_entity.as_dict( )['business_number'] == updated_entity_info['businessNumber']
def test_update_entity_existing_failures(session, monkeypatch): # pylint:disable=unused-argument """Assert that an Entity can be updated from a dictionary.""" entity = EntityService.save_entity({ 'businessIdentifier': TestEntityInfo.bc_entity_passcode3['businessIdentifier'], 'businessNumber': TestEntityInfo.bc_entity_passcode3['businessNumber'], 'passCode': TestEntityInfo.bc_entity_passcode3['passCode'], 'name': TestEntityInfo.bc_entity_passcode3['name'], 'corpTypeCode': TestEntityInfo.bc_entity_passcode3['corpTypeCode'] }) assert entity assert entity.as_dict()['corp_type']['code'] == 'BC' updated_entity_info = { 'businessIdentifier': TestEntityInfo.bc_entity_passcode4['businessIdentifier'], 'businessNumber': TestEntityInfo.bc_entity_passcode4['businessNumber'], 'name': TestEntityInfo.bc_entity_passcode4['name'], 'corpTypeCode': TestEntityInfo.bc_entity_passcode4['corpTypeCode'] } user_with_token = TestUserInfo.user_test user_with_token['keycloak_guid'] = TestJwtClaims.public_user_role['sub'] with pytest.raises(BusinessException) as exception: patch_token_info( { 'loginSource': '', 'realm_access': { 'roles': ['system'] }, 'corp_type': 'INVALID_CP' }, monkeypatch) EntityService.update_entity('invalidbusinessnumber', updated_entity_info) assert exception.value.code == Error.DATA_NOT_FOUND.name
def test_save_entity_new(session): # pylint:disable=unused-argument """Assert that an Entity can be created from a dictionary.""" entity = EntityService.save_entity({ 'businessIdentifier': TestEntityInfo.entity_passcode['businessIdentifier'], 'businessNumber': TestEntityInfo.entity_passcode['businessNumber'], 'passCode': TestEntityInfo.entity_passcode['passCode'], 'name': TestEntityInfo.entity_passcode['name'], 'corpTypeCode': TestEntityInfo.entity_passcode['corpTypeCode'] }) assert entity is not None dictionary = entity.as_dict() assert dictionary['business_identifier'] == TestEntityInfo.entity_passcode[ 'businessIdentifier']
def post(): """Post a new Entity using the request body.""" request_json = request.get_json() valid_format, errors = schema_utils.validate(request_json, 'entity') if not valid_format: return { 'message': schema_utils.serialize(errors) }, http_status.HTTP_400_BAD_REQUEST try: response, status = EntityService.save_entity( request_json).as_dict(), http_status.HTTP_201_CREATED except BusinessException as exception: response, status = { 'code': exception.code, 'message': exception.message }, exception.status_code return response, status
def post(): """Post a new Entity using the request body.""" request_json = request.get_json() # If the record exists, just return existing record. entity = EntityService.find_by_business_identifier(request_json.get('businessIdentifier'), allowed_roles=ALL_ALLOWED_ROLES) if entity: return entity.as_dict(), http_status.HTTP_202_ACCEPTED valid_format, errors = schema_utils.validate(request_json, 'entity') if not valid_format: return {'message': schema_utils.serialize(errors)}, http_status.HTTP_400_BAD_REQUEST try: response, status = EntityService.save_entity(request_json).as_dict(), http_status.HTTP_201_CREATED except BusinessException as exception: response, status = {'code': exception.code, 'message': exception.message}, exception.status_code return response, status
def test_update_entity_existing_failures_no_cp_for_saved_entity(session): # pylint:disable=unused-argument """Assert that an Entity can be updated from a dictionary.""" entity = EntityService.save_entity({ 'businessIdentifier': TestEntityInfo.bc_entity_passcode3['businessIdentifier'], 'businessNumber': TestEntityInfo.bc_entity_passcode3['businessNumber'], 'passCode': TestEntityInfo.bc_entity_passcode3['passCode'], 'name': TestEntityInfo.bc_entity_passcode3['name'] }) assert entity assert entity.as_dict().get('corpType', None) is None updated_entity_info = { 'businessIdentifier': TestEntityInfo.bc_entity_passcode4['businessIdentifier'], 'businessNumber': TestEntityInfo.bc_entity_passcode4['businessNumber'], 'name': TestEntityInfo.bc_entity_passcode4['name'], 'corpTypeCode': TestEntityInfo.bc_entity_passcode4['corpTypeCode'] } user_with_token = TestUserInfo.user_test user_with_token['keycloak_guid'] = TestJwtClaims.edit_role['sub'] with pytest.raises(BusinessException) as exception: EntityService.update_entity( entity.as_dict().get('businessIdentifier'), updated_entity_info, { 'loginSource': '', 'realm_access': { 'roles': ['system'] }, 'corp_type': 'INVALID_CP' }) assert exception.value.code == Error.DATA_NOT_FOUND.name
def create_new_business_affiliation(org_id, # pylint: disable=too-many-arguments, too-many-locals business_identifier=None, email=None, phone=None, bearer_token: str = None): """Initiate a new incorporation.""" current_app.logger.info(f'<create_affiliation org_id:{org_id} business_identifier:{business_identifier}') if not email and not phone: raise BusinessException(Error.NR_INVALID_CONTACT, None) # Validate if org_id is valid by calling Org Service. org = OrgService.find_by_org_id(org_id, allowed_roles=CLIENT_AUTH_ROLES) if org is None: raise BusinessException(Error.DATA_NOT_FOUND, None) entity = EntityService.find_by_business_identifier(business_identifier, skip_auth=True) # If entity already exists and passcode is already claimed, throw error if entity and entity.as_dict()['pass_code_claimed']: raise BusinessException(Error.NR_CONSUMED, None) # Call the legal-api to verify the NR details nr_json = Affiliation._get_nr_details(business_identifier, bearer_token) if nr_json: status = nr_json.get('state') nr_phone = nr_json.get('applicants').get('phoneNumber') nr_email = nr_json.get('applicants').get('emailAddress') if status not in (NRStatus.APPROVED.value, NRStatus.CONDITIONAL.value): raise BusinessException(Error.NR_NOT_APPROVED, None) # If consentFlag is not R, N or Null for a CONDITIONAL NR throw error if status == NRStatus.CONDITIONAL.value and nr_json.get('consentFlag', None) not in (None, 'R', 'N'): raise BusinessException(Error.NR_NOT_APPROVED, None) if (phone and phone != nr_phone) or (email and email.casefold() != nr_email.casefold()): raise BusinessException(Error.NR_INVALID_CONTACT, None) # Create an entity with the Name from NR if entity doesn't exist if not entity: # Filter the names from NR response and get the name which has status APPROVED as the name. # Filter the names from NR response and get the name which has status CONDITION as the name. nr_name_state = NRNameStatus.APPROVED.value if status == NRStatus.APPROVED.value \ else NRNameStatus.CONDITION.value name = next((name.get('name') for name in nr_json.get('names') if name.get('state', None) == nr_name_state), None) entity = EntityService.save_entity({ 'businessIdentifier': business_identifier, 'name': name, 'corpTypeCode': CorpType.NR.value, 'passCodeClaimed': True }) # Create an affiliation with org affiliation_model = AffiliationModel(org_id=org_id, entity_id=entity.identifier) affiliation_model.save() if entity.corp_type != CorpType.RTMP.value: ActivityLogPublisher.publish_activity(Activity(org_id, ActivityAction.CREATE_AFFILIATION.value, name=entity.name, id=entity.business_identifier)) entity.set_pass_code_claimed(True) else: raise BusinessException(Error.NR_NOT_FOUND, None) return Affiliation(affiliation_model)
def test_save_entity_no_input(session): # pylint:disable=unused-argument """Assert that an Entity can not be updated with no input.""" updated_entity = EntityService.save_entity(None) assert updated_entity is None