Skip to content
Closed
Show file tree
Hide file tree
Changes from 14 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
58 changes: 58 additions & 0 deletions .github/workflows/unittest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
on:
pull_request:
branches:
- main
name: unittest
jobs:
unit:
runs-on: ubuntu-22.04
strategy:
matrix:
python: ['3.7', '3.8']
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python }}
- name: Install nox
run: |
python -m pip install --upgrade setuptools pip wheel
python -m pip install nox
- name: Run unit tests
env:
COVERAGE_FILE: .coverage-${{ matrix.python }}
run: |
nox -s unit-${{ matrix.python }}
- name: Upload coverage results
uses: actions/upload-artifact@v4
with:
name: coverage-artifact-${{ matrix.python }}
path: .coverage-${{ matrix.python }}
include-hidden-files: true

cover:
runs-on: ubuntu-latest
needs:
- unit
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.13"
- name: Install coverage
run: |
python -m pip install --upgrade setuptools pip wheel
python -m pip install coverage
- name: Download coverage results
uses: actions/download-artifact@v4
with:
path: .coverage-results/
- name: Report coverage results
run: |
find .coverage-results -type f -name '*.zip' -exec unzip {} \;
coverage combine .coverage-results/**/.coverage*
coverage report --show-missing --fail-under=99
3 changes: 2 additions & 1 deletion google/auth/_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
Implements application default credentials and project ID detection.
"""
from __future__ import annotations

from collections.abc import Sequence
from typing import Sequence
import io
import json
import logging
Expand Down
1 change: 1 addition & 0 deletions google/oauth2/id_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
http://openid.net/specs/openid-connect-core-1_0.html#IDToken
.. _CacheControl: https://cachecontrol.readthedocs.io
"""
from __future__ import annotations

import http.client as http_client
import json
Expand Down
15 changes: 10 additions & 5 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@
]

DEFAULT_PYTHON_VERSION = "3.14"
# TODO(https://github.com/googleapis/google-auth-library-python/issues/1787):
# Remove or restore testing for Python 3.7/3.8
UNIT_TEST_PYTHON_VERSIONS = ["3.9", "3.10", "3.11", "3.12", "3.13", "3.14"]
UNIT_TEST_PYTHON_VERSIONS = [
"3.7",
"3.8",
"3.9",
"3.10",
"3.11",
"3.12",
"3.13",
"3.14",
]

# Error if a python version is missing
nox.options.error_on_missing_interpreters = True
Expand All @@ -44,8 +51,6 @@
"lint",
"blacken",
"mypy",
# TODO(https://github.com/googleapis/google-auth-library-python/issues/1787):
# Remove or restore testing for Python 3.7/3.8
"unit-3.9",
"unit-3.10",
"unit-3.11",
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
# TODO(https://github.com/googleapis/google-auth-library-python/issues/1722): `test_aiohttp_requests` depend on
# aiohttp < 3.10.0 which is a bug. Investigate and remove the pinned aiohttp version.
"aiohttp < 3.10.0",
"mock; python_version < '3.8'",
]

