Skip to content

Added tests for LoginRequiredMiddleware #368

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

Open
wants to merge 1 commit into
base: main
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 tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"BOOLEAN_CLAIM_MAPPING": {"is_staff": "user_is_staff",
"is_superuser": "user_is_superuser"},
"CONFIG_RELOAD_INTERVAL": 0, # Always reload settings
"LOGIN_EXEMPT_URLS": ["^api", "nested/path/"],
}

LOGIN_URL = "django_auth_adfs:login"
Expand Down
37 changes: 37 additions & 0 deletions tests/test_middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from django.contrib.auth.models import AnonymousUser
from django.http import HttpResponseRedirect
from django.test import RequestFactory, TestCase
from django_auth_adfs.middleware import LoginRequiredMiddleware


class LoginRequiredMiddlewareTestCase(TestCase):
def setUp(self):
self.rf = RequestFactory()
self.middleware = LoginRequiredMiddleware(get_response=lambda *args: None)

def test_not_exempt_redirects(self):
request = self.rf.get('/page/')
request.user = AnonymousUser()
response = self.middleware(request)
assert isinstance(response, HttpResponseRedirect)

request = self.rf.get('/nested/redirect/')
request.user = AnonymousUser()
response = self.middleware(request)
assert isinstance(response, HttpResponseRedirect)

def test_exempt(self):
request = self.rf.get('/api')
request.user = AnonymousUser()
response = self.middleware(request)
assert response is None

request = self.rf.get('/nested/path/')
request.user = AnonymousUser()
response = self.middleware(request)
assert response is None

request = self.rf.get('/nested/path/further')
request.user = AnonymousUser()
response = self.middleware(request)
assert response is None
5 changes: 4 additions & 1 deletion tests/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from django.urls import include, re_path
from django.urls import include, re_path, path

from .views import page

urlpatterns = [
re_path(r'^oauth2/', include('django_auth_adfs.urls')),
re_path(r'^oauth2/', include('django_auth_adfs.drf_urls')),
path("page", page),
]
4 changes: 4 additions & 0 deletions tests/views.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
def test_failed_response(request, error_message, status):
pass


def page(request):
pass
Loading