Commits

Chris Beaven committed 11f714b

Fix tests

  • Participants
  • Parent commits ae4f1e2

Comments (0)

Files changed (4)

File lockdown/middleware.py

 class LockdownMiddleware(object):
     def __init__(self, form=None, logout_key=None, session_key=None,
                  url_exceptions=None, **form_kwargs):
-        if form is None:
-            form = _default_form
         if logout_key is None:
             logout_key = settings.LOGOUT_KEY
         if session_key is None:
             session_key = settings.SESSION_KEY
-        if url_exceptions is None:
-            url_exceptions = _default_url_exceptions
         self.form = form
         self.form_kwargs = form_kwargs
         self.logout_key = logout_key
                                        'sessions framework')
 
         # Don't lock down if the URL matches an exception pattern.
-        for pattern in self.url_exceptions:
+        if self.url_exceptions is None:
+            url_exceptions = _default_url_exceptions
+        else:
+            url_exceptions = self.url_exceptions
+        for pattern in url_exceptions:
             if pattern.search(request.path):
                 return None
 
         form_data = request.method == 'POST' and request.POST or None
-        form = self.form(data=form_data, **self.form_kwargs)
+        if self.form is None:
+            form_class = _default_form
+        else:
+            form_class = self.form
+        form = form_class(data=form_data, **self.form_kwargs)
 
         authorized = False
         token = session.get(self.session_key)

File tests/tests.py

 class DecoratorTests(TestCase):
     _url = '/locked/view/'
     _contents = 'A locked view.'
-    
+
     def setUp(self):
         self._old_pw = settings.PASSWORDS
-        settings.PASSWORDS = 'letmein'
+        settings.PASSWORDS = ('letmein',)
         self._old_form = settings.FORM
         settings.FORM = 'lockdown.forms.LockdownForm'
         middleware._default_form = middleware.get_lockdown_form(settings.FORM)
         middleware._default_url_exceptions = \
                 middleware.compile_url_exceptions(settings.URL_EXCEPTIONS)
 
-        response = self.client.get(self._url)
-        self.assertContains(response, self._contents)
+        try:
+            response = self.client.get(self._url)
+            self.assertContains(response, self._contents)
+        finally:
+            settings.URL_EXCEPTIONS = _old_url_exceptions
+            middleware._default_url_exceptions = \
+                    middleware.compile_url_exceptions(settings.URL_EXCEPTIONS)
 
-        settings.URL_EXCEPTIONS = _old_url_exceptions
-        middleware._default_url_exceptions = \
-                middleware.compile_url_exceptions(settings.URL_EXCEPTIONS)
-        
     def test_submit_password(self):
         response = self.client.post(self._url, {'password': 'letmein'},
                                     follow=True)
         _old_form = settings.FORM
         settings.FORM = 'tests.forms.CustomLockdownForm'
         middleware._default_form = middleware.get_lockdown_form(settings.FORM)
-        
-        response = self.client.post(self._url, {'answer': '42'},
-                                    follow=True)
-        self.assertContains(response, self._contents)
-                                    
-        settings.FORM = _old_form
-        middleware._default_form = middleware.get_lockdown_form(settings.FORM)
-    
+
+        try:
+            response = self.client.post(self._url, {'answer': '42'},
+                                        follow=True)
+            self.assertContains(response, self._contents)
+        finally:
+            settings.FORM = _old_form
+            middleware._default_form = middleware.get_lockdown_form(
+                                                                settings.FORM)
+
 
 class MiddlewareTests(DecoratorTests):
     _url = '/a/view/'
     _contents = 'A view.'
-    
+
     def setUp(self):
         self._old_middleware_classes = django_settings.MIDDLEWARE_CLASSES
         django_settings.MIDDLEWARE_CLASSES += (

File tests/urls.py

 from django.conf.urls.defaults import *
 
-urlpatterns = patterns(
-    'tests.views',
+urlpatterns = patterns('tests.views',
     (r'^a/view/$', 'aview'),
     (r'^locked/view/$', 'lockedview'),
-    )
+)

File tests/views.py

 
 from lockdown.decorators import lockdown
 
-def index(request):
-    return HttpResponse('The index view.')
 
 def aview(request):
     return HttpResponse('A view.')
 
-@lockdown
+
+@lockdown()
 def lockedview(request):
     return HttpResponse('A locked view.')
+
+
+@lockdown(passwords=('squirrel',))
+def nutcracker(request):
+    return HttpResponse('A locked view.')