1
0
mirror of https://github.com/quay/quay.git synced 2026-01-26 06:21:37 +03:00
Files
quay/oauth/test/test_rhsso.py
Harish Govindarajulu 849da76256 oauth: Fallback to using depricated_sub since rhsso now has a new value for sub field (PROJQUAY-9124) (#4152)
* oauth: Fallback to depricated_sub when sub has new value (PROJQUAY-9124)

Signed-off-by: harishsurf <hgovinda@redhat.com>

* Add test for rhsso backward compatability for deprecated_sub

* Address review comments

* Fix circular import

* Add additional tests for codecov

---------

Signed-off-by: harishsurf <hgovinda@redhat.com>
2025-07-31 08:48:03 -04:00

60 lines
1.9 KiB
Python

import pytest
from oauth.login import OAuthLoginException
from oauth.login_utils import get_sub_username_email_from_token
from oauth.services.rhsso import RHSSOOAuthService
@pytest.fixture
def rhsso_service():
config = {"RHSSO_LOGIN_CONFIG": {}}
return RHSSOOAuthService(config, "RHSSO_LOGIN_CONFIG")
def test_get_user_id_with_digit_sub_returns_deprecated_sub(rhsso_service):
"""Test that when sub is a digit, deprecated_sub is returned."""
decoded_token = {
"sub": "12345678",
"deprecated_sub": "f:uuid-1234:original_username",
}
result = rhsso_service.get_user_id(decoded_token)
assert result == "f:uuid-1234:original_username"
def test_get_user_id_with_digit_sub_missing_deprecated_sub_returns_sub(rhsso_service):
"""Test fallback to sub when deprecated_sub is missing."""
decoded_token = {
"sub": "87654321",
}
result = rhsso_service.get_user_id(decoded_token)
assert result == "87654321"
def test_get_user_id_missing_sub_field_raises_exception(rhsso_service):
"""Test that missing sub field raises OAuthLoginException."""
decoded_token = {
"deprecated_sub": "f:uuid-1234:original_username",
}
with pytest.raises(OAuthLoginException, match="Token missing 'sub' field"):
rhsso_service.get_user_id(decoded_token)
def test_get_sub_username_email_with_login_service(rhsso_service):
"""Test get_sub_username_email_from_token with login_service provided."""
decoded_token = {
"sub": "12345678",
"deprecated_sub": "f:uuid-1234:original_username",
"email": "test@example.com",
"email_verified": True,
}
user_id, username, email, additional_info = get_sub_username_email_from_token(
decoded_token, login_service=rhsso_service
)
# Should use the RHSSO service's get_user_id which returns deprecated_sub
assert user_id == "f:uuid-1234:original_username"