Skip to content

Commit

Permalink
test: add authenticate method check
Browse files Browse the repository at this point in the history
  • Loading branch information
sgaist committed Oct 8, 2023
1 parent fb0cc77 commit 3b1d2ba
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
8 changes: 4 additions & 4 deletions multiauthenticator/multiauthenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ def check_blocked_users(self, username, authentication=None):
authenticator = WrapperAuthenticator(**authenticator_configuration)

if service_name is not None:
if service_name.endswith(PREFIX_SEPARATOR):
raise ValueError(f"Service name cannot end with {PREFIX_SEPARATOR}")
if PREFIX_SEPARATOR in service_name:
raise ValueError(f"Service name cannot contain {PREFIX_SEPARATOR}")
authenticator.service_name = service_name
elif authenticator.login_service.endswith(PREFIX_SEPARATOR):
raise ValueError(f"Login service cannot end with {PREFIX_SEPARATOR}")
elif PREFIX_SEPARATOR in authenticator.login_service:
raise ValueError(f"Login service cannot contain {PREFIX_SEPARATOR}")

self._authenticators.append(authenticator)

Expand Down
29 changes: 25 additions & 4 deletions multiauthenticator/tests/test_multiauthenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""Test module for the MultiAuthenticator class"""
import pytest

from jupyterhub.auth import DummyAuthenticator
from jupyterhub.auth import PAMAuthenticator
from oauthenticator.github import GitHubOAuthenticator
from oauthenticator.gitlab import GitLabOAuthenticator
Expand Down Expand Up @@ -205,8 +206,28 @@ def test_username_prefix():

multi_authenticator = MultiAuthenticator()
assert len(multi_authenticator._authenticators) == 2
assert multi_authenticator._authenticators[0].username_prefix == "GitLab:"
assert multi_authenticator._authenticators[1].username_prefix == "PAM:"
assert (
multi_authenticator._authenticators[0].username_prefix
== f"GitLab{PREFIX_SEPARATOR}"
)
assert (
multi_authenticator._authenticators[1].username_prefix
== f"PAM{PREFIX_SEPARATOR}"
)


@pytest.mark.asyncio
async def test_authenticated_username_prefix():
MultiAuthenticator.authenticators = [
(DummyAuthenticator, "/pam", {"service_name": "Dummy"}),
]

multi_authenticator = MultiAuthenticator()
assert len(multi_authenticator._authenticators) == 1
username = await multi_authenticator._authenticators[0].authenticate(
None, {"username": "test"}
)
assert username == f"Dummy{PREFIX_SEPARATOR}test"


def test_username_prefix_checks():
Expand Down Expand Up @@ -251,7 +272,7 @@ def test_username_prefix_validation():
with pytest.raises(ValueError) as excinfo:
MultiAuthenticator()

assert f"Service name cannot end with {PREFIX_SEPARATOR}" in str(excinfo.value)
assert f"Service name cannot contain {PREFIX_SEPARATOR}" in str(excinfo.value)

MultiAuthenticator.authenticators = [
(
Expand All @@ -270,4 +291,4 @@ def test_username_prefix_validation():
with pytest.raises(ValueError) as excinfo:
MultiAuthenticator()

assert f"Login service cannot end with {PREFIX_SEPARATOR}" in str(excinfo.value)
assert f"Login service cannot contain {PREFIX_SEPARATOR}" in str(excinfo.value)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dependencies = [
]

[project.optional-dependencies]
test = ["pytest", "pytest-cov"]
test = ["pytest", "pytest-cov", "pytest-asyncio"]
dev = ["pre-commit", "jupyterhub-multiauthenticator[test]"]

[tool.setuptools]
Expand Down

0 comments on commit 3b1d2ba

Please sign in to comment.