Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Adds tests for successful user redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
djperrefort committed Oct 9, 2023
1 parent f395104 commit 61e55e9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion crc_jupyter_auth/remote_user_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def get(self):
# Facilitate user authentication
user = self.user_from_username(remote_user)
self.set_login_cookie(user)
self.redirect(url_path_join(self.hub.server.base_url, 'home'))
self.redirect_or_raise(url_path_join(self.hub.server.base_url, 'home'))

def redirect_or_raise(self, url, raise_status=404):
"""Redirect to the given url
Expand Down
31 changes: 28 additions & 3 deletions tests/test_request_routing.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
"""Test HTTP request routing by the ``RemoteUserLoginHandler`` class."""

from datetime import datetime
from typing import Type
from unittest import TestCase
from unittest.mock import MagicMock, patch

from jupyterhub.auth import Authenticator
from jupyterhub.objects import Server
from jupyterhub.objects import Hub
from jupyterhub.orm import User
from jupyterhub.utils import new_token, url_path_join
from tornado import web
from tornado.httputil import HTTPConnection, HTTPHeaders, HTTPServerRequest
from tornado.web import Application
Expand Down Expand Up @@ -37,7 +40,7 @@ def create_http_request_handler(
"""

# Create a tornado application running a jupyterhub server
application = Application(hub=Server(), authenticator=authenticator_type())
application = Application(hub=Hub(), authenticator=authenticator_type(), cookie_secret='secret')

Check warning on line 43 in tests/test_request_routing.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/test_request_routing.py#L43

Possible hardcoded password: 'secret'

# HTTP connections are used by the application to write HTTP responses
# The `set_close_callback` method is required to exist by JupyterHub
Expand All @@ -51,7 +54,7 @@ def create_http_request_handler(


class RoutingByUsername(TestUtils, TestCase):
"""Test missing or invalid username information results in a HTTP 401 error"""
"""Test missing or invalid username information results in an HTTP 401 error"""

def test_missing_username_401(self) -> None:
"""Test for a 401 error when the username is missing from the HTTP header"""
Expand Down Expand Up @@ -143,3 +146,25 @@ def test_incorrect_role_redirect(self, mock_redirect_call: MagicMock) -> None:
request_handler.get()

mock_redirect_call.assert_called_once_with(request_handler.authenticator.missing_role_redirect)


class SuccessfulLoginRouting(TestUtils, TestCase):
"""Test the routing of users after a successful login"""

@staticmethod
def mock_user() -> User:
"""Create a jupyterhub user object with mock/dummy data"""

return User(id=1, name='mock_user', admin=False, created=datetime.now(), cookie_id=new_token())

@patch.object(RemoteUserLoginHandler, 'redirect', return_value=None)
@patch.object(RemoteUserLoginHandler, 'user_from_username', return_value=mock_user())
def test_user_is_redirected(self, mock_user: User, mock_redirect_call: MagicMock) -> None:
"""Test the user is redirected to the jupyterhub server"""

request_data = {AuthenticatorSettings().username_header: 'username'}
request_handler = self.create_http_request_handler(request_data)
request_handler.get()

expected_url = url_path_join(request_handler.hub.server.base_url, 'home')
mock_redirect_call.assert_called_once_with(expected_url)

0 comments on commit 61e55e9

Please sign in to comment.