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) 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_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
def put(org_id): """Update the org specified by the provided id with the request body.""" request_json = request.get_json() valid_format, errors = schema_utils.validate(request_json, 'org') token_info = g.jwt_oidc_token_info if not valid_format: return { 'message': schema_utils.serialize(errors) }, http_status.HTTP_400_BAD_REQUEST try: org = OrgService.find_by_org_id(org_id, allowed_roles=(*CLIENT_ADMIN_ROLES, STAFF)) if org and org.as_dict().get('accessType', None) == AccessType.ANONYMOUS.value and \ Role.STAFF_CREATE_ACCOUNTS.value not in token_info.get('realm_access').get('roles'): return {'message': 'The organisation can only be updated by a staff admin.'}, \ http_status.HTTP_401_UNAUTHORIZED if org: response, status = org.update_org(org_info=request_json).as_dict(), \ http_status.HTTP_200_OK else: response, status = {'message': 'The requested organization could not be found.'}, \ 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_delete_org_with_affiliation_fail(session, auth_mock, keycloak_mock): # pylint:disable=unused-argument """Assert that an org cannot be deleted.""" user_with_token = TestUserInfo.user_test user_with_token['keycloak_guid'] = TestJwtClaims.public_user_role['sub'] user = factory_user_model(user_info=user_with_token) org = OrgService.create_org(TestOrgInfo.org1, user.id) org_id = org.as_dict()['id'] entity_service = factory_entity_service(entity_info=TestEntityInfo.entity_lear_mock) entity_dictionary = entity_service.as_dict() business_identifier = entity_dictionary['businessIdentifier'] AffiliationService.create_affiliation(org_id, business_identifier, TestEntityInfo.entity_lear_mock['passCode'], {}) with pytest.raises(BusinessException) as exception: OrgService.delete_org(org_id, TestJwtClaims.public_user_role) assert exception.value.code == Error.ORG_CANNOT_BE_DISSOLVED.name AffiliationService.delete_affiliation(org_id, business_identifier, TestEntityInfo.entity_lear_mock['passCode']) OrgService.delete_org(org.as_dict()['id'], TestJwtClaims.public_user_role) org_inactive = OrgService.find_by_org_id(org.as_dict()['id']) assert org_inactive.as_dict()['org_status'] == 'INACTIVE'
def put(org_id): """Update the org specified by the provided id with the request body.""" request_json = request.get_json() valid_format, errors = schema_utils.validate(request_json, 'org') if not valid_format: return { 'message': schema_utils.serialize(errors) }, http_status.HTTP_400_BAD_REQUEST try: org = OrgService.find_by_org_id(org_id, g.jwt_oidc_token_info, allowed_roles=CLIENT_ADMIN_ROLES) if org: response, status = org.update_org( request_json).as_dict(), http_status.HTTP_200_OK else: response, status = {'message': 'The requested organization could not be found.'}, \ 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 post(org_id): """Create a new contact for the specified org.""" request_json = request.get_json() valid_format, errors = schema_utils.validate(request_json, 'contact') if not valid_format: return { 'message': schema_utils.serialize(errors) }, http_status.HTTP_400_BAD_REQUEST try: org = OrgService.find_by_org_id(org_id, g.jwt_oidc_token_info, allowed_roles=CLIENT_ADMIN_ROLES) if org: response, status = org.add_contact( request_json).as_dict(), http_status.HTTP_201_CREATED else: response, status = {'message': 'The requested organization could not be found.'}, \ 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 get(org_id): """Retrieve the set of payment settings associated with the specified org.""" try: org = OrgService.find_by_org_id(org_id, allowed_roles=(*CLIENT_ADMIN_ROLES, STAFF)) response, status = org.get_payment_info(), http_status.HTTP_200_OK except BusinessException as exception: response, status = {'code': exception.code, 'message': exception.message}, exception.status_code return response, status
def test_delete_org_with_members_success(session, auth_mock, keycloak_mock): # pylint:disable=unused-argument """Assert that an org can be deleted.""" user_with_token = TestUserInfo.user_test user_with_token['keycloak_guid'] = TestJwtClaims.public_user_role['sub'] user = factory_user_model(user_info=user_with_token) org = OrgService.create_org(TestOrgInfo.org1, user.id) OrgService.delete_org(org.as_dict()['id'], TestJwtClaims.public_user_role) org_inactive = OrgService.find_by_org_id(org.as_dict()['id']) assert org_inactive.as_dict()['org_status'] == 'INACTIVE'
def get(org_id): """Get the org specified by the provided id.""" org = OrgService.find_by_org_id(org_id, allowed_roles=ALL_ALLOWED_ROLES) if org is None: response, status = {'message': 'The requested organization could not be found.'}, \ http_status.HTTP_404_NOT_FOUND else: response, status = org.as_dict(), http_status.HTTP_200_OK return response, status
def test_find_org_by_id(session): # pylint:disable=unused-argument """Assert that an org can be retrieved by its id.""" org = factory_org_service(session, **TEST_ORG_INFO) dictionary = org.as_dict() org_id = dictionary['id'] found_org = OrgService.find_by_org_id(org_id) assert found_org dictionary = found_org.as_dict() assert dictionary['name'] == TEST_ORG_INFO['name']
def test_find_org_by_id(session, auth_mock): # pylint:disable=unused-argument """Assert that an org can be retrieved by its id.""" org = factory_org_service() dictionary = org.as_dict() org_id = dictionary['id'] found_org = OrgService.find_by_org_id(org_id) assert found_org dictionary = found_org.as_dict() assert dictionary['name'] == TestOrgInfo.org1['name']
def delete(org_id): """Delete the contact info for the specified org.""" try: org = OrgService.find_by_org_id(org_id) if org: response, status = org.delete_contact().as_dict(), http_status.HTTP_200_OK else: response, status = {'message': 'The requested organization could not be found.'}, \ 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_reset_user_without_tester_role(session, auth_mock): # pylint: disable=unused-argument """Assert that can not be reset data by the user doesn't have tester role.""" 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) response = ResetDataService.reset(TestJwtClaims.public_user_role) assert response is None found_org = OrgService.find_by_org_id(org.id) assert found_org is not None
def put(org_id): """Update the org specified by the provided id with the request body.""" org = OrgService.find_by_org_id(org_id) request_json = request.get_json() valid_format, errors = schema_utils.validate(request_json, 'org') if not valid_format: return {'message': schema_utils.serialize(errors)}, http_status.HTTP_400_BAD_REQUEST try: response, status = org.update_org(request_json).as_dict(), http_status.HTTP_200_OK except BusinessException as exception: response, status = {'code': exception.code, 'message': exception.message}, exception.status_code return response, status
def put(org_id): """Update an existing contact for the specified org.""" request_json = request.get_json() valid_format, errors = schema_utils.validate(request_json, 'contact') if not valid_format: return {'message': schema_utils.serialize(errors)}, http_status.HTTP_400_BAD_REQUEST try: org = OrgService.find_by_org_id(org_id) if org: response, status = org.update_contact(request_json).as_dict(), http_status.HTTP_200_OK else: response, status = {'message': 'The requested organization could not be found.'}, \ 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_reset_bceid_user(session, auth_mock): # pylint: disable=unused-argument """Assert that reset data from a bceid user.""" keycloak_service = KeycloakService() request = KeycloakScenario.create_user_by_user_info( TestJwtClaims.tester_bceid_role) keycloak_service.add_user(request, return_if_exists=True) user = keycloak_service.get_user_by_username(request.user_name) assert user is not None user_id = user.id user_with_token = TestUserInfo.user_bceid_tester user_with_token['keycloak_guid'] = user_id user = factory_user_model(user_info=user_with_token) org = factory_org_model(user_id=user.id) response = ResetDataService.reset( TestJwtClaims.get_test_user(user_id, 'BCEID')) assert response is None found_org = OrgService.find_by_org_id(org.id) assert found_org is None
def test_accept_invitation(session, auth_mock): # pylint:disable=unused-argument """Accept the invitation and add membership from the invitation to the org.""" with patch.object(InvitationService, 'send_invitation', return_value=None): with patch.object(auth, 'check_auth', return_value=True): with patch.object(InvitationService, 'notify_admin', return_value=None): user = factory_user_model(TestUserInfo.user_test) org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id) org_dictionary = org.as_dict() invitation_info = factory_invitation(org_dictionary['id']) new_invitation = InvitationService.create_invitation( invitation_info, User(user), {}, '') new_invitation_dict = new_invitation.as_dict() InvitationService.accept_invitation(new_invitation_dict['id'], User(user), '') org_dict = OrgService.find_by_org_id( org_dictionary['id'], allowed_roles={'basic'}).as_dict() assert len( org_dict['members'] ) == 2 # Member count will be 2 only if the invite accept is successful.
def put(org_id): """Update the org specified by the provided id with the request body.""" request_json = request.get_json() action = request.args.get('action', '').upper() valid_format, errors = schema_utils.validate(request_json, 'org') toke_info = g.jwt_oidc_token_info bearer_token = request.headers['Authorization'].replace('Bearer ', '') origin = request.environ.get('HTTP_ORIGIN', 'localhost') if not valid_format: return { 'message': schema_utils.serialize(errors) }, http_status.HTTP_400_BAD_REQUEST try: org = OrgService.find_by_org_id(org_id, g.jwt_oidc_token_info, allowed_roles=(*CLIENT_ADMIN_ROLES, STAFF)) if org and org.as_dict().get('accessType', None) == AccessType.ANONYMOUS.value and \ Role.STAFF_CREATE_ACCOUNTS.value not in toke_info.get('realm_access').get('roles'): return {'message': 'The organisation can only be updated by a staff admin.'}, \ http_status.HTTP_401_UNAUTHORIZED if org: if action in (ChangeType.DOWNGRADE.value, ChangeType.UPGRADE.value): response, status = org.change_org_ype( request_json, action, bearer_token).as_dict(), http_status.HTTP_200_OK else: response, status = org.update_org(org_info=request_json, token_info=toke_info, bearer_token=bearer_token, origin_url=origin).as_dict(), \ http_status.HTTP_200_OK else: response, status = {'message': 'The requested organization could not be found.'}, \ 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 get(org_id): """Retrieve the set of invitations for the given org.""" try: invitation_status = request.args.get( 'status').upper() if request.args.get('status') else 'ALL' org = OrgService.find_by_org_id(org_id, g.jwt_oidc_token_info, allowed_roles=(*CLIENT_ADMIN_ROLES, STAFF)) if org: response, status = jsonify(org.get_invitations(invitation_status, g.jwt_oidc_token_info)), \ http_status.HTTP_200_OK else: response, status = {'message': 'The requested organization could not be found.'}, \ 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_find_org_by_id_no_org(session): # pylint:disable=unused-argument """Assert that an org which does not exist cannot be retrieved.""" org = OrgService.find_by_org_id(99) assert org is None