Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_infinite_retries_deadline_and_backoff(backoff):
service = _AlwaysUnavailable()
server = grpc_server(service.handler)
with default_channel() as channel:
interceptor = RetryInterceptor(max_retry_count=-1, retriable_codes=[grpc.StatusCode.UNAVAILABLE],
add_retry_count_to_header=True, back_off_func=backoff)
ch = grpc.intercept_channel(channel, interceptor)
client = zone_service_pb2_grpc.ZoneServiceStub(ch)
with pytest.raises(grpc.RpcError) as e:
client.Get(zone_service_pb2.GetZoneRequest(zone_id="id"), timeout=5)
assert e.value.code() == grpc.StatusCode.DEADLINE_EXCEEDED
server.stop(0)
def test_five_retries():
service = _FailFirstAttempts(5)
server = grpc_server(service.handler)
with default_channel() as channel:
for max_retry_count in range(4):
interceptor = RetryInterceptor(max_retry_count=max_retry_count)
ch = grpc.intercept_channel(channel, interceptor)
client = zone_service_pb2_grpc.ZoneServiceStub(ch)
with pytest.raises(grpc.RpcError) as e:
client.Get(zone_service_pb2.GetZoneRequest(zone_id="id"))
assert e.value.code() == grpc.StatusCode.UNAVAILABLE
service.reset(5)
interceptor = RetryInterceptor(max_retry_count=5)
ch = grpc.intercept_channel(channel, interceptor)
client = zone_service_pb2_grpc.ZoneServiceStub(ch)
res = client.Get(zone_service_pb2.GetZoneRequest(zone_id="id"))
assert res == DEFAULT_ZONE
def test_idempotency_token_not_changed():
token = str(uuid.uuid4())
service = _TokenUnchanged(token)
server = grpc_server(service.handler)
with default_channel() as channel:
interceptor = RetryInterceptor(max_retry_count=100, retriable_codes=[grpc.StatusCode.UNAVAILABLE],
add_retry_count_to_header=True)
ch = grpc.intercept_channel(channel, interceptor)
client = zone_service_pb2_grpc.ZoneServiceStub(ch)
with pytest.raises(grpc.RpcError) as e:
client.Get(zone_service_pb2.GetZoneRequest(zone_id="id"), metadata=[("idempotency-key", token)])
assert e.value.code() == grpc.StatusCode.UNAVAILABLE
assert not service.token_changed
server.stop(0)
service = _RetriableCodes(retriable_codes)
server = grpc_server(service.handler)
with default_channel() as channel:
for retry_qty in range(len(retriable_codes)):
interceptor = RetryInterceptor(max_retry_count=retry_qty, retriable_codes=retriable_codes)
ch = grpc.intercept_channel(channel, interceptor)
client = zone_service_pb2_grpc.ZoneServiceStub(ch)
with pytest.raises(grpc.RpcError) as e:
client.Get(zone_service_pb2.GetZoneRequest(zone_id="id"))
assert e.value.code() == retriable_codes[retry_qty]
service.reset_state()
interceptor = RetryInterceptor(max_retry_count=len(retriable_codes), retriable_codes=retriable_codes)
ch = grpc.intercept_channel(channel, interceptor)
client = zone_service_pb2_grpc.ZoneServiceStub(ch)
assert client.Get(zone_service_pb2.GetZoneRequest(zone_id="id")) == DEFAULT_ZONE
server.stop(0)
def test_header_token_and_retry_count():
service = _HeaderTokenAndRetryCount()
server = grpc_server(service.handler)
with default_channel() as channel:
interceptor = RetryInterceptor(max_retry_count=100, retriable_codes=[grpc.StatusCode.UNAVAILABLE],
add_retry_count_to_header=True)
ch = grpc.intercept_channel(channel, interceptor)
client = zone_service_pb2_grpc.ZoneServiceStub(ch)
with pytest.raises(grpc.RpcError) as e:
client.Get(zone_service_pb2.GetZoneRequest(zone_id="id"))
assert e.value.code() == grpc.StatusCode.UNAVAILABLE
assert not service.error
server.stop(0)
def test_invalid_service_account_type():
with pytest.raises(RuntimeError) as e:
get_auth_token_requester(service_account_key=[]).get_token_request()
assert str(e.value).startswith("Invalid Service Account Key: expecting dictionary, actually got")
def test_both_params_error(token, service_account_key):
with pytest.raises(RuntimeError) as e:
get_auth_token_requester(token=token, service_account_key=service_account_key).get_token_request()
assert str(e.value) == "Conflicting API credentials properties 'token' and 'service-account-key' are set."
def test_oauth_token(token):
request_func = get_auth_token_requester(token=token).get_token_request
request = request_func()
assert token == request.yandex_passport_oauth_token
def test_service_account_key(service_account_key):
request_func = get_auth_token_requester(service_account_key=service_account_key).get_token_request
request = request_func()
now = int(time.time())
headers = jwt.get_unverified_header(request.jwt)
parsed = jwt.decode(request.jwt, secret=service_account_key["public_key"], algorithms=['PS256'], verify=False)
assert headers["typ"] == "JWT"
assert headers["alg"] == "PS256"
assert headers["kid"] == service_account_key["id"]
assert parsed["iss"] == service_account_key["service_account_id"]
assert parsed["aud"] == "https://iam.api.cloud.yandex.net/iam/v1/tokens"
assert now - 60 <= int(parsed["iat"]) <= now
def test_service_account_no_id(service_account_key, key, error_msg):
service_account_key.pop(key)
with pytest.raises(RuntimeError) as e:
get_auth_token_requester(service_account_key=service_account_key).get_token_request()
assert str(e.value) == error_msg