Skip to content

Commit

Permalink
test: Support running the test suite without oauth2client (#2021)
Browse files Browse the repository at this point in the history
* test: make MockCredentials class standalone

Remove the inheritance of MockCredentials from oauth2client.Credentials.
This is unnecessary, and removing it makes it possible to run
the respective tests without oauth2client installed.

* test: Support testing without oauth2client

Make it possible to run the test suite without oauth2client, by skipping
the few tests that specifically require it.  Given that oauth2client
is deprecated, we are aiming towards removing it entirely from Gentoo
and the unconditional imports in the test suite are a blocker for that.

* test: Test without oauth2client as well
  • Loading branch information
mgorny authored Jan 5, 2023
1 parent ce9188c commit fbacd50
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
4 changes: 3 additions & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def format(session):
@nox.parametrize(
"oauth2client",
[
None,
"oauth2client<2dev",
"oauth2client>=2,<=3dev",
"oauth2client>=3,<=4dev",
Expand All @@ -96,7 +97,8 @@ def unit(session, oauth2client):
shutil.rmtree("build", ignore_errors=True)

session.install(*test_dependencies)
session.install(oauth2client)
if oauth2client is not None:
session.install(oauth2client)

# Create and install wheels
session.run("python3", "setup.py", "bdist_wheel")
Expand Down
9 changes: 8 additions & 1 deletion tests/test__auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
import google.auth.credentials
import google_auth_httplib2
import httplib2
import oauth2client.client

try:
import oauth2client.client

HAS_OAUTH2CLIENT = True
except ImportError:
HAS_OAUTH2CLIENT = False

from googleapiclient import _auth

Expand Down Expand Up @@ -105,6 +111,7 @@ def test_authorized_http(self):
self.assertGreater(authorized_http.http.timeout, 0)


@unittest.skipIf(not HAS_OAUTH2CLIENT, "oauth2client unavailable.")
class TestAuthWithOAuth2Client(unittest.TestCase):
def setUp(self):
_auth.HAS_GOOGLE_AUTH = False
Expand Down
12 changes: 10 additions & 2 deletions tests/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,17 @@
from google.auth.exceptions import MutualTLSChannelError
import google_auth_httplib2
import httplib2
from oauth2client import GOOGLE_TOKEN_URI
from oauth2client.client import GoogleCredentials, OAuth2Credentials
from parameterized import parameterized
import uritemplate

try:
from oauth2client import GOOGLE_TOKEN_URI
from oauth2client.client import GoogleCredentials, OAuth2Credentials

HAS_OAUTH2CLIENT = True
except ImportError:
HAS_OAUTH2CLIENT = False

from googleapiclient import _helpers as util
from googleapiclient.discovery import (
DISCOVERY_URI,
Expand Down Expand Up @@ -1513,6 +1519,7 @@ def test_plus_resources(self):
self.assertTrue(getattr(plus, "activities"))
self.assertTrue(getattr(plus, "people"))

@unittest.skipIf(not HAS_OAUTH2CLIENT, "oauth2client unavailable.")
def test_oauth2client_credentials(self):
credentials = mock.Mock(spec=GoogleCredentials)
credentials.create_scoped_required.return_value = False
Expand Down Expand Up @@ -2198,6 +2205,7 @@ def _dummy_token(self):
user_agent,
)

@unittest.skipIf(not HAS_OAUTH2CLIENT, "oauth2client unavailable.")
def test_pickle_with_credentials(self):
credentials = self._dummy_token()
http = self._dummy_zoo_request()
Expand Down
3 changes: 1 addition & 2 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import urllib

import httplib2
from oauth2client.client import Credentials

from googleapiclient.discovery import build
from googleapiclient.errors import BatchError, HttpError, InvalidChunkSizeError
Expand All @@ -60,7 +59,7 @@
from googleapiclient.model import JsonModel


class MockCredentials(Credentials):
class MockCredentials:
"""Mock class for all Credentials objects."""

def __init__(self, bearer_token, expired=False):
Expand Down

0 comments on commit fbacd50

Please sign in to comment.