def test_delete_user_as_only_admin_returns_400(client, jwt, session, keycloak_mock): # pylint:disable=unused-argument """Test if the user is the only owner of a team assert status is 400.""" 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.commit() org = OrgService.create_org(TestOrgInfo.org1, user_id=user_model.id) org_dictionary = org.as_dict() org_id = org_dictionary['id'] entity = factory_entity_model(entity_info=TestEntityInfo.entity_lear_mock) affiliation = AffiliationModel(org_id=org_id, entity_id=entity.id) affiliation.save() claims = copy.deepcopy(TestJwtClaims.edit_role.value) claims['sub'] = str(user_model.keycloak_guid) headers = factory_auth_header(jwt=jwt, claims=claims) rv = client.delete('/api/v1/users/@me', headers=headers, content_type='application/json') assert rv.status_code == http_status.HTTP_400_BAD_REQUEST
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 test_delete_user_is_member_returns_204(client, jwt, session, keycloak_mock): # pylint:disable=unused-argument """Test if the user is the member of a team assert status is 204.""" 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.commit() org = OrgService.create_org(TestOrgInfo.org1, user_id=user_model.id) org_dictionary = org.as_dict() org_id = org_dictionary['id'] entity = factory_entity_model(entity_info=TestEntityInfo.entity_lear_mock) affiliation = AffiliationModel(org_id=org_id, entity_id=entity.id) affiliation.save() user_model2 = factory_user_model(user_info=TestUserInfo.user2) contact = factory_contact_model() contact_link = ContactLinkModel() contact_link.contact = contact contact_link.user = user_model2 contact_link.commit() membership = MembershipModel(org_id=org_id, user_id=user_model2.id, membership_type_code='MEMBER', membership_type_status=Status.ACTIVE.value) membership.save() claims = copy.deepcopy(TestJwtClaims.public_user_role.value) claims['sub'] = str(user_model2.keycloak_guid) headers = factory_auth_header(jwt=jwt, claims=claims) rv = client.delete('/api/v1/users/@me', headers=headers, content_type='application/json') assert rv.status_code == http_status.HTTP_204_NO_CONTENT
def test_delete_user_where_org_has_another_owner(session, auth_mock, keycloak_mock, monkeypatch): # pylint:disable=unused-argument """Assert that a user can be deleted.""" # Create a user and org 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.commit() patch_token_info(TestJwtClaims.get_test_user(user_model.keycloak_guid), monkeypatch) org = OrgService.create_org(TestOrgInfo.org1, user_id=user_model.id) org_dictionary = org.as_dict() org_id = org_dictionary['id'] entity = factory_entity_model(entity_info=TestEntityInfo.entity_lear_mock) affiliation = AffiliationModel(org_id=org_id, entity_id=entity.id) affiliation.save() # Create another user and add membership to the above org user_model2 = factory_user_model(user_info=TestUserInfo.user2) contact = factory_contact_model() contact_link = ContactLinkModel() contact_link.contact = contact contact_link.user = user_model2 contact_link.commit() membership = MembershipModel(org_id=org_id, user_id=user_model2.id, membership_type_code='ADMIN', membership_type_status=Status.ACTIVE.value) membership.save() membership.commit() # with pytest.raises(BusinessException) as exception: patch_token_info(TestJwtClaims.get_test_user(user_model2.keycloak_guid), monkeypatch) UserService.delete_user() updated_user = UserModel.find_by_jwt_token() assert len(updated_user.contacts) == 0 user_orgs = MembershipModel.find_orgs_for_user(updated_user.id) for org in user_orgs: assert org.status_code == 'INACTIVE'
def factory_affiliation_model(entity_id, org_id): """Produce a templated affiliation model.""" affiliation = AffiliationModel(entity_id=entity_id, org_id=org_id) affiliation.save() return affiliation
def factory_affiliation_service(entity_id, org_id): """Produce a templated affiliation service.""" affiliation = AffiliationModel(entity=entity_id, org=org_id) affiliation.save() affiliation_service = AffiliationService(affiliation) return affiliation_service
async def process_name_events(event_message: Dict[str, any]): """Process name events. 1. Check if the NR already exists in entities table, if yes apply changes. If not create entity record. 2. Check if new status is DRAFT, if yes call pay-api and get the account details for the payments against the NR. 3. If an account is found, affiliate to that account. Args: event_message (object): cloud event message, sample below. { 'specversion': '1.0.1', 'type': 'bc.registry.names.events', 'source': '/requests/6724165', 'id': id, 'time': '', 'datacontenttype': 'application/json', 'identifier': '781020202', 'data': { 'request': { 'nrNum': 'NR 5659951', 'newState': 'APPROVED', 'previousState': 'DRAFT' } } } """ logger.debug('>>>>>>>process_name_events>>>>>') request_data = event_message.get('data').get('request') nr_number = request_data['nrNum'] nr_status = request_data['newState'] nr_entity = EntityModel.find_by_business_identifier(nr_number) if nr_entity is None: logger.info('Entity doesn' 't exist, creating a new entity.') nr_entity = EntityModel(business_identifier=nr_number, corp_type_code=CorpType.NR.value) nr_entity.status = nr_status nr_entity.name = request_data.get( 'name', '') # its not part of event now, this is to handle if they include it. nr_entity.last_modified_by = None # TODO not present in event message. nr_entity.last_modified = parser.parse(event_message.get('time')) if nr_status == 'DRAFT' and AffiliationModel.find_affiliations_by_business_identifier( nr_number) is None: logger.info('Status is DRAFT, getting invoices for account') # Find account details for the NR. invoices = RestService.get( f'{APP_CONFIG.PAY_API_URL}/payment-requests?businessIdentifier={nr_number}', token=RestService.get_service_account_token()).json() # Ideally there should be only one or two (priority fees) payment request for the NR. if invoices and (auth_account_id := invoices['invoices'][0].get('paymentAccount').get('accountId')) \ and str(auth_account_id).isnumeric(): logger.info('Account ID received : %s', auth_account_id) # Auth account id can be service account value too, so doing a query lookup than find_by_id org: OrgModel = db.session.query(OrgModel).filter( OrgModel.id == auth_account_id).one_or_none() if org: nr_entity.pass_code_claimed = True # Create an affiliation. logger.info( 'Creating affiliation between Entity : %s and Org : %s', nr_entity, org) affiliation: AffiliationModel = AffiliationModel( entity=nr_entity, org=org) affiliation.flush()