From 8136cdb5074d6e141bb97909b81570aa77fcf8dd Mon Sep 17 00:00:00 2001 From: Guilhem Allaman <40383801+gounux@users.noreply.github.com> Date: Sun, 21 Jul 2024 14:34:47 +0200 Subject: [PATCH] Add websocket tests (#13) --- tests/test_api.py | 34 ++++++++++++++++++++++++++++++---- tests/test_websocket.py | 25 +++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 tests/test_websocket.py diff --git a/tests/test_api.py b/tests/test_api.py index 38347ec..20295ef 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -6,14 +6,14 @@ from tests.conftest import test_rooms -def test_version(client: TestClient): +def test_get_version(client: TestClient): response = client.get("/version") assert response.status_code == 200 assert response.json()["version"] == get_poetry_version() @pytest.mark.parametrize("room", test_rooms()) -def test_status(client: TestClient, room: str): +def test_get_status(client: TestClient, room: str): response = client.get("/status") assert response.status_code == 200 data = response.json() @@ -27,13 +27,39 @@ def test_status(client: TestClient, room: str): @pytest.mark.parametrize("room", test_rooms()) -def test_rooms(client: TestClient, room: str): +def test_get_rooms(client: TestClient, room: str): response = client.get("/rooms") assert response.status_code == 200 assert room in response.json() -def test_rules(client: TestClient): +def test_get_rules(client: TestClient): response = client.get("/rules") assert response.status_code == 200 assert response.json()["rules"] == TEST_RULES + + +@pytest.mark.parametrize("room", test_rooms()) +def test_put_message(client: TestClient, room: str): + response = client.put( + f"/room/{room}/message", + json={"message": "fromage", "author": f"ws-tester-{room}"}, + ) + assert response.status_code == 200 + assert response.json()["message"] == "fromage" + assert response.json()["author"] == f"ws-tester-{room}" + + +def test_put_message_wrong_room(client: TestClient): + assert ( + client.put( + "/room/fromage/message", json={"message": "fromage", "author": "ws-tester"} + ).status_code + == 404 + ) + assert ( + client.put( + "/room/void/message", json={"message": "fromage", "author": "ws-tester"} + ).status_code + == 404 + ) diff --git a/tests/test_websocket.py b/tests/test_websocket.py new file mode 100644 index 0000000..c45056a --- /dev/null +++ b/tests/test_websocket.py @@ -0,0 +1,25 @@ +import pytest +from fastapi.testclient import TestClient + +from tests.conftest import test_rooms + +TEST_MESSAGE = "Is this websocket working ?" + + +@pytest.mark.parametrize("room", test_rooms()) +def test_websocket_put_message(client: TestClient, room: str): + with client.websocket_connect(f"/room/{room}/ws") as websocket: + client.put( + f"/room/{room}/message", + json={"message": TEST_MESSAGE, "author": f"ws-tester-{room}"}, + ) + data = websocket.receive_json() + assert data == {"message": TEST_MESSAGE, "author": f"ws-tester-{room}"} + + +@pytest.mark.parametrize("room", test_rooms()) +def test_websocket_send_message(client: TestClient, room: str): + with client.websocket_connect(f"/room/{room}/ws") as websocket: + websocket.send_json({"message": TEST_MESSAGE, "author": f"ws-tester-{room}"}) + data = websocket.receive_json() + assert data == {"message": TEST_MESSAGE, "author": f"ws-tester-{room}"}