Ejemplo n.º 1
0
def test_returns_a_429_limit_exceeded_if_rate_limit_exceeded(
        client, sample_service, mocker, notification_type, key_send_to,
        send_to):
    sample = create_template(service=sample_service,
                             template_type=notification_type)
    persist_mock = mocker.patch(
        'app.v2.notifications.post_notifications.persist_notification')
    deliver_mock = mocker.patch(
        'app.v2.notifications.post_notifications.send_notification_to_queue')
    mocker.patch('app.v2.notifications.post_notifications.check_rate_limiting',
                 side_effect=RateLimitError("LIMIT", "INTERVAL", "TYPE"))

    data = {key_send_to: send_to, 'template_id': str(sample.id)}

    auth_header = create_authorization_header(service_id=sample.service_id)

    response = client.post(
        path='/v2/notifications/{}'.format(notification_type),
        data=json.dumps(data),
        headers=[('Content-Type', 'application/json'), auth_header])

    error = json.loads(response.data)['errors'][0]['error']
    message = json.loads(response.data)['errors'][0]['message']
    status_code = json.loads(response.data)['status_code']
    assert response.status_code == 429
    assert error == 'RateLimitError'
    assert message == 'Exceeded rate limit for key type TYPE of LIMIT requests per INTERVAL seconds'
    assert status_code == 429

    assert not persist_mock.called
    assert not deliver_mock.called
Ejemplo n.º 2
0
def test_returns_a_429_limit_exceeded_if_rate_limit_exceeded(
        client, sample_letter_template, mocker):
    persist_mock = mocker.patch(
        'app.v2.notifications.post_notifications.persist_notification')
    mocker.patch('app.v2.notifications.post_notifications.check_rate_limiting',
                 side_effect=RateLimitError('LIMIT', 'INTERVAL', 'TYPE'))

    data = {
        'template_id': str(sample_letter_template.id),
        'personalisation': test_address
    }

    error_json = letter_request(client,
                                data,
                                service_id=sample_letter_template.service_id,
                                _expected_status=429)

    assert error_json['status_code'] == 429
    assert error_json['errors'] == [{
        'error':
        'RateLimitError',
        'message':
        'Exceeded rate limit for key type TYPE of LIMIT requests per INTERVAL seconds'
    }]

    assert not persist_mock.called
def test_returns_a_429_limit_exceeded_if_rate_limit_exceeded(
        client, sample_letter_template, mocker):
    persist_mock = mocker.patch(
        "app.v2.notifications.post_notifications.persist_notification")
    mocker.patch(
        "app.v2.notifications.post_notifications.check_rate_limiting",
        side_effect=RateLimitError("LIMIT", "INTERVAL", "TYPE"),
    )

    data = {
        "template_id": str(sample_letter_template.id),
        "personalisation": test_address,
    }

    error_json = letter_request(client,
                                data,
                                service_id=sample_letter_template.service_id,
                                _expected_status=429)

    assert error_json["status_code"] == 429
    assert error_json["errors"] == [{
        "error":
        "RateLimitError",
        "message":
        "Exceeded rate limit for key type TYPE of LIMIT requests per INTERVAL seconds",
    }]

    assert not persist_mock.called
Ejemplo n.º 4
0
def check_service_over_api_rate_limit(service, api_key):
    if current_app.config['API_RATE_LIMIT_ENABLED'] and current_app.config['REDIS_ENABLED']:
        cache_key = rate_limit_cache_key(service.id, api_key.key_type)
        rate_limit = service.rate_limit
        interval = 60
        if redis_store.exceeded_rate_limit(cache_key, rate_limit, interval):
            current_app.logger.info("service {} has been rate limited for throughput".format(service.id))
            raise RateLimitError(rate_limit, interval, api_key.key_type)