2026-04-21 02:05:59 -03:00
|
|
|
from tests.unit.services.keycloak_admin_helpers import *
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_user_parses_location(monkeypatch) -> None:
|
|
|
|
|
dummy_settings = types.SimpleNamespace(
|
|
|
|
|
keycloak_admin_url="http://kc",
|
|
|
|
|
keycloak_admin_realm="atlas",
|
|
|
|
|
keycloak_admin_client_id="client",
|
|
|
|
|
keycloak_admin_client_secret="secret",
|
|
|
|
|
keycloak_realm="atlas",
|
|
|
|
|
)
|
|
|
|
|
monkeypatch.setattr("ariadne.services.keycloak_admin.settings", dummy_settings)
|
|
|
|
|
client = KeycloakAdminClient()
|
|
|
|
|
client._token = "token"
|
|
|
|
|
client._expires_at = 9999999999
|
|
|
|
|
|
|
|
|
|
dummy = DummyClient([DummyResponse({}, headers={"Location": "http://kc/admin/realms/atlas/users/abc"})])
|
|
|
|
|
monkeypatch.setattr("ariadne.services.keycloak_admin.httpx.Client", lambda *args, **kwargs: dummy)
|
|
|
|
|
|
|
|
|
|
assert client.create_user({"username": "alice"}) == "abc"
|
|
|
|
|
|
|
|
|
|
def test_create_user_missing_location(monkeypatch) -> None:
|
|
|
|
|
dummy_settings = types.SimpleNamespace(
|
|
|
|
|
keycloak_admin_url="http://kc",
|
|
|
|
|
keycloak_admin_realm="atlas",
|
|
|
|
|
keycloak_admin_client_id="client",
|
|
|
|
|
keycloak_admin_client_secret="secret",
|
|
|
|
|
keycloak_realm="atlas",
|
|
|
|
|
)
|
|
|
|
|
monkeypatch.setattr("ariadne.services.keycloak_admin.settings", dummy_settings)
|
|
|
|
|
client = KeycloakAdminClient()
|
|
|
|
|
client._token = "token"
|
|
|
|
|
client._expires_at = 9999999999
|
|
|
|
|
|
|
|
|
|
dummy = DummyClient([DummyResponse({}, headers={})])
|
|
|
|
|
monkeypatch.setattr("ariadne.services.keycloak_admin.httpx.Client", lambda *args, **kwargs: dummy)
|
|
|
|
|
|
|
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
|
client.create_user({"username": "alice"})
|
|
|
|
|
|
|
|
|
|
def test_reset_password_raises_on_error(monkeypatch) -> None:
|
|
|
|
|
dummy_settings = types.SimpleNamespace(
|
|
|
|
|
keycloak_admin_url="http://kc",
|
|
|
|
|
keycloak_admin_realm="atlas",
|
|
|
|
|
keycloak_admin_client_id="client",
|
|
|
|
|
keycloak_admin_client_secret="secret",
|
|
|
|
|
keycloak_realm="atlas",
|
|
|
|
|
)
|
|
|
|
|
monkeypatch.setattr("ariadne.services.keycloak_admin.settings", dummy_settings)
|
|
|
|
|
client = KeycloakAdminClient()
|
|
|
|
|
client._token = "token"
|
|
|
|
|
client._expires_at = 9999999999
|
|
|
|
|
|
|
|
|
|
dummy = DummyClient([DummyResponse({}, status_code=400)])
|
|
|
|
|
monkeypatch.setattr("ariadne.services.keycloak_admin.httpx.Client", lambda *args, **kwargs: dummy)
|
|
|
|
|
|
|
|
|
|
with pytest.raises(httpx.HTTPStatusError):
|
|
|
|
|
client.reset_password("user", "pw", temporary=True)
|
2026-05-21 02:29:20 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_client_lifecycle_helpers(monkeypatch) -> None:
|
|
|
|
|
dummy_settings = types.SimpleNamespace(
|
|
|
|
|
keycloak_admin_url="http://kc",
|
|
|
|
|
keycloak_admin_realm="atlas",
|
|
|
|
|
keycloak_admin_client_id="client",
|
|
|
|
|
keycloak_admin_client_secret="secret",
|
|
|
|
|
keycloak_realm="atlas",
|
|
|
|
|
)
|
|
|
|
|
monkeypatch.setattr("ariadne.services.keycloak_admin.settings", dummy_settings)
|
|
|
|
|
client = KeycloakAdminClient()
|
|
|
|
|
client._token = "token"
|
|
|
|
|
client._expires_at = 9999999999
|
|
|
|
|
dummy = DummyClient(
|
|
|
|
|
[
|
|
|
|
|
DummyResponse([{"id": "uuid", "clientId": "wolf"}]),
|
|
|
|
|
DummyResponse({}),
|
|
|
|
|
DummyResponse({}),
|
|
|
|
|
DummyResponse({"value": "secret"}),
|
|
|
|
|
DummyResponse([{"id": "scope", "name": "groups"}]),
|
|
|
|
|
DummyResponse({}),
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
monkeypatch.setattr("ariadne.services.keycloak_admin.httpx.Client", lambda *args, **kwargs: dummy)
|
|
|
|
|
|
|
|
|
|
assert client.find_client("wolf")["id"] == "uuid"
|
|
|
|
|
client.create_client({"clientId": "wolf"})
|
|
|
|
|
client.update_client("uuid", {"clientId": "wolf"})
|
|
|
|
|
assert client.get_client_secret("uuid") == "secret"
|
|
|
|
|
assert client.find_client_scope_id("groups") == "scope"
|
|
|
|
|
client.attach_optional_client_scope("uuid", "scope")
|
|
|
|
|
assert dummy.calls[-1][0] == "put"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_client_helpers_handle_missing_payloads(monkeypatch) -> None:
|
|
|
|
|
dummy_settings = types.SimpleNamespace(
|
|
|
|
|
keycloak_admin_url="http://kc",
|
|
|
|
|
keycloak_admin_realm="atlas",
|
|
|
|
|
keycloak_admin_client_id="client",
|
|
|
|
|
keycloak_admin_client_secret="secret",
|
|
|
|
|
keycloak_realm="atlas",
|
|
|
|
|
)
|
|
|
|
|
monkeypatch.setattr("ariadne.services.keycloak_admin.settings", dummy_settings)
|
|
|
|
|
client = KeycloakAdminClient()
|
|
|
|
|
client._token = "token"
|
|
|
|
|
client._expires_at = 9999999999
|
|
|
|
|
dummy = DummyClient([DummyResponse([]), DummyResponse([]), DummyResponse({})])
|
|
|
|
|
monkeypatch.setattr("ariadne.services.keycloak_admin.httpx.Client", lambda *args, **kwargs: dummy)
|
|
|
|
|
|
|
|
|
|
assert client.find_client("wolf") is None
|
|
|
|
|
assert client.find_client_scope_id("groups") is None
|
|
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
|
client.get_client_secret("uuid")
|