本文整理汇总了Python中google.auth.credentials方法的典型用法代码示例。如果您正苦于以下问题:Python auth.credentials方法的具体用法?Python auth.credentials怎么用?Python auth.credentials使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类google.auth
的用法示例。
在下文中一共展示了auth.credentials方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_constructor_explicit
# 需要导入模块: from google import auth [as 别名]
# 或者: from google.auth import credentials [as 别名]
def test_constructor_explicit(self):
credentials = _make_credentials()
database = "now-db"
client_info = mock.Mock()
client_options = mock.Mock()
client = self._make_one(
project=self.PROJECT,
credentials=credentials,
database=database,
client_info=client_info,
client_options=client_options,
)
self.assertEqual(client.project, self.PROJECT)
self.assertEqual(client._credentials, credentials)
self.assertEqual(client._database, database)
self.assertIs(client._client_info, client_info)
self.assertIs(client._client_options, client_options)
示例2: test__rpc_metadata_property_with_emulator
# 需要导入模块: from google import auth [as 别名]
# 或者: from google.auth import credentials [as 别名]
def test__rpc_metadata_property_with_emulator(self):
emulator_host = "localhost:8081"
with mock.patch("os.getenv") as getenv:
getenv.return_value = emulator_host
credentials = _make_credentials()
database = "quanta"
client = self._make_one(
project=self.PROJECT, credentials=credentials, database=database
)
self.assertEqual(
client._rpc_metadata,
[
("google-cloud-resource-prefix", client._database_string),
("authorization", "Bearer owner"),
],
)
示例3: test___database_string_property
# 需要导入模块: from google import auth [as 别名]
# 或者: from google.auth import credentials [as 别名]
def test___database_string_property(self):
credentials = _make_credentials()
database = "cheeeeez"
with pytest.deprecated_call():
client = self._make_one(
project=self.PROJECT, credentials=credentials, database=database
)
self.assertIsNone(client._database_string_internal)
database_string = client._database_string
expected = "projects/{}/databases/{}".format(client.project, client._database)
self.assertEqual(database_string, expected)
self.assertIs(database_string, client._database_string_internal)
# Swap it out with a unique value to verify it is cached.
client._database_string_internal = mock.sentinel.cached
self.assertIs(client._database_string, mock.sentinel.cached)
示例4: test_requests
# 需要导入模块: from google import auth [as 别名]
# 或者: from google.auth import credentials [as 别名]
def test_requests():
credentials, project_id = google.auth.default()
credentials = google.auth.credentials.with_scopes_if_required(
credentials, ["https://www.googleapis.com/auth/pubsub"]
)
authed_session = google.auth.transport.requests.AuthorizedSession(credentials)
authed_session.configure_mtls_channel()
# If the devices has default client cert source, then a mutual TLS channel
# is supposed to be created.
assert authed_session.is_mtls == mtls.has_default_client_cert_source()
# Sleep 1 second to avoid 503 error.
time.sleep(1)
if authed_session.is_mtls:
response = authed_session.get(MTLS_ENDPOINT.format(project_id))
else:
response = authed_session.get(REGULAR_ENDPOINT.format(project_id))
assert response.ok
示例5: test_urllib3
# 需要导入模块: from google import auth [as 别名]
# 或者: from google.auth import credentials [as 别名]
def test_urllib3():
credentials, project_id = google.auth.default()
credentials = google.auth.credentials.with_scopes_if_required(
credentials, ["https://www.googleapis.com/auth/pubsub"]
)
authed_http = google.auth.transport.urllib3.AuthorizedHttp(credentials)
is_mtls = authed_http.configure_mtls_channel()
# If the devices has default client cert source, then a mutual TLS channel
# is supposed to be created.
assert is_mtls == mtls.has_default_client_cert_source()
# Sleep 1 second to avoid 503 error.
time.sleep(1)
if is_mtls:
response = authed_http.request("GET", MTLS_ENDPOINT.format(project_id))
else:
response = authed_http.request("GET", REGULAR_ENDPOINT.format(project_id))
assert response.status == 200
示例6: test_requests_with_default_client_cert_source
# 需要导入模块: from google import auth [as 别名]
# 或者: from google.auth import credentials [as 别名]
def test_requests_with_default_client_cert_source():
credentials, project_id = google.auth.default()
credentials = google.auth.credentials.with_scopes_if_required(
credentials, ["https://www.googleapis.com/auth/pubsub"]
)
authed_session = google.auth.transport.requests.AuthorizedSession(credentials)
if mtls.has_default_client_cert_source():
authed_session.configure_mtls_channel(
client_cert_callback=mtls.default_client_cert_source()
)
assert authed_session.is_mtls
# Sleep 1 second to avoid 503 error.
time.sleep(1)
response = authed_session.get(MTLS_ENDPOINT.format(project_id))
assert response.ok
示例7: test_urllib3_with_default_client_cert_source
# 需要导入模块: from google import auth [as 别名]
# 或者: from google.auth import credentials [as 别名]
def test_urllib3_with_default_client_cert_source():
credentials, project_id = google.auth.default()
credentials = google.auth.credentials.with_scopes_if_required(
credentials, ["https://www.googleapis.com/auth/pubsub"]
)
authed_http = google.auth.transport.urllib3.AuthorizedHttp(credentials)
if mtls.has_default_client_cert_source():
assert authed_http.configure_mtls_channel(
client_cert_callback=mtls.default_client_cert_source()
)
# Sleep 1 second to avoid 503 error.
time.sleep(1)
response = authed_http.request("GET", MTLS_ENDPOINT.format(project_id))
assert response.status == 200
示例8: test_grpc_request_with_regular_credentials
# 需要导入模块: from google import auth [as 别名]
# 或者: from google.auth import credentials [as 别名]
def test_grpc_request_with_regular_credentials(http_request):
credentials, project_id = google.auth.default()
credentials = google.auth.credentials.with_scopes_if_required(
credentials, ["https://www.googleapis.com/auth/pubsub"]
)
transport = publisher_grpc_transport.PublisherGrpcTransport(
address=publisher_client.PublisherClient.SERVICE_ADDRESS,
credentials=credentials,
)
# Create a pub/sub client.
client = pubsub_v1.PublisherClient(transport=transport)
# list the topics and drain the iterator to test that an authorized API
# call works.
list_topics_iter = client.list_topics(project="projects/{}".format(project_id))
list(list_topics_iter)
示例9: test_grpc_request_with_on_demand_jwt_credentials
# 需要导入模块: from google import auth [as 别名]
# 或者: from google.auth import credentials [as 别名]
def test_grpc_request_with_on_demand_jwt_credentials():
credentials, project_id = google.auth.default()
credentials = google.auth.jwt.OnDemandCredentials.from_signing_credentials(
credentials
)
transport = publisher_grpc_transport.PublisherGrpcTransport(
address=publisher_client.PublisherClient.SERVICE_ADDRESS,
credentials=credentials,
)
# Create a pub/sub client.
client = pubsub_v1.PublisherClient(transport=transport)
# list the topics and drain the iterator to test that an authorized API
# call works.
list_topics_iter = client.list_topics(project="projects/{}".format(project_id))
list(list_topics_iter)
示例10: test_request_no_refresh
# 需要导入模块: from google import auth [as 别名]
# 或者: from google.auth import credentials [as 别名]
def test_request_no_refresh(self):
credentials = mock.Mock(wraps=CredentialsStub())
response = make_response()
adapter = AdapterStub([response])
authed_session = google.auth.transport.requests.AuthorizedSession(credentials)
authed_session.mount(self.TEST_URL, adapter)
result = authed_session.request("GET", self.TEST_URL)
assert response == result
assert credentials.before_request.called
assert not credentials.refresh.called
assert len(adapter.requests) == 1
assert adapter.requests[0].url == self.TEST_URL
assert adapter.requests[0].headers["authorization"] == "token"
示例11: test_request_max_allowed_time_timeout_error
# 需要导入模块: from google import auth [as 别名]
# 或者: from google.auth import credentials [as 别名]
def test_request_max_allowed_time_timeout_error(self, frozen_time):
tick_one_second = functools.partial(
frozen_time.tick, delta=datetime.timedelta(seconds=1.0)
)
credentials = mock.Mock(
wraps=TimeTickCredentialsStub(time_tick=tick_one_second)
)
adapter = TimeTickAdapterStub(
time_tick=tick_one_second, responses=[make_response(status=http_client.OK)]
)
authed_session = google.auth.transport.requests.AuthorizedSession(credentials)
authed_session.mount(self.TEST_URL, adapter)
# Because a request takes a full mocked second, max_allowed_time shorter
# than that will cause a timeout error.
with pytest.raises(requests.exceptions.Timeout):
authed_session.request("GET", self.TEST_URL, max_allowed_time=0.9)
示例12: test_request_max_allowed_time_w_transport_timeout_no_error
# 需要导入模块: from google import auth [as 别名]
# 或者: from google.auth import credentials [as 别名]
def test_request_max_allowed_time_w_transport_timeout_no_error(self, frozen_time):
tick_one_second = functools.partial(
frozen_time.tick, delta=datetime.timedelta(seconds=1.0)
)
credentials = mock.Mock(
wraps=TimeTickCredentialsStub(time_tick=tick_one_second)
)
adapter = TimeTickAdapterStub(
time_tick=tick_one_second,
responses=[
make_response(status=http_client.UNAUTHORIZED),
make_response(status=http_client.OK),
],
)
authed_session = google.auth.transport.requests.AuthorizedSession(credentials)
authed_session.mount(self.TEST_URL, adapter)
# A short configured transport timeout does not affect max_allowed_time.
# The latter is not adjusted to it and is only concerned with the actual
# execution time. The call below should thus not raise a timeout error.
authed_session.request("GET", self.TEST_URL, timeout=0.5, max_allowed_time=3.1)
示例13: test_configure_mtls_channel_with_callback
# 需要导入模块: from google import auth [as 别名]
# 或者: from google.auth import credentials [as 别名]
def test_configure_mtls_channel_with_callback(self):
mock_callback = mock.Mock()
mock_callback.return_value = (
pytest.public_cert_bytes,
pytest.private_key_bytes,
)
auth_session = google.auth.transport.requests.AuthorizedSession(
credentials=mock.Mock()
)
auth_session.configure_mtls_channel(mock_callback)
assert auth_session.is_mtls
assert isinstance(
auth_session.adapters["https://"],
google.auth.transport.requests._MutualTlsAdapter,
)
示例14: test_get_credentials_default_credentials
# 需要导入模块: from google import auth [as 别名]
# 或者: from google.auth import credentials [as 别名]
def test_get_credentials_default_credentials(monkeypatch):
import google.auth
import google.auth.credentials
import google.cloud.bigquery
def mock_default_credentials(scopes=None, request=None):
return (
mock.create_autospec(google.auth.credentials.Credentials),
"default-project",
)
monkeypatch.setattr(google.auth, "default", mock_default_credentials)
credentials, project = auth.get_credentials()
assert project == "default-project"
assert credentials is not None
示例15: test_get_credentials_load_user_no_default
# 需要导入模块: from google import auth [as 别名]
# 或者: from google.auth import credentials [as 别名]
def test_get_credentials_load_user_no_default(monkeypatch):
import google.auth
import google.auth.credentials
import pydata_google_auth.cache
def mock_default_credentials(scopes=None, request=None):
return (None, None)
monkeypatch.setattr(google.auth, "default", mock_default_credentials)
mock_user_credentials = mock.create_autospec(
google.auth.credentials.Credentials
)
mock_cache = mock.create_autospec(
pydata_google_auth.cache.CredentialsCache
)
mock_cache.load.return_value = mock_user_credentials
monkeypatch.setattr(auth, "get_credentials_cache", lambda _: mock_cache)
credentials, project = auth.get_credentials()
assert project is None
assert credentials is mock_user_credentials