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.public_user_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.public_user_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_user_where_org_has_affiliations(session, auth_mock, keycloak_mock): # pylint:disable=unused-argument """Assert that a user can be deleted.""" user_model = factory_user_model(user_info=TestUserInfo.user_test) contact = factory_contact_model() contact_link = ContactLinkModel() contact_link.contact = contact contact_link.user = user_model contact_link = contact_link.flush() contact_link.commit() org = OrgService.create_org(TestOrgInfo.org1, user_id=user_model.id).as_dict() org_id = org['id'] entity = factory_entity_model(entity_info=TestEntityInfo.entity_lear_mock) affiliation = AffiliationModel(org_id=org_id, entity_id=entity.id) affiliation.save() with pytest.raises(BusinessException) as exception: UserService.delete_user(TestJwtClaims.user_test) assert exception.code == Error.DELETE_FAILED_ONLY_OWNER updated_user = UserModel.find_by_jwt_token(TestJwtClaims.user_test) contacts = UserService.get_contacts(TestJwtClaims.user_test) assert len(contacts) == 1 user_orgs = MembershipModel.find_orgs_for_user(updated_user.id) for org in user_orgs: assert org.status_code == 'ACTIVE'
def add_contact(token, contact_info: dict): """Add contact information for an existing user.""" current_app.logger.debug('add_contact') user = UserModel.find_by_jwt_token(token) if user is None: raise BusinessException(Error.DATA_NOT_FOUND, None) # check for existing contact (we only want one contact per user) contact_link = ContactLinkModel.find_by_user_id(user.id) if contact_link is not None: raise BusinessException(Error.DATA_ALREADY_EXISTS, None) contact = ContactModel(**camelback2snake(contact_info)) contact = contact.flush() contact.commit() contact_link = ContactLinkModel() contact_link.user = user contact_link.contact = contact contact_link = contact_link.flush() contact_link.commit() return ContactService(contact)
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.commit() contact_link = ContactLinkModel() contact_link.contact = contact contact_link.org = org contact_link = contact_link.flush() contact_link.commit() current_app.logger.debug('<add_contact') return ContactService(contact)