Skip to content

Commit

Permalink
feature: add avatars (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
gounux authored Jul 25, 2024
1 parent c5f9ccb commit aa24cd8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
2 changes: 2 additions & 0 deletions gischat/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from typing import Optional

from pydantic import BaseModel

Expand All @@ -25,6 +26,7 @@ class RulesModel(BaseModel):
class MessageModel(BaseModel):
message: str
author: str
avatar: Optional[str] = None

def __str__(self) -> str:
return f"[{self.author}]: '{self.message}'"
Expand Down
4 changes: 3 additions & 1 deletion gischat/templates/ws-page.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ <h1>gischat websocket client</h1>
<button id="connectButton" onclick="onConnectButtonClick(event)">Connect</button>
<br>
<label>Author: <input type="text" id="authorId" autocomplete="off" value=""/></label>
<label>Avatar: <input type="text" id="avatarId" autocomplete="off" value="mGeoPackage.svg"/></label>
<hr>
<label>Message: <input type="text" id="messageText" autocomplete="off"/></label>
<button onclick="sendMessage(event)">Send</button>
Expand Down Expand Up @@ -112,11 +113,12 @@ <h1>gischat websocket client</h1>
}
const message = document.getElementById("messageText");
const author = document.getElementById("authorId");
const avatar = document.getElementById("avatarId");
if (!message.value || !author.value) {
alert("Author and message can not be empty");
return;
}
websocket.send(JSON.stringify({message: message.value, author: author.value}));
websocket.send(JSON.stringify({message: message.value, author: author.value, avatar: avatar.value}));
message.value = '';
event.preventDefault();
}
Expand Down
5 changes: 3 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ def test_get_rules(client: TestClient):
def test_put_message(client: TestClient, room: str):
response = client.put(
f"/room/{room}/message",
json={"message": "fromage", "author": f"ws-tester-{room}"},
json={"message": "fromage", "author": f"ws-tester-{room}", "avatar": "postgis"},
)
assert response.status_code == 200
assert response.json()["message"] == "fromage"
assert response.json()["author"] == f"ws-tester-{room}"
assert response.json()["avatar"] == "postgis"


def test_put_message_wrong_room(client: TestClient):
Expand Down Expand Up @@ -83,7 +84,7 @@ def test_put_message_author_not_alphanum(client: TestClient, room: str):
def test_put_message_author_too_short(client: TestClient, room: str):
response = client.put(
f"/room/{room}/message",
json={"message": "fromage", "author": "ch"},
json={"message": "fromage", "author": "ch", "avatar": "postgis"},
)
assert response.status_code == 420
payload = response.json()["detail"]
Expand Down
18 changes: 15 additions & 3 deletions tests/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,18 @@ def test_websocket_put_message(client: TestClient, room: str):
}
client.put(
f"/room/{room}/message",
json={"message": TEST_MESSAGE, "author": f"ws-tester-{room}"},
json={
"message": TEST_MESSAGE,
"author": f"ws-tester-{room}",
"avatar": "postgis",
},
)
data = websocket.receive_json()
assert data == {"message": TEST_MESSAGE, "author": f"ws-tester-{room}"}
assert data == {
"message": TEST_MESSAGE,
"author": f"ws-tester-{room}",
"avatar": "postgis",
}


@pytest.mark.parametrize("room", test_rooms())
Expand All @@ -40,7 +48,11 @@ def test_websocket_send_message(client: TestClient, room: str):
}
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}"}
assert data == {
"message": TEST_MESSAGE,
"author": f"ws-tester-{room}",
"avatar": None,
}


def nb_connected_users(json: dict[str, Any], room: str) -> bool:
Expand Down

0 comments on commit aa24cd8

Please sign in to comment.