From f5708594a781a05652cc03cf79eb4cfeb0c26b64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20I=C3=9Fbr=C3=BCcker?= Date: Mon, 23 Sep 2024 16:20:55 +0200 Subject: [PATCH] Add basic fail2ban support (#847) --- bookmarks/tests/test_login_view.py | 16 ++++++++++++++-- siteroot/urls.py | 5 +++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/bookmarks/tests/test_login_view.py b/bookmarks/tests/test_login_view.py index 3eb25bd..5aa55a6 100644 --- a/bookmarks/tests/test_login_view.py +++ b/bookmarks/tests/test_login_view.py @@ -1,7 +1,7 @@ from django.test import TestCase, override_settings from django.urls import path, include -from bookmarks.tests.helpers import HtmlTestMixin +from bookmarks.tests.helpers import BookmarkFactoryMixin, HtmlTestMixin from siteroot.urls import urlpatterns as base_patterns # Register OIDC urls for this test, otherwise login template can not render when OIDC is enabled @@ -9,7 +9,19 @@ urlpatterns = base_patterns + [path("oidc/", include("mozilla_django_oidc.urls") @override_settings(ROOT_URLCONF=__name__) -class LoginViewTestCase(TestCase, HtmlTestMixin): +class LoginViewTestCase(TestCase, BookmarkFactoryMixin, HtmlTestMixin): + + def test_failed_login_should_return_401(self): + response = self.client.post("/login/", {"username": "test", "password": "test"}) + self.assertEqual(response.status_code, 401) + + def test_successful_login_should_redirect(self): + user = self.setup_user(name="test") + user.set_password("test") + user.save() + + response = self.client.post("/login/", {"username": "test", "password": "test"}) + self.assertEqual(response.status_code, 302) def test_should_not_show_oidc_login_by_default(self): response = self.client.get("/login/") diff --git a/siteroot/urls.py b/siteroot/urls.py index 5431f02..4b14f4f 100644 --- a/siteroot/urls.py +++ b/siteroot/urls.py @@ -34,6 +34,11 @@ class LinkdingLoginView(auth_views.LoginView): context["enable_oidc"] = settings.LD_ENABLE_OIDC return context + def form_invalid(self, form): + response = super().form_invalid(form) + response.status_code = 401 + return response + urlpatterns = [ path("admin/", linkding_admin_site.urls),