Skip to content

Commit c6b30bf

Browse files
pferateJon Wayne Parrott
authored and
Jon Wayne Parrott
committed
Clean up imports (googleapis#625)
* Treat `tests` as part of the local package * Import modules instead of objects Some imports that slipped by last time this was cleaned up.
1 parent e4ad1be commit c6b30bf

15 files changed

+66
-68
lines changed

tests/contrib/appengine/test_appengine.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from oauth2client import client
3939
from oauth2client import clientsecrets
4040
from oauth2client.contrib import appengine
41-
from ... import http_mock
41+
from tests import http_mock
4242

4343
__author__ = '[email protected] (Joe Gregorio)'
4444

tests/contrib/django_util/test_decorators.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@
1818

1919
from django import http
2020
import django.conf
21-
from django.contrib.auth.models import AnonymousUser, User
21+
from django.contrib.auth import models as django_models
2222
import mock
2323
from six.moves import http_client
2424
from six.moves import reload_module
2525
from six.moves.urllib import parse
26-
from tests.contrib.django_util import TestWithDjangoEnvironment
2726

2827
import oauth2client.contrib.django_util
2928
from oauth2client.contrib.django_util import decorators
29+
from tests.contrib import django_util as tests_django_util
3030

3131

32-
class OAuth2EnabledDecoratorTest(TestWithDjangoEnvironment):
32+
class OAuth2EnabledDecoratorTest(tests_django_util.TestWithDjangoEnvironment):
3333

3434
def setUp(self):
3535
super(OAuth2EnabledDecoratorTest, self).setUp()
@@ -39,7 +39,7 @@ def setUp(self):
3939
# at import time, so in order for us to reload the settings
4040
# we need to reload the module
4141
reload_module(oauth2client.contrib.django_util)
42-
self.user = User.objects.create_user(
42+
self.user = django_models.User.objects.create_user(
4343
username='bill', email='[email protected]', password='hunter2')
4444

4545
def tearDown(self):
@@ -106,14 +106,14 @@ def test_view(request):
106106
self.assertFalse(request.oauth.has_credentials())
107107

108108

109-
class OAuth2RequiredDecoratorTest(TestWithDjangoEnvironment):
109+
class OAuth2RequiredDecoratorTest(tests_django_util.TestWithDjangoEnvironment):
110110

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

115115
reload_module(oauth2client.contrib.django_util)
116-
self.user = User.objects.create_user(
116+
self.user = django_models.User.objects.create_user(
117117
username='bill', email='[email protected]', password='hunter2')
118118

119119
def tearDown(self):
@@ -195,7 +195,8 @@ def test_view(request):
195195
response.status_code, django.http.HttpResponseRedirect.status_code)
196196

197197

198-
class OAuth2RequiredDecoratorStorageModelTest(TestWithDjangoEnvironment):
198+
class OAuth2RequiredDecoratorStorageModelTest(
199+
tests_django_util.TestWithDjangoEnvironment):
199200

200201
def setUp(self):
201202
super(OAuth2RequiredDecoratorStorageModelTest, self).setUp()
@@ -209,7 +210,7 @@ def setUp(self):
209210
django.conf.settings.GOOGLE_OAUTH2_STORAGE_MODEL = STORAGE_MODEL
210211

211212
reload_module(oauth2client.contrib.django_util)
212-
self.user = User.objects.create_user(
213+
self.user = django_models.User.objects.create_user(
213214
username='bill', email='[email protected]', password='hunter2')
214215

215216
def tearDown(self):
@@ -219,7 +220,7 @@ def tearDown(self):
219220
def test_redirects_anonymous_to_login(self):
220221
request = self.factory.get('/test')
221222
request.session = self.session
222-
request.user = AnonymousUser()
223+
request.user = django_models.AnonymousUser()
223224

224225
@decorators.oauth_required
225226
def test_view(request):
@@ -233,7 +234,7 @@ def test_view(request):
233234
def test_redirects_user_to_oauth_authorize(self):
234235
request = self.factory.get('/test')
235236
request.session = self.session
236-
request.user = User.objects.create_user(
237+
request.user = django_models.User.objects.create_user(
237238
username='bill3', email='[email protected]', password='hunter2')
238239

239240
@decorators.oauth_required

tests/contrib/django_util/test_django_models.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,19 @@
2121
import pickle
2222
import unittest
2323

24-
from tests.contrib.django_util.models import CredentialsModel
25-
2624
from oauth2client import _helpers
27-
from oauth2client.client import Credentials
28-
from oauth2client.contrib.django_util.models import CredentialsField
25+
from oauth2client import client
26+
from oauth2client.contrib.django_util import models
27+
from tests.contrib.django_util import models as tests_models
2928

3029

3130
class TestCredentialsField(unittest.TestCase):
3231

3332
def setUp(self):
34-
self.fake_model = CredentialsModel()
33+
self.fake_model = tests_models.CredentialsModel()
3534
self.fake_model_field = self.fake_model._meta.get_field('credentials')
36-
self.field = CredentialsField(null=True)
37-
self.credentials = Credentials()
35+
self.field = models.CredentialsField(null=True)
36+
self.credentials = client.Credentials()
3837
self.pickle_str = _helpers._from_bytes(
3938
base64.b64encode(pickle.dumps(self.credentials)))
4039

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

4443
def test_field_unpickled(self):
4544
self.assertIsInstance(
46-
self.field.to_python(self.pickle_str), Credentials)
45+
self.field.to_python(self.pickle_str), client.Credentials)
4746

4847
def test_field_already_unpickled(self):
4948
self.assertIsInstance(
50-
self.field.to_python(self.credentials), Credentials)
49+
self.field.to_python(self.credentials), client.Credentials)
5150

5251
def test_none_field_unpickled(self):
5352
self.assertIsNone(self.field.to_python(None))
5453

5554
def test_from_db_value(self):
5655
value = self.field.from_db_value(
5756
self.pickle_str, None, None, None)
58-
self.assertIsInstance(value, Credentials)
57+
self.assertIsInstance(value, client.Credentials)
5958

6059
def test_field_unpickled_none(self):
6160
self.assertEqual(self.field.to_python(None), None)
@@ -76,11 +75,11 @@ def test_field_value_to_string_none(self):
7675
self.assertIsNone(value_str)
7776

7877
def test_credentials_without_null(self):
79-
credentials = CredentialsField()
78+
credentials = models.CredentialsField()
8079
self.assertTrue(credentials.null)
8180

8281

83-
class CredentialWithSetStore(CredentialsField):
82+
class CredentialWithSetStore(models.CredentialsField):
8483
def __init__(self):
8584
self.model = CredentialWithSetStore
8685

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

9695
class FakeCredentialsModelMockNoSet(object):
9796

98-
credentials = CredentialsField()
97+
credentials = models.CredentialsField()

tests/contrib/django_util/test_django_util.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@
1919

2020
import django.conf
2121
from django.conf.urls import include, url
22-
from django.contrib.auth.models import AnonymousUser
22+
from django.contrib.auth import models as django_models
2323
from django.core import exceptions
2424
import mock
2525
from six.moves import reload_module
26-
from tests.contrib.django_util import TestWithDjangoEnvironment
2726

2827
from oauth2client.contrib import django_util
2928
import oauth2client.contrib.django_util
30-
from oauth2client.contrib.django_util import (
31-
_CREDENTIALS_KEY, get_storage, site, UserOAuth2)
29+
from oauth2client.contrib.django_util import site
30+
from tests.contrib import django_util as tests_django_util
3231

3332

3433
urlpatterns = [
@@ -135,7 +134,7 @@ def __init__(self, session):
135134
self.session = session
136135

137136

138-
class SessionStorageTest(TestWithDjangoEnvironment):
137+
class SessionStorageTest(tests_django_util.TestWithDjangoEnvironment):
139138

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

149148
def test_session_delete(self):
150-
self.session[_CREDENTIALS_KEY] = "test_val"
149+
self.session[django_util._CREDENTIALS_KEY] = "test_val"
151150
request = MockObjectWithSession(self.session)
152-
django_storage = get_storage(request)
151+
django_storage = django_util.get_storage(request)
153152
django_storage.delete()
154-
self.assertIsNone(self.session.get(_CREDENTIALS_KEY))
153+
self.assertIsNone(self.session.get(django_util._CREDENTIALS_KEY))
155154

156155
def test_session_delete_nothing(self):
157156
request = MockObjectWithSession(self.session)
158-
django_storage = get_storage(request)
157+
django_storage = django_util.get_storage(request)
159158
django_storage.delete()
160159

161160

162-
class TestUserOAuth2Object(TestWithDjangoEnvironment):
161+
class TestUserOAuth2Object(tests_django_util.TestWithDjangoEnvironment):
163162

164163
def setUp(self):
165164
super(TestUserOAuth2Object, self).setUp()
@@ -181,6 +180,6 @@ def test_get_credentials_anon_user(self):
181180
request = self.factory.get('oauth2/oauth2authorize',
182181
data={'return_url': '/return_endpoint'})
183182
request.session = self.session
184-
request.user = AnonymousUser()
185-
oauth2 = UserOAuth2(request)
183+
request.user = django_models.AnonymousUser()
184+
oauth2 = django_util.UserOAuth2(request)
186185
self.assertIsNone(oauth2.credentials)

tests/contrib/django_util/test_views.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,26 @@
2020
import django
2121
from django import http
2222
import django.conf
23-
from django.contrib.auth.models import AnonymousUser, User
23+
from django.contrib.auth import models as django_models
2424
import mock
2525
from six.moves import reload_module
2626

27-
from tests.contrib.django_util import TestWithDjangoEnvironment
28-
from tests.contrib.django_util.models import CredentialsModel
29-
30-
from oauth2client.client import FlowExchangeError, OAuth2WebServerFlow
27+
from oauth2client import client
3128
import oauth2client.contrib.django_util
3229
from oauth2client.contrib.django_util import views
33-
from oauth2client.contrib.django_util.models import CredentialsField
30+
from oauth2client.contrib.django_util import models
31+
from tests.contrib import django_util as tests_django_util
32+
from tests.contrib.django_util import models as tests_models
3433

3534

36-
class OAuth2AuthorizeTest(TestWithDjangoEnvironment):
35+
class OAuth2AuthorizeTest(tests_django_util.TestWithDjangoEnvironment):
3736

3837
def setUp(self):
3938
super(OAuth2AuthorizeTest, self).setUp()
4039
self.save_settings = copy.deepcopy(django.conf.settings)
4140
reload_module(oauth2client.contrib.django_util)
42-
self.user = User.objects.create_user(
43-
username='bill', email='[email protected]', password='hunter2')
41+
self.user = django_models.User.objects.create_user(
42+
username='bill', email='[email protected]', password='hunter2')
4443

4544
def tearDown(self):
4645
django.conf.settings = copy.deepcopy(self.save_settings)
@@ -55,7 +54,7 @@ def test_authorize_works(self):
5554
def test_authorize_anonymous_user(self):
5655
request = self.factory.get('oauth2/oauth2authorize')
5756
request.session = self.session
58-
request.user = AnonymousUser()
57+
request.user = django_models.AnonymousUser()
5958
response = views.oauth2_authorize(request)
6059
self.assertIsInstance(response, http.HttpResponseRedirect)
6160

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

7069

71-
class Oauth2AuthorizeStorageModelTest(TestWithDjangoEnvironment):
70+
class Oauth2AuthorizeStorageModelTest(
71+
tests_django_util.TestWithDjangoEnvironment):
7272

7373
def setUp(self):
7474
super(Oauth2AuthorizeStorageModelTest, self).setUp()
@@ -85,7 +85,7 @@ def setUp(self):
8585
# at import time, so in order for us to reload the settings
8686
# we need to reload the module
8787
reload_module(oauth2client.contrib.django_util)
88-
self.user = User.objects.create_user(
88+
self.user = django_models.User.objects.create_user(
8989
username='bill', email='[email protected]', password='hunter2')
9090

9191
def tearDown(self):
@@ -103,7 +103,7 @@ def test_authorize_works(self):
103103
def test_authorize_anonymous_user_redirects_login(self):
104104
request = self.factory.get('oauth2/oauth2authorize')
105105
request.session = self.session
106-
request.user = AnonymousUser()
106+
request.user = django_models.AnonymousUser()
107107
response = views.oauth2_authorize(request)
108108
self.assertIsInstance(response, http.HttpResponseRedirect)
109109
# redirects to Django login
@@ -122,11 +122,11 @@ def test_authorized_user_not_logged_in_redirects(self):
122122
data={'return_url': '/return_endpoint'})
123123
request.session = self.session
124124

125-
authorized_user = User.objects.create_user(
125+
authorized_user = django_models.User.objects.create_user(
126126
username='bill2', email='[email protected]', password='hunter2')
127-
credentials = CredentialsField()
127+
credentials = models.CredentialsField()
128128

129-
CredentialsModel.objects.create(
129+
tests_models.CredentialsModel.objects.create(
130130
user_id=authorized_user,
131131
credentials=credentials)
132132

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

137137

138-
class Oauth2CallbackTest(TestWithDjangoEnvironment):
138+
class Oauth2CallbackTest(tests_django_util.TestWithDjangoEnvironment):
139139

140140
def setUp(self):
141141
super(Oauth2CallbackTest, self).setUp()
@@ -149,7 +149,7 @@ def setUp(self):
149149
'return_url': self.RETURN_URL,
150150
'scopes': django.conf.settings.GOOGLE_OAUTH2_SCOPES
151151
}
152-
self.user = User.objects.create_user(
152+
self.user = django_models.User.objects.create_user(
153153
username='bill', email='[email protected]', password='hunter2')
154154

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

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

164-
flow = OAuth2WebServerFlow(
164+
flow = client.OAuth2WebServerFlow(
165165
client_id='clientid',
166166
client_secret='clientsecret',
167167
scope=['email'],
@@ -190,7 +190,7 @@ def test_callback_handles_bad_flow_exchange(self, pickle):
190190

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

193-
flow = OAuth2WebServerFlow(
193+
flow = client.OAuth2WebServerFlow(
194194
client_id='clientid',
195195
client_secret='clientsecret',
196196
scope=['email'],
@@ -201,7 +201,7 @@ def test_callback_handles_bad_flow_exchange(self, pickle):
201201
self.session[session_key] = pickle.dumps(flow)
202202

203203
def local_throws(code):
204-
raise FlowExchangeError('test')
204+
raise client.FlowExchangeError('test')
205205

206206
flow.step2_exchange = local_throws
207207
pickle.loads.return_value = flow

tests/contrib/test_flask_util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from oauth2client import client
2929
from oauth2client import clientsecrets
3030
from oauth2client.contrib import flask_util
31-
from .. import http_mock
31+
from tests import http_mock
3232

3333

3434
__author__ = '[email protected] (Jon Wayne Parrott)'

tests/contrib/test_gce.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from oauth2client import client
2525
from oauth2client.contrib import _metadata
2626
from oauth2client.contrib import gce
27-
from .. import http_mock
27+
from tests import http_mock
2828

2929

3030
SERVICE_ACCOUNT_INFO = {

tests/contrib/test_metadata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from six.moves import http_client
2121

2222
from oauth2client.contrib import _metadata
23-
from .. import http_mock
23+
from tests import http_mock
2424

2525

2626
PATH = 'instance/service-accounts/default'

tests/contrib/test_multiprocess_file_storage.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929

3030
from oauth2client import client
3131
from oauth2client.contrib import multiprocess_file_storage
32-
33-
from .. import http_mock
32+
from tests import http_mock
3433

3534

3635
@contextlib.contextmanager

0 commit comments

Comments
 (0)