Skip to content

Commit e175b61

Browse files
committed
refactored the tests into a test project, made them python3 compatible, and added classifiers to setup.py for tested versions of python
1 parent daadc79 commit e175b61

File tree

21 files changed

+123
-128
lines changed

21 files changed

+123
-128
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
*.pyc
1+
*.pyc
2+
.idea/

.travis.yml

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,29 @@ language: python
33
python:
44
- "2.6"
55
- "2.7"
6+
- "3.3"
7+
- "3.4"
8+
- "pypy"
69

710
env:
8-
- DJANGO=django==1.5.1 --use-mirrors
9-
- DJANGO=django==1.4.5 --use-mirrors
11+
- DJANGO="django>=1.4,<1.5"
12+
- DJANGO="django>=1.5,<1.6"
13+
- DJANGO="django>=1.6,<1.7"
14+
- DJANGO="django>=1.7,<1.8"
1015

1116
install:
1217
- pip install $DJANGO
18+
- python setup.py install
1319

1420
script:
15-
- "python tests/runtests.py"
16-
- "python tests/runtests_custom_user.py"
21+
- "DJANGO_SETTINGS_MODULE=test_project.settings python test_project/manage.py test test_app"
22+
- "if [ $DJANGO != 'django>=1.4,<1.5' ]; then DJANGO_SETTINGS_MODULE=test_project.custom_user_settings python test_project/manage.py test test_app_custom_user; fi"
23+
24+
matrix:
25+
exclude:
26+
- python: "2.6"
27+
env: DJANGO="django>=1.7,<1.8"
28+
- python: "3.3"
29+
env: DJANGO="django>=1.4,<1.5"
30+
- python: "3.4"
31+
env: DJANGO="django>=1.4,<1.5"

cached_auth/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
try:
1313
from django.contrib.auth import get_user_model
14-
User = get_user_model()
1514
except ImportError:
1615
from django.contrib.auth.models import User
16+
get_user_model = lambda: User
1717

1818
CACHE_KEY = 'cached_auth_middleware:%s'
1919

@@ -50,7 +50,7 @@ def profile_preprocessor(user, request):
5050

5151

5252
def invalidate_cache(sender, instance, **kwargs):
53-
if isinstance(instance, User):
53+
if isinstance(instance, get_user_model()):
5454
key = CACHE_KEY % instance.id
5555
else:
5656
key = CACHE_KEY % instance.user_id
@@ -76,8 +76,8 @@ def get_cached_user(request):
7676
class Middleware(object):
7777

7878
def __init__(self):
79-
post_save.connect(invalidate_cache, sender=User)
80-
post_delete.connect(invalidate_cache, sender=User)
79+
post_save.connect(invalidate_cache, sender=get_user_model())
80+
post_delete.connect(invalidate_cache, sender=get_user_model())
8181
if profile_model:
8282
post_save.connect(invalidate_cache, sender=profile_model)
8383
post_delete.connect(invalidate_cache, sender=profile_model)

setup.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22
from setuptools import setup
33

4+
45
setup(
56
name='django-cached_authentication_middleware',
67
version='0.2.0',
@@ -13,7 +14,7 @@
1314
long_description=open('README.rst').read(),
1415
zip_safe=False,
1516
include_package_data=True,
16-
package_data = { '': ['README.rst'] },
17+
package_data={'': ['README.rst']},
1718
install_requires=['django'],
1819
classifiers=[
1920
'Development Status :: 4 - Beta',
@@ -23,6 +24,14 @@
2324
'License :: OSI Approved :: MIT License',
2425
'Operating System :: OS Independent',
2526
'Programming Language :: Python',
27+
'Programming Language :: Python :: 2',
28+
'Programming Language :: Python :: 2.6',
29+
'Programming Language :: Python :: 2.7',
30+
'Programming Language :: Python :: 3',
31+
'Programming Language :: Python :: 3.3',
32+
'Programming Language :: Python :: 3.4',
33+
'Programming Language :: Python :: Implementation :: CPython',
34+
'Programming Language :: Python :: Implementation :: PyPy',
2635
'Topic :: Internet :: WWW/HTTP',
2736
'Topic :: Software Development :: Libraries :: Python Modules',
2837
]
File renamed without changes.

test_project/manage.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
from __future__ import unicode_literals
4+
5+
import sys
6+
7+
8+
if __name__ == '__main__':
9+
10+
from django.core.management import execute_from_command_line
11+
12+
execute_from_command_line(sys.argv)

test_project/test_app/__init__.py

Whitespace-only changes.

test_project/test_app/models.py

Whitespace-only changes.

cached_auth/tests.py renamed to test_project/test_app/tests.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
14
from django.core.cache import cache
25
from django.core.urlresolvers import reverse
36
from django.test import TestCase
47
from django.test.client import Client
58
from django.test.utils import override_settings
6-
from cached_auth import CACHE_KEY
7-
from django.conf import settings
89

910
import cached_auth
1011

1112
try:
1213
from django.contrib.auth import get_user_model
13-
User = get_user_model()
1414
except ImportError:
1515
from django.contrib.auth.models import User
16+
get_user_model = lambda: User
1617

17-
18-
def auth_preprocessor(user, request):
19-
user.username = 'test_auth'
20-
return user
18+
try:
19+
# Python 3.4+ includes reload in importlib
20+
from importlib import reload
21+
except ImportError:
22+
try:
23+
# Python 3.3 includes reload in imp
24+
from imp import reload
25+
except ImportError:
26+
# Python 2 includes reload as a builtin
27+
pass
2128

2229

2330
class MiddlewareTest(TestCase):
2431

2532
def setUp(self):
26-
self.user = User.objects.create_user(username='test', password='a')
33+
user_model = get_user_model()
34+
self.user = user_model.objects.create_user(username='test', password='a')
2735
self.user.is_superuser = True
2836
self.user.is_staff = True
2937
self.user.save()
@@ -32,13 +40,13 @@ def setUp(self):
3240
def test_anonymous(self):
3341
# Anonymous user doesn't cause cache to be set
3442
client = Client()
35-
key = CACHE_KEY % self.user.id
43+
key = cached_auth.CACHE_KEY % self.user.id
3644
client.get(reverse('admin:index'))
3745
self.assertEqual(cache.get(key), None)
3846

3947
def test_cached_middleware(self):
4048
client = Client()
41-
key = CACHE_KEY % self.user.id
49+
key = cached_auth.CACHE_KEY % self.user.id
4250
self.assertEqual(cache.get(key), None)
4351

4452
# Visiting admin causes the cache to be populated
@@ -56,11 +64,11 @@ def test_cached_middleware(self):
5664
self.user.delete()
5765
self.assertEqual(cache.get(key), None)
5866

59-
@override_settings(CACHED_AUTH_PREPROCESSOR='cached_auth.tests.auth_preprocessor')
67+
@override_settings(CACHED_AUTH_PREPROCESSOR='test_project.utils.auth_preprocessor')
6068
def test_cached_auth_preprocessor_function(self):
6169
reload(cached_auth)
6270
client = Client()
63-
key = CACHE_KEY % self.user.id
71+
key = cached_auth.CACHE_KEY % self.user.id
6472
self.assertEqual(cache.get(key), None)
6573

6674
client.login(username='test', password='a')

test_project/test_app/views.py

Whitespace-only changes.

0 commit comments

Comments
 (0)