def test_validate_pass_code(app, session): # pylint:disable=unused-argument """Assert that a valid passcode can be correctly validated.""" entity_model = factory_entity_model(entity_info=TestEntityInfo.entity_passcode) entity = EntityService(entity_model) validated = entity.validate_pass_code(entity_model.pass_code) assert validated
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'), token_info=g.jwt_oidc_token_info, 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(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'], '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: EntityService.update_entity('invalidbusinessnumber', updated_entity_info, {'loginSource': '', 'realm_access': {'roles': ['system']}, 'corp_type': 'INVALID_CP'}) assert exception.value.code == Error.DATA_NOT_FOUND.name
def test_update_entity_existing_success(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'], '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'] updated_entity = EntityService.update_entity(entity.as_dict().get('business_identifier'), updated_entity_info, {'loginSource': '', 'realm_access': {'roles': ['system']}, 'corp_type': 'BC'}) 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 patch(business_identifier): """Update an existing business by it's business number.""" 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 passcode_reset = request_json.get('resetPasscode', False) try: if passcode_reset: entity = EntityService.reset_passcode(business_identifier, email_addresses=request_json.get('passcodeResetEmail', None)) else: entity = EntityService.update_entity(business_identifier, request_json) if entity is not None: response, status = entity.as_dict(), http_status.HTTP_200_OK else: response, status = {'message': 'A business for {} was not found.'.format(business_identifier)}, \ http_status.HTTP_404_NOT_FOUND except BusinessException as exception: response, status = {'code': exception.code, 'message': exception.message}, exception.status_code return response, status
def test_validate_invalid_pass_code(app, session): # pylint:disable=unused-argument """Assert that an invalid passcode in not validated.""" entity_model = factory_entity_model(business_identifier='CP1234567', pass_code='12345678') entity = EntityService(entity_model) validated = entity.validate_pass_code('1234') assert not validated
def test_delete_contact_entity_link(session, auth_mock): # pylint:disable=unused-argument """Assert that a contact can not be deleted without entity.""" entity_model = factory_entity_model() entity = EntityService(entity_model) org = factory_org_service() org_dictionary = org.as_dict() org_id = org_dictionary['id'] contact = factory_contact_model() contact_link = ContactLinkModel() contact_link.contact = contact contact_link.entity = entity._model # pylint:disable=protected-access contact_link.org = org._model # pylint:disable=protected-access contact_link.commit() updated_entity = entity.delete_contact() dictionary = None dictionary = updated_entity.as_dict() assert len(dictionary['contacts']) == 0 delete_contact_link = ContactLinkModel.find_by_entity_id(entity.identifier) assert not delete_contact_link exist_contact_link = ContactLinkModel.find_by_org_id(org_id) assert exist_contact_link
def test_validate_pass_code(app, session): # pylint:disable=unused-argument """Assert that a valid passcode can be correctly validated.""" entity_model = factory_entity_model(business_identifier='CP1234567', pass_code='12345678') entity = EntityService(entity_model) validated = entity.validate_pass_code(entity_model.pass_code) assert validated
def test_save_entity_existing(session): # pylint:disable=unused-argument """Assert that an Entity can be updated 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 updated_entity_info = { 'businessIdentifier': TestEntityInfo.entity_passcode2['businessIdentifier'], 'businessNumber': TestEntityInfo.entity_passcode2['businessNumber'], 'passCode': TestEntityInfo.entity_passcode['passCode'], 'name': TestEntityInfo.entity_passcode['name'], 'corpTypeCode': TestEntityInfo.entity_passcode['corpTypeCode'] } updated_entity = EntityService.save_entity(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_as_dict(session): # pylint:disable=unused-argument """Assert that the Entity is exported correctly as a dictionary.""" entity_model = factory_entity_model() entity = EntityService(entity_model) dictionary = entity.as_dict() assert dictionary['business_identifier'] == TestEntityInfo.entity1['businessIdentifier']
def test_validate_invalid_pass_code(app, session): # pylint:disable=unused-argument """Assert that an invalid passcode in not validated.""" entity_model = factory_entity_model(entity_info=TestEntityInfo.entity_passcode) entity = EntityService(entity_model) validated = entity.validate_pass_code('222222222') assert not validated
def test_update_contact_no_contact(session): # pylint:disable=unused-argument """Assert that a contact for a non-existent contact cannot be updated.""" entity_model = factory_entity_model(business_identifier='CP1234567') entity = EntityService(entity_model) with pytest.raises(BusinessException) as exception: entity.update_contact(TEST_UPDATED_CONTACT_INFO) assert exception.value.code == Error.DATA_NOT_FOUND.name
def test_as_dict(): """Assert that the Entity is exported correctly as a dictionary.""" entity_model = EntityModel(business_identifier='CP1234567') entity = EntityService(entity_model) assert entity.as_dict() == { 'businessIdentifier': 'CP1234567' }
def test_update_contact_no_contact(session): # pylint:disable=unused-argument """Assert that a contact for a non-existent contact cannot be updated.""" entity_model = factory_entity_model() entity = EntityService(entity_model) with pytest.raises(BusinessException) as exception: entity.update_contact(TestContactInfo.contact2) assert exception.value.code == Error.DATA_NOT_FOUND.name
def test_entity_find_by_entity_id_no_id(session, auth_mock): # pylint:disable=unused-argument """Assert that an Entity can not be retrieved when no id input or entity not exists.""" entity = EntityService.find_by_entity_id(None) assert entity is None entity = EntityService.find_by_entity_id(9999) assert entity is None
def test_get_contact_by_business_identifier(session): # pylint:disable=unused-argument """Assert that a contact can be retrieved by the associated business id.""" entity_model = factory_entity_model() entity = EntityService(entity_model) entity.add_contact(TestContactInfo.contact1) contact = entity.get_contact() assert contact is not None assert contact.email == TestContactInfo.contact1['email']
def test_add_contact_duplicate(session): # pylint:disable=unused-argument """Assert that a contact cannot be added to an Entity if that Entity already has a contact.""" entity_model = factory_entity_model(business_identifier='CP1234567') entity = EntityService(entity_model) entity.add_contact(TEST_CONTACT_INFO) with pytest.raises(BusinessException) as exception: entity.add_contact(TEST_UPDATED_CONTACT_INFO) assert exception.value.code == Error.DATA_ALREADY_EXISTS.name
def test_delete_contact(session): # pylint:disable=unused-argument """Assert that a contact can be deleted to an Entity.""" entity_model = factory_entity_model() entity = EntityService(entity_model) entity.add_contact(TestContactInfo.contact1) updated_entity = entity.delete_contact() dictionary = updated_entity.as_dict() assert not dictionary['contacts']
def test_get_contact_by_business_identifier(session): # pylint:disable=unused-argument """Assert that a contact can be retrieved by the associated business id.""" entity_model = factory_entity_model(business_identifier='CP1234567') entity = EntityService(entity_model) entity.add_contact(TEST_CONTACT_INFO) contact = entity.get_contact() assert contact is not None assert contact.email == TEST_CONTACT_INFO['email']
def test_entity_name_sync(app, session): # pylint:disable=unused-argument """Assert that the name syncing for entity affiliation is working correctly.""" entity_model = factory_entity_model( entity_info=TestEntityInfo.entity_lear_mock) entity = EntityService(entity_model) entity.sync_name() dictionary = entity.as_dict() assert dictionary['name'] == 'Legal Name CP0002103'
def test_add_contact_duplicate(session): # pylint:disable=unused-argument """Assert that a contact cannot be added to an Entity if that Entity already has a contact.""" entity_model = factory_entity_model() entity = EntityService(entity_model) entity.add_contact(TestContactInfo.contact1) with pytest.raises(BusinessException) as exception: entity.add_contact(TestContactInfo.contact2) assert exception.value.code == Error.DATA_ALREADY_EXISTS.name
def test_entity_find_by_entity_id(session, auth_mock): # pylint:disable=unused-argument """Assert that an Entity can be retrieved by entity identifier.""" entity_model = factory_entity_model() entity = EntityService(entity_model) entity = EntityService.find_by_entity_id(entity.identifier) assert entity is not None dictionary = entity.as_dict() assert dictionary['business_identifier'] == TestEntityInfo.entity1['businessIdentifier']
def test_add_contact(session): # pylint:disable=unused-argument """Assert that a contact can be added to an Entity.""" entity_model = factory_entity_model() entity = EntityService(entity_model) entity.add_contact(TestContactInfo.contact1) dictionary = entity.as_dict() assert dictionary['contacts'] assert len(dictionary['contacts']) == 1 assert dictionary['contacts'][0]['email'] == TestContactInfo.contact1['email']
def test_add_contact(session): # pylint:disable=unused-argument """Assert that a contact can be added to an Entity.""" entity_model = factory_entity_model(business_identifier='CP1234567') entity = EntityService(entity_model) entity.add_contact(TEST_CONTACT_INFO) dictionary = entity.as_dict() assert dictionary['contacts'] assert len(dictionary['contacts']) == 1 assert dictionary['contacts'][0]['email'] == TEST_CONTACT_INFO['email']
def test_reset_pass_code(app, session): # pylint:disable=unused-argument """Assert that the new passcode in not the same as old passcode.""" entity_model = factory_entity_model(entity_info=TestEntityInfo.entity_passcode) entity = EntityService(entity_model) old_passcode = entity.pass_code entity.reset_passcode(entity.business_identifier, '', TestJwtClaims.user_test) new_passcode = entity.pass_code assert old_passcode != new_passcode
def test_delete_contact_no_entity(session, auth_mock): # pylint:disable=unused-argument """Assert that a contact can not be deleted without entity.""" entity_model = factory_entity_model() entity = EntityService(entity_model) entity.add_contact(TestContactInfo.contact1) updated_entity = entity.delete_contact() with pytest.raises(BusinessException) as exception: updated_entity.delete_contact() assert exception.value.code == Error.DATA_NOT_FOUND.name
def test_reset_pass_code_confirm_activity_log(app, session): # pylint:disable=unused-argument """Assert that the new passcode in not the same as old passcode.""" entity_model = factory_entity_model(entity_info=TestEntityInfo.entity_passcode) entity = EntityService(entity_model) old_passcode = entity.pass_code with patch.object(activity_log_publisher, 'publish_activity', return_value=None) as mock_send: entity.reset_passcode(entity.business_identifier, '', TestJwtClaims.user_test) new_passcode = entity.pass_code assert old_passcode != new_passcode mock_send.assert_called
def delete_affiliation(org_id, business_identifier, email_addresses: str = None, reset_passcode: bool = False, log_delete_draft: bool = False): """Delete the affiliation for the provided org id and business id.""" current_app.logger.info(f'<delete_affiliation org_id:{org_id} business_identifier:{business_identifier}') org = OrgService.find_by_org_id(org_id, allowed_roles=(*CLIENT_AUTH_ROLES, STAFF)) if org is None: raise BusinessException(Error.DATA_NOT_FOUND, None) entity = EntityService.find_by_business_identifier(business_identifier, allowed_roles=(*CLIENT_AUTH_ROLES, STAFF)) if entity is None: raise BusinessException(Error.DATA_NOT_FOUND, None) entity_id = entity.identifier affiliation = AffiliationModel.find_affiliation_by_org_and_entity_ids(org_id=org_id, entity_id=entity_id) if affiliation is None: raise BusinessException(Error.DATA_NOT_FOUND, None) if reset_passcode: entity.reset_passcode(entity.business_identifier, email_addresses) affiliation.delete() entity.set_pass_code_claimed(False) # When registering a business it will affiliate a NR -> unaffiliate a NR draft -> affiliate a business. # Users can also intentionally delete a draft. We want to log this action. should_publish = (log_delete_draft or not (entity.status == NRStatus.DRAFT.value and entity.corp_type == CorpType.NR.value)) if entity.corp_type != CorpType.RTMP.value and should_publish: name = entity.name if len(entity.name) > 0 else entity.business_identifier ActivityLogPublisher.publish_activity(Activity(org_id, ActivityAction.REMOVE_AFFILIATION.value, name=name, id=entity.business_identifier))
def delete_affiliation(org_id, business_identifier, email_addresses: str = None, reset_passcode: bool = False, token_info: Dict = None): """Delete the affiliation for the provided org id and business id.""" current_app.logger.info(f'<delete_affiliation org_id:{org_id} business_identifier:{business_identifier}') org = OrgService.find_by_org_id(org_id, token_info=token_info, allowed_roles=(*CLIENT_AUTH_ROLES, STAFF)) if org is None: raise BusinessException(Error.DATA_NOT_FOUND, None) entity = EntityService.find_by_business_identifier(business_identifier, token_info=token_info, allowed_roles=(*CLIENT_AUTH_ROLES, STAFF)) if entity is None: raise BusinessException(Error.DATA_NOT_FOUND, None) entity_id = entity.identifier affiliation = AffiliationModel.find_affiliation_by_org_and_entity_ids(org_id=org_id, entity_id=entity_id) if affiliation is None: raise BusinessException(Error.DATA_NOT_FOUND, None) if reset_passcode: entity.reset_passcode(entity.business_identifier, email_addresses, token_info) affiliation.delete() entity.set_pass_code_claimed(False) publish_activity(f'{ActivityAction.REMOVE_AFFILIATION.value}-{entity.name}', entity.name, business_identifier, org_id)
def test_reset(session, auth_mock): # pylint: disable=unused-argument """Assert that can be reset data by the provided token.""" user_with_token = TestUserInfo.user_tester user_with_token['keycloak_guid'] = TestJwtClaims.tester_role['sub'] user = factory_user_model(user_info=user_with_token) org = factory_org_model(user_id=user.id) factory_membership_model(user.id, org.id) entity = factory_entity_model(user_id=user.id) ResetDataService.reset(TestJwtClaims.tester_role) with pytest.raises(BusinessException) as exception: UserService.find_by_jwt_token(user_with_token) assert exception.value.code == Error.DATA_NOT_FOUND.name found_org = OrgService.find_by_org_id(org.id) assert found_org is None found_entity = EntityService.find_by_entity_id(entity.id) assert found_entity is not None dictionary = found_entity.as_dict() assert dictionary['business_identifier'] == TestEntityInfo.entity1[ 'businessIdentifier'] assert not dictionary['pass_code_claimed'] found_memeber = MembershipService.get_members_for_org(org.id) assert found_memeber is None