xref: /aosp_15_r20/external/grpc-grpc/examples/python/auth/test/_auth_example_test.py (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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
16import asyncio
17import unittest
18
19import grpc
20
21from examples.python.auth import _credentials
22from examples.python.auth import async_customized_auth_client
23from examples.python.auth import async_customized_auth_server
24from examples.python.auth import customized_auth_client
25from examples.python.auth import customized_auth_server
26
27_SERVER_ADDR_TEMPLATE = "localhost:%d"
28
29
30class AuthExampleTest(unittest.TestCase):
31    def test_successful_call(self):
32        with customized_auth_server.run_server(0) as (_, port):
33            with customized_auth_client.create_client_channel(
34                _SERVER_ADDR_TEMPLATE % port
35            ) 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            )
50            with grpc.secure_channel(
51                _SERVER_ADDR_TEMPLATE % port, channel_credential
52            ) as channel:
53                resp = customized_auth_client.send_rpc(channel)
54                self.assertEqual(resp.code(), grpc.StatusCode.UNAUTHENTICATED)
55
56    def test_successful_call_asyncio(self):
57        async def test_body():
58            server, port = await async_customized_auth_server.run_server(0)
59            channel = async_customized_auth_client.create_client_channel(
60                _SERVER_ADDR_TEMPLATE % port
61            )
62            await async_customized_auth_client.send_rpc(channel)
63            await channel.close()
64            await server.stop(0)
65            # No unhandled exception raised, test passed!
66
67        asyncio.get_event_loop().run_until_complete(test_body())
68
69
70if __name__ == "__main__":
71    unittest.main(verbosity=2)
72