• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 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"""Tests of credentials."""
15
16import unittest
17import logging
18import six
19
20import grpc
21
22
23class CredentialsTest(unittest.TestCase):
24
25    def test_call_credentials_composition(self):
26        first = grpc.access_token_call_credentials('abc')
27        second = grpc.access_token_call_credentials('def')
28        third = grpc.access_token_call_credentials('ghi')
29
30        first_and_second = grpc.composite_call_credentials(first, second)
31        first_second_and_third = grpc.composite_call_credentials(
32            first, second, third)
33
34        self.assertIsInstance(first_and_second, grpc.CallCredentials)
35        self.assertIsInstance(first_second_and_third, grpc.CallCredentials)
36
37    def test_channel_credentials_composition(self):
38        first_call_credentials = grpc.access_token_call_credentials('abc')
39        second_call_credentials = grpc.access_token_call_credentials('def')
40        third_call_credentials = grpc.access_token_call_credentials('ghi')
41        channel_credentials = grpc.ssl_channel_credentials()
42
43        channel_and_first = grpc.composite_channel_credentials(
44            channel_credentials, first_call_credentials)
45        channel_first_and_second = grpc.composite_channel_credentials(
46            channel_credentials, first_call_credentials,
47            second_call_credentials)
48        channel_first_second_and_third = grpc.composite_channel_credentials(
49            channel_credentials, first_call_credentials,
50            second_call_credentials, third_call_credentials)
51
52        self.assertIsInstance(channel_and_first, grpc.ChannelCredentials)
53        self.assertIsInstance(channel_first_and_second, grpc.ChannelCredentials)
54        self.assertIsInstance(channel_first_second_and_third,
55                              grpc.ChannelCredentials)
56
57    @unittest.skipIf(six.PY2, 'only invalid in Python3')
58    def test_invalid_string_certificate(self):
59        self.assertRaises(
60            TypeError,
61            grpc.ssl_channel_credentials,
62            root_certificates='A Certificate',
63            private_key=None,
64            certificate_chain=None,
65        )
66
67
68if __name__ == '__main__':
69    logging.basicConfig()
70    unittest.main(verbosity=2)
71