diff options
author | Danny Hermes <daniel.j.hermes@gmail.com> | 2015-08-20 14:05:56 -0700 |
---|---|---|
committer | Danny Hermes <daniel.j.hermes@gmail.com> | 2015-08-21 08:23:07 -0700 |
commit | 28445b98bb81c60189dc73119904b0125ca73cd3 (patch) | |
tree | 2ee4eba0fbb8cfaaa2d2ec468194f9f16683a682 /tests/test_jwt.py | |
parent | 22981ac17bcba14f8743f4b852fbe0eb5f1f1c8d (diff) | |
download | oauth2client-28445b98bb81c60189dc73119904b0125ca73cd3.tar.gz |
Making tests/ files pass PEP8.
This is all errors except E402: module level import not at top of file.
This is because in most (all?) files the __author__ global comes
before imports begin.
Ref: http://stackoverflow.com/a/24859703/1068170
Diffstat (limited to 'tests/test_jwt.py')
-rw-r--r-- | tests/test_jwt.py | 121 |
1 files changed, 63 insertions, 58 deletions
diff --git a/tests/test_jwt.py b/tests/test_jwt.py index a56f24e..53aa656 100644 --- a/tests/test_jwt.py +++ b/tests/test_jwt.py @@ -58,7 +58,8 @@ class CryptTests(unittest.TestCase): self._check_sign_and_verify('privatekey.%s' % self.format) def test_sign_and_verify_from_converted_pkcs12(self): - # Tests that following instructions to convert from PKCS12 to PEM works. + # Tests that following instructions to convert from PKCS12 to + # PEM works. if self.format == 'pem': self._check_sign_and_verify('pem_from_pkcs12.pem') @@ -69,7 +70,7 @@ class CryptTests(unittest.TestCase): # We pass in a non-bytes password to make sure all branches # are traversed in tests. signer = self.signer.from_string(private_key, - password=u'notasecret') + password=u'notasecret') signature = signer.sign('foo') verifier = self.verifier.from_string(public_key, True) @@ -83,7 +84,7 @@ class CryptTests(unittest.TestCase): public_key = datafile('publickey.pem') certs = {'foo': public_key} audience = ('https://www.googleapis.com/auth/id?client_id=' - 'external_public_key@testing.gserviceaccount.com') + 'external_public_key@testing.gserviceaccount.com') try: crypt.verify_signed_jwt_with_certs(jwt, certs, audience) self.fail() @@ -97,11 +98,11 @@ class CryptTests(unittest.TestCase): now = int(time.time()) return crypt.make_signed_jwt(signer, { - 'aud': audience, - 'iat': now, - 'exp': now + 300, - 'user': 'billy bob', - 'metadata': {'meta': 'data'}, + 'aud': audience, + 'iat': now, + 'exp': now + 300, + 'user': 'billy bob', + 'metadata': {'meta': 'data'}, }) def test_verify_id_token(self): @@ -117,11 +118,12 @@ class CryptTests(unittest.TestCase): jwt = self._create_signed_jwt() http = HttpMockSequence([ - ({'status': '200'}, datafile('certs.json')), + ({'status': '200'}, datafile('certs.json')), ]) contents = verify_id_token( - jwt, 'some_audience_address@testing.gserviceaccount.com', http=http) + jwt, 'some_audience_address@testing.gserviceaccount.com', + http=http) self.assertEqual('billy bob', contents['user']) self.assertEqual('data', contents['metadata']['meta']) @@ -129,12 +131,12 @@ class CryptTests(unittest.TestCase): jwt = self._create_signed_jwt() http = HttpMockSequence([ - ({'status': '404'}, datafile('certs.json')), + ({'status': '404'}, datafile('certs.json')), ]) self.assertRaises(VerifyJwtTokenError, verify_id_token, jwt, - 'some_audience_address@testing.gserviceaccount.com', - http=http) + 'some_audience_address@testing.gserviceaccount.com', + http=http) def test_verify_id_token_bad_tokens(self): private_key = datafile('privatekey.%s' % self.format) @@ -146,47 +148,49 @@ class CryptTests(unittest.TestCase): self._check_jwt_failure('foo.bar.baz', 'Can\'t parse token') # Bad signature - jwt = b'.'.join([b'foo', crypt._urlsafe_b64encode('{"a":"b"}'), b'baz']) + jwt = b'.'.join([b'foo', + crypt._urlsafe_b64encode('{"a":"b"}'), + b'baz']) self._check_jwt_failure(jwt, 'Invalid token signature') # No expiration signer = self.signer.from_string(private_key) audience = ('https:#www.googleapis.com/auth/id?client_id=' - 'external_public_key@testing.gserviceaccount.com') + 'external_public_key@testing.gserviceaccount.com') jwt = crypt.make_signed_jwt(signer, { - 'aud': audience, - 'iat': time.time(), + 'aud': audience, + 'iat': time.time(), }) self._check_jwt_failure(jwt, 'No exp field in token') # No issued at jwt = crypt.make_signed_jwt(signer, { - 'aud': 'audience', - 'exp': time.time() + 400, + 'aud': 'audience', + 'exp': time.time() + 400, }) self._check_jwt_failure(jwt, 'No iat field in token') # Too early jwt = crypt.make_signed_jwt(signer, { - 'aud': 'audience', - 'iat': time.time() + 301, - 'exp': time.time() + 400, + 'aud': 'audience', + 'iat': time.time() + 301, + 'exp': time.time() + 400, }) self._check_jwt_failure(jwt, 'Token used too early') # Too late jwt = crypt.make_signed_jwt(signer, { - 'aud': 'audience', - 'iat': time.time() - 500, - 'exp': time.time() - 301, + 'aud': 'audience', + 'iat': time.time() - 500, + 'exp': time.time() - 301, }) self._check_jwt_failure(jwt, 'Token used too late') # Wrong target jwt = crypt.make_signed_jwt(signer, { - 'aud': 'somebody else', - 'iat': time.time(), - 'exp': time.time() + 300, + 'aud': 'somebody else', + 'iat': time.time(), + 'exp': time.time() + 300, }) self._check_jwt_failure(jwt, 'Wrong recipient') @@ -223,13 +227,13 @@ class SignedJwtAssertionCredentialsTests(unittest.TestCase): def test_credentials_good(self): private_key = datafile('privatekey.%s' % self.format) credentials = SignedJwtAssertionCredentials( - 'some_account@example.com', - private_key, - scope='read+write', - sub='joe@example.org') + 'some_account@example.com', + private_key, + scope='read+write', + sub='joe@example.org') http = HttpMockSequence([ - ({'status': '200'}, b'{"access_token":"1/3w","expires_in":3600}'), - ({'status': '200'}, 'echo_request_headers'), + ({'status': '200'}, b'{"access_token":"1/3w","expires_in":3600}'), + ({'status': '200'}, 'echo_request_headers'), ]) http = credentials.authorize(http) resp, content = http.request('http://example.org') @@ -238,23 +242,23 @@ class SignedJwtAssertionCredentialsTests(unittest.TestCase): def test_credentials_to_from_json(self): private_key = datafile('privatekey.%s' % self.format) credentials = SignedJwtAssertionCredentials( - 'some_account@example.com', - private_key, - scope='read+write', - sub='joe@example.org') + 'some_account@example.com', + private_key, + scope='read+write', + sub='joe@example.org') json = credentials.to_json() restored = Credentials.new_from_json(json) self.assertEqual(credentials.private_key, restored.private_key) self.assertEqual(credentials.private_key_password, - restored.private_key_password) + restored.private_key_password) self.assertEqual(credentials.kwargs, restored.kwargs) def _credentials_refresh(self, credentials): http = HttpMockSequence([ - ({'status': '200'}, b'{"access_token":"1/3w","expires_in":3600}'), - ({'status': '401'}, b''), - ({'status': '200'}, b'{"access_token":"3/3w","expires_in":3600}'), - ({'status': '200'}, 'echo_request_headers'), + ({'status': '200'}, b'{"access_token":"1/3w","expires_in":3600}'), + ({'status': '401'}, b''), + ({'status': '200'}, b'{"access_token":"3/3w","expires_in":3600}'), + ({'status': '200'}, 'echo_request_headers'), ]) http = credentials.authorize(http) _, content = http.request('http://example.org') @@ -263,10 +267,10 @@ class SignedJwtAssertionCredentialsTests(unittest.TestCase): def test_credentials_refresh_without_storage(self): private_key = datafile('privatekey.%s' % self.format) credentials = SignedJwtAssertionCredentials( - 'some_account@example.com', - private_key, - scope='read+write', - sub='joe@example.org') + 'some_account@example.com', + private_key, + scope='read+write', + sub='joe@example.org') content = self._credentials_refresh(credentials) @@ -275,12 +279,12 @@ class SignedJwtAssertionCredentialsTests(unittest.TestCase): def test_credentials_refresh_with_storage(self): private_key = datafile('privatekey.%s' % self.format) credentials = SignedJwtAssertionCredentials( - 'some_account@example.com', - private_key, - scope='read+write', - sub='joe@example.org') + 'some_account@example.com', + private_key, + scope='read+write', + sub='joe@example.org') - (filehandle, filename) = tempfile.mkstemp() + filehandle, filename = tempfile.mkstemp() os.close(filehandle) store = Storage(filename) store.put(credentials) @@ -293,7 +297,7 @@ class SignedJwtAssertionCredentialsTests(unittest.TestCase): class PEMSignedJwtAssertionCredentialsOpenSSLTests( - SignedJwtAssertionCredentialsTests): + SignedJwtAssertionCredentialsTests): def setUp(self): self.format = 'pem' @@ -301,7 +305,7 @@ class PEMSignedJwtAssertionCredentialsOpenSSLTests( class PEMSignedJwtAssertionCredentialsPyCryptoTests( - SignedJwtAssertionCredentialsTests): + SignedJwtAssertionCredentialsTests): def setUp(self): self.format = 'pem' @@ -314,10 +318,10 @@ class PKCSSignedJwtAssertionCredentialsPyCryptoTests(unittest.TestCase): crypt.Signer = crypt.PyCryptoSigner private_key = datafile('privatekey.p12') credentials = SignedJwtAssertionCredentials( - 'some_account@example.com', - private_key, - scope='read+write', - sub='joe@example.org') + 'some_account@example.com', + private_key, + scope='read+write', + sub='joe@example.org') try: credentials._generate_assertion() self.fail() @@ -326,6 +330,7 @@ class PKCSSignedJwtAssertionCredentialsPyCryptoTests(unittest.TestCase): class TestHasOpenSSLFlag(unittest.TestCase): + def test_true(self): self.assertEqual(True, HAS_OPENSSL) self.assertEqual(True, HAS_CRYPTO) |