Skip to content
This repository has been archived by the owner on Nov 5, 2019. It is now read-only.

Commit

Permalink
More informative error when failing to open secrets file.
Browse files Browse the repository at this point in the history
Fixes #348.

Fix Django ORM Tests for Django 1.9.

Disables Django tests for 2.6 and 3.3 due to lack of Django support.
  • Loading branch information
dhermes authored and Bill Prin committed Dec 3, 2015
1 parent 4cf98dc commit 25b299d
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 19 deletions.
6 changes: 6 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ Running Tests
least version 2.6 of `pypy` installed. See the [docs][13] for
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
--------------------
Expand Down Expand Up @@ -196,3 +200,5 @@ we'll be able to accept your pull requests.
[11]: #include-tests
[12]: #make-the-pull-request
[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
5 changes: 3 additions & 2 deletions oauth2client/clientsecrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ def _loadfile(filename):
try:
with open(filename, 'r') as fp:
obj = json.load(fp)
except IOError:
raise InvalidClientSecretsError('File not found: "%s"' % filename)
except IOError as exc:
raise InvalidClientSecretsError('Error opening file', exc.filename,
exc.strerror, exc.errno)
return _validate_clientsecrets(obj)


Expand Down
18 changes: 13 additions & 5 deletions tests/test_clientsecrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

"""Unit tests for oauth2client.clientsecrets."""

import errno
from io import StringIO
import os
import tempfile
Expand All @@ -32,7 +33,8 @@
DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
VALID_FILE = os.path.join(DATA_DIR, '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):
Expand Down Expand Up @@ -222,12 +224,18 @@ def test_validate_error(self):
except clientsecrets.InvalidClientSecretsError as e:
self.assertTrue(str(e).startswith(match))

def test_load_by_filename(self):
def test_load_by_filename_missing_file(self):
exc_caught = None
try:
clientsecrets._loadfile(NONEXISTENT_FILE)
self.fail('should fail to load a missing client_secrets file.')
except clientsecrets.InvalidClientSecretsError as e:
self.assertTrue(str(e).startswith('File'))
except clientsecrets.InvalidClientSecretsError as exc:
exc_caught = exc

expected_args = ('Error opening file',
NONEXISTENT_FILE,
'No such file or directory',
errno.ENOENT)
self.assertEqual(exc_caught.args, expected_args)


class CachedClientsecretsTests(unittest.TestCase):
Expand Down
26 changes: 19 additions & 7 deletions tests/test_django_orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,28 @@
import pickle
import sys
import unittest

# Mock a Django environment
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.client import Credentials
from oauth2client.client import Flow
Expand Down
34 changes: 34 additions & 0 deletions tests/test_django_settings.py
Original file line number Diff line number Diff line change
@@ -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 changes: 22 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,28 @@ deps = {[testenv]deps}
coverage
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]
basepython = {[coverbase]basepython}
commands =
Expand Down Expand Up @@ -72,11 +94,6 @@ deps =
webapp2
commands = {toxinidir}/scripts/build-docs

[testenv:py26]
basepython = python2.6
deps = {[testenv]basedeps}
django>=1.5,<1.6

[testenv:gae]
basepython = python2.7
deps = {[testenv]basedeps}
Expand Down

0 comments on commit 25b299d

Please sign in to comment.