Skip to content

Commit

Permalink
Redirects from login page and register page if user is logged in (#2984)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
waseem18 authored and di committed Feb 19, 2018
1 parent 4e0c8c3 commit 4ff4846
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
17 changes: 15 additions & 2 deletions tests/unit/accounts/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion warehouse/accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 4ff4846

Please sign in to comment.