Debian Codegraph @

examples/python/auth/test/_auth_example_test.py

raw
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
32 def test_successful_call(self):
33 with customized_auth_server.run_server(0) as (_, port):
34 with customized_auth_client.create_client_channel(
35 _SERVER_ADDR_TEMPLATE % port) as channel:
36 customized_auth_client.send_rpc(channel)
37 # No unhandled exception raised, test passed!
38
39 def test_no_channel_credential(self):
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
45 def test_no_call_credential(self):
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
54 def test_successful_call_asyncio(self):
55
56 async def test_body():
57 server, port = await async_customized_auth_server.run_server(0)
58 channel = async_customized_auth_client.create_client_channel(
59 _SERVER_ADDR_TEMPLATE % port)
60 await async_customized_auth_client.send_rpc(channel)
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)