linkding/bookmarks/tests/test_auth_proxy_support.py

59 lines
2.2 KiB
Python
Raw Normal View History

from unittest.mock import patch, PropertyMock
from django.test import TestCase, modify_settings
from django.urls import reverse
from bookmarks.models import User
from bookmarks.middlewares import CustomRemoteUserMiddleware
class AuthProxySupportTest(TestCase):
# Reproducing configuration from the settings logic here
# ideally this test would just override the respective options
@modify_settings(
2024-01-27 10:29:16 +00:00
MIDDLEWARE={"append": "bookmarks.middlewares.CustomRemoteUserMiddleware"},
AUTHENTICATION_BACKENDS={
"prepend": "django.contrib.auth.backends.RemoteUserBackend"
},
)
def test_auth_proxy_authentication(self):
2024-01-27 10:29:16 +00:00
user = User.objects.create_user(
"auth_proxy_user", "user@example.com", "password123"
)
2024-01-27 10:29:16 +00:00
headers = {"REMOTE_USER": user.username}
response = self.client.get(reverse("bookmarks:index"), **headers)
self.assertEqual(response.status_code, 200)
# Reproducing configuration from the settings logic here
# ideally this test would just override the respective options
@modify_settings(
2024-01-27 10:29:16 +00:00
MIDDLEWARE={"append": "bookmarks.middlewares.CustomRemoteUserMiddleware"},
AUTHENTICATION_BACKENDS={
"prepend": "django.contrib.auth.backends.RemoteUserBackend"
},
)
def test_auth_proxy_with_custom_header(self):
2024-01-27 10:29:16 +00:00
with patch.object(
CustomRemoteUserMiddleware, "header", new_callable=PropertyMock
) as mock:
mock.return_value = "Custom-User"
user = User.objects.create_user(
"auth_proxy_user", "user@example.com", "password123"
)
2024-01-27 10:29:16 +00:00
headers = {"Custom-User": user.username}
response = self.client.get(reverse("bookmarks:index"), **headers)
self.assertEqual(response.status_code, 200)
def test_auth_proxy_is_disabled_by_default(self):
2024-01-27 10:29:16 +00:00
user = User.objects.create_user(
"auth_proxy_user", "user@example.com", "password123"
)
2024-01-27 10:29:16 +00:00
headers = {"REMOTE_USER": user.username}
response = self.client.get(reverse("bookmarks:index"), **headers, follow=True)
2024-01-27 10:29:16 +00:00
self.assertRedirects(response, "/login/?next=%2Fbookmarks")