Skip to content

Commit 2da8ccd

Browse files
authored
Merge pull request googleapis#676 from chripede/django-jsonpickle
Use jsonpickle in django contrib
2 parents 92e4ad3 + d007870 commit 2da8ccd

File tree

6 files changed

+24
-6
lines changed

6 files changed

+24
-6
lines changed

oauth2client/contrib/devshell.py

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class CommunicationError(Error):
3737
class NoDevshellServer(Error):
3838
"""Error when no Developer Shell server can be contacted."""
3939

40+
4041
# The request for credential information to the Developer Shell client socket
4142
# is always an empty PBLite-formatted JSON object, so just define it as a
4243
# constant.

oauth2client/contrib/django_util/models.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from django.db import models
2121
from django.utils import encoding
22+
import jsonpickle
2223

2324
import oauth2client
2425

@@ -48,7 +49,12 @@ def to_python(self, value):
4849
elif isinstance(value, oauth2client.client.Credentials):
4950
return value
5051
else:
51-
return pickle.loads(base64.b64decode(encoding.smart_bytes(value)))
52+
try:
53+
return jsonpickle.decode(
54+
base64.b64decode(encoding.smart_bytes(value)).decode())
55+
except ValueError:
56+
return pickle.loads(
57+
base64.b64decode(encoding.smart_bytes(value)))
5258

5359
def get_prep_value(self, value):
5460
"""Overrides ``models.Field`` method. This is used to convert
@@ -58,7 +64,8 @@ def get_prep_value(self, value):
5864
if value is None:
5965
return None
6066
else:
61-
return encoding.smart_text(base64.b64encode(pickle.dumps(value)))
67+
return encoding.smart_text(
68+
base64.b64encode(jsonpickle.encode(value).encode()))
6269

6370
def value_to_string(self, obj):
6471
"""Convert the field value from the provided model to a string.

oauth2client/tools.py

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def _CreateArgumentParser():
9292
help='Set the logging level of detail.')
9393
return parser
9494

95+
9596
# argparser is an ArgumentParser that contains command-line options expected
9697
# by tools.run(). Pass it in as part of the 'parents' argument to your own
9798
# ArgumentParser.

tests/contrib/django_util/test_django_models.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import pickle
2222
import unittest
2323

24+
import jsonpickle
25+
2426
from oauth2client import _helpers
2527
from oauth2client import client
2628
from oauth2client.contrib.django_util import models
@@ -36,6 +38,8 @@ def setUp(self):
3638
self.credentials = client.Credentials()
3739
self.pickle_str = _helpers._from_bytes(
3840
base64.b64encode(pickle.dumps(self.credentials)))
41+
self.jsonpickle_str = _helpers._from_bytes(
42+
base64.b64encode(jsonpickle.encode(self.credentials).encode()))
3943

4044
def test_field_is_text(self):
4145
self.assertEqual(self.field.get_internal_type(), 'BinaryField')
@@ -44,6 +48,10 @@ def test_field_unpickled(self):
4448
self.assertIsInstance(
4549
self.field.to_python(self.pickle_str), client.Credentials)
4650

51+
def test_field_jsonunpickled(self):
52+
self.assertIsInstance(
53+
self.field.to_python(self.jsonpickle_str), client.Credentials)
54+
4755
def test_field_already_unpickled(self):
4856
self.assertIsInstance(
4957
self.field.to_python(self.credentials), client.Credentials)
@@ -62,12 +70,12 @@ def test_field_unpickled_none(self):
6270
def test_field_pickled(self):
6371
prep_value = self.field.get_db_prep_value(self.credentials,
6472
connection=None)
65-
self.assertEqual(prep_value, self.pickle_str)
73+
self.assertEqual(prep_value, self.jsonpickle_str)
6674

6775
def test_field_value_to_string(self):
6876
self.fake_model.credentials = self.credentials
6977
value_str = self.fake_model_field.value_to_string(self.fake_model)
70-
self.assertEqual(value_str, self.pickle_str)
78+
self.assertEqual(value_str, self.jsonpickle_str)
7179

7280
def test_field_value_to_string_none(self):
7381
self.fake_model.credentials = None

tests/contrib/test_devshell.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ def run(self):
160160
s.recv(to_read, socket.MSG_WAITALL))
161161
if resp_buffer != devshell.CREDENTIAL_INFO_REQUEST_JSON:
162162
self.bad_request = True
163-
l = len(self.response)
164-
s.sendall('{0}\n{1}'.format(l, self.response).encode())
163+
response_len = len(self.response)
164+
s.sendall('{0}\n{1}'.format(response_len, self.response).encode())
165165
finally:
166166
# Will fail if s is None, but these tests never encounter
167167
# that scenario.

tests/test_service_account.py

+1
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ def test_access_token(self, utcnow):
369369

370370
self.assertEqual(credentials.access_token, token2)
371371

372+
372373
TOKEN_LIFE = service_account._JWTAccessCredentials._MAX_TOKEN_LIFETIME_SECS
373374
T1 = 42
374375
T1_DATE = datetime.datetime(1970, 1, 1, second=T1)

0 commit comments

Comments
 (0)