|
| 1 | +import asyncio |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +import pytest |
| 5 | +from fastapi import WebSocketDisconnect |
| 6 | +from fastapi.testclient import TestClient |
| 7 | + |
| 8 | +from api.main import app |
| 9 | +from api.models.database_models import Sensor, SensorData |
| 10 | +from api.utils.database import get_engine |
| 11 | +from api.utils.websocket_connection_handler import get_websocket_handler |
| 12 | +from tests.utils.fake_db import override_get_engine |
| 13 | +from tests.utils.fixtures import superuser_token, token |
| 14 | +from tests.utils.sensor_utils import create_sensor |
| 15 | + |
| 16 | +app.dependency_overrides[get_engine] = override_get_engine |
| 17 | +client: TestClient = TestClient(app) |
| 18 | + |
| 19 | + |
| 20 | +def test_ws_connect_no_authentication(): |
| 21 | + try: |
| 22 | + with client.websocket_connect("/ws") as _: |
| 23 | + return |
| 24 | + except WebSocketDisconnect as exception: |
| 25 | + assert exception.code == 1008 |
| 26 | + |
| 27 | + |
| 28 | +def test_ws_connect_no_bearer(): |
| 29 | + try: |
| 30 | + with client.websocket_connect("/ws", headers={"Authorization": "1234"}) as _: |
| 31 | + return |
| 32 | + except WebSocketDisconnect as exception: |
| 33 | + assert exception.code == 1008 |
| 34 | + |
| 35 | + |
| 36 | +def test_ws_connect_wrong_token(token: str): |
| 37 | + try: |
| 38 | + with client.websocket_connect("/ws", headers={"Authorization": f"Bearer {token}1234"}) as _: |
| 39 | + return |
| 40 | + except WebSocketDisconnect as exception: |
| 41 | + assert exception.code == 1008 |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.asyncio |
| 45 | +async def test_ws_connect(superuser_token: str): |
| 46 | + ws_handler = get_websocket_handler() |
| 47 | + task = asyncio.get_event_loop().create_task(get_websocket_handler().event_loop()) |
| 48 | + |
| 49 | + sensor: Sensor = await create_sensor(name="TestSensorWS") |
| 50 | + |
| 51 | + with client.websocket_connect("/ws", headers={"Authorization": f"Bearer {superuser_token}"}) as websocket: |
| 52 | + data = websocket.receive_json() |
| 53 | + assert data == {"message": "Hello World!"} |
| 54 | + |
| 55 | + data: dict[str, Any] = { |
| 56 | + "id": -1, |
| 57 | + "temperature": 32.1, |
| 58 | + "humidity": 56.78, |
| 59 | + "pressure": 123.45, |
| 60 | + "voltage": 3.45, |
| 61 | + "sensor_id": sensor.id, |
| 62 | + } |
| 63 | + sensor_data: SensorData = SensorData(**data) |
| 64 | + await ws_handler.add_event(sensor_data) |
| 65 | + |
| 66 | + # As the websocket.receive_json() is synchronous and TestClient does not really work with async ws, |
| 67 | + # the delay is need, so that the ws_handler can run its loop. |
| 68 | + await asyncio.sleep(0.1) |
| 69 | + |
| 70 | + received_data = websocket.receive_json() |
| 71 | + assert data.get("id") == received_data.get("id") |
| 72 | + assert data.get("temperature") == received_data.get("temperature") |
| 73 | + task.cancel() |
| 74 | + |
| 75 | + |
| 76 | +# ToDo Investigate performance issue |
| 77 | +def test_ws_connect_double_connection(token: str): |
| 78 | + with client.websocket_connect("/ws", headers={"Authorization": f"Bearer {token}"}) as websocket: |
| 79 | + data = websocket.receive_json() |
| 80 | + assert data == {"message": "Hello World!"} |
| 81 | + |
| 82 | + try: |
| 83 | + with client.websocket_connect("/ws", headers={"Authorization": f"Bearer {token}"}) as _: |
| 84 | + pass |
| 85 | + except WebSocketDisconnect as exception: |
| 86 | + assert exception.code == 1008 |
0 commit comments