chore: blacken (#375)
diff --git a/tests/test__default.py b/tests/test__default.py
index d143479..2c86f3f 100644
--- a/tests/test__default.py
+++ b/tests/test__default.py
@@ -27,94 +27,96 @@
import google.oauth2.credentials
-DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
-AUTHORIZED_USER_FILE = os.path.join(DATA_DIR, 'authorized_user.json')
+DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
+AUTHORIZED_USER_FILE = os.path.join(DATA_DIR, "authorized_user.json")
with open(AUTHORIZED_USER_FILE) as fh:
AUTHORIZED_USER_FILE_DATA = json.load(fh)
AUTHORIZED_USER_CLOUD_SDK_FILE = os.path.join(
- DATA_DIR, 'authorized_user_cloud_sdk.json')
+ DATA_DIR, "authorized_user_cloud_sdk.json"
+)
-SERVICE_ACCOUNT_FILE = os.path.join(DATA_DIR, 'service_account.json')
+SERVICE_ACCOUNT_FILE = os.path.join(DATA_DIR, "service_account.json")
with open(SERVICE_ACCOUNT_FILE) as fh:
SERVICE_ACCOUNT_FILE_DATA = json.load(fh)
LOAD_FILE_PATCH = mock.patch(
- 'google.auth._default._load_credentials_from_file', return_value=(
- mock.sentinel.credentials, mock.sentinel.project_id), autospec=True)
+ "google.auth._default._load_credentials_from_file",
+ return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
+ autospec=True,
+)
def test__load_credentials_from_missing_file():
with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
- _default._load_credentials_from_file('')
+ _default._load_credentials_from_file("")
- assert excinfo.match(r'not found')
+ assert excinfo.match(r"not found")
def test__load_credentials_from_file_invalid_json(tmpdir):
- jsonfile = tmpdir.join('invalid.json')
- jsonfile.write('{')
+ jsonfile = tmpdir.join("invalid.json")
+ jsonfile.write("{")
with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
_default._load_credentials_from_file(str(jsonfile))
- assert excinfo.match(r'not a valid json file')
+ assert excinfo.match(r"not a valid json file")
def test__load_credentials_from_file_invalid_type(tmpdir):
- jsonfile = tmpdir.join('invalid.json')
- jsonfile.write(json.dumps({'type': 'not-a-real-type'}))
+ jsonfile = tmpdir.join("invalid.json")
+ jsonfile.write(json.dumps({"type": "not-a-real-type"}))
with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
_default._load_credentials_from_file(str(jsonfile))
- assert excinfo.match(r'does not have a valid type')
+ assert excinfo.match(r"does not have a valid type")
def test__load_credentials_from_file_authorized_user():
- credentials, project_id = _default._load_credentials_from_file(
- AUTHORIZED_USER_FILE)
+ credentials, project_id = _default._load_credentials_from_file(AUTHORIZED_USER_FILE)
assert isinstance(credentials, google.oauth2.credentials.Credentials)
assert project_id is None
def test__load_credentials_from_file_authorized_user_bad_format(tmpdir):
- filename = tmpdir.join('authorized_user_bad.json')
- filename.write(json.dumps({'type': 'authorized_user'}))
+ filename = tmpdir.join("authorized_user_bad.json")
+ filename.write(json.dumps({"type": "authorized_user"}))
with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
_default._load_credentials_from_file(str(filename))
- assert excinfo.match(r'Failed to load authorized user')
- assert excinfo.match(r'missing fields')
+ assert excinfo.match(r"Failed to load authorized user")
+ assert excinfo.match(r"missing fields")
def test__load_credentials_from_file_authorized_user_cloud_sdk():
- with pytest.warns(UserWarning, match='Cloud SDK'):
+ with pytest.warns(UserWarning, match="Cloud SDK"):
credentials, project_id = _default._load_credentials_from_file(
- AUTHORIZED_USER_CLOUD_SDK_FILE)
+ AUTHORIZED_USER_CLOUD_SDK_FILE
+ )
assert isinstance(credentials, google.oauth2.credentials.Credentials)
assert project_id is None
def test__load_credentials_from_file_service_account():
- credentials, project_id = _default._load_credentials_from_file(
- SERVICE_ACCOUNT_FILE)
+ credentials, project_id = _default._load_credentials_from_file(SERVICE_ACCOUNT_FILE)
assert isinstance(credentials, service_account.Credentials)
- assert project_id == SERVICE_ACCOUNT_FILE_DATA['project_id']
+ assert project_id == SERVICE_ACCOUNT_FILE_DATA["project_id"]
def test__load_credentials_from_file_service_account_bad_format(tmpdir):
- filename = tmpdir.join('serivce_account_bad.json')
- filename.write(json.dumps({'type': 'service_account'}))
+ filename = tmpdir.join("serivce_account_bad.json")
+ filename.write(json.dumps({"type": "service_account"}))
with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
_default._load_credentials_from_file(str(filename))
- assert excinfo.match(r'Failed to load service account')
- assert excinfo.match(r'missing fields')
+ assert excinfo.match(r"Failed to load service account")
+ assert excinfo.match(r"missing fields")
@mock.patch.dict(os.environ, {}, clear=True)
@@ -124,19 +126,19 @@
@LOAD_FILE_PATCH
def test__get_explicit_environ_credentials(load, monkeypatch):
- monkeypatch.setenv(environment_vars.CREDENTIALS, 'filename')
+ monkeypatch.setenv(environment_vars.CREDENTIALS, "filename")
credentials, project_id = _default._get_explicit_environ_credentials()
assert credentials is mock.sentinel.credentials
assert project_id is mock.sentinel.project_id
- load.assert_called_with('filename')
+ load.assert_called_with("filename")
@LOAD_FILE_PATCH
def test__get_explicit_environ_credentials_no_project_id(load, monkeypatch):
load.return_value = mock.sentinel.credentials, None
- monkeypatch.setenv(environment_vars.CREDENTIALS, 'filename')
+ monkeypatch.setenv(environment_vars.CREDENTIALS, "filename")
credentials, project_id = _default._get_explicit_environ_credentials()
@@ -146,8 +148,8 @@
@LOAD_FILE_PATCH
@mock.patch(
- 'google.auth._cloud_sdk.get_application_default_credentials_path',
- autospec=True)
+ "google.auth._cloud_sdk.get_application_default_credentials_path", autospec=True
+)
def test__get_gcloud_sdk_credentials(get_adc_path, load):
get_adc_path.return_value = SERVICE_ACCOUNT_FILE
@@ -159,10 +161,10 @@
@mock.patch(
- 'google.auth._cloud_sdk.get_application_default_credentials_path',
- autospec=True)
+ "google.auth._cloud_sdk.get_application_default_credentials_path", autospec=True
+)
def test__get_gcloud_sdk_credentials_non_existent(get_adc_path, tmpdir):
- non_existent = tmpdir.join('non-existent')
+ non_existent = tmpdir.join("non-existent")
get_adc_path.return_value = str(non_existent)
credentials, project_id = _default._get_gcloud_sdk_credentials()
@@ -172,12 +174,13 @@
@mock.patch(
- 'google.auth._cloud_sdk.get_project_id',
- return_value=mock.sentinel.project_id, autospec=True)
-@mock.patch('os.path.isfile', return_value=True, autospec=True)
+ "google.auth._cloud_sdk.get_project_id",
+ return_value=mock.sentinel.project_id,
+ autospec=True,
+)
+@mock.patch("os.path.isfile", return_value=True, autospec=True)
@LOAD_FILE_PATCH
-def test__get_gcloud_sdk_credentials_project_id(
- load, unused_isfile, get_project_id):
+def test__get_gcloud_sdk_credentials_project_id(load, unused_isfile, get_project_id):
# Don't return a project ID from load file, make the function check
# the Cloud SDK project.
load.return_value = mock.sentinel.credentials, None
@@ -189,13 +192,10 @@
assert get_project_id.called
-@mock.patch(
- 'google.auth._cloud_sdk.get_project_id',
- return_value=None, autospec=True)
-@mock.patch('os.path.isfile', return_value=True)
+@mock.patch("google.auth._cloud_sdk.get_project_id", return_value=None, autospec=True)
+@mock.patch("os.path.isfile", return_value=True)
@LOAD_FILE_PATCH
-def test__get_gcloud_sdk_credentials_no_project_id(
- load, unused_isfile, get_project_id):
+def test__get_gcloud_sdk_credentials_no_project_id(load, unused_isfile, get_project_id):
# Don't return a project ID from load file, make the function check
# the Cloud SDK project.
load.return_value = mock.sentinel.credentials, None
@@ -212,6 +212,7 @@
See https://cloud.google.com/appengine/docs/standard/python/refdocs\
/google.appengine.api.app_identity.app_identity
"""
+
def get_application_id(self):
raise NotImplementedError()
@@ -219,10 +220,8 @@
@pytest.fixture
def app_identity(monkeypatch):
"""Mocks the app_identity module for google.auth.app_engine."""
- app_identity_module = mock.create_autospec(
- _AppIdentityModule, instance=True)
- monkeypatch.setattr(
- app_engine, 'app_identity', app_identity_module)
+ app_identity_module = mock.create_autospec(_AppIdentityModule, instance=True)
+ monkeypatch.setattr(app_engine, "app_identity", app_identity_module)
yield app_identity_module
@@ -237,8 +236,9 @@
def test__get_gae_credentials_no_app_engine():
import sys
- with mock.patch.dict('sys.modules'):
- sys.modules['google.auth.app_engine'] = None
+
+ with mock.patch.dict("sys.modules"):
+ sys.modules["google.auth.app_engine"] = None
credentials, project_id = _default._get_gae_credentials()
assert credentials is None
assert project_id is None
@@ -249,21 +249,23 @@
@mock.patch(
- 'google.auth.compute_engine._metadata.ping', return_value=True,
- autospec=True)
+ "google.auth.compute_engine._metadata.ping", return_value=True, autospec=True
+)
@mock.patch(
- 'google.auth.compute_engine._metadata.get_project_id',
- return_value='example-project', autospec=True)
+ "google.auth.compute_engine._metadata.get_project_id",
+ return_value="example-project",
+ autospec=True,
+)
def test__get_gce_credentials(unused_get, unused_ping):
credentials, project_id = _default._get_gce_credentials()
assert isinstance(credentials, compute_engine.Credentials)
- assert project_id == 'example-project'
+ assert project_id == "example-project"
@mock.patch(
- 'google.auth.compute_engine._metadata.ping', return_value=False,
- autospec=True)
+ "google.auth.compute_engine._metadata.ping", return_value=False, autospec=True
+)
def test__get_gce_credentials_no_ping(unused_ping):
credentials, project_id = _default._get_gce_credentials()
@@ -272,11 +274,13 @@
@mock.patch(
- 'google.auth.compute_engine._metadata.ping', return_value=True,
- autospec=True)
+ "google.auth.compute_engine._metadata.ping", return_value=True, autospec=True
+)
@mock.patch(
- 'google.auth.compute_engine._metadata.get_project_id',
- side_effect=exceptions.TransportError(), autospec=True)
+ "google.auth.compute_engine._metadata.get_project_id",
+ side_effect=exceptions.TransportError(),
+ autospec=True,
+)
def test__get_gce_credentials_no_project_id(unused_get, unused_ping):
credentials, project_id = _default._get_gce_credentials()
@@ -286,110 +290,125 @@
def test__get_gce_credentials_no_compute_engine():
import sys
- with mock.patch.dict('sys.modules'):
- sys.modules['google.auth.compute_engine'] = None
+
+ with mock.patch.dict("sys.modules"):
+ sys.modules["google.auth.compute_engine"] = None
credentials, project_id = _default._get_gce_credentials()
assert credentials is None
assert project_id is None
@mock.patch(
- 'google.auth.compute_engine._metadata.ping', return_value=False,
- autospec=True)
+ "google.auth.compute_engine._metadata.ping", return_value=False, autospec=True
+)
def test__get_gce_credentials_explicit_request(ping):
_default._get_gce_credentials(mock.sentinel.request)
ping.assert_called_with(request=mock.sentinel.request)
@mock.patch(
- 'google.auth._default._get_explicit_environ_credentials',
+ "google.auth._default._get_explicit_environ_credentials",
return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
- autospec=True)
+ autospec=True,
+)
def test_default_early_out(unused_get):
- assert _default.default() == (
- mock.sentinel.credentials, mock.sentinel.project_id)
+ assert _default.default() == (mock.sentinel.credentials, mock.sentinel.project_id)
@mock.patch(
- 'google.auth._default._get_explicit_environ_credentials',
+ "google.auth._default._get_explicit_environ_credentials",
return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
- autospec=True)
+ autospec=True,
+)
def test_default_explict_project_id(unused_get, monkeypatch):
- monkeypatch.setenv(environment_vars.PROJECT, 'explicit-env')
- assert _default.default() == (
- mock.sentinel.credentials, 'explicit-env')
+ monkeypatch.setenv(environment_vars.PROJECT, "explicit-env")
+ assert _default.default() == (mock.sentinel.credentials, "explicit-env")
@mock.patch(
- 'google.auth._default._get_explicit_environ_credentials',
+ "google.auth._default._get_explicit_environ_credentials",
return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
- autospec=True)
+ autospec=True,
+)
def test_default_explict_legacy_project_id(unused_get, monkeypatch):
- monkeypatch.setenv(environment_vars.LEGACY_PROJECT, 'explicit-env')
- assert _default.default() == (
- mock.sentinel.credentials, 'explicit-env')
+ monkeypatch.setenv(environment_vars.LEGACY_PROJECT, "explicit-env")
+ assert _default.default() == (mock.sentinel.credentials, "explicit-env")
+@mock.patch("logging.Logger.warning", autospec=True)
@mock.patch(
- 'logging.Logger.warning',
- autospec=True)
+ "google.auth._default._get_explicit_environ_credentials",
+ return_value=(mock.sentinel.credentials, None),
+ autospec=True,
+)
@mock.patch(
- 'google.auth._default._get_explicit_environ_credentials',
- return_value=(mock.sentinel.credentials, None), autospec=True)
+ "google.auth._default._get_gcloud_sdk_credentials",
+ return_value=(mock.sentinel.credentials, None),
+ autospec=True,
+)
@mock.patch(
- 'google.auth._default._get_gcloud_sdk_credentials',
- return_value=(mock.sentinel.credentials, None), autospec=True)
+ "google.auth._default._get_gae_credentials",
+ return_value=(mock.sentinel.credentials, None),
+ autospec=True,
+)
@mock.patch(
- 'google.auth._default._get_gae_credentials',
- return_value=(mock.sentinel.credentials, None), autospec=True)
-@mock.patch(
- 'google.auth._default._get_gce_credentials',
- return_value=(mock.sentinel.credentials, None), autospec=True)
+ "google.auth._default._get_gce_credentials",
+ return_value=(mock.sentinel.credentials, None),
+ autospec=True,
+)
def test_default_without_project_id(
- unused_gce, unused_gae, unused_sdk, unused_explicit, logger_warning):
- assert _default.default() == (
- mock.sentinel.credentials, None)
+ unused_gce, unused_gae, unused_sdk, unused_explicit, logger_warning
+):
+ assert _default.default() == (mock.sentinel.credentials, None)
logger_warning.assert_called_with(mock.ANY, mock.ANY, mock.ANY)
@mock.patch(
- 'google.auth._default._get_explicit_environ_credentials',
- return_value=(None, None), autospec=True)
+ "google.auth._default._get_explicit_environ_credentials",
+ return_value=(None, None),
+ autospec=True,
+)
@mock.patch(
- 'google.auth._default._get_gcloud_sdk_credentials',
- return_value=(None, None), autospec=True)
+ "google.auth._default._get_gcloud_sdk_credentials",
+ return_value=(None, None),
+ autospec=True,
+)
@mock.patch(
- 'google.auth._default._get_gae_credentials',
- return_value=(None, None), autospec=True)
+ "google.auth._default._get_gae_credentials",
+ return_value=(None, None),
+ autospec=True,
+)
@mock.patch(
- 'google.auth._default._get_gce_credentials',
- return_value=(None, None), autospec=True)
+ "google.auth._default._get_gce_credentials",
+ return_value=(None, None),
+ autospec=True,
+)
def test_default_fail(unused_gce, unused_gae, unused_sdk, unused_explicit):
with pytest.raises(exceptions.DefaultCredentialsError):
assert _default.default()
@mock.patch(
- 'google.auth._default._get_explicit_environ_credentials',
+ "google.auth._default._get_explicit_environ_credentials",
return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
- autospec=True)
-@mock.patch(
- 'google.auth.credentials.with_scopes_if_required', autospec=True)
+ autospec=True,
+)
+@mock.patch("google.auth.credentials.with_scopes_if_required", autospec=True)
def test_default_scoped(with_scopes, unused_get):
- scopes = ['one', 'two']
+ scopes = ["one", "two"]
credentials, project_id = _default.default(scopes=scopes)
assert credentials == with_scopes.return_value
assert project_id == mock.sentinel.project_id
- with_scopes.assert_called_once_with(
- mock.sentinel.credentials, scopes)
+ with_scopes.assert_called_once_with(mock.sentinel.credentials, scopes)
@mock.patch(
- 'google.auth._default._get_explicit_environ_credentials',
+ "google.auth._default._get_explicit_environ_credentials",
return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
- autospec=True)
+ autospec=True,
+)
def test_default_no_app_engine_compute_engine_module(unused_get):
"""
google.auth.compute_engine and google.auth.app_engine are both optional
@@ -397,8 +416,11 @@
that default fails gracefully if these modules are absent
"""
import sys
- with mock.patch.dict('sys.modules'):
- sys.modules['google.auth.compute_engine'] = None
- sys.modules['google.auth.app_engine'] = None
+
+ with mock.patch.dict("sys.modules"):
+ sys.modules["google.auth.compute_engine"] = None
+ sys.modules["google.auth.app_engine"] = None
assert _default.default() == (
- mock.sentinel.credentials, mock.sentinel.project_id)
+ mock.sentinel.credentials,
+ mock.sentinel.project_id,
+ )