Merge pull request #350 from waprin/fix-348

More informative error when failing to open secrets file.
This commit is contained in:
Nathaniel Manista
2015-12-03 16:35:35 -08:00
6 changed files with 94 additions and 19 deletions

View File

@@ -125,6 +125,10 @@ Running Tests
least version 2.6 of `pypy` installed. See the [docs][13] for least version 2.6 of `pypy` installed. See the [docs][13] for
more information. more information.
- **Note** that `django` related tests are turned off for Python 2.6
and 3.3. This is because `django` dropped support for
[2.6 in `django==1.7`][14] and for [3.3 in `django==1.9`][15].
Running System Tests Running System Tests
-------------------- --------------------
@@ -196,3 +200,5 @@ we'll be able to accept your pull requests.
[11]: #include-tests [11]: #include-tests
[12]: #make-the-pull-request [12]: #make-the-pull-request
[13]: http://oauth2client.readthedocs.org/en/latest/#using-pypy [13]: http://oauth2client.readthedocs.org/en/latest/#using-pypy
[14]: https://docs.djangoproject.com/en/1.7/faq/install/#what-python-version-can-i-use-with-django
[15]: https://docs.djangoproject.com/en/1.9/faq/install/#what-python-version-can-i-use-with-django

View File

@@ -121,8 +121,9 @@ def _loadfile(filename):
try: try:
with open(filename, 'r') as fp: with open(filename, 'r') as fp:
obj = json.load(fp) obj = json.load(fp)
except IOError: except IOError as exc:
raise InvalidClientSecretsError('File not found: "%s"' % filename) raise InvalidClientSecretsError('Error opening file', exc.filename,
exc.strerror, exc.errno)
return _validate_clientsecrets(obj) return _validate_clientsecrets(obj)

View File

@@ -14,6 +14,7 @@
"""Unit tests for oauth2client.clientsecrets.""" """Unit tests for oauth2client.clientsecrets."""
import errno
from io import StringIO from io import StringIO
import os import os
import tempfile import tempfile
@@ -32,7 +33,8 @@ __author__ = 'jcgregorio@google.com (Joe Gregorio)'
DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
VALID_FILE = os.path.join(DATA_DIR, 'client_secrets.json') VALID_FILE = os.path.join(DATA_DIR, 'client_secrets.json')
INVALID_FILE = os.path.join(DATA_DIR, 'unfilled_client_secrets.json') INVALID_FILE = os.path.join(DATA_DIR, 'unfilled_client_secrets.json')
NONEXISTENT_FILE = os.path.join(__file__, '..', 'afilethatisntthere.json') NONEXISTENT_FILE = os.path.join(
os.path.dirname(__file__), 'afilethatisntthere.json')
class Test__validate_clientsecrets(unittest.TestCase): class Test__validate_clientsecrets(unittest.TestCase):
@@ -222,12 +224,15 @@ class OAuth2CredentialsTests(unittest.TestCase):
except clientsecrets.InvalidClientSecretsError as e: except clientsecrets.InvalidClientSecretsError as e:
self.assertTrue(str(e).startswith(match)) self.assertTrue(str(e).startswith(match))
def test_load_by_filename(self): def test_load_by_filename_missing_file(self):
caught_exception = None
try: try:
clientsecrets._loadfile(NONEXISTENT_FILE) clientsecrets._loadfile(NONEXISTENT_FILE)
self.fail('should fail to load a missing client_secrets file.') except clientsecrets.InvalidClientSecretsError as exc:
except clientsecrets.InvalidClientSecretsError as e: caught_exception = exc
self.assertTrue(str(e).startswith('File'))
self.assertEquals(caught_exception.args[1], NONEXISTENT_FILE)
self.assertEquals(caught_exception.args[3], errno.ENOENT)
class CachedClientsecretsTests(unittest.TestCase): class CachedClientsecretsTests(unittest.TestCase):

View File

@@ -26,16 +26,28 @@ import os
import pickle import pickle
import sys import sys
import unittest import unittest
# Mock a Django environment # Mock a Django environment
from django.conf import global_settings from django.conf import global_settings
global_settings.SECRET_KEY = 'NotASecret'
os.environ['DJANGO_SETTINGS_MODULE'] = 'django_settings'
sys.modules['django_settings'] = django_settings = imp.new_module(
'django_settings')
django_settings.SECRET_KEY = 'xyzzy'
from django.db import models
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.test_django_settings'
from django.conf import settings
settings.SECRET_KEY = 'this string is not a real Django SECRET_KEY'
settings.INSTALLED_APPS = ['tests.test_django_orm']
import django
django.setup()
from django.apps import AppConfig
class DjangoOrmTestApp(AppConfig):
"""App Config for Django Helper."""
name = 'oauth2client.tests.test_django_orm'
verbose_name = "Django Test App"
from django.db import models
from oauth2client._helpers import _from_bytes from oauth2client._helpers import _from_bytes
from oauth2client.client import Credentials from oauth2client.client import Credentials
from oauth2client.client import Flow from oauth2client.client import Flow

View File

@@ -0,0 +1,34 @@
# 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.
import os
SECRET_KEY = 'this string is not a real django secret key'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join('.', 'db.sqlite3'),
}
}
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware'
)
ALLOWED_HOSTS = ['testserver']
GOOGLE_OAUTH2_CLIENT_ID = 'client_id2'
GOOGLE_OAUTH2_CLIENT_SECRET = 'hunter2'
GOOGLE_OAUTH2_SCOPES = ('https://www.googleapis.com/auth/cloud-platform',)
ROOT_URLCONF = 'tests.test_django_util'

27
tox.ini
View File

@@ -42,6 +42,28 @@ deps = {[testenv]deps}
coverage coverage
nosegae nosegae
[testenv:py26]
basepython =
python2.6
commands =
nosetests \
--ignore-files=test_appengine\.py \
--ignore-files=test_django_orm\.py \
--ignore-files=test_django_settings\.py \
{posargs}
deps = {[testenv]basedeps}
[testenv:py33]
basepython =
python3.3
commands =
nosetests \
--ignore-files=test_appengine\.py \
--ignore-files=test_django_orm\.py \
--ignore-files=test_django_settings\.py \
{posargs}
deps = {[testenv]basedeps}
[testenv:cover] [testenv:cover]
basepython = {[coverbase]basepython} basepython = {[coverbase]basepython}
commands = commands =
@@ -72,11 +94,6 @@ deps =
webapp2 webapp2
commands = {toxinidir}/scripts/build-docs commands = {toxinidir}/scripts/build-docs
[testenv:py26]
basepython = python2.6
deps = {[testenv]basedeps}
django>=1.5,<1.6
[testenv:gae] [testenv:gae]
basepython = python2.7 basepython = python2.7
deps = {[testenv]basedeps} deps = {[testenv]basedeps}