diff --git a/DjangoPlugin/tracdjangoplugin/plugins.py b/DjangoPlugin/tracdjangoplugin/plugins.py index 363858d..94992f2 100644 --- a/DjangoPlugin/tracdjangoplugin/plugins.py +++ b/DjangoPlugin/tracdjangoplugin/plugins.py @@ -121,6 +121,11 @@ def process_request(self, req): raise RequestDone def do_get(self, req): + # Not 100% sure why, but for some links (RSS especially) Trac likes + # to generate URLs pointing to `/login?referer=` when + # the user is already authenticated. + if req.is_authenticated: + req.redirect(self._get_safe_redirect_url(req)) return "plainlogin.html", { "form": AuthenticationForm(), "referer": req.args.get("referer", ""), diff --git a/DjangoPlugin/tracdjangoplugin/tests.py b/DjangoPlugin/tracdjangoplugin/tests.py index c60ba54..e10cec3 100644 --- a/DjangoPlugin/tracdjangoplugin/tests.py +++ b/DjangoPlugin/tracdjangoplugin/tests.py @@ -149,6 +149,20 @@ def test_login_invalid_inactive_user(self): User.objects.create_user(username="test", password="test", is_active=False) self.assertLoginFails(username="test", password="test") + def test_login_page_redirects_if_already_logged_in(self): + self.env.config.set("trac", "base_url", "") + request = self.request_factory( + method="GET", + path_info="/login", + args={"referer": "/test"}, + authname="admin", + ) + + with self.assertRaises(RequestDone): + self.component.process_request(request) + + self.assertEqual(request.headers_sent["Location"], "/test") + class DjangoDBManagementMiddlewareTestCase(SimpleTestCase): @classmethod