_auth_example_test.py
Go to the documentation of this file.
1 # Copyright 2019 The gRPC Authors
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 """Test for gRPC Python authentication example."""
15 
16 import asyncio
17 import unittest
18 
19 import grpc
20 
21 from examples.python.auth import _credentials
22 from examples.python.auth import async_customized_auth_client
23 from examples.python.auth import async_customized_auth_server
24 from examples.python.auth import customized_auth_client
25 from examples.python.auth import customized_auth_server
26 
27 _SERVER_ADDR_TEMPLATE = 'localhost:%d'
28 
29 
30 class AuthExampleTest(unittest.TestCase):
31 
33  with customized_auth_server.run_server(0) as (_, port):
35  _SERVER_ADDR_TEMPLATE % port) as channel:
37  # No unhandled exception raised, test passed!
38 
40  with customized_auth_server.run_server(0) as (_, port):
41  with grpc.insecure_channel(_SERVER_ADDR_TEMPLATE % port) as channel:
42  resp = customized_auth_client.send_rpc(channel)
43  self.assertEqual(resp.code(), grpc.StatusCode.UNAVAILABLE)
44 
46  with customized_auth_server.run_server(0) as (_, port):
47  channel_credential = grpc.ssl_channel_credentials(
48  _credentials.ROOT_CERTIFICATE)
49  with grpc.secure_channel(_SERVER_ADDR_TEMPLATE % port,
50  channel_credential) as channel:
51  resp = customized_auth_client.send_rpc(channel)
52  self.assertEqual(resp.code(), grpc.StatusCode.UNAUTHENTICATED)
53 
55 
56  async def test_body():
57  server, port = await async_customized_auth_server.run_server(0)
59  _SERVER_ADDR_TEMPLATE % port)
61  await channel.close()
62  await server.stop(0)
63  # No unhandled exception raised, test passed!
64 
65  asyncio.get_event_loop().run_until_complete(test_body())
66 
67 
68 if __name__ == '__main__':
69  unittest.main(verbosity=2)
grpc.insecure_channel
def insecure_channel(target, options=None, compression=None)
Definition: src/python/grpcio/grpc/__init__.py:1962
customized_auth_client.create_client_channel
def create_client_channel(addr)
Definition: customized_auth_client.py:55
customized_auth_client.send_rpc
def send_rpc(channel)
Definition: customized_auth_client.py:71
_auth_example_test.AuthExampleTest.test_no_call_credential
def test_no_call_credential(self)
Definition: _auth_example_test.py:45
_auth_example_test.AuthExampleTest
Definition: _auth_example_test.py:30
async_customized_auth_client.create_client_channel
grpc.aio.Channel create_client_channel(str addr)
Definition: async_customized_auth_client.py:55
_auth_example_test.AuthExampleTest.test_successful_call_asyncio
def test_successful_call_asyncio(self)
Definition: _auth_example_test.py:54
async_customized_auth_server.run_server
Tuple[grpc.aio.Server, int] run_server(int port)
Definition: async_customized_auth_server.py:67
_auth_example_test.AuthExampleTest.test_no_channel_credential
def test_no_channel_credential(self)
Definition: _auth_example_test.py:39
grpc.ssl_channel_credentials
def ssl_channel_credentials(root_certificates=None, private_key=None, certificate_chain=None)
Definition: src/python/grpcio/grpc/__init__.py:1607
async_customized_auth_client.send_rpc
helloworld_pb2.HelloReply send_rpc(grpc.aio.Channel channel)
Definition: async_customized_auth_client.py:71
_auth_example_test.AuthExampleTest.test_successful_call
def test_successful_call(self)
Definition: _auth_example_test.py:32
customized_auth_server.run_server
def run_server(port)
Definition: customized_auth_server.py:63
grpc.secure_channel
def secure_channel(target, credentials, options=None, compression=None)
Definition: src/python/grpcio/grpc/__init__.py:1982


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:26