Skip to content

Commit

Permalink
refactored the tests into a test project, made them python3 compatibl…
Browse files Browse the repository at this point in the history
…e, and added classifiers to setup.py for tested versions of python
  • Loading branch information
hwkns committed Oct 13, 2014
1 parent daadc79 commit e175b61
Show file tree
Hide file tree
Showing 21 changed files with 123 additions and 128 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.pyc
*.pyc
.idea/
23 changes: 19 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,29 @@ language: python
python:
- "2.6"
- "2.7"
- "3.3"
- "3.4"
- "pypy"

env:
- DJANGO=django==1.5.1 --use-mirrors
- DJANGO=django==1.4.5 --use-mirrors
- DJANGO="django>=1.4,<1.5"
- DJANGO="django>=1.5,<1.6"
- DJANGO="django>=1.6,<1.7"
- DJANGO="django>=1.7,<1.8"

install:
- pip install $DJANGO
- python setup.py install

script:
- "python tests/runtests.py"
- "python tests/runtests_custom_user.py"
- "DJANGO_SETTINGS_MODULE=test_project.settings python test_project/manage.py test test_app"
- "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"

matrix:
exclude:
- python: "2.6"
env: DJANGO="django>=1.7,<1.8"
- python: "3.3"
env: DJANGO="django>=1.4,<1.5"
- python: "3.4"
env: DJANGO="django>=1.4,<1.5"
8 changes: 4 additions & 4 deletions cached_auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

try:
from django.contrib.auth import get_user_model
User = get_user_model()
except ImportError:
from django.contrib.auth.models import User
get_user_model = lambda: User

CACHE_KEY = 'cached_auth_middleware:%s'

Expand Down Expand Up @@ -50,7 +50,7 @@ def profile_preprocessor(user, request):


def invalidate_cache(sender, instance, **kwargs):
if isinstance(instance, User):
if isinstance(instance, get_user_model()):
key = CACHE_KEY % instance.id
else:
key = CACHE_KEY % instance.user_id
Expand All @@ -76,8 +76,8 @@ def get_cached_user(request):
class Middleware(object):

def __init__(self):
post_save.connect(invalidate_cache, sender=User)
post_delete.connect(invalidate_cache, sender=User)
post_save.connect(invalidate_cache, sender=get_user_model())
post_delete.connect(invalidate_cache, sender=get_user_model())
if profile_model:
post_save.connect(invalidate_cache, sender=profile_model)
post_delete.connect(invalidate_cache, sender=profile_model)
Expand Down
11 changes: 10 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from setuptools import setup


setup(
name='django-cached_authentication_middleware',
version='0.2.0',
Expand All @@ -13,7 +14,7 @@
long_description=open('README.rst').read(),
zip_safe=False,
include_package_data=True,
package_data = { '': ['README.rst'] },
package_data={'': ['README.rst']},
install_requires=['django'],
classifiers=[
'Development Status :: 4 - Beta',
Expand All @@ -23,6 +24,14 @@
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: Implementation :: CPython',
'Programming Language :: Python :: Implementation :: PyPy',
'Topic :: Internet :: WWW/HTTP',
'Topic :: Software Development :: Libraries :: Python Modules',
]
Expand Down
File renamed without changes.
12 changes: 12 additions & 0 deletions test_project/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import sys


if __name__ == '__main__':

from django.core.management import execute_from_command_line

execute_from_command_line(sys.argv)
Empty file.
Empty file added test_project/test_app/models.py
Empty file.
32 changes: 20 additions & 12 deletions cached_auth/tests.py → test_project/test_app/tests.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.core.cache import cache
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.test.client import Client
from django.test.utils import override_settings
from cached_auth import CACHE_KEY
from django.conf import settings

import cached_auth

try:
from django.contrib.auth import get_user_model
User = get_user_model()
except ImportError:
from django.contrib.auth.models import User
get_user_model = lambda: User


def auth_preprocessor(user, request):
user.username = 'test_auth'
return user
try:
# Python 3.4+ includes reload in importlib
from importlib import reload
except ImportError:
try:
# Python 3.3 includes reload in imp
from imp import reload
except ImportError:
# Python 2 includes reload as a builtin
pass


class MiddlewareTest(TestCase):

def setUp(self):
self.user = User.objects.create_user(username='test', password='a')
user_model = get_user_model()
self.user = user_model.objects.create_user(username='test', password='a')
self.user.is_superuser = True
self.user.is_staff = True
self.user.save()
Expand All @@ -32,13 +40,13 @@ def setUp(self):
def test_anonymous(self):
# Anonymous user doesn't cause cache to be set
client = Client()
key = CACHE_KEY % self.user.id
key = cached_auth.CACHE_KEY % self.user.id
client.get(reverse('admin:index'))
self.assertEqual(cache.get(key), None)

def test_cached_middleware(self):
client = Client()
key = CACHE_KEY % self.user.id
key = cached_auth.CACHE_KEY % self.user.id
self.assertEqual(cache.get(key), None)

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

@override_settings(CACHED_AUTH_PREPROCESSOR='cached_auth.tests.auth_preprocessor')
@override_settings(CACHED_AUTH_PREPROCESSOR='test_project.utils.auth_preprocessor')
def test_cached_auth_preprocessor_function(self):
reload(cached_auth)
client = Client()
key = CACHE_KEY % self.user.id
key = cached_auth.CACHE_KEY % self.user.id
self.assertEqual(cache.get(key), None)

client.login(username='test', password='a')
Expand Down
Empty file added test_project/test_app/views.py
Empty file.
Empty file.
File renamed without changes.
1 change: 1 addition & 0 deletions test_project/test_app_custom_user/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from test_app.tests import *
Empty file.
Empty file.
11 changes: 11 additions & 0 deletions test_project/test_project/custom_user_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from .settings import *


INSTALLED_APPS += (
'test_app_custom_user',
)

AUTH_USER_MODEL = 'test_app_custom_user.User'
37 changes: 37 additions & 0 deletions test_project/test_project/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals


DEBUG = True

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
}
}

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.admin',
'cached_auth',
'test_app',
)

CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': '127.0.0.1:11211',
'TIMEOUT': 36000,
}
}

MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'cached_auth.Middleware',
)

ROOT_URLCONF = 'test_project.urls'

SECRET_KEY = 'django-cached-authentication-middleware'
File renamed without changes.
7 changes: 7 additions & 0 deletions test_project/test_project/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals


def auth_preprocessor(user, request):
user.username = 'test_auth'
return user
53 changes: 0 additions & 53 deletions tests/runtests.py

This file was deleted.

53 changes: 0 additions & 53 deletions tests/runtests_custom_user.py

This file was deleted.

0 comments on commit e175b61

Please sign in to comment.