Skip to content

Commit 1952d76

Browse files
committed
test: add trailing slash handling to KeycloakOpenID
1 parent b535bb1 commit 1952d76

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

tests/test_keycloak_openid.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,35 @@ def test_auth_url(env: KeycloakTestEnv, oid: KeycloakOpenID) -> None:
139139
)
140140

141141

142+
@pytest.mark.parametrize("trailing_slash", [True, False])
143+
def test_openid_subpath_request_normalization(env: KeycloakTestEnv, trailing_slash: bool) -> None:
144+
"""
145+
Test that KeycloakOpenID builds correct request URLs when a sub-path is present.
146+
147+
:param env: Environment fixture
148+
:type env: KeycloakTestEnv
149+
:param trailing_slash: Indicator of trailing slash in server URL
150+
:type trailing_slash: bool
151+
"""
152+
host_port = f"http://{env.keycloak_host}:{env.keycloak_port}/auth"
153+
server_url = f"{host_port}/" if trailing_slash else host_port
154+
155+
oid = KeycloakOpenID(
156+
server_url=server_url,
157+
realm_name="master",
158+
client_id="admin-cli",
159+
)
160+
161+
with mock.patch.object(oid.connection._s, "get") as mock_get:
162+
mock_get.return_value = mock.Mock(status_code=200, json=lambda: {"issuer": "test"})
163+
164+
oid.well_known()
165+
assert (
166+
mock_get.call_args[0][0]
167+
== f"http://{env.keycloak_host}:{env.keycloak_port}/auth/realms/master/.well-known/openid-configuration"
168+
)
169+
170+
142171
def test_token(oid_with_credentials: tuple[KeycloakOpenID, str, str]) -> None:
143172
"""
144173
Test the token method.
@@ -685,6 +714,35 @@ async def test_a_auth_url(env: KeycloakTestEnv, oid: KeycloakOpenID) -> None:
685714
)
686715

687716

717+
@pytest.mark.asyncio
718+
@pytest.mark.parametrize("trailing_slash", [True, False])
719+
async def test_a_openid_subpath_request_normalization(
720+
env: KeycloakTestEnv, trailing_slash: bool
721+
) -> None:
722+
"""
723+
Test that KeycloakOpenID builds correct request URLs when a sub-path is present asynchronously.
724+
725+
:param env: Environment fixture
726+
:type env: KeycloakTestEnv
727+
:param trailing_slash: Indicator of trailing slash in server URL
728+
:type trailing_slash: bool
729+
"""
730+
host_port = f"http://{env.keycloak_host}:{env.keycloak_port}/auth"
731+
server_url = f"{host_port}/" if trailing_slash else host_port
732+
733+
oid = KeycloakOpenID(server_url=server_url, realm_name="master", client_id="admin-cli")
734+
735+
with mock.patch.object(oid.connection.async_s, "get", new_callable=mock.AsyncMock) as mock_get:
736+
mock_get.return_value = mock.Mock(status_code=200, json=lambda: {"issuer": "test"})
737+
738+
await oid.a_well_known()
739+
740+
expected = f"http://{env.keycloak_host}:{env.keycloak_port}/auth/realms/master/.well-known/openid-configuration"
741+
742+
mock_get.assert_called()
743+
assert str(mock_get.call_args[0][0]) == expected
744+
745+
688746
@pytest.mark.asyncio
689747
async def test_a_token(oid_with_credentials: tuple[KeycloakOpenID, str, str]) -> None:
690748
"""

0 commit comments

Comments
 (0)