Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: more verbose rules endpoint #25

Merged
merged 2 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions gischat/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ async def get_rules() -> RulesModel:
return RulesModel(
rules=os.environ.get("RULES", "YOLO"),
main_lang=os.environ.get("MAIN_LANG", "en"),
min_author_length=int(os.environ.get("MIN_AUTHOR_LENGTH", 3)),
max_author_length=int(os.environ.get("MAX_AUTHOR_LENGTH", 32)),
max_message_length=int(os.environ.get("MAX_MESSAGE_LENGTH", 255)),
)


Expand Down
3 changes: 3 additions & 0 deletions gischat/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class StatusModel(BaseModel):
class RulesModel(BaseModel):
rules: str
main_lang: str
min_author_length: int
max_author_length: int
max_message_length: int


class MessageModel(BaseModel):
Expand Down
18 changes: 17 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from fastapi.testclient import TestClient

from gischat.utils import get_poetry_version
from tests import MAX_AUTHOR_LENGTH, MIN_AUTHOR_LENGTH, TEST_RULES
from tests import MAX_AUTHOR_LENGTH, MAX_MESSAGE_LENGTH, MIN_AUTHOR_LENGTH, TEST_RULES
from tests.conftest import test_rooms


Expand Down Expand Up @@ -38,6 +38,9 @@ def test_get_rules(client: TestClient):
assert response.status_code == 200
assert response.json()["rules"] == TEST_RULES
assert response.json()["main_lang"] == "en"
assert response.json()["min_author_length"] == int(MIN_AUTHOR_LENGTH)
assert response.json()["max_author_length"] == int(MAX_AUTHOR_LENGTH)
assert response.json()["max_message_length"] == int(MAX_MESSAGE_LENGTH)


@pytest.mark.parametrize("room", test_rooms())
Expand Down Expand Up @@ -106,3 +109,16 @@ def test_put_message_author_too_long(client: TestClient, room: str):
payload = response.json()["detail"]
assert payload["message"] == "Uncompliant message"
assert f"Author too long: max {MAX_AUTHOR_LENGTH} characters" in payload["errors"]


@pytest.mark.parametrize("room", test_rooms())
def test_put_message_too_long(client: TestClient, room: str):
message = "".join(["a" for _ in range(int(MAX_MESSAGE_LENGTH) + 1)])
response = client.put(
f"/room/{room}/message",
json={"message": message, "author": "stephanie"},
)
assert response.status_code == 420
payload = response.json()["detail"]
assert payload["message"] == "Uncompliant message"
assert f"Message too long: max {MAX_MESSAGE_LENGTH} characters" in payload["errors"]