def test__to_base_type_valid_creds(self):
creds_prop = TestNDBModel.creds
creds = client.Credentials()
creds_json = json.loads(creds_prop._to_base_type(creds))
self.assertDictEqual(creds_json, {
'_class': 'Credentials',
'_module': 'oauth2client.client',
'token_expiry': None,
})
python类Credentials()的实例源码
def test__from_base_type_valid_creds(self):
creds_prop = TestNDBModel.creds
creds_json = json.dumps({
'_class': 'Credentials',
'_module': 'oauth2client.client',
'token_expiry': None,
})
creds = creds_prop._from_base_type(creds_json)
self.assertIsInstance(creds, client.Credentials)
def test_to_from_json(self):
credentials = client.Credentials()
json = credentials.to_json()
client.Credentials.new_from_json(json)
def test_authorize_abstract(self):
credentials = client.Credentials()
http = object()
with self.assertRaises(NotImplementedError):
credentials.authorize(http)
def test_revoke_abstract(self):
credentials = client.Credentials()
http = object()
with self.assertRaises(NotImplementedError):
credentials.revoke(http)
def test_apply_abstract(self):
credentials = client.Credentials()
headers = {}
with self.assertRaises(NotImplementedError):
credentials.apply(headers)
def test__to_json_basic(self):
credentials = client.Credentials()
json_payload = credentials._to_json([])
# str(bytes) in Python2 and str(unicode) in Python3
self.assertIsInstance(json_payload, str)
payload = json.loads(json_payload)
expected_payload = {
'_class': client.Credentials.__name__,
'_module': client.Credentials.__module__,
'token_expiry': None,
}
self.assertEqual(payload, expected_payload)
def test_new_from_json_no_data(self):
creds_data = {}
json_data = json.dumps(creds_data)
with self.assertRaises(KeyError):
client.Credentials.new_from_json(json_data)
def test_new_from_json_basic_data(self):
creds_data = {
'_module': 'oauth2client.client',
'_class': 'Credentials',
}
json_data = json.dumps(creds_data)
credentials = client.Credentials.new_from_json(json_data)
self.assertIsInstance(credentials, client.Credentials)
def test_new_from_json_old_name(self):
creds_data = {
'_module': 'oauth2client.googleapiclient.client',
'_class': 'Credentials',
}
json_data = json.dumps(creds_data)
credentials = client.Credentials.new_from_json(json_data)
self.assertIsInstance(credentials, client.Credentials)
def test_new_from_json_bad_module(self):
creds_data = {
'_module': 'oauth2client.foobar',
'_class': 'Credentials',
}
json_data = json.dumps(creds_data)
with self.assertRaises(ImportError):
client.Credentials.new_from_json(json_data)
def test_new_from_json_bad_class(self):
creds_data = {
'_module': 'oauth2client.client',
'_class': 'NopeNotCredentials',
}
json_data = json.dumps(creds_data)
with self.assertRaises(AttributeError):
client.Credentials.new_from_json(json_data)
def make_value_from_datastore(self, value):
logger.info("make: Got type " + str(type(value)))
if value is None:
return None
if len(value) == 0:
return None
try:
credentials = client.Credentials.new_from_json(value)
except ValueError:
credentials = None
return credentials
def validate(self, value):
value = super(CredentialsProperty, self).validate(value)
logger.info("validate: Got type " + str(type(value)))
if value is not None and not isinstance(value, client.Credentials):
raise db.BadValueError(
'Property {0} must be convertible '
'to a Credentials instance ({1})'.format(self.name, value))
return value
def locked_put(self, credentials):
"""Write a Credentials to the datastore.
Args:
credentials: Credentials, the credentials to store.
"""
entity = self._model.get_or_insert(self._key_name)
setattr(entity, self._property_name, credentials)
entity.put()
if self._cache:
self._cache.set(self._key_name, credentials.to_json())
def get_credentials(self):
"""A thread local Credentials object.
Returns:
A client.Credentials object, or None if credentials hasn't been set
in this thread yet, which may happen when calling has_credentials
inside oauth_aware.
"""
return getattr(self._tls, 'credentials', None)
def _validate(self, value):
"""Validates a value as a proper credentials object.
Args:
value: A value to be set on the property.
Raises:
TypeError if the value is not an instance of Credentials.
"""
_LOGGER.info('validate: Got type %s', type(value))
if value is not None and not isinstance(value, client.Credentials):
raise TypeError(
'Property {0} must be convertible to a credentials '
'instance; received: {1}.'.format(self._name, value))
def setUp(self):
self.fake_model = tests_models.CredentialsModel()
self.fake_model_field = self.fake_model._meta.get_field('credentials')
self.field = models.CredentialsField(null=True)
self.credentials = client.Credentials()
self.pickle_str = _helpers._from_bytes(
base64.b64encode(pickle.dumps(self.credentials)))
self.jsonpickle_str = _helpers._from_bytes(
base64.b64encode(jsonpickle.encode(self.credentials).encode()))
def test_field_unpickled(self):
self.assertIsInstance(
self.field.to_python(self.pickle_str), client.Credentials)
def test_field_jsonunpickled(self):
self.assertIsInstance(
self.field.to_python(self.jsonpickle_str), client.Credentials)