Repository: pypi/warehouse

Issue:
Redirect from password reset pages if user is logged in
Currently the two password reset views will still render even if there's a user logged in:

* `accounts.request-password-reset`
* `accounts.reset-password`

Since it doesn't make much sense for a logged in user to request a password reset, or reset their password, these views should check if there is a logged in user, and redirect to the `index` route if there is.

---

**Good First Issue**: This issue is good for first time contributors. If there is not a corresponding pull request for this issue, it is up for grabs. For directions for getting set up, see our [Getting Started Guide](https://warehouse.pypa.io/development/getting-started/). If you are working on this issue and have questions, please feel free to ask them here, [`#pypa-dev` on Freenode](https://webchat.freenode.net/?channels=%23pypa-dev), or the [pypa-dev mailing list](https://groups.google.com/forum/#!forum/pypa-dev).


Discussion:
I am a beginner and would like to work on this issue. Can i work on it and be guided along?
@ShubhGupta2125 Absolutely!

Take a look at our [Getting Started Guide](https://warehouse.readthedocs.io/development/getting-started/) for setting up your development environment and feel free to ask any questions here.
@ShubhGupta2125 - are you still on this? Otherwise I would like to tackle it as a first to get involved. (cc: @di)

Resolving patch:
diff --git a/warehouse/accounts/views.py b/warehouse/accounts/views.py
--- a/warehouse/accounts/views.py
+++ b/warehouse/accounts/views.py
@@ -263,6 +263,9 @@ def register(request, _form_class=RegistrationForm):
     require_methods=False,
 )
 def request_password_reset(request, _form_class=RequestPasswordResetForm):
+    if request.authenticated_userid is not None:
+        return HTTPSeeOther(request.route_path('index'))
+
     user_service = request.find_service(IUserService, context=None)
     form = _form_class(request.POST, user_service=user_service)
 
@@ -288,6 +291,9 @@ def request_password_reset(request, _form_class=RequestPasswordResetForm):
     require_methods=False,
 )
 def reset_password(request, _form_class=ResetPasswordForm):
+    if request.authenticated_userid is not None:
+        return HTTPSeeOther(request.route_path('index'))
+
     user_service = request.find_service(IUserService, context=None)
     token_service = request.find_service(ITokenService, name="password")
 


Test patch:
diff --git a/tests/unit/accounts/test_views.py b/tests/unit/accounts/test_views.py
--- a/tests/unit/accounts/test_views.py
+++ b/tests/unit/accounts/test_views.py
@@ -581,6 +581,15 @@ def test_request_password_reset_with_wrong_credentials(
         ]
         assert send_password_reset_email.calls == []
 
+    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.request_password_reset(pyramid_request)
+        assert isinstance(result, HTTPSeeOther)
+        assert result.headers["Location"] == "/the-redirect"
+
 
 class TestResetPassword:
 
@@ -872,6 +881,15 @@ def test_reset_password_password_date_changed(self, pyramid_request):
             ),
         ]
 
+    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.reset_password(pyramid_request)
+        assert isinstance(result, HTTPSeeOther)
+        assert result.headers["Location"] == "/the-redirect"
+
 
 class TestVerifyEmail: