sessions_test.go

Overview

sessions_test.go contains comprehensive unit tests for the session management REST API controller defined in the controllers package. It verifies the correctness of CRUD operations on session resources by exercising HTTP handlers that manage user sessions within applications. The file leverages fake in-memory session services from the internal/fakes package to simulate backend data storage and focuses on validating API behavior such as session retrieval, creation, deletion, and listing.

These tests ensure that the session controller correctly handles input validation, error conditions, and response formatting, including proper HTTP status codes and JSON payloads. The tests use the httptest package to simulate HTTP requests and responses, and mux for URL variable extraction. Test cases cover both successful and failure scenarios to verify robustness.

This file is part of the session management domain, closely related to the Session Management topic, particularly in exercising the REST API layer described in REST API and Web Launchers.


Test Functions and Their Details

TestGetSession

Validates the behavior of the GetSessionHandler method, which handles HTTP GET requests to fetch a specific session.

TestCreateSession

Tests the CreateSessionHandler method which handles HTTP POST requests to create a new session.

TestDeleteSession

Tests the DeleteSessionHandler method which handles HTTP DELETE requests to remove a session.

TestListSessions

Tests the ListSessionsHandler method which handles HTTP GET requests to list all sessions for a given user.


Helper Functions

sessionVars(sessionID fakes.SessionKey) map[string]string

Generates a map of URL path variables (app_name, user_id, session_id) from a fakes.SessionKey struct to simulate mux route parameters in tests.

EquateApproxInt(margin int64) cmp.Option

Creates a custom comparer for go-cmp that treats two integer values as equal if their absolute difference is within the specified margin. Used for comparing UpdatedAt timestamps approximately.


Implementation Details and Algorithms


Interactions with Other Components

These interactions ensure isolated, fast, and reliable testing of the session REST API functionality, corresponding to the REST API controller layer described in REST API and Web Launchers and the underlying session logic in Session Management.


Diagram: Session API Test Flow

flowchart TD
A[Test Setup] --> B[Initialize FakeSessionService]
B --> C[Create SessionsAPIController]
C --> D[Create HTTP Request with mux Vars]
D --> E["Invoke Handler (Get/Create/Delete/List)"]
E --> F[Record HTTP Response]
F --> G{Check HTTP Status}
G -->|Matches| H[Decode Response JSON]
H --> I[Compare with Expected Data]
G -->|Mismatch| J[Fail Test]
I --> K{Data Matches?}
K -->|Yes| L[Test Pass]
K -->|No| J

Summary of Main Test Functions

Function

Purpose

HTTP Method

Endpoint Tested

TestGetSession

Tests retrieving a session

GET

/apps/{app_name}/users/{user_id}/sessions/{session_id}

TestCreateSession

Tests creating a new session

POST

/apps/{app_name}/users/{user_id}/sessions/{session_id}

TestDeleteSession

Tests deleting an existing session

DELETE

/apps/{app_name}/users/{user_id}/sessions/{session_id}

TestListSessions

Tests listing all sessions for user

GET

/apps/{app_name}/users/{user_id}/sessions


End of sessions_test.go Documentation