extras = {
Expand Down
8 changes: 6 additions & 2 deletions tests/transport/aio/test_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
# limitations under the License.

import asyncio
from unittest.mock import AsyncMock, Mock, patch

from unittest.mock import Mock, patch
try:
from unittest.mock import AsyncMock
except ImportError:
# Fallback for Python < 3.8
from mock import AsyncMock
from aioresponses import aioresponses # type: ignore
import pytest # type: ignore
import pytest_asyncio # type: ignore
Expand Down
81 changes: 43 additions & 38 deletions tests_async/oauth2/test__client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
import http.client as http_client
import json
from unittest import mock
try:
from unittest.mock import AsyncMock
except ImportError:
# Fallback for Python < 3.8
from mock import AsyncMock
import urllib

import pytest # type: ignore
Expand All @@ -29,13 +34,13 @@


def make_request(response_data, status=http_client.OK, text=False):
response = mock.AsyncMock(spec=["transport.Response"])
response = AsyncMock(spec=["transport.Response"])
response.status = status
data = response_data if text else json.dumps(response_data).encode("utf-8")
response.data = mock.AsyncMock(spec=["__call__", "read"])
response.data.read = mock.AsyncMock(spec=["__call__"], return_value=data)
response.content = mock.AsyncMock(spec=["__call__"], return_value=data)
request = mock.AsyncMock(spec=["transport.Request"])
response.data = AsyncMock(spec=["__call__", "read"])
response.data.read = AsyncMock(spec=["__call__"], return_value=data)
response.content = AsyncMock(spec=["__call__"], return_value=data)
request = AsyncMock(spec=["transport.Request"])
request.return_value = response
return request

Expand Down Expand Up @@ -142,21 +147,21 @@ async def test__token_endpoint_request_internal_failure_error():

@pytest.mark.asyncio
async def test__token_endpoint_request_internal_failure_and_retry_failure_error():
retryable_error = mock.AsyncMock(spec=["transport.Response"])
retryable_error = AsyncMock(spec=["transport.Response"])
retryable_error.status = http_client.BAD_REQUEST
data = json.dumps({"error_description": "internal_failure"}).encode("utf-8")
retryable_error.data = mock.AsyncMock(spec=["__call__", "read"])
retryable_error.data.read = mock.AsyncMock(spec=["__call__"], return_value=data)
retryable_error.content = mock.AsyncMock(spec=["__call__"], return_value=data)
retryable_error.data = AsyncMock(spec=["__call__", "read"])
retryable_error.data.read = AsyncMock(spec=["__call__"], return_value=data)
retryable_error.content = AsyncMock(spec=["__call__"], return_value=data)

unretryable_error = mock.AsyncMock(spec=["transport.Response"])
unretryable_error = AsyncMock(spec=["transport.Response"])
unretryable_error.status = http_client.BAD_REQUEST
data = json.dumps({"error_description": "invalid_scope"}).encode("utf-8")
unretryable_error.data = mock.AsyncMock(spec=["__call__", "read"])
unretryable_error.data.read = mock.AsyncMock(spec=["__call__"], return_value=data)
unretryable_error.content = mock.AsyncMock(spec=["__call__"], return_value=data)
unretryable_error.data = AsyncMock(spec=["__call__", "read"])
unretryable_error.data.read = AsyncMock(spec=["__call__"], return_value=data)
unretryable_error.content = AsyncMock(spec=["__call__"], return_value=data)

request = mock.AsyncMock(spec=["transport.Request"])
request = AsyncMock(spec=["transport.Request"])
request.side_effect = [retryable_error, retryable_error, unretryable_error]

with pytest.raises(exceptions.RefreshError):
Expand All @@ -170,21 +175,21 @@ async def test__token_endpoint_request_internal_failure_and_retry_failure_error(

@pytest.mark.asyncio
async def test__token_endpoint_request_internal_failure_and_retry_succeeds():
retryable_error = mock.AsyncMock(spec=["transport.Response"])
retryable_error = AsyncMock(spec=["transport.Response"])
retryable_error.status = http_client.BAD_REQUEST
data = json.dumps({"error_description": "internal_failure"}).encode("utf-8")
retryable_error.data = mock.AsyncMock(spec=["__call__", "read"])
retryable_error.data.read = mock.AsyncMock(spec=["__call__"], return_value=data)
retryable_error.content = mock.AsyncMock(spec=["__call__"], return_value=data)
retryable_error.data = AsyncMock(spec=["__call__", "read"])
retryable_error.data.read = AsyncMock(spec=["__call__"], return_value=data)
retryable_error.content = AsyncMock(spec=["__call__"], return_value=data)

response = mock.AsyncMock(spec=["transport.Response"])
response = AsyncMock(spec=["transport.Response"])
response.status = http_client.OK
data = json.dumps({"hello": "world"}).encode("utf-8")
response.data = mock.AsyncMock(spec=["__call__", "read"])
response.data.read = mock.AsyncMock(spec=["__call__"], return_value=data)
response.content = mock.AsyncMock(spec=["__call__"], return_value=data)
response.data = AsyncMock(spec=["__call__", "read"])
response.data.read = AsyncMock(spec=["__call__"], return_value=data)
response.content = AsyncMock(spec=["__call__"], return_value=data)

request = mock.AsyncMock(spec=["transport.Request"])
request = AsyncMock(spec=["transport.Request"])
request.side_effect = [retryable_error, response]

_ = await _client._token_endpoint_request(
Expand Down Expand Up @@ -399,7 +404,7 @@ async def test_jwt_grant_retry_with_retry(
mock_token_endpoint_request, mock_expiry, can_retry
):
_ = await _client.jwt_grant(
mock.AsyncMock(), mock.Mock(), mock.Mock(), can_retry=can_retry
AsyncMock(), mock.Mock(), mock.Mock(), can_retry=can_retry
)
mock_token_endpoint_request.assert_called_with(
mock.ANY, mock.ANY, mock.ANY, can_retry=can_retry
Expand All @@ -426,7 +431,7 @@ async def test_id_token_jwt_grant_retry_with_retry(
mock_token_endpoint_request, mock_jwt_decode, can_retry
):
_ = await _client.id_token_jwt_grant(
mock.AsyncMock(), mock.AsyncMock(), mock.AsyncMock(), can_retry=can_retry
AsyncMock(), AsyncMock(), AsyncMock(), can_retry=can_retry
)
mock_token_endpoint_request.assert_called_with(
mock.ANY, mock.ANY, mock.ANY, can_retry=can_retry
Expand All @@ -440,11 +445,11 @@ async def test_refresh_grant_retry_default(
mock_token_endpoint_request, mock_parse_expiry
):
_ = await _client.refresh_grant(
mock.AsyncMock(),
mock.AsyncMock(),
mock.AsyncMock(),
mock.AsyncMock(),
mock.AsyncMock(),
AsyncMock(),
AsyncMock(),
AsyncMock(),
AsyncMock(),
AsyncMock(),
)
mock_token_endpoint_request.assert_called_with(
mock.ANY, mock.ANY, mock.ANY, can_retry=True
Expand All @@ -459,11 +464,11 @@ async def test_refresh_grant_retry_with_retry(
mock_token_endpoint_request, mock_parse_expiry, can_retry
):
_ = await _client.refresh_grant(
mock.AsyncMock(),
mock.AsyncMock(),
mock.AsyncMock(),
mock.AsyncMock(),
mock.AsyncMock(),
AsyncMock(),
AsyncMock(),
AsyncMock(),
AsyncMock(),
AsyncMock(),
can_retry=can_retry,
)
mock_token_endpoint_request.assert_called_with(
Expand All @@ -481,10 +486,10 @@ async def test__token_endpoint_request_no_throw_with_retry(can_retry):

_ = await _client._token_endpoint_request_no_throw(
mock_request,
mock.AsyncMock(),
AsyncMock(),
"body",
mock.AsyncMock(),
mock.AsyncMock(),
AsyncMock(),
AsyncMock(),
can_retry=can_retry,
)

Expand Down
15 changes: 10 additions & 5 deletions tests_async/oauth2/test_credentials_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
import pickle
import sys
from unittest import mock
try:
from unittest.mock import AsyncMock
except ImportError:
# Fallback for Python < 3.8
from mock import AsyncMock

import pytest # type: ignore

Expand Down Expand Up @@ -82,7 +87,7 @@ async def test_refresh_success(self, unused_utcnow, refresh_grant):
rapt_token,
)

request = mock.AsyncMock(spec=["transport.Request"])
request = AsyncMock(spec=["transport.Request"])
creds = self.make_credentials()

# Refresh credentials
Expand Down Expand Up @@ -112,7 +117,7 @@ async def test_refresh_success(self, unused_utcnow, refresh_grant):

@pytest.mark.asyncio
async def test_refresh_no_refresh_token(self):
request = mock.AsyncMock(spec=["transport.Request"])
request = AsyncMock(spec=["transport.Request"])
credentials_ = _credentials_async.Credentials(token=None, refresh_token=None)

with pytest.raises(exceptions.RefreshError, match="necessary fields"):
Expand Down Expand Up @@ -147,7 +152,7 @@ async def test_credentials_with_scopes_requested_refresh_success(
rapt_token,
)

request = mock.AsyncMock(spec=["transport.Request"])
request = AsyncMock(spec=["transport.Request"])
creds = _credentials_async.Credentials(
token=None,
refresh_token=self.REFRESH_TOKEN,
Expand Down Expand Up @@ -211,7 +216,7 @@ async def test_credentials_with_scopes_returned_refresh_success(
rapt_token,
)

request = mock.AsyncMock(spec=["transport.Request"])
request = AsyncMock(spec=["transport.Request"])
creds = _credentials_async.Credentials(
token=None,
refresh_token=self.REFRESH_TOKEN,
Expand Down Expand Up @@ -278,7 +283,7 @@ async def test_credentials_with_scopes_refresh_failure_raises_refresh_error(
rapt_token,
)

request = mock.AsyncMock(spec=["transport.Request"])
request = AsyncMock(spec=["transport.Request"])
creds = _credentials_async.Credentials(
token=None,
refresh_token=self.REFRESH_TOKEN,
Expand Down
Loading
Loading