From 9749429c79fce52c9e644ebd38181ed200ded90b Mon Sep 17 00:00:00 2001 From: "coderabbitai[bot]" <136622811+coderabbitai[bot]@users.noreply.github.com> Date: Thu, 15 May 2025 19:59:01 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9D=20Add=20docstrings=20to=20`pre-rel?= =?UTF-8?q?ease`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Docstrings generation was requested by @LordLumineer. * https://github.com/EventKit-Stream/KoFi-WebSocket/pull/29#issuecomment-2884912763 The following files were modified: * `app/main.py` * `tests/test_main.py` --- app/main.py | 15 ++++++--------- tests/test_main.py | 13 +++++++++++-- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/app/main.py b/app/main.py index 5e2975f..07dd2cb 100644 --- a/app/main.py +++ b/app/main.py @@ -45,6 +45,9 @@ @app.get("/version") async def _version(): + """ + Returns the current version of the FastAPI application as a JSON object. + """ return {"version": app.version} @@ -97,15 +100,9 @@ async def ko_fi_webhook(data: str = Form(...)): @app.websocket("/ws/{verification_token}") async def websocket_endpoint(websocket: WebSocket, verification_token: str): """ - Establishes a WebSocket connection with the client and forwards incoming - Ko-fi webhooks to the corresponding connection. - - The endpoint expects a verification token as a path parameter, which is used - to identify the connection. The endpoint will keep the connection alive by - sending a "pong" response to the "ping" message sent by the client. - - If the connection is closed, the endpoint will remove the connection from the - active connections dictionary. + Handles a WebSocket connection for receiving Ko-fi webhook notifications. + + Establishes a WebSocket connection associated with the provided verification token, maintains connection health via ping/pong messages, and ensures proper cleanup when the connection is closed. """ await websocket.accept() active_connections[verification_token].add(websocket) diff --git a/tests/test_main.py b/tests/test_main.py index 0f4c37b..60ee6f0 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -146,7 +146,11 @@ async def mock_send_json(*args, **kwargs): @pytest.mark.asyncio async def test_webhook_websocket_disconnect(client, sample_webhook_data): - """Test webhook WebSocket disconnect.""" + """ + Tests that when a WebSocket disconnect occurs during webhook processing, the connection is removed from active connections. + + Opens a WebSocket connection, mocks the send_json method to raise a disconnect, posts webhook data, and verifies the connection is cleaned up. + """ async def mock_send_json(*args, **kwargs): raise WebSocketDisconnect() @@ -167,7 +171,12 @@ async def mock_send_json(*args, **kwargs): def test_version_endpoint(client): - """Test the version endpoint.""" + """ + Tests that the /version endpoint returns the correct application version. + + Sends a GET request to /version and asserts the response status is 200 and the JSON + payload contains the expected version string. + """ response = client.get("/version") assert response.status_code == 200 assert response.json() == {"version": app.version}