Skip to content

Commit

Permalink
test: add ws image resized test
Browse files Browse the repository at this point in the history
  • Loading branch information
gounux committed Oct 26, 2024
1 parent 27118f5 commit f107eb0
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ repos:
rev: v5.0.0
hooks:
- id: check-added-large-files
args: ["--maxkb=500"]
args: ["--maxkb=750"]
- id: check-ast
- id: check-builtin-literals
- id: check-case-conflict
Expand Down
4 changes: 2 additions & 2 deletions gischat/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class GischatMessageModel(BaseModel):
class GischatTextMessage(GischatMessageModel):
type: GischatMessageTypeEnum = GischatMessageTypeEnum.TEXT
author: str = GISCHAT_NICKNAME_FIELD
avatar: Optional[str] = None
avatar: Optional[str] = Field(default=None)
text: str = GISCHAT_TEXT_MESSAGE_FIELD

def __str__(self) -> str:
Expand All @@ -67,7 +67,7 @@ def __str__(self) -> str:
class GischatImageMessage(GischatMessageModel):
type: GischatMessageTypeEnum = GischatMessageTypeEnum.IMAGE
author: str = GISCHAT_NICKNAME_FIELD
avatar: Optional[str] = Field()
avatar: Optional[str] = Field(default=None)
image_data: str = Field(description="String of the encoded image")


Expand Down
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
MIN_AUTHOR_LENGTH = str(3)
MAX_AUTHOR_LENGTH = str(32)
MAX_MESSAGE_LENGTH = str(255)
MAX_IMAGE_SIZE = str(800)
9 changes: 8 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
import pytest
from fastapi.testclient import TestClient

from tests import MAX_MESSAGE_LENGTH, MIN_AUTHOR_LENGTH, TEST_ROOMS, TEST_RULES
from tests import (
MAX_IMAGE_SIZE,
MAX_MESSAGE_LENGTH,
MIN_AUTHOR_LENGTH,
TEST_ROOMS,
TEST_RULES,
)


def get_test_rooms() -> list[str]:
Expand All @@ -17,6 +23,7 @@ def client() -> Generator[TestClient, None, None]:
os.environ["RULES"] = TEST_RULES
os.environ["MIN_AUTHOR_LENGTH"] = MIN_AUTHOR_LENGTH
os.environ["MAX_MESSAGE_LENGTH"] = MAX_MESSAGE_LENGTH
os.environ["MAX_IMAGE_SIZE"] = MAX_IMAGE_SIZE
from gischat.app import app

yield TestClient(app)
Binary file added tests/img/cat.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions tests/test_images.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import base64
from io import BytesIO

import pytest
from PIL import Image
from starlette.testclient import TestClient

from gischat.models import GischatMessageTypeEnum
from tests import MAX_IMAGE_SIZE
from tests.conftest import get_test_rooms

CAT_IMAGE_PATH = "tests/img/cat.jpg"


@pytest.mark.parametrize("room", get_test_rooms())
def test_send_image_resized(client: TestClient, room: str):
original_image = Image.open(CAT_IMAGE_PATH)
ow, oh = original_image.size
assert ow == 1200
assert oh == 800

with client.websocket_connect(f"/room/{room}/ws") as websocket:
assert websocket.receive_json() == {
"type": GischatMessageTypeEnum.NB_USERS.value,
"nb_users": 1,
}
with open(CAT_IMAGE_PATH, "rb") as file:
image_data = file.read()
websocket.send_json(
{
"type": GischatMessageTypeEnum.IMAGE.value,
"author": f"ws-tester-{room}",
"avatar": "cat",
"image_data": base64.b64encode(image_data).decode("utf-8"),
}
)
data = websocket.receive_json()
assert data["type"] == GischatMessageTypeEnum.IMAGE.value
assert data["author"] == f"ws-tester-{room}"
assert data["avatar"] == "cat"
assert "image_data" in data
assert data["image_data"]

image = Image.open(BytesIO(base64.b64decode(data["image_data"])))
w, h = image.size
assert max(w, h) == int(MAX_IMAGE_SIZE)

0 comments on commit f107eb0

Please sign in to comment.