From 404393b3a7570409dd8f463c2129209a2072d6ea Mon Sep 17 00:00:00 2001 From: Marios Hadjiantonis Date: Sun, 23 Aug 2026 20:37:55 +0200 Subject: [PATCH] Reapply "Add username pattern validation" This reverts commit bba13d72c156b578d8fa31ee477e29fa3eacb2d0. --- jsonplaceholder_api_proxy/users/models.py | 2 +- tests/users/test_api_router.py | 35 +++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/jsonplaceholder_api_proxy/users/models.py b/jsonplaceholder_api_proxy/users/models.py index fbd87b5..434c371 100644 --- a/jsonplaceholder_api_proxy/users/models.py +++ b/jsonplaceholder_api_proxy/users/models.py @@ -22,7 +22,7 @@ class Company(BaseModel): business_slogan: Annotated[str, Field(alias="bs")] name: str - username: str + username: Annotated[str, Field(pattern=r"^[A-Za-z][A-Za-z0-9_.]{0,19}$")] email: EmailStr phone: str address: Address diff --git a/tests/users/test_api_router.py b/tests/users/test_api_router.py index d50387e..a7ddb50 100644 --- a/tests/users/test_api_router.py +++ b/tests/users/test_api_router.py @@ -159,6 +159,41 @@ async def test_create_user_invalid_email(client: tuple[TestClient, AsyncMockType assert response.status_code == status.HTTP_422_UNPROCESSABLE_CONTENT +@pytest.mark.asyncio +async def test_create_user_invalid_username(client: tuple[TestClient, AsyncMockType]): + """Test POST /users with invalid email""" + test_client, _ = client + + invalid_usernames = [ + "0test", + "test+example", + "test" * 10, + ] + for username in invalid_usernames: + payload: dict[str, Any] = { + "name": "John Doe", + "username": username, + "email": "test@example.com", + "phone": "1-770-736-8031", + "address": { + "street": "Kulas Light", + "suite": "Apt. 556", + "city": "Gwenborough", + "zipcode": "92998-3874", + "geo": {"lat": 37.7749, "lng": -122.4194}, + }, + "company": { + "name": "Romaguera-Crona", + "catchPhrase": "Multi-layered client-server neural-net", + "bs": "harness real-time e-markets", + }, + } + + response = test_client.post("/users", json=payload) + + assert response.status_code == status.HTTP_422_UNPROCESSABLE_CONTENT + + @pytest.mark.asyncio async def test_create_user_missing_required_field( client: tuple[TestClient, AsyncMockType],