From 4ff4846f1f0a8dbd6ef21aa8dba66a99a2863a94 Mon Sep 17 00:00:00 2001 From: Wasim Thabraze Date: Tue, 20 Feb 2018 01:49:22 +0530 Subject: [PATCH] Redirects from login page and register page if user is logged in (#2984) * Redirects from login page and register page to /manage/projects if user if logged in * Addressed lint error - removed extra blank line * Added test related to redirection of authenticated user from login page to projects page --- tests/unit/accounts/test_views.py | 17 +++++++++++++++-- warehouse/accounts/views.py | 4 +++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/tests/unit/accounts/test_views.py b/tests/unit/accounts/test_views.py index ccb488d0765a..4830b24074b8 100644 --- a/tests/unit/accounts/test_views.py +++ b/tests/unit/accounts/test_views.py @@ -238,6 +238,15 @@ def test_post_validate_no_redirects(self, pyramid_request, assert isinstance(result, HTTPSeeOther) assert result.headers["Location"] == observed_next_url + def test_redirect_authenticated_user(self): + pyramid_request = pretend.stub(authenticated_userid=1) + pyramid_request.route_path = pretend.call_recorder( + lambda a: '/the-redirect' + ) + result = views.login(pyramid_request) + assert isinstance(result, HTTPSeeOther) + assert result.headers["Location"] == "/the-redirect" + class TestLogout: @@ -303,9 +312,13 @@ def test_get(self, db_request): assert result["form"] is form_inst def test_redirect_authenticated_user(self): - result = views.register(pretend.stub(authenticated_userid=1)) + pyramid_request = pretend.stub(authenticated_userid=1) + pyramid_request.route_path = pretend.call_recorder( + lambda a: '/the-redirect' + ) + result = views.register(pyramid_request) assert isinstance(result, HTTPSeeOther) - assert result.headers["Location"] == "/" + assert result.headers["Location"] == "/the-redirect" def test_register_redirect(self, db_request, monkeypatch): db_request.method = "POST" diff --git a/warehouse/accounts/views.py b/warehouse/accounts/views.py index e17cfbf6d964..668bf6cc0f4c 100644 --- a/warehouse/accounts/views.py +++ b/warehouse/accounts/views.py @@ -103,6 +103,8 @@ def login(request, redirect_field_name=REDIRECT_FIELD_NAME, # TODO: Logging in should reset request.user # TODO: Configure the login view as the default view for not having # permission to view something. + if request.authenticated_userid is not None: + return HTTPSeeOther(request.route_path('manage.projects')) user_service = request.find_service(IUserService, context=None) @@ -213,7 +215,7 @@ def logout(request, redirect_field_name=REDIRECT_FIELD_NAME): ) def register(request, _form_class=RegistrationForm): if request.authenticated_userid is not None: - return HTTPSeeOther("/") + return HTTPSeeOther(request.route_path('manage.projects')) if AdminFlag.is_enabled(request.db, 'disallow-new-user-registration'): request.session.flash(