Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Commit

Permalink
Clean up imports (#625)
Browse files Browse the repository at this point in the history
* Treat `tests` as part of the local package
* Import modules instead of objects

Some imports that slipped by last time this was cleaned up.
  • Loading branch information
pferate authored and Jon Wayne Parrott committed Aug 11, 2016
1 parent e4ad1be commit c6b30bf
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 68 deletions.
2 changes: 1 addition & 1 deletion tests/contrib/appengine/test_appengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from oauth2client import client
from oauth2client import clientsecrets
from oauth2client.contrib import appengine
from ... import http_mock
from tests import http_mock

__author__ = 'jcgregorio@google.com (Joe Gregorio)'

Expand Down
21 changes: 11 additions & 10 deletions tests/contrib/django_util/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@

from django import http
import django.conf
from django.contrib.auth.models import AnonymousUser, User
from django.contrib.auth import models as django_models
import mock
from six.moves import http_client
from six.moves import reload_module
from six.moves.urllib import parse
from tests.contrib.django_util import TestWithDjangoEnvironment

import oauth2client.contrib.django_util
from oauth2client.contrib.django_util import decorators
from tests.contrib import django_util as tests_django_util


class OAuth2EnabledDecoratorTest(TestWithDjangoEnvironment):
class OAuth2EnabledDecoratorTest(tests_django_util.TestWithDjangoEnvironment):

def setUp(self):
super(OAuth2EnabledDecoratorTest, self).setUp()
Expand All @@ -39,7 +39,7 @@ def setUp(self):
# at import time, so in order for us to reload the settings
# we need to reload the module
reload_module(oauth2client.contrib.django_util)
self.user = User.objects.create_user(
self.user = django_models.User.objects.create_user(
username='bill', email='bill@example.com', password='hunter2')

def tearDown(self):
Expand Down Expand Up @@ -106,14 +106,14 @@ def test_view(request):
self.assertFalse(request.oauth.has_credentials())


class OAuth2RequiredDecoratorTest(TestWithDjangoEnvironment):
class OAuth2RequiredDecoratorTest(tests_django_util.TestWithDjangoEnvironment):

def setUp(self):
super(OAuth2RequiredDecoratorTest, self).setUp()
self.save_settings = copy.deepcopy(django.conf.settings)

reload_module(oauth2client.contrib.django_util)
self.user = User.objects.create_user(
self.user = django_models.User.objects.create_user(
username='bill', email='bill@example.com', password='hunter2')

def tearDown(self):
Expand Down Expand Up @@ -195,7 +195,8 @@ def test_view(request):
response.status_code, django.http.HttpResponseRedirect.status_code)


class OAuth2RequiredDecoratorStorageModelTest(TestWithDjangoEnvironment):
class OAuth2RequiredDecoratorStorageModelTest(
tests_django_util.TestWithDjangoEnvironment):

def setUp(self):
super(OAuth2RequiredDecoratorStorageModelTest, self).setUp()
Expand All @@ -209,7 +210,7 @@ def setUp(self):
django.conf.settings.GOOGLE_OAUTH2_STORAGE_MODEL = STORAGE_MODEL

reload_module(oauth2client.contrib.django_util)
self.user = User.objects.create_user(
self.user = django_models.User.objects.create_user(
username='bill', email='bill@example.com', password='hunter2')

def tearDown(self):
Expand All @@ -219,7 +220,7 @@ def tearDown(self):
def test_redirects_anonymous_to_login(self):
request = self.factory.get('/test')
request.session = self.session
request.user = AnonymousUser()
request.user = django_models.AnonymousUser()

@decorators.oauth_required
def test_view(request):
Expand All @@ -233,7 +234,7 @@ def test_view(request):
def test_redirects_user_to_oauth_authorize(self):
request = self.factory.get('/test')
request.session = self.session
request.user = User.objects.create_user(
request.user = django_models.User.objects.create_user(
username='bill3', email='bill@example.com', password='hunter2')

@decorators.oauth_required
Expand Down
25 changes: 12 additions & 13 deletions tests/contrib/django_util/test_django_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,19 @@
import pickle
import unittest

from tests.contrib.django_util.models import CredentialsModel

from oauth2client import _helpers
from oauth2client.client import Credentials
from oauth2client.contrib.django_util.models import CredentialsField
from oauth2client import client
from oauth2client.contrib.django_util import models
from tests.contrib.django_util import models as tests_models


class TestCredentialsField(unittest.TestCase):

def setUp(self):
self.fake_model = CredentialsModel()
self.fake_model = tests_models.CredentialsModel()
self.fake_model_field = self.fake_model._meta.get_field('credentials')
self.field = CredentialsField(null=True)
self.credentials = Credentials()
self.field = models.CredentialsField(null=True)
self.credentials = client.Credentials()
self.pickle_str = _helpers._from_bytes(
base64.b64encode(pickle.dumps(self.credentials)))

Expand All @@ -43,19 +42,19 @@ def test_field_is_text(self):

def test_field_unpickled(self):
self.assertIsInstance(
self.field.to_python(self.pickle_str), Credentials)
self.field.to_python(self.pickle_str), client.Credentials)

def test_field_already_unpickled(self):
self.assertIsInstance(
self.field.to_python(self.credentials), Credentials)
self.field.to_python(self.credentials), client.Credentials)

def test_none_field_unpickled(self):
self.assertIsNone(self.field.to_python(None))

def test_from_db_value(self):
value = self.field.from_db_value(
self.pickle_str, None, None, None)
self.assertIsInstance(value, Credentials)
self.assertIsInstance(value, client.Credentials)

def test_field_unpickled_none(self):
self.assertEqual(self.field.to_python(None), None)
Expand All @@ -76,11 +75,11 @@ def test_field_value_to_string_none(self):
self.assertIsNone(value_str)

def test_credentials_without_null(self):
credentials = CredentialsField()
credentials = models.CredentialsField()
self.assertTrue(credentials.null)


class CredentialWithSetStore(CredentialsField):
class CredentialWithSetStore(models.CredentialsField):
def __init__(self):
self.model = CredentialWithSetStore

Expand All @@ -95,4 +94,4 @@ class FakeCredentialsModelMock(object):

class FakeCredentialsModelMockNoSet(object):

credentials = CredentialsField()
credentials = models.CredentialsField()
23 changes: 11 additions & 12 deletions tests/contrib/django_util/test_django_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@

import django.conf
from django.conf.urls import include, url
from django.contrib.auth.models import AnonymousUser
from django.contrib.auth import models as django_models
from django.core import exceptions
import mock
from six.moves import reload_module
from tests.contrib.django_util import TestWithDjangoEnvironment

from oauth2client.contrib import django_util
import oauth2client.contrib.django_util
from oauth2client.contrib.django_util import (
_CREDENTIALS_KEY, get_storage, site, UserOAuth2)
from oauth2client.contrib.django_util import site
from tests.contrib import django_util as tests_django_util


urlpatterns = [
Expand Down Expand Up @@ -135,7 +134,7 @@ def __init__(self, session):
self.session = session


class SessionStorageTest(TestWithDjangoEnvironment):
class SessionStorageTest(tests_django_util.TestWithDjangoEnvironment):

def setUp(self):
super(SessionStorageTest, self).setUp()
Expand All @@ -147,19 +146,19 @@ def tearDown(self):
django.conf.settings = copy.deepcopy(self.save_settings)

def test_session_delete(self):
self.session[_CREDENTIALS_KEY] = "test_val"
self.session[django_util._CREDENTIALS_KEY] = "test_val"
request = MockObjectWithSession(self.session)
django_storage = get_storage(request)
django_storage = django_util.get_storage(request)
django_storage.delete()
self.assertIsNone(self.session.get(_CREDENTIALS_KEY))
self.assertIsNone(self.session.get(django_util._CREDENTIALS_KEY))

def test_session_delete_nothing(self):
request = MockObjectWithSession(self.session)
django_storage = get_storage(request)
django_storage = django_util.get_storage(request)
django_storage.delete()


class TestUserOAuth2Object(TestWithDjangoEnvironment):
class TestUserOAuth2Object(tests_django_util.TestWithDjangoEnvironment):

def setUp(self):
super(TestUserOAuth2Object, self).setUp()
Expand All @@ -181,6 +180,6 @@ def test_get_credentials_anon_user(self):
request = self.factory.get('oauth2/oauth2authorize',
data={'return_url': '/return_endpoint'})
request.session = self.session
request.user = AnonymousUser()
oauth2 = UserOAuth2(request)
request.user = django_models.AnonymousUser()
oauth2 = django_util.UserOAuth2(request)
self.assertIsNone(oauth2.credentials)
42 changes: 21 additions & 21 deletions tests/contrib/django_util/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,26 @@
import django
from django import http
import django.conf
from django.contrib.auth.models import AnonymousUser, User
from django.contrib.auth import models as django_models
import mock
from six.moves import reload_module

from tests.contrib.django_util import TestWithDjangoEnvironment
from tests.contrib.django_util.models import CredentialsModel

from oauth2client.client import FlowExchangeError, OAuth2WebServerFlow
from oauth2client import client
import oauth2client.contrib.django_util
from oauth2client.contrib.django_util import views
from oauth2client.contrib.django_util.models import CredentialsField
from oauth2client.contrib.django_util import models
from tests.contrib import django_util as tests_django_util
from tests.contrib.django_util import models as tests_models


class OAuth2AuthorizeTest(TestWithDjangoEnvironment):
class OAuth2AuthorizeTest(tests_django_util.TestWithDjangoEnvironment):

def setUp(self):
super(OAuth2AuthorizeTest, self).setUp()
self.save_settings = copy.deepcopy(django.conf.settings)
reload_module(oauth2client.contrib.django_util)
self.user = User.objects.create_user(
username='bill', email='bill@example.com', password='hunter2')
self.user = django_models.User.objects.create_user(
username='bill', email='bill@example.com', password='hunter2')

def tearDown(self):
django.conf.settings = copy.deepcopy(self.save_settings)
Expand All @@ -55,7 +54,7 @@ def test_authorize_works(self):
def test_authorize_anonymous_user(self):
request = self.factory.get('oauth2/oauth2authorize')
request.session = self.session
request.user = AnonymousUser()
request.user = django_models.AnonymousUser()
response = views.oauth2_authorize(request)
self.assertIsInstance(response, http.HttpResponseRedirect)

Expand All @@ -68,7 +67,8 @@ def test_authorize_works_explicit_return_url(self):
self.assertIsInstance(response, http.HttpResponseRedirect)


class Oauth2AuthorizeStorageModelTest(TestWithDjangoEnvironment):
class Oauth2AuthorizeStorageModelTest(
tests_django_util.TestWithDjangoEnvironment):

def setUp(self):
super(Oauth2AuthorizeStorageModelTest, self).setUp()
Expand All @@ -85,7 +85,7 @@ def setUp(self):
# at import time, so in order for us to reload the settings
# we need to reload the module
reload_module(oauth2client.contrib.django_util)
self.user = User.objects.create_user(
self.user = django_models.User.objects.create_user(
username='bill', email='bill@example.com', password='hunter2')

def tearDown(self):
Expand All @@ -103,7 +103,7 @@ def test_authorize_works(self):
def test_authorize_anonymous_user_redirects_login(self):
request = self.factory.get('oauth2/oauth2authorize')
request.session = self.session
request.user = AnonymousUser()
request.user = django_models.AnonymousUser()
response = views.oauth2_authorize(request)
self.assertIsInstance(response, http.HttpResponseRedirect)
# redirects to Django login
Expand All @@ -122,11 +122,11 @@ def test_authorized_user_not_logged_in_redirects(self):
data={'return_url': '/return_endpoint'})
request.session = self.session

authorized_user = User.objects.create_user(
authorized_user = django_models.User.objects.create_user(
username='bill2', email='bill@example.com', password='hunter2')
credentials = CredentialsField()
credentials = models.CredentialsField()

CredentialsModel.objects.create(
tests_models.CredentialsModel.objects.create(
user_id=authorized_user,
credentials=credentials)

Expand All @@ -135,7 +135,7 @@ def test_authorized_user_not_logged_in_redirects(self):
self.assertIsInstance(response, http.HttpResponseRedirect)


class Oauth2CallbackTest(TestWithDjangoEnvironment):
class Oauth2CallbackTest(tests_django_util.TestWithDjangoEnvironment):

def setUp(self):
super(Oauth2CallbackTest, self).setUp()
Expand All @@ -149,7 +149,7 @@ def setUp(self):
'return_url': self.RETURN_URL,
'scopes': django.conf.settings.GOOGLE_OAUTH2_SCOPES
}
self.user = User.objects.create_user(
self.user = django_models.User.objects.create_user(
username='bill', email='bill@example.com', password='hunter2')

@mock.patch('oauth2client.contrib.django_util.views.pickle')
Expand All @@ -161,7 +161,7 @@ def test_callback_works(self, pickle):

self.session['google_oauth2_csrf_token'] = self.CSRF_TOKEN

flow = OAuth2WebServerFlow(
flow = client.OAuth2WebServerFlow(
client_id='clientid',
client_secret='clientsecret',
scope=['email'],
Expand Down Expand Up @@ -190,7 +190,7 @@ def test_callback_handles_bad_flow_exchange(self, pickle):

self.session['google_oauth2_csrf_token'] = self.CSRF_TOKEN

flow = OAuth2WebServerFlow(
flow = client.OAuth2WebServerFlow(
client_id='clientid',
client_secret='clientsecret',
scope=['email'],
Expand All @@ -201,7 +201,7 @@ def test_callback_handles_bad_flow_exchange(self, pickle):
self.session[session_key] = pickle.dumps(flow)

def local_throws(code):
raise FlowExchangeError('test')
raise client.FlowExchangeError('test')

flow.step2_exchange = local_throws
pickle.loads.return_value = flow
Expand Down
2 changes: 1 addition & 1 deletion tests/contrib/test_flask_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from oauth2client import client
from oauth2client import clientsecrets
from oauth2client.contrib import flask_util
from .. import http_mock
from tests import http_mock


__author__ = 'jonwayne@google.com (Jon Wayne Parrott)'
Expand Down
2 changes: 1 addition & 1 deletion tests/contrib/test_gce.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from oauth2client import client
from oauth2client.contrib import _metadata
from oauth2client.contrib import gce
from .. import http_mock
from tests import http_mock


SERVICE_ACCOUNT_INFO = {
Expand Down
2 changes: 1 addition & 1 deletion tests/contrib/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from six.moves import http_client

from oauth2client.contrib import _metadata
from .. import http_mock
from tests import http_mock


PATH = 'instance/service-accounts/default'
Expand Down
3 changes: 1 addition & 2 deletions tests/contrib/test_multiprocess_file_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@

from oauth2client import client
from oauth2client.contrib import multiprocess_file_storage

from .. import http_mock
from tests import http_mock


@contextlib.contextmanager
Expand Down
Loading

0 comments on commit c6b30bf

Please sign in to comment.