Skip to content

Commit 1b94f9f

Browse files
committed
Making unit tests pass after regression3 test fixes.
- Adding test for branch miss in credentials._get_signed_query_params - Making storage.batch._unpack_batch_response work correctly in Python2 and Python3 (parser expects str in both) NOTE: Yet again %s caused issues between Py2 and Py3 (as in the PR #126 in oauth2client).
1 parent 11904c3 commit 1b94f9f

File tree

3 files changed

+63
-11
lines changed

3 files changed

+63
-11
lines changed

gcloud/storage/batch.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,22 @@ def __exit__(self, exc_type, exc_val, exc_tb):
170170
def _unpack_batch_response(response, content):
171171
"""Convert response, content -> [(status, reason, payload)]."""
172172
parser = Parser()
173-
if isinstance(content, six.binary_type):
174-
content = content.decode('utf-8')
175-
faux_message = ''.join([
176-
'Content-Type: ',
177-
response['content-type'],
178-
'\nMIME-Version: 1.0\n\n',
173+
if not isinstance(content, six.binary_type):
174+
content = content.encode('utf-8')
175+
content_type = response['content-type']
176+
if not isinstance(content_type, six.binary_type):
177+
content_type = content_type.encode('utf-8')
178+
faux_message = b''.join([
179+
b'Content-Type: ',
180+
content_type,
181+
b'\nMIME-Version: 1.0\n\n',
179182
content,
180183
])
181184

182-
message = parser.parsestr(faux_message)
185+
if six.PY2:
186+
message = parser.parsestr(faux_message)
187+
else: # pragma: NO COVER Python3
188+
message = parser.parsestr(faux_message.decode('utf-8'))
183189

184190
if not isinstance(message._payload, list):
185191
raise ValueError('Bad response: not multi-part')

gcloud/storage/test_batch.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,34 @@ def test_as_context_mgr_w_error(self):
347347
self.assertEqual(len(batch._responses), 0)
348348

349349

350+
class Test__unpack_batch_response(unittest2.TestCase):
351+
352+
def _callFUT(self, response, content):
353+
from gcloud.storage.batch import _unpack_batch_response
354+
return _unpack_batch_response(response, content)
355+
356+
def test_bytes(self):
357+
RESPONSE = {'content-type': b'multipart/mixed; boundary="DEADBEEF="'}
358+
CONTENT = _THREE_PART_MIME_RESPONSE.encode('utf-8')
359+
result = list(self._callFUT(RESPONSE, CONTENT))
360+
self.assertEqual(len(result), 3)
361+
self.assertEqual(result[0], ('200', 'OK', {u'bar': 2, u'foo': 1}))
362+
self.assertEqual(result[1], ('200', 'OK', {u'foo': 1, u'bar': 3}))
363+
self.assertEqual(result[2], ('204', 'No Content', ''))
364+
365+
def test_unicode(self):
366+
import six
367+
RESPONSE = {'content-type': u'multipart/mixed; boundary="DEADBEEF="'}
368+
CONTENT = _THREE_PART_MIME_RESPONSE
369+
if isinstance(CONTENT, six.binary_type): # pragma: NO COVER Python3
370+
CONTENT = CONTENT.decode('utf-8')
371+
result = list(self._callFUT(RESPONSE, CONTENT))
372+
self.assertEqual(len(result), 3)
373+
self.assertEqual(result[0], ('200', 'OK', {u'bar': 2, u'foo': 1}))
374+
self.assertEqual(result[1], ('200', 'OK', {u'foo': 1, u'bar': 3}))
375+
self.assertEqual(result[2], ('204', 'No Content', ''))
376+
377+
350378
_THREE_PART_MIME_RESPONSE = """\
351379
--DEADBEEF=
352380
Content-Type: application/http

gcloud/test_credentials.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,13 @@ def _get_pem_key(credentials):
176176
SIGNATURE_STRING = 'dummy_signature'
177177
with _Monkey(MUT, RSA=rsa, PKCS1_v1_5=pkcs_v1_5,
178178
SHA256=sha256, _get_pem_key=_get_pem_key):
179-
self.assertRaises(NameError, self._callFUT,
179+
self.assertRaises(UnboundLocalError, self._callFUT,
180180
BAD_CREDENTIALS, EXPIRATION, SIGNATURE_STRING)
181181

182-
def _run_test_with_credentials(self, credentials, account_name):
182+
def _run_test_with_credentials(self, credentials, account_name,
183+
signature_string=None):
183184
import base64
185+
import six
184186
from gcloud._testing import _Monkey
185187
from gcloud import credentials as MUT
186188

@@ -190,7 +192,7 @@ def _run_test_with_credentials(self, credentials, account_name):
190192
sha256 = _SHA256()
191193

192194
EXPIRATION = '100'
193-
SIGNATURE_STRING = b'dummy_signature'
195+
SIGNATURE_STRING = signature_string or b'dummy_signature'
194196
with _Monkey(MUT, crypt=crypt, RSA=rsa, PKCS1_v1_5=pkcs_v1_5,
195197
SHA256=sha256):
196198
result = self._callFUT(credentials, EXPIRATION, SIGNATURE_STRING)
@@ -199,7 +201,12 @@ def _run_test_with_credentials(self, credentials, account_name):
199201
self.assertEqual(crypt._private_key_text,
200202
base64.b64encode(b'dummy_private_key_text'))
201203
self.assertEqual(crypt._private_key_password, 'notasecret')
202-
self.assertEqual(sha256._signature_string, SIGNATURE_STRING)
204+
# sha256._signature_string is always bytes.
205+
if isinstance(SIGNATURE_STRING, six.binary_type):
206+
self.assertEqual(sha256._signature_string, SIGNATURE_STRING)
207+
else:
208+
self.assertEqual(sha256._signature_string,
209+
SIGNATURE_STRING.encode('utf-8'))
203210
SIGNED = base64.b64encode(b'DEADBEEF')
204211
expected_query = {
205212
'Expires': EXPIRATION,
@@ -217,6 +224,17 @@ def test_signed_jwt_for_p12(self):
217224
ACCOUNT_NAME, b'dummy_private_key_text', scopes)
218225
self._run_test_with_credentials(credentials, ACCOUNT_NAME)
219226

227+
def test_signature_non_bytes(self):
228+
from oauth2client import client
229+
230+
scopes = []
231+
ACCOUNT_NAME = 'dummy_service_account_name'
232+
SIGNATURE_STRING = u'dummy_signature'
233+
credentials = client.SignedJwtAssertionCredentials(
234+
ACCOUNT_NAME, b'dummy_private_key_text', scopes)
235+
self._run_test_with_credentials(credentials, ACCOUNT_NAME,
236+
signature_string=SIGNATURE_STRING)
237+
220238
def test_service_account_via_json_key(self):
221239
from oauth2client import service_account
222240
from gcloud._testing import _Monkey

0 commit comments

Comments
 (0)