Skip to content

Commit

Permalink
fix: forbid multiple registrations with the same nickname (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
gounux authored Oct 28, 2024
1 parent d9ac74c commit b2fe8b3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
10 changes: 10 additions & 0 deletions gischat/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,16 @@ async def websocket_endpoint(websocket: WebSocket, room: str) -> None:
# newcomer message
if message.type == GischatMessageTypeEnum.NEWCOMER:
message = GischatNewcomerMessage(**payload)
# check if user is already registered
if notifier.is_user_present(room, message.newcomer):
logger.info(
f"User '{message.newcomer}' wants to register but there is already a '{message.newcomer}' in room {room}"
)
message = GischatUncompliantMessage(
reason=f"User '{message.newcomer}' already registered in room {room}"
)
await websocket.send_json(jsonable_encoder(message))
continue
notifier.register_user(websocket, message.newcomer)
logger.info(f"Newcomer in room {room}: {message.newcomer}")
await notifier.notify_newcomer(room, message.newcomer)
Expand Down
25 changes: 25 additions & 0 deletions tests/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,31 @@ def test_websocket_send_newcomer(client: TestClient, room: str):
}


@pytest.mark.parametrize("room", get_test_rooms())
def test_websocket_send_newcomer_twice(client: TestClient, room: str):
with client.websocket_connect(f"/room/{room}/ws") as websocket:
# Isidore sends first registration
websocket.send_json(
{"type": GischatMessageTypeEnum.NEWCOMER.value, "newcomer": "Isidore"}
)
assert websocket.receive_json() == {
"type": GischatMessageTypeEnum.NB_USERS.value,
"nb_users": 1,
}
assert websocket.receive_json() == {
"type": GischatMessageTypeEnum.NEWCOMER.value,
"newcomer": "Isidore",
}
# Isidore sends second registration -> forbidden
websocket.send_json(
{"type": GischatMessageTypeEnum.NEWCOMER.value, "newcomer": "Isidore"}
)
assert websocket.receive_json() == {
"type": GischatMessageTypeEnum.UNCOMPLIANT.value,
"reason": f"User 'Isidore' already registered in room {room}",
}


@pytest.mark.parametrize("room", get_test_rooms())
def test_websocket_send_newcomer_multiple(client: TestClient, room: str):
with client.websocket_connect(f"/room/{room}/ws") as websocket1:
Expand Down

0 comments on commit b2fe8b3

Please sign in to comment.