@@ -176,11 +176,13 @@ def _get_pem_key(credentials):
176
176
SIGNATURE_STRING = 'dummy_signature'
177
177
with _Monkey (MUT , RSA = rsa , PKCS1_v1_5 = pkcs_v1_5 ,
178
178
SHA256 = sha256 , _get_pem_key = _get_pem_key ):
179
- self .assertRaises (NameError , self ._callFUT ,
179
+ self .assertRaises (UnboundLocalError , self ._callFUT ,
180
180
BAD_CREDENTIALS , EXPIRATION , SIGNATURE_STRING )
181
181
182
- def _run_test_with_credentials (self , credentials , account_name ):
182
+ def _run_test_with_credentials (self , credentials , account_name ,
183
+ signature_string = None ):
183
184
import base64
185
+ import six
184
186
from gcloud ._testing import _Monkey
185
187
from gcloud import credentials as MUT
186
188
@@ -190,7 +192,7 @@ def _run_test_with_credentials(self, credentials, account_name):
190
192
sha256 = _SHA256 ()
191
193
192
194
EXPIRATION = '100'
193
- SIGNATURE_STRING = b'dummy_signature'
195
+ SIGNATURE_STRING = signature_string or b'dummy_signature'
194
196
with _Monkey (MUT , crypt = crypt , RSA = rsa , PKCS1_v1_5 = pkcs_v1_5 ,
195
197
SHA256 = sha256 ):
196
198
result = self ._callFUT (credentials , EXPIRATION , SIGNATURE_STRING )
@@ -199,7 +201,12 @@ def _run_test_with_credentials(self, credentials, account_name):
199
201
self .assertEqual (crypt ._private_key_text ,
200
202
base64 .b64encode (b'dummy_private_key_text' ))
201
203
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' ))
203
210
SIGNED = base64 .b64encode (b'DEADBEEF' )
204
211
expected_query = {
205
212
'Expires' : EXPIRATION ,
@@ -217,6 +224,17 @@ def test_signed_jwt_for_p12(self):
217
224
ACCOUNT_NAME , b'dummy_private_key_text' , scopes )
218
225
self ._run_test_with_credentials (credentials , ACCOUNT_NAME )
219
226
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
+
220
238
def test_service_account_via_json_key (self ):
221
239
from oauth2client import service_account
222
240
from gcloud ._testing import _Monkey
0 commit comments