Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support named URL patterns for LOGIN_URL #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

# ignore setup.py build dir
build/
*.egg-info/

# ignore sphinx built documentation
_build/
Expand Down
3 changes: 2 additions & 1 deletion security/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from django.core.exceptions import ImproperlyConfigured
from django.urls import reverse, resolve
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import resolve_url
from django.test.signals import setting_changed
from django.utils import timezone
from django.utils.deprecation import MiddlewareMixin
Expand Down Expand Up @@ -1208,7 +1209,7 @@ class LoginRequiredMiddleware(BaseMiddleware, CustomLogoutMixin):

def load_setting(self, setting, value):
if setting == 'LOGIN_URL':
self.login_url = value
self.login_url = resolve_url(value)
elif setting == 'LOGIN_EXEMPT_URLS':
self.exempt_urls = [compile(expr) for expr in (value or ())]
else:
Expand Down
16 changes: 16 additions & 0 deletions testing/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,22 @@ def test_redirects_to_custom_login_url(self):
{"login_url": '/custom-login/'},
)

def test_redirects_to_named_url_pattern(self):
with self.settings(
LOGIN_URL="login", # <- use a named pattern from urls.py
):
response = self.client.get('/home/')
self.assertRedirects(response, reverse('login') + '?next=/home/')
response = self.client.get(
'/home/',
HTTP_X_REQUESTED_WITH='XMLHttpRequest',
)
self.assertEqual(response.status_code, 401)
self.assertEqual(
json.loads(response.content.decode('utf-8')),
{"login_url": reverse('login')},
)

def test_logs_out_inactive_users(self):
user = User.objects.create_user(
username="foo",
Expand Down
6 changes: 4 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ deps =
Sphinx
django==1.11
ua_parser==0.7.1
coverage
commands =
make clean
make html
Expand All @@ -31,10 +32,11 @@ basepython = python3.6
deps=
pep8-naming
hacking
flake8==2.6.0
flake8==3.8.3
coverage
commands=flake8 security testing

[flake8]
ignore=E131,H306,H301,H404,H405,H101,N802,N812
ignore=E305,E128,E131,E303,E501,H306,H301,H404,H405,H101,N802,N812,W504,
max-complexity=10
exclude=*migrations*