Improved unit-test line coverage and quality.
In particular - Upgraded some unit test modules to `unittest2`. - Got to 100% line coverage in `test_jwt`, `test_clientsecrets`, and `test_service_account` by using `self.assertRaises` instead of `self.fail()` - In some places used `self.assertRaises` as a context manager so the thrown exception can be inspected / compared against. This is done instead of manually catching the exception and holding on to it (`unittest2` backports this feature from `unittest` that was not available in Python 2.6) - Added license to `test_multistore_file` - Removed legacy `python2.4` hashbang from `test_jwt` and `test_service_account` - Added `unittest2` as a test dependency in `tox.ini` - Updated `test_clientsecrets.test_load_by_filename_missing_file` to use the `self.assertRaises` context manager (this was a problem in #349 and #350) - Updated `test_jwt` module docstring to reflect actual purpose
This commit is contained in:
@@ -1,3 +1,17 @@
|
|||||||
|
# Copyright 2015 Google Inc. All rights reserved.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
"""Unit tests for oauth2client.multistore_file."""
|
"""Unit tests for oauth2client.multistore_file."""
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import errno
|
|||||||
from io import StringIO
|
from io import StringIO
|
||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
import unittest2
|
||||||
|
|
||||||
from oauth2client._helpers import _from_bytes
|
from oauth2client._helpers import _from_bytes
|
||||||
from oauth2client import GOOGLE_AUTH_URI
|
from oauth2client import GOOGLE_AUTH_URI
|
||||||
@@ -37,7 +37,7 @@ NONEXISTENT_FILE = os.path.join(
|
|||||||
os.path.dirname(__file__), 'afilethatisntthere.json')
|
os.path.dirname(__file__), 'afilethatisntthere.json')
|
||||||
|
|
||||||
|
|
||||||
class Test__validate_clientsecrets(unittest.TestCase):
|
class Test__validate_clientsecrets(unittest2.TestCase):
|
||||||
|
|
||||||
def test_with_none(self):
|
def test_with_none(self):
|
||||||
self.assertRaises(clientsecrets.InvalidClientSecretsError,
|
self.assertRaises(clientsecrets.InvalidClientSecretsError,
|
||||||
@@ -157,7 +157,7 @@ class Test__validate_clientsecrets(unittest.TestCase):
|
|||||||
self.assertEqual(result, (clientsecrets.TYPE_INSTALLED, client_info))
|
self.assertEqual(result, (clientsecrets.TYPE_INSTALLED, client_info))
|
||||||
|
|
||||||
|
|
||||||
class Test__loadfile(unittest.TestCase):
|
class Test__loadfile(unittest2.TestCase):
|
||||||
|
|
||||||
def test_success(self):
|
def test_success(self):
|
||||||
client_type, client_info = clientsecrets._loadfile(VALID_FILE)
|
client_type, client_info = clientsecrets._loadfile(VALID_FILE)
|
||||||
@@ -186,7 +186,7 @@ class Test__loadfile(unittest.TestCase):
|
|||||||
clientsecrets._loadfile, filename)
|
clientsecrets._loadfile, filename)
|
||||||
|
|
||||||
|
|
||||||
class OAuth2CredentialsTests(unittest.TestCase):
|
class OAuth2CredentialsTests(unittest2.TestCase):
|
||||||
|
|
||||||
def test_validate_error(self):
|
def test_validate_error(self):
|
||||||
payload = (
|
payload = (
|
||||||
@@ -210,32 +210,30 @@ class OAuth2CredentialsTests(unittest.TestCase):
|
|||||||
# Ensure that it is unicode
|
# Ensure that it is unicode
|
||||||
src = _from_bytes(src)
|
src = _from_bytes(src)
|
||||||
# Test load(s)
|
# Test load(s)
|
||||||
try:
|
with self.assertRaises(
|
||||||
|
clientsecrets.InvalidClientSecretsError) as exc_manager:
|
||||||
clientsecrets.loads(src)
|
clientsecrets.loads(src)
|
||||||
self.fail(src + ' should not be a valid client_secrets file.')
|
|
||||||
except clientsecrets.InvalidClientSecretsError as e:
|
self.assertTrue(str(exc_manager.exception).startswith(match))
|
||||||
self.assertTrue(str(e).startswith(match))
|
|
||||||
|
|
||||||
# Test loads(fp)
|
# Test loads(fp)
|
||||||
try:
|
with self.assertRaises(
|
||||||
|
clientsecrets.InvalidClientSecretsError) as exc_manager:
|
||||||
fp = StringIO(src)
|
fp = StringIO(src)
|
||||||
clientsecrets.load(fp)
|
clientsecrets.load(fp)
|
||||||
self.fail(src + ' should not be a valid client_secrets file.')
|
|
||||||
except clientsecrets.InvalidClientSecretsError as e:
|
self.assertTrue(str(exc_manager.exception).startswith(match))
|
||||||
self.assertTrue(str(e).startswith(match))
|
|
||||||
|
|
||||||
def test_load_by_filename_missing_file(self):
|
def test_load_by_filename_missing_file(self):
|
||||||
caught_exception = None
|
with self.assertRaises(
|
||||||
try:
|
clientsecrets.InvalidClientSecretsError) as exc_manager:
|
||||||
clientsecrets._loadfile(NONEXISTENT_FILE)
|
clientsecrets._loadfile(NONEXISTENT_FILE)
|
||||||
except clientsecrets.InvalidClientSecretsError as exc:
|
|
||||||
caught_exception = exc
|
|
||||||
|
|
||||||
self.assertEquals(caught_exception.args[1], NONEXISTENT_FILE)
|
self.assertEquals(exc_manager.exception.args[1], NONEXISTENT_FILE)
|
||||||
self.assertEquals(caught_exception.args[3], errno.ENOENT)
|
self.assertEquals(exc_manager.exception.args[3], errno.ENOENT)
|
||||||
|
|
||||||
|
|
||||||
class CachedClientsecretsTests(unittest.TestCase):
|
class CachedClientsecretsTests(unittest2.TestCase):
|
||||||
|
|
||||||
class CacheMock(object):
|
class CacheMock(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@@ -282,12 +280,8 @@ class CachedClientsecretsTests(unittest.TestCase):
|
|||||||
self.assertEqual(None, self.cache_mock.last_set_ns)
|
self.assertEqual(None, self.cache_mock.last_set_ns)
|
||||||
|
|
||||||
def test_validation(self):
|
def test_validation(self):
|
||||||
try:
|
with self.assertRaises(clientsecrets.InvalidClientSecretsError):
|
||||||
clientsecrets.loadfile(INVALID_FILE, cache=self.cache_mock)
|
clientsecrets.loadfile(INVALID_FILE, cache=self.cache_mock)
|
||||||
self.fail('Expected InvalidClientSecretsError to be raised '
|
|
||||||
'while loading %s' % INVALID_FILE)
|
|
||||||
except clientsecrets.InvalidClientSecretsError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def test_without_cache(self):
|
def test_without_cache(self):
|
||||||
# this also ensures loadfile() is backward compatible
|
# this also ensures loadfile() is backward compatible
|
||||||
@@ -297,4 +291,4 @@ class CachedClientsecretsTests(unittest.TestCase):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__': # pragma: NO COVER
|
if __name__ == '__main__': # pragma: NO COVER
|
||||||
unittest.main()
|
unittest2.main()
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
#!/usr/bin/python2.4
|
|
||||||
#
|
|
||||||
# Copyright 2014 Google Inc. All rights reserved.
|
# Copyright 2014 Google Inc. All rights reserved.
|
||||||
#
|
#
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
@@ -14,15 +12,12 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
"""Oauth2client tests
|
"""Unit tests for JWT related methods in oauth2client."""
|
||||||
|
|
||||||
Unit tests for oauth2client.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
import time
|
import time
|
||||||
import unittest
|
import unittest2
|
||||||
|
|
||||||
from .http_mock import HttpMockSequence
|
from .http_mock import HttpMockSequence
|
||||||
from oauth2client.client import Credentials
|
from oauth2client.client import Credentials
|
||||||
@@ -45,7 +40,7 @@ def datafile(filename):
|
|||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
class CryptTests(unittest.TestCase):
|
class CryptTests(unittest2.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.format = 'p12'
|
self.format = 'p12'
|
||||||
@@ -83,11 +78,11 @@ class CryptTests(unittest.TestCase):
|
|||||||
certs = {'foo': public_key}
|
certs = {'foo': public_key}
|
||||||
audience = ('https://www.googleapis.com/auth/id?client_id='
|
audience = ('https://www.googleapis.com/auth/id?client_id='
|
||||||
'external_public_key@testing.gserviceaccount.com')
|
'external_public_key@testing.gserviceaccount.com')
|
||||||
try:
|
|
||||||
|
with self.assertRaises(crypt.AppIdentityError) as exc_manager:
|
||||||
crypt.verify_signed_jwt_with_certs(jwt, certs, audience)
|
crypt.verify_signed_jwt_with_certs(jwt, certs, audience)
|
||||||
self.fail()
|
|
||||||
except crypt.AppIdentityError as e:
|
self.assertTrue(expected_error in str(exc_manager.exception))
|
||||||
self.assertTrue(expected_error in str(e))
|
|
||||||
|
|
||||||
def _create_signed_jwt(self):
|
def _create_signed_jwt(self):
|
||||||
private_key = datafile('privatekey.%s' % self.format)
|
private_key = datafile('privatekey.%s' % self.format)
|
||||||
@@ -216,7 +211,7 @@ class PEMCryptTestsOpenSSL(CryptTests):
|
|||||||
self.verifier = crypt.OpenSSLVerifier
|
self.verifier = crypt.OpenSSLVerifier
|
||||||
|
|
||||||
|
|
||||||
class SignedJwtAssertionCredentialsTests(unittest.TestCase):
|
class SignedJwtAssertionCredentialsTests(unittest2.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.format = 'p12'
|
self.format = 'p12'
|
||||||
@@ -310,7 +305,7 @@ class PEMSignedJwtAssertionCredentialsPyCryptoTests(
|
|||||||
crypt.Signer = crypt.PyCryptoSigner
|
crypt.Signer = crypt.PyCryptoSigner
|
||||||
|
|
||||||
|
|
||||||
class PKCSSignedJwtAssertionCredentialsPyCryptoTests(unittest.TestCase):
|
class PKCSSignedJwtAssertionCredentialsPyCryptoTests(unittest2.TestCase):
|
||||||
|
|
||||||
def test_for_failure(self):
|
def test_for_failure(self):
|
||||||
crypt.Signer = crypt.PyCryptoSigner
|
crypt.Signer = crypt.PyCryptoSigner
|
||||||
@@ -320,14 +315,12 @@ class PKCSSignedJwtAssertionCredentialsPyCryptoTests(unittest.TestCase):
|
|||||||
private_key,
|
private_key,
|
||||||
scope='read+write',
|
scope='read+write',
|
||||||
sub='joe@example.org')
|
sub='joe@example.org')
|
||||||
try:
|
|
||||||
credentials._generate_assertion()
|
self.assertRaises(NotImplementedError,
|
||||||
self.fail()
|
credentials._generate_assertion)
|
||||||
except NotImplementedError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class TestHasOpenSSLFlag(unittest.TestCase):
|
class TestHasOpenSSLFlag(unittest2.TestCase):
|
||||||
|
|
||||||
def test_true(self):
|
def test_true(self):
|
||||||
self.assertEqual(True, HAS_OPENSSL)
|
self.assertEqual(True, HAS_OPENSSL)
|
||||||
@@ -335,4 +328,4 @@ class TestHasOpenSSLFlag(unittest.TestCase):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__': # pragma: NO COVER
|
if __name__ == '__main__': # pragma: NO COVER
|
||||||
unittest.main()
|
unittest2.main()
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
#!/usr/bin/python2.4
|
|
||||||
#
|
|
||||||
# Copyright 2014 Google Inc. All rights reserved.
|
# Copyright 2014 Google Inc. All rights reserved.
|
||||||
#
|
#
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
@@ -61,17 +59,11 @@ class ServiceAccountCredentialsTests(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertTrue(rsa.pkcs1.verify(b'Google', signature, pub_key))
|
self.assertTrue(rsa.pkcs1.verify(b'Google', signature, pub_key))
|
||||||
|
|
||||||
try:
|
self.assertRaises(rsa.pkcs1.VerificationError,
|
||||||
rsa.pkcs1.verify(b'Orest', signature, pub_key)
|
rsa.pkcs1.verify, b'Orest', signature, pub_key)
|
||||||
self.fail('Verification should have failed!')
|
self.assertRaises(rsa.pkcs1.VerificationError,
|
||||||
except rsa.pkcs1.VerificationError:
|
rsa.pkcs1.verify,
|
||||||
pass # Expected
|
b'Google', b'bad signature', pub_key)
|
||||||
|
|
||||||
try:
|
|
||||||
rsa.pkcs1.verify(b'Google', b'bad signature', pub_key)
|
|
||||||
self.fail('Verification should have failed!')
|
|
||||||
except rsa.pkcs1.VerificationError:
|
|
||||||
pass # Expected
|
|
||||||
|
|
||||||
def test_service_account_email(self):
|
def test_service_account_email(self):
|
||||||
self.assertEqual(self.service_account_email,
|
self.assertEqual(self.service_account_email,
|
||||||
|
|||||||
Reference in New Issue
Block a user