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_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_user_link(session, auth_mock, keycloak_mock): # pylint:disable=unused-argument """Assert that a contact can not be deleted if contact link exists.""" user_with_token = TestUserInfo.user_test user_with_token['keycloak_guid'] = TestJwtClaims.edit_role['sub'] user_model = factory_user_model(user_info=user_with_token) user = UserService(user_model) org = OrgService.create_org(TestOrgInfo.org1, user_id=user.identifier) org_dictionary = org.as_dict() org_id = org_dictionary['id'] contact = factory_contact_model() contact_link = ContactLinkModel() contact_link.contact = contact contact_link.user = user_model contact_link.org = org._model # pylint:disable=protected-access contact_link = contact_link.flush() contact_link.commit() deleted_contact = UserService.delete_contact(TestJwtClaims.edit_role) assert deleted_contact is None delete_contact_link = ContactLinkModel.find_by_user_id(user.identifier) assert not delete_contact_link exist_contact_link = ContactLinkModel.find_by_org_id(org_id) assert exist_contact_link
def test_delete_contact_user_link(session, auth_mock): # pylint:disable=unused-argument """Assert that a contact can not be deleted if contact link exists.""" user_model = factory_user_model(user_info=TestUserInfo.user_test) user = UserService(user_model) org = OrgService.create_org(TestOrgInfo.org1, user_id=user.identifier) org_dictionary = org.as_dict() org_id = org_dictionary['id'] contact = factory_contact_model() contact_link = ContactLinkModel() contact_link.contact = contact contact_link.user = user_model contact_link.org = org._model # pylint:disable=protected-access contact_link.commit() updated_user = user.delete_contact(TestJwtClaims.user_test) dictionary = None dictionary = updated_user.as_dict() assert len(dictionary['contacts']) == 0 delete_contact_link = ContactLinkModel.find_by_user_id(user.identifier) assert not delete_contact_link exist_contact_link = ContactLinkModel.find_by_org_id(org_id) assert exist_contact_link
def add_contact_to_org(mailing_address, org): """Update the passed organization with the mailing address.""" contact = ContactModel(**camelback2snake(mailing_address)) contact = contact.add_to_session() contact_link = ContactLinkModel() contact_link.contact = contact contact_link.org = org contact_link.add_to_session()
def add_contact(self, contact_info): """Create a new contact for this org.""" # check for existing contact (only one contact per org for now) current_app.logger.debug('>add_contact ') contact_link = ContactLinkModel.find_by_org_id(self._model.id) if contact_link is not None: raise BusinessException(Error.DATA_ALREADY_EXISTS, None) contact = ContactModel(**camelback2snake(contact_info)) contact.commit() contact_link = ContactLinkModel() contact_link.contact = contact contact_link.org = self._model contact_link.commit() current_app.logger.debug('>add_contact ') return self
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 add_contact(org_id, contact_info): """Create a new contact for this org.""" # check for existing contact (only one contact per org for now) current_app.logger.debug('>add_contact') org = OrgModel.find_by_org_id(org_id) if org is None: raise BusinessException(Error.DATA_NOT_FOUND, None) contact_link = ContactLinkModel.find_by_org_id(org_id) if contact_link is not None: raise BusinessException(Error.DATA_ALREADY_EXISTS, None) contact = ContactModel(**camelback2snake(contact_info)) contact = contact.flush() contact_link = ContactLinkModel() contact_link.contact = contact contact_link.org = org contact_link.save() current_app.logger.debug('<add_contact') return ContactService(contact)