Skip to content

Commit

Permalink
Add websocket tests (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
gounux authored Jul 21, 2024
1 parent 1430355 commit 8136cdb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
34 changes: 30 additions & 4 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
)
25 changes: 25 additions & 0 deletions tests/test_websocket.py
Original file line number Diff line number Diff line change
@@ -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}"}

0 comments on commit 8136cdb

Please sign in to comment.