def test_get_subscription_by_id__does_not_belong_to_the_user__returns_none(
        generate_subscription):
    subscription1 = generate_subscription()
    subscription2 = generate_subscription()

    assert get_subscription_by_id(subscription1.id,
                                  subscription2.user_id) is None
    assert get_subscription_by_id(subscription2.id,
                                  subscription1.user_id) is None
def test_put_subscription__subscription_is_updated_in_db_and_broker(
        mock_broker_client, test_client, generate_subscription, test_user):
    mock_broker_client.bind_queue_to_topic = mock.Mock(return_value=None)
    mock_broker_client.delete_queue_binding = mock.Mock(return_value=None)

    subscription = generate_subscription(user=test_user,
                                         with_broker_queue=True)

    subscription_data = {'active': not subscription.active}

    url = f'{BASE_PATH}/subscriptions/{subscription.id}'

    response = test_client.put(url,
                               data=json.dumps(subscription_data),
                               content_type='application/json',
                               headers=basic_auth_header(test_user))

    assert 200 == response.status_code

    response_data = json.loads(response.data)
    assert subscription_data['active'] == response_data['active']

    db_subscription = get_subscription_by_id(subscription.id)
    assert subscription_data['active'] == db_subscription.active

    # remove the queue from the broker
    broker.delete_queue(subscription.queue)
def test_get_subscription_by_id__object_exists_and_is_returned(
        generate_subscription):
    subscription = generate_subscription()

    db_subscription = get_subscription_by_id(subscription.id,
                                             subscription.user_id)

    assert isinstance(db_subscription, Subscription)
    assert subscription.id == db_subscription.id
    assert subscription.user_id == db_subscription.user_id
Exemplo n.º 4
0
def get_subscription(subscription_id: int) -> Subscription:
    """
    GET /subscription/{subscription_id}

    :raises: backend.errors.UnauthorizedError (HTTP error 401)
             backend.errors.ForbiddenError (HTTP error 403)
             backend.errors.NotFoundError (HTTP error 404)

    """
    user = request.user
    params = {} if user.is_admin else {'user_id': user.id}

    result = db.get_subscription_by_id(subscription_id, **params)

    if result is None:
        raise NotFoundError(
            f"Subscription with id {subscription_id} does not exist")

    return result
def test_post_subscription__subscription_is_saved_in_db(
        mock_broker_client, test_client, generate_topic, test_user):
    mock_broker_client.create_queue_for_topic = mock.Mock(return_value=None)

    topic = generate_topic('test_topic')

    subscription_data = {
        'topic_id': topic.id,
        'active': True,
        'qos': QOS.EXACTLY_ONCE.value,
        'durable': True
    }

    url = f'{BASE_PATH}/subscriptions/'

    response = test_client.post(url,
                                data=json.dumps(subscription_data),
                                content_type='application/json',
                                headers=basic_auth_header(test_user))

    assert 201 == response.status_code

    response_data = json.loads(response.data)
    assert 'id' in response_data
    assert isinstance(response_data['id'], int)
    assert 'queue' in response_data
    assert isinstance(response_data['queue'], str)

    assert subscription_data['durable'] == response_data['durable']
    assert subscription_data['active'] == response_data['active']
    assert subscription_data['qos'] == response_data['qos']
    assert subscription_data['topic_id'] == response_data['topic']['id']

    db_subscription = get_subscription_by_id(response_data['id'])
    assert db_subscription is not None
    assert subscription_data['qos'] == db_subscription.qos.value
    assert subscription_data['active'] == db_subscription.active
    assert subscription_data['durable'] == db_subscription.durable
    assert subscription_data['topic_id'] == db_subscription.topic.id

    broker.delete_queue(response_data['queue'])
Exemplo n.º 6
0
def delete_subscription(subscription_id: int) -> t.Tuple[None, int]:
    """
    DELETE /subscriptions/{subscription_id}

    :raises: backend.errors.UnauthorizedError (HTTP error 401)
             backend.errors.NotFoundError (HTTP error 404)
    """
    user = request.user
    params = {} if user.is_admin else {'user_id': user.id}

    subscription = db.get_subscription_by_id(subscription_id, **params)

    if subscription is None:
        raise NotFoundError(f"Topic with id {subscription_id} does not exist")

    try:
        events.delete_subscription_event(subscription)
    except broker.BrokerError as e:
        raise BadGatewayError(f"Error while accessing broker: {str(e)}")

    return None, 204
Exemplo n.º 7
0
def put_subscription(subscription_id: int) -> JSONType:
    """
    PUT /subscriptions/{subscription_id}

    :raises: backend.errors.UnauthorizedError (HTTP error 401)
             backend.errors.ForbiddenError (HTTP error 403)
             backend.errors.NotFoundError (HTTP error 404)
             backend.errors.BadRequestError (HTTP error 400)
             backend.errors.ConflictError (HTTP error 409)
             backend.errors.BadGatewayError (HTTP error 502)
    """

    user = request.user
    params = {} if user.is_admin else {'user_id': user.id}

    subscription = db.get_subscription_by_id(subscription_id, **params)

    if subscription is None:
        raise NotFoundError(
            f"Subscription with id {subscription_id} does not exist")

    current_subscription = deepcopy(subscription)
    try:
        updated_subscription = SubscriptionSchema().load(
            data=request.get_json(), instance=subscription)

        events.update_subscription_event(current_subscription,
                                         updated_subscription)
    except ValidationError as e:
        raise BadRequestError(str(e))
    except broker.BrokerError as e:
        raise BadGatewayError(f"Error while accessing broker: {str(e)}")
    except SQLAlchemyError as e:
        if is_duplicate_record_error(e):
            raise ConflictError(
                "Subscription with same data already exists in DB")
        raise

    return updated_subscription
def test_get_subscription_by_id__does_not_exist__returns_none():
    assert get_subscription_by_id(1111) is None
def test_delete_subscription(generate_subscription):
    subscription = generate_subscription()

    delete_subscription(subscription)

    assert None is get_subscription_by_id(subscription.id)