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 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_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_delete_contact_org_link(session, auth_mock): # pylint:disable=unused-argument """Assert that a contact can not be deleted if it's still being used by an 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() OrgService.delete_contact(org_id=org_id) org = OrgService.find_by_org_id(org_id) response = OrgService.get_contacts(org_id) assert len(response['contacts']) == 0 delete_contact_link = ContactLinkModel.find_by_entity_id(entity.identifier) assert delete_contact_link exist_contact_link = ContactLinkModel.find_by_org_id(org_id) assert not exist_contact_link
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_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_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_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 find_affiliations_by_org_id(org_id): """Return business affiliations for the org.""" data = [] affiliation_models = AffiliationModel.find_affiliations_by_org_id( org_id) for affiliation_model in affiliation_models: affiliation = EntityService(affiliation_model.entity).as_dict() data.append(affiliation) return data
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_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(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_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_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 find_affiliations_by_org_id(org_id): """Return business affiliations for the org.""" # Accomplished in service instead of model (easier to avoid circular reference issues). subquery = db.session.query(AffiliationModel.entity_id, AffiliationModel.created) \ .join(Entity).filter(AffiliationModel.org_id == org_id) \ .subquery() entities = db.session.query(Entity).join(subquery, subquery.c.entity_id == Entity.id) \ .order_by(subquery.c.created.desc()) \ .all() return [EntityService(entity).as_dict() for entity in entities]
def find_affiliated_entities_by_org_id(org_id, token_info: Dict = None): """Given an org_id, this will return the entities affiliated with it.""" current_app.logger.debug('<find_affiliations_by_org_id for org_id {}'.format(org_id)) if not org_id: raise BusinessException(Error.DATA_NOT_FOUND, None) org = OrgService.find_by_org_id(org_id, token_info=token_info, allowed_roles=ALL_ALLOWED_ROLES) if org is None: raise BusinessException(Error.DATA_NOT_FOUND, None) data = [] affiliation_models = AffiliationModel.find_affiliations_by_org_id(org_id) if affiliation_models is None: raise BusinessException(Error.DATA_NOT_FOUND, None) for affiliation_model in affiliation_models: affiliation = EntityService(affiliation_model.entity).as_dict() data.append(affiliation) # 3806 : Filter out the NR affiliation if there is IA affiliation for the same NR. # Dict with key as NR number and value as name nr_number_name_dict = {d['business_identifier']: d['name'] for d in data if d['corp_type']['code'] == CorpType.NR.value} # Create a list of all temporary business names tmp_business_list = [d['name'] for d in data if d['corp_type']['code'] == CorpType.TMP.value] # NR Numbers nr_numbers = nr_number_name_dict.keys() filtered_affiliations: list = [] for entity in data: if entity['corp_type']['code'] == CorpType.NR.value: # If there is a TMP affiliation present for the NR, do not show NR if not entity['business_identifier'] in tmp_business_list: filtered_affiliations.append(entity) elif entity['corp_type']['code'] == CorpType.TMP.value: # If affiliation is not for a named company IA, and not a Numbered company # (name and businessIdentifier same) # --> Its a Temp affiliation with incorporation complete. # In this case, a TMP affiliation will be there but the name will be BC... if entity['name'] in nr_numbers or entity['name'] == entity['business_identifier']: # If temp affiliation is for an NR, change the name to NR's name if entity['name'] in nr_numbers: entity['name'] = nr_number_name_dict[entity['name']] filtered_affiliations.append(entity) else: filtered_affiliations.append(entity) current_app.logger.debug('>find_affiliations_by_org_id') return filtered_affiliations
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 test_update_contact(session): # pylint:disable=unused-argument """Assert that a contact for an existing Entity can be updated.""" entity_model = factory_entity_model() entity = EntityService(entity_model) entity.add_contact(TestContactInfo.contact1) dictionary = entity.as_dict() assert len(dictionary['contacts']) == 1 assert dictionary['contacts'][0]['email'] == TestContactInfo.contact1['email'] entity.update_contact(TestContactInfo.contact2) dictionary = None dictionary = entity.as_dict() assert len(dictionary['contacts']) == 1 assert dictionary['contacts'][0]['email'] == TestContactInfo.contact2['email']
def test_update_contact(session): # pylint:disable=unused-argument """Assert that a contact for an existing Entity can be updated.""" entity_model = factory_entity_model(business_identifier='CP1234567') entity = EntityService(entity_model) entity.add_contact(TEST_CONTACT_INFO) dictionary = entity.as_dict() assert len(dictionary['contacts']) == 1 assert dictionary['contacts'][0]['email'] == \ TEST_CONTACT_INFO['email'] entity.update_contact(TEST_UPDATED_CONTACT_INFO) dictionary = None dictionary = entity.as_dict() assert len(dictionary['contacts']) == 1 assert dictionary['contacts'][0]['email'] == \ TEST_UPDATED_CONTACT_INFO['email']
def test_delete_entity(app, session): # pylint:disable=unused-argument """Assert that an entity can be deleted.""" entity_model = factory_entity_model() entity = EntityService(entity_model) org = factory_org_service() 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() entity.delete() entity = EntityService.find_by_entity_id(entity.identifier) assert entity is None
def find_affiliated_entities_by_org_id(org_id): """Given an org_id, this will return the entities affiliated with it.""" current_app.logger.debug( '<find_affiliations_by_org_id for org_id {}'.format(org_id)) if not org_id: raise BusinessException(Error.DATA_NOT_FOUND, None) org = OrgService.find_by_org_id(org_id) if org is None: raise BusinessException(Error.DATA_NOT_FOUND, None) data = [] affiliation_models = AffiliationModel.find_affiliations_by_org_id( org_id) if affiliation_models is None: raise BusinessException(Error.DATA_NOT_FOUND, None) for affiliation_model in affiliation_models: if affiliation_model: data.append(EntityService(affiliation_model.entity).as_dict()) current_app.logger.debug('>find_affiliations_by_org_id') return data
def entity(self): """Return the entity for this affiliation as a service.""" return EntityService(self._model.entity)