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 (#88)
Browse files Browse the repository at this point in the history
* Adds lock file to .gitignore

* Runs PEP8 linter

* Adds tests for successful user redirect

* Make static method a global function
  • Loading branch information
djperrefort committed Oct 9, 2023
1 parent edd26f8 commit 5cede02
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ dist/

# IDE files
.idea/

# Poetry files
poetry.lock
4 changes: 1 addition & 3 deletions crc_jupyter_auth/remote_user_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
---------------
"""

import os

from jupyterhub.auth import Authenticator, LocalAuthenticator
from jupyterhub.handlers import BaseHandler
from jupyterhub.utils import url_path_join
Expand Down Expand Up @@ -51,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
35 changes: 32 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 All @@ -14,6 +17,16 @@
from crc_jupyter_auth.remote_user_auth import AuthenticatorSettings, RemoteUserLoginHandler


def mock_user() -> User:
"""Create a jupyterhub user object with mock/dummy data
Returns:
A partially instantiated User instance
"""

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


class TestUtils:
"""Base class for general testing/setup utilities"""

Expand All @@ -37,7 +50,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')

# 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 +64,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 +156,19 @@ 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"""

@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 5cede02

Please sign in to comment.