Skip to content

Commit

Permalink
Refactoring and tests for getting the lockdown form
Browse files Browse the repository at this point in the history
Streamlines the get_lockdown_form method a bit and adds tests to check it for
properly returning exceptions in case of a form which couldn't be loaded.
  • Loading branch information
Dunedan committed Apr 6, 2015
1 parent ad1dee3 commit d4bc06a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
18 changes: 10 additions & 8 deletions lockdown/middleware.py
Expand Up @@ -17,20 +17,22 @@ def compile_url_exceptions(url_exceptions):


def get_lockdown_form(form_path):
form_path = settings.FORM
if not form_path or '.' not in form_path:
raise ImproperlyConfigured('The form module path was not provided.')
last_dot = form_path.rfind('.')
module, attr = form_path[:last_dot], form_path[last_dot + 1:]
if not form_path:
raise ImproperlyConfigured('No LOCKDOWN_FORM specified.')
form_path_list = form_path.split(".")
module = ".".join(form_path_list[:-1])
attr = form_path_list[-1]
try:
mod = import_module(module)
except (ImportError, ValueError) as exc:
raise ImproperlyConfigured('Error importing LOCKDOWN_FORM %s: "%s"'
% (form_path, exc))
raise ImproperlyConfigured('Module configured in LOCKDOWN_FORM (%s) to'
' contain the form class couldn\'t be '
'found.' % module)
try:
form = getattr(mod, attr)
except AttributeError:
raise ImproperlyConfigured('Module "%s" does not define a "%s" form.'
raise ImproperlyConfigured('The module configured in LOCKDOWN_FORM '
' (%s) doesn\'t define a "%s" form.'
% (module, attr))
return form

Expand Down
21 changes: 19 additions & 2 deletions lockdown/tests/tests.py
@@ -1,10 +1,11 @@
import datetime
import os

from django.test import TestCase
from django.conf import settings as django_settings
from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase

from lockdown import settings, middleware
from lockdown import middleware, settings
from lockdown.forms import AuthForm

__all__ = ['DecoratorTests', 'MiddlewareTests']
Expand Down Expand Up @@ -99,6 +100,22 @@ def test_custom_form(self):
middleware._default_form = middleware.get_lockdown_form(
settings.FORM)

def test_invalid_custom_form(self):
"""Test that pointing to an invalid form properly produces an error"""

# no form configured at all
self.assertRaises(ImproperlyConfigured,
middleware.get_lockdown_form, None)
# invalid module name in the configured form
self.assertRaises(ImproperlyConfigured,
middleware.get_lockdown_form, 'invalidform')
# not existing module for form
self.assertRaises(ImproperlyConfigured,
middleware.get_lockdown_form, 'invalid.form')
# existing module, but no form with that name in the module
self.assertRaises(ImproperlyConfigured,
middleware.get_lockdown_form, 'lockdown.forms.foo')

def test_locked_until(self):
_old_until_date = settings.UNTIL_DATE
yesterday = datetime.datetime.now() - datetime.timedelta(days=1)
Expand Down

0 comments on commit d4bc06a

Please sign in to comment.