• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16from __future__ import absolute_import
17from __future__ import division
18from __future__ import print_function
19
20import unittest
21
22import grpc
23from examples.python.auth import _credentials
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
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
55if __name__ == '__main__':
56    unittest.main(verbosity=2)
57