1# Copyright 2019 Google LLC 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 15import pytest 16 17from google.api_core import client_options 18 19 20def get_client_cert(): 21 return b"cert", b"key" 22 23 24def get_client_encrypted_cert(): 25 return "cert_path", "key_path", b"passphrase" 26 27 28def test_constructor(): 29 30 options = client_options.ClientOptions( 31 api_endpoint="foo.googleapis.com", 32 client_cert_source=get_client_cert, 33 quota_project_id="quote-proj", 34 credentials_file="path/to/credentials.json", 35 scopes=[ 36 "https://www.googleapis.com/auth/cloud-platform", 37 "https://www.googleapis.com/auth/cloud-platform.read-only", 38 ], 39 ) 40 41 assert options.api_endpoint == "foo.googleapis.com" 42 assert options.client_cert_source() == (b"cert", b"key") 43 assert options.quota_project_id == "quote-proj" 44 assert options.credentials_file == "path/to/credentials.json" 45 assert options.scopes == [ 46 "https://www.googleapis.com/auth/cloud-platform", 47 "https://www.googleapis.com/auth/cloud-platform.read-only", 48 ] 49 50 51def test_constructor_with_encrypted_cert_source(): 52 53 options = client_options.ClientOptions( 54 api_endpoint="foo.googleapis.com", 55 client_encrypted_cert_source=get_client_encrypted_cert, 56 ) 57 58 assert options.api_endpoint == "foo.googleapis.com" 59 assert options.client_encrypted_cert_source() == ( 60 "cert_path", 61 "key_path", 62 b"passphrase", 63 ) 64 65 66def test_constructor_with_both_cert_sources(): 67 with pytest.raises(ValueError): 68 client_options.ClientOptions( 69 api_endpoint="foo.googleapis.com", 70 client_cert_source=get_client_cert, 71 client_encrypted_cert_source=get_client_encrypted_cert, 72 ) 73 74 75def test_from_dict(): 76 options = client_options.from_dict( 77 { 78 "api_endpoint": "foo.googleapis.com", 79 "client_cert_source": get_client_cert, 80 "quota_project_id": "quote-proj", 81 "credentials_file": "path/to/credentials.json", 82 "scopes": [ 83 "https://www.googleapis.com/auth/cloud-platform", 84 "https://www.googleapis.com/auth/cloud-platform.read-only", 85 ], 86 } 87 ) 88 89 assert options.api_endpoint == "foo.googleapis.com" 90 assert options.client_cert_source() == (b"cert", b"key") 91 assert options.quota_project_id == "quote-proj" 92 assert options.credentials_file == "path/to/credentials.json" 93 assert options.scopes == [ 94 "https://www.googleapis.com/auth/cloud-platform", 95 "https://www.googleapis.com/auth/cloud-platform.read-only", 96 ] 97 98 99def test_from_dict_bad_argument(): 100 with pytest.raises(ValueError): 101 client_options.from_dict( 102 { 103 "api_endpoint": "foo.googleapis.com", 104 "bad_arg": "1234", 105 "client_cert_source": get_client_cert, 106 } 107 ) 108 109 110def test_repr(): 111 options = client_options.ClientOptions(api_endpoint="foo.googleapis.com") 112 113 assert ( 114 repr(options) 115 == "ClientOptions: {'api_endpoint': 'foo.googleapis.com', 'client_cert_source': None, 'client_encrypted_cert_source': None}" 116 or "ClientOptions: {'client_encrypted_cert_source': None, 'client_cert_source': None, 'api_endpoint': 'foo.googleapis.com'}" 117 ) 118