1# Copyright 2024 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 16from google.api_core import universe 17 18 19class _Fake_Credentials: 20 def __init__(self, universe_domain=None): 21 if universe_domain: 22 self.universe_domain = universe_domain 23 24 25def test_determine_domain(): 26 domain_client = "foo.com" 27 domain_env = "bar.com" 28 29 assert universe.determine_domain(domain_client, domain_env) == domain_client 30 assert universe.determine_domain(None, domain_env) == domain_env 31 assert universe.determine_domain(domain_client, None) == domain_client 32 assert universe.determine_domain(None, None) == universe.DEFAULT_UNIVERSE 33 34 with pytest.raises(universe.EmptyUniverseError): 35 universe.determine_domain("", None) 36 37 with pytest.raises(universe.EmptyUniverseError): 38 universe.determine_domain(None, "") 39 40 41def test_compare_domains(): 42 fake_domain = "foo.com" 43 another_fake_domain = "bar.com" 44 45 assert universe.compare_domains(universe.DEFAULT_UNIVERSE, _Fake_Credentials()) 46 assert universe.compare_domains(fake_domain, _Fake_Credentials(fake_domain)) 47 48 with pytest.raises(universe.UniverseMismatchError) as excinfo: 49 universe.compare_domains( 50 universe.DEFAULT_UNIVERSE, _Fake_Credentials(fake_domain) 51 ) 52 assert str(excinfo.value).find(universe.DEFAULT_UNIVERSE) >= 0 53 assert str(excinfo.value).find(fake_domain) >= 0 54 55 with pytest.raises(universe.UniverseMismatchError) as excinfo: 56 universe.compare_domains(fake_domain, _Fake_Credentials()) 57 assert str(excinfo.value).find(fake_domain) >= 0 58 assert str(excinfo.value).find(universe.DEFAULT_UNIVERSE) >= 0 59 60 with pytest.raises(universe.UniverseMismatchError) as excinfo: 61 universe.compare_domains(fake_domain, _Fake_Credentials(another_fake_domain)) 62 assert str(excinfo.value).find(fake_domain) >= 0 63 assert str(excinfo.value).find(another_fake_domain) >= 0 64