Example #1
0
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'
Example #2
0
def test_delete_does_not_remove_user_from_account_holder_group(session, monkeypatch,
                                                               auth_mock):  # pylint:disable=unused-argument
    """Assert that if the user has multiple Orgs, and deleting one doesn't remove account holders group."""
    # Create a user in keycloak
    keycloak_service = KeycloakService()
    request = KeycloakScenario.create_user_request()
    keycloak_service.add_user(request, return_if_exists=True)
    kc_user = keycloak_service.get_user_by_username(request.user_name)
    user = factory_user_model(TestUserInfo.get_user_with_kc_guid(kc_guid=kc_user.id))

    # Patch token info
    def token_info():  # pylint: disable=unused-argument; mocks of library methods
        return {
            'sub': str(kc_user.id),
            'username': '******',
            'realm_access': {
                'roles': [
                ]
            }
        }

    monkeypatch.setattr('auth_api.services.keycloak.KeycloakService._get_token_info', token_info)
    org1 = OrgService.create_org(TestOrgInfo.org1, user_id=user.id)
    OrgService.create_org(TestOrgInfo.org2, user_id=user.id)
    OrgService.delete_org(org1.as_dict().get('id'), token_info())

    user_groups = keycloak_service.get_user_groups(user_id=kc_user.id)
    groups = []
    for group in user_groups:
        groups.append(group.get('name'))
    assert GROUP_ACCOUNT_HOLDERS in groups
Example #3
0
def test_delete_org_with_affiliation(session, auth_mock, keycloak_mock,
                                     monkeypatch):  # 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)

    patch_token_info({'sub': user.keycloak_guid}, monkeypatch)
    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['business_identifier']
    AffiliationService.create_affiliation(
        org_id, business_identifier,
        TestEntityInfo.entity_lear_mock['passCode'])

    patch_token_info(TestJwtClaims.public_user_role, monkeypatch)
    patch_pay_account_delete(monkeypatch)
    OrgService.delete_org(org_id)

    assert len(
        AffiliationService.find_visible_affiliations_by_org_id(org_id)) == 0
Example #4
0
 def delete(org_id):
     """Inactivates the org if it has no active members or affiliations."""
     try:
         OrgService.delete_org(org_id)
         response, status = '', http_status.HTTP_204_NO_CONTENT
     except BusinessException as exception:
         response, status = {'code': exception.code, 'message': exception.message}, exception.status_code
     return response, status
Example #5
0
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'
Example #6
0
def test_delete_org_with_members_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)
    user2 = factory_user_model(user_info=TestUserInfo.user2)
    factory_membership_model(user2.id, org._model.id, member_type='ADMIN')
    user3 = factory_user_model(user_info=TestUserInfo.user3)
    factory_membership_model(user3.id, org._model.id, member_type='OWNER')

    with pytest.raises(BusinessException) as exception:
        OrgService.delete_org(org.as_dict()['id'], TestJwtClaims.public_user_role)

    assert exception.value.code == Error.ORG_CANNOT_BE_DISSOLVED.name
Example #7
0
def test_delete_org_with_members(session, auth_mock, keycloak_mock, monkeypatch):  # 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)

    patch_token_info({'sub': user.keycloak_guid}, monkeypatch)
    org = OrgService.create_org(TestOrgInfo.org1, user.id)
    user2 = factory_user_model(user_info=TestUserInfo.user2)
    factory_membership_model(user2.id, org._model.id, member_type='COORDINATOR')
    user3 = factory_user_model(user_info=TestUserInfo.user3)
    factory_membership_model(user3.id, org._model.id, member_type='ADMIN')

    patch_token_info(TestJwtClaims.public_user_role, monkeypatch)
    patch_pay_account_delete(monkeypatch)
    org_id = org.as_dict()['id']

    OrgService.delete_org(org_id)
    assert len(MembershipService.get_members_for_org(org_id)) == 0
Example #8
0
def test_delete_org_removes_user_from_account_holders_group(session, auth_mock,
                                                            monkeypatch):  # pylint:disable=unused-argument
    """Assert that an Org deletion removes the user from account holders group."""
    # Create a user in keycloak
    keycloak_service = KeycloakService()
    request = KeycloakScenario.create_user_request()
    keycloak_service.add_user(request, return_if_exists=True)
    kc_user = keycloak_service.get_user_by_username(request.user_name)
    user = factory_user_model(TestUserInfo.get_user_with_kc_guid(kc_guid=kc_user.id))

    patch_token_info({'sub': user.keycloak_guid}, monkeypatch)
    patch_pay_account_delete(monkeypatch)
    org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id)
    OrgService.delete_org(org.as_dict().get('id'))

    user_groups = keycloak_service.get_user_groups(user_id=kc_user.id)
    groups = []
    for group in user_groups:
        groups.append(group.get('name'))
    assert GROUP_ACCOUNT_HOLDERS not in groups