|
| 1 | +import os |
| 2 | +import uuid |
| 3 | +from typing import Generator |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +import zitadel_client as zitadel |
| 8 | +from zitadel_client.exceptions import ApiException |
| 9 | +from zitadel_client.models import ( |
| 10 | + SessionServiceChecks, |
| 11 | + SessionServiceCheckUser, |
| 12 | + SessionServiceCreateSessionRequest, |
| 13 | + SessionServiceCreateSessionResponse, |
| 14 | + SessionServiceDeleteSessionBody, |
| 15 | + SessionServiceGetSessionResponse, |
| 16 | + SessionServiceListSessionsRequest, |
| 17 | + SessionServiceListSessionsResponse, |
| 18 | + SessionServiceSetSessionRequest, |
| 19 | + SessionServiceSetSessionResponse, |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture(scope="module") |
| 24 | +def base_url() -> str: |
| 25 | + """Provides the base URL for tests, skipping if unset.""" |
| 26 | + url = os.getenv("BASE_URL") |
| 27 | + if not url: |
| 28 | + pytest.skip("Environment variable BASE_URL must be set", allow_module_level=True) |
| 29 | + return url |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture(scope="module") |
| 33 | +def auth_token() -> str: |
| 34 | + """Provides a valid personal access token, skipping if unset.""" |
| 35 | + token = os.getenv("AUTH_TOKEN") |
| 36 | + if not token: |
| 37 | + pytest.skip("Environment variable AUTH_TOKEN must be set", allow_module_level=True) |
| 38 | + return token |
| 39 | + |
| 40 | + |
| 41 | +@pytest.fixture(scope="module") |
| 42 | +def client(base_url: str, auth_token: str) -> zitadel.Zitadel: |
| 43 | + """Provides a Zitadel client configured with a personal access token.""" |
| 44 | + return zitadel.Zitadel.with_access_token(base_url, auth_token) |
| 45 | + |
| 46 | + |
| 47 | +@pytest.fixture |
| 48 | +def session(client: zitadel.Zitadel) -> Generator[SessionServiceCreateSessionResponse, None, None]: |
| 49 | + """Creates a fresh session for each test and cleans up afterward.""" |
| 50 | + request = SessionServiceCreateSessionRequest( |
| 51 | + checks=SessionServiceChecks(user=SessionServiceCheckUser(loginName="johndoe")), |
| 52 | + lifetime="18000s", |
| 53 | + ) |
| 54 | + response = client.sessions.session_service_create_session(request) |
| 55 | + yield response |
| 56 | + # Teardown |
| 57 | + delete_body = SessionServiceDeleteSessionBody() |
| 58 | + try: |
| 59 | + client.sessions.session_service_delete_session( |
| 60 | + response.session_id if response.session_id is not None else "", |
| 61 | + delete_body, |
| 62 | + ) |
| 63 | + except ApiException: |
| 64 | + pass |
| 65 | + |
| 66 | + |
| 67 | +class TestSessionServiceSanityCheckSpec: |
| 68 | + """ |
| 69 | + SessionService Integration Tests |
| 70 | +
|
| 71 | + This suite verifies the Zitadel SessionService API's basic operations using a |
| 72 | + personal access token: |
| 73 | +
|
| 74 | + 1. Create a session with specified checks and lifetime |
| 75 | + 2. Retrieve the session by ID |
| 76 | + 3. List sessions and ensure the created session appears |
| 77 | + 4. Update the session's lifetime and confirm a new token is returned |
| 78 | + 5. Error when retrieving a non-existent session |
| 79 | +
|
| 80 | + Each test runs in isolation: a new session is created in the `session` fixture and |
| 81 | + deleted after the test to ensure a clean state. |
| 82 | + """ |
| 83 | + |
| 84 | + def test_retrieves_session_details_by_id( |
| 85 | + self, |
| 86 | + client: zitadel.Zitadel, |
| 87 | + session: SessionServiceCreateSessionResponse, |
| 88 | + ) -> None: |
| 89 | + """Retrieves the session details by ID.""" |
| 90 | + response: SessionServiceGetSessionResponse = client.sessions.session_service_get_session( |
| 91 | + session.session_id if session.session_id is not None else "" |
| 92 | + ) |
| 93 | + assert response.session is not None |
| 94 | + assert response.session.id == session.session_id |
| 95 | + |
| 96 | + def test_includes_created_session_when_listing( |
| 97 | + self, |
| 98 | + client: zitadel.Zitadel, |
| 99 | + session: SessionServiceCreateSessionResponse, |
| 100 | + ) -> None: |
| 101 | + """Includes the created session when listing all sessions.""" |
| 102 | + request = SessionServiceListSessionsRequest(queries=[]) |
| 103 | + response: SessionServiceListSessionsResponse = client.sessions.session_service_list_sessions(request) |
| 104 | + assert response.sessions is not None |
| 105 | + assert session.session_id in [session.id for session in response.sessions] |
| 106 | + |
| 107 | + def test_updates_session_lifetime_and_returns_new_token( |
| 108 | + self, |
| 109 | + client: zitadel.Zitadel, |
| 110 | + session: SessionServiceCreateSessionResponse, |
| 111 | + ) -> None: |
| 112 | + """Updates the session lifetime and returns a new token.""" |
| 113 | + request = SessionServiceSetSessionRequest(lifetime="36000s") |
| 114 | + response: SessionServiceSetSessionResponse = client.sessions.session_service_set_session( |
| 115 | + session.session_id if session.session_id is not None else "", |
| 116 | + request, |
| 117 | + ) |
| 118 | + assert isinstance(response.session_token, str) |
| 119 | + |
| 120 | + def test_raises_api_exception_for_nonexistent_session( |
| 121 | + self, |
| 122 | + client: zitadel.Zitadel, |
| 123 | + session: SessionServiceCreateSessionResponse, |
| 124 | + ) -> None: |
| 125 | + """Raises an ApiException when retrieving a non-existent session.""" |
| 126 | + with pytest.raises(ApiException): |
| 127 | + client.sessions.session_service_get_session( |
| 128 | + str(uuid.uuid4()), |
| 129 | + session_token=session.session_token, |
| 130 | + ) |
0 commit comments