Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions tests/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,13 +495,6 @@ def test_ResourceMethodParameters_zoo_animals_patch(self):
self.assertEqual(parameters.enum_params, {})


class Discovery(unittest.TestCase):
def test_discovery_http_is_closed(self):
http = HttpMock(datafile("malformed.json"), {"status": "200"})
service = build("plus", "v1", credentials=mock.sentinel.credentials)
http.close.assert_called_once()


class DiscoveryErrors(unittest.TestCase):
def test_tests_should_be_run_with_strict_positional_enforcement(self):
try:
Expand Down Expand Up @@ -1549,6 +1542,19 @@ def test_file_based_cache(self):


class Discovery(unittest.TestCase):
@mock.patch("googleapiclient.discovery.build_http")
def test_discovery_http_is_closed(self, mock_build_http):
mock_http = mock.Mock()
mock_build_http.return_value = mock_http
creds = mock.Mock()
service = build("youtube", "v3", credentials=creds)
Comment on lines +1549 to +1550

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using a plain mock.Mock() for credentials can cause test failures in environments where HAS_UNIVERSE is False. In those environments, _check_api_core_compatible_with_credentials_universe is executed and accesses credentials.universe_domain. Since creds is a plain mock, getattr(creds, "universe_domain", None) returns a new Mock object (which is truthy and not equal to "googleapis.com"), triggering an unexpected APICoreVersionError.

To make the test robust and consistent with other tests in this suite, use mock.Mock(spec=google.auth.credentials.Credentials) and call _reset_universe_domain(creds).

Suggested change
creds = mock.Mock()
service = build("youtube", "v3", credentials=creds)
creds = mock.Mock(spec=google.auth.credentials.Credentials)
_reset_universe_domain(creds)
service = build("youtube", "v3", credentials=creds)

mock_http.close.assert_called_once()

def test_provided_http_is_not_closed(self):
provided_http = mock.Mock()
service = build("youtube", "v3", http=provided_http)
provided_http.close.assert_not_called()

def test_method_error_checking(self):
self.http = HttpMock(datafile("plus.json"), {"status": "200"})
plus = build("plus", "v1", http=self.http, static_discovery=False)
Expand Down
Loading