Exemplo n.º 1
0
def test_create_product_single_subscription_duplicate_error(
        session, keycloak_mock):  # pylint:disable=unused-argument
    """Assert that an Org can be created."""
    user_with_token = TestUserInfo.user_bceid_tester
    user_with_token['keycloak_guid'] = TestJwtClaims.public_bceid_user['sub']
    user = factory_user_model(user_with_token)
    org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id)
    assert org
    dictionary = org.as_dict()
    assert dictionary['name'] == TestOrgInfo.org1['name']
    subscriptions = ProductService.create_product_subscription(
        dictionary['id'],
        TestOrgProductsInfo.org_products1,
        skip_auth=True,
        token_info=TestJwtClaims.public_bceid_user)
    assert len(subscriptions) == 1
    assert subscriptions[0].product_code == TestOrgProductsInfo.org_products1[
        'subscriptions'][0]['productCode']
    with pytest.raises(BusinessException) as exception:
        ProductService.create_product_subscription(
            dictionary['id'],
            TestOrgProductsInfo.org_products1,
            skip_auth=True,
            token_info=TestJwtClaims.public_bceid_user)
    assert exception.value.code == Error.PRODUCT_SUBSCRIPTION_EXISTS.name
Exemplo n.º 2
0
def test_create_product_single_subscription_duplicate_error(
        session, keycloak_mock, monkeypatch):  # pylint:disable=unused-argument
    """Assert that an Org can be created."""
    user_with_token = TestUserInfo.user_bceid_tester
    user_with_token['keycloak_guid'] = TestJwtClaims.public_bceid_user['sub']
    user = factory_user_model(user_with_token)
    patch_token_info({'sub': user.keycloak_guid}, monkeypatch)
    org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id)
    assert org
    dictionary = org.as_dict()
    assert dictionary['name'] == TestOrgInfo.org1['name']

    patch_token_info(TestJwtClaims.public_bceid_user, monkeypatch)
    subscriptions = ProductService.create_product_subscription(
        dictionary['id'],
        TestOrgProductsInfo.org_products_business,
        skip_auth=True)
    assert next(prod for prod in subscriptions
                if prod.get('code') == TestOrgProductsInfo.
                org_products_business['subscriptions'][0]['productCode'])

    with pytest.raises(BusinessException) as exception:
        ProductService.create_product_subscription(
            dictionary['id'],
            TestOrgProductsInfo.org_products_business,
            skip_auth=True)
    assert exception.value.code == Error.PRODUCT_SUBSCRIPTION_EXISTS.name
Exemplo n.º 3
0
def test_create_product_multiple_subscription(session, keycloak_mock, monkeypatch):  # pylint:disable=unused-argument
    """Assert that an Org can be created."""
    user_with_token = TestUserInfo.user_bceid_tester
    user_with_token['keycloak_guid'] = TestJwtClaims.public_bceid_user['sub']
    user = factory_user_model(user_with_token)

    def token_info():  # pylint: disable=unused-argument; mocks of library methods
        return {
            'sub': str(user_with_token['keycloak_guid']),
            'username': '******',
            'realm_access': {
                'roles': [
                    'edit'
                ]
            }
        }

    monkeypatch.setattr('auth_api.utils.user_context._get_token_info', token_info)

    org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id)
    assert org
    dictionary = org.as_dict()
    assert dictionary['name'] == TestOrgInfo.org1['name']
    subscriptions = ProductService.create_product_subscription(dictionary['id'],
                                                               TestOrgProductsInfo.org_products2,
                                                               skip_auth=True,
                                                               token_info=TestJwtClaims.public_bceid_user)
    assert next(prod for prod in subscriptions
                if prod.get('code') == TestOrgProductsInfo.org_products2['subscriptions'][0]['productCode'])
    assert next(prod for prod in subscriptions
                if prod.get('code') == TestOrgProductsInfo.org_products2['subscriptions'][1]['productCode'])
Exemplo n.º 4
0
    def post(org_id):
        """Post a new product subscription to the org using the request body."""
        request_json = request.get_json()
        valid_format, errors = schema_utils.validate(
            request_json, 'org_product_subscription')
        if not valid_format:
            return {
                'message': schema_utils.serialize(errors)
            }, http_status.HTTP_400_BAD_REQUEST

        try:
            subscriptions = ProductService.create_product_subscription(
                org_id, request_json)
            if subscriptions is None:
                response, status = {'message': 'Not authorized to perform this action'}, \
                                   http_status.HTTP_401_UNAUTHORIZED
            else:
                response, status = {'subscriptions': ProductSubscriptionSchema().dump(subscriptions, many=True)}, \
                                   http_status.HTTP_201_CREATED
        except BusinessException as exception:
            response, status = {
                'code': exception.code,
                'message': exception.message
            }, exception.status_code
        return response, status
Exemplo n.º 5
0
def test_create_product_single_subscription(session, keycloak_mock):  # pylint:disable=unused-argument
    """Assert that an Org can be created."""
    user = factory_user_model()
    org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id)
    assert org
    dictionary = org.as_dict()
    assert dictionary['name'] == TestOrgInfo.org1['name']
    subscriptions = ProductService.create_product_subscription(dictionary['id'], TestOrgProductsInfo.org_products1, )
    assert len(subscriptions) == 1
    assert subscriptions[0].product_code == TestOrgProductsInfo.org_products1['subscriptions'][0]['productCode']
Exemplo n.º 6
0
def test_create_product_single_subscription(session, keycloak_mock):  # pylint:disable=unused-argument
    """Assert that an Org can be created."""
    user_with_token = TestUserInfo.user_bceid_tester
    user_with_token['keycloak_guid'] = TestJwtClaims.public_bceid_user['sub']
    user = factory_user_model(user_with_token)
    org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id)
    assert org
    dictionary = org.as_dict()
    assert dictionary['name'] == TestOrgInfo.org1['name']
    subscriptions = ProductService.create_product_subscription(dictionary['id'],
                                                               TestOrgProductsInfo.org_products1,
                                                               skip_auth=True,
                                                               token_info=TestJwtClaims.public_bceid_user)
    assert next(prod for prod in subscriptions
                if prod.get('code') == TestOrgProductsInfo.org_products1['subscriptions'][0]['productCode'])
Exemplo n.º 7
0
    def post(org_id):
        """Post a new product subscription to the org using the request body."""
        request_json = request.get_json()
        valid_format, errors = schema_utils.validate(
            request_json, 'org_product_subscription')
        if not valid_format:
            return {
                'message': schema_utils.serialize(errors)
            }, http_status.HTTP_400_BAD_REQUEST

        try:
            subscriptions = ProductService.create_product_subscription(
                org_id, request_json)
            response, status = {
                'subscriptions': subscriptions
            }, http_status.HTTP_201_CREATED
        except BusinessException as exception:
            response, status = {
                'code': exception.code,
                'message': exception.message
            }, exception.status_code
        return response, status