userver: samples/grpc_middleware_service/tests/test_middlewares.py
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
samples/grpc_middleware_service/tests/test_middlewares.py
1from grpc import StatusCode
2from grpc.aio._call import AioRpcError
3import samples.greeter_pb2 as greeter_protos # noqa: E402, E501
4
5
6# /// [grpc authentication tests]
7async def test_correct_credentials(grpc_client):
8 request = greeter_protos.GreetingRequest(name='Python')
9 response = await grpc_client.SayHello(
10 request=request, metadata=[('x-key', 'secret-credentials')],
11 )
12 assert response.greeting == 'Hello, Python!'
13
14
15async def test_incorrect_credentials(grpc_client):
16 request = greeter_protos.GreetingRequest(name='Python')
17
18 try:
19 await grpc_client.SayHello(
20 request=request, metadata=[('x-key', 'secretcredentials')],
21 )
22 assert False
23 except AioRpcError as err:
24 assert err.code() == StatusCode.PERMISSION_DENIED
25
26
27async def test_no_credentials(grpc_client):
28 request = greeter_protos.GreetingRequest(name='Python')
29
30 try:
31 await grpc_client.SayHello(request=request)
32 assert False
33 except AioRpcError as err:
34 assert err.code() == StatusCode.PERMISSION_DENIED
35
36
37# /// [grpc authentication tests]