fix: Lint fixes of last tests

This commit is contained in:
JSC
2025-08-01 09:30:15 +02:00
parent dc29915fbc
commit fceff92ca1
20 changed files with 326 additions and 313 deletions

View File

@@ -1,4 +1,5 @@
"""Tests for API token endpoints."""
# ruff: noqa: ARG002, PLR2004, PLC0415, BLE001, E501
from datetime import UTC, datetime, timedelta
from unittest.mock import patch
@@ -17,7 +18,7 @@ class TestApiTokenEndpoints:
self,
authenticated_client: AsyncClient,
authenticated_user: User,
):
) -> None:
"""Test successful API token generation."""
request_data = {"expires_days": 30}
@@ -45,7 +46,7 @@ class TestApiTokenEndpoints:
async def test_generate_api_token_default_expiry(
self,
authenticated_client: AsyncClient,
):
) -> None:
"""Test API token generation with default expiry."""
response = await authenticated_client.post("/api/v1/auth/api-token", json={})
@@ -54,9 +55,7 @@ class TestApiTokenEndpoints:
expires_at_str = data["expires_at"]
# Handle both ISO format with/without timezone info
if expires_at_str.endswith("Z"):
expires_at = datetime.fromisoformat(expires_at_str.replace("Z", "+00:00"))
elif "+" in expires_at_str or expires_at_str.count("-") > 2:
if expires_at_str.endswith("Z") or "+" in expires_at_str or expires_at_str.count("-") > 2:
expires_at = datetime.fromisoformat(expires_at_str)
else:
# Naive datetime, assume UTC
@@ -71,7 +70,7 @@ class TestApiTokenEndpoints:
async def test_generate_api_token_custom_expiry(
self,
authenticated_client: AsyncClient,
):
) -> None:
"""Test API token generation with custom expiry."""
expires_days = 90
request_data = {"expires_days": expires_days}
@@ -86,9 +85,7 @@ class TestApiTokenEndpoints:
expires_at_str = data["expires_at"]
# Handle both ISO format with/without timezone info
if expires_at_str.endswith("Z"):
expires_at = datetime.fromisoformat(expires_at_str.replace("Z", "+00:00"))
elif "+" in expires_at_str or expires_at_str.count("-") > 2:
if expires_at_str.endswith("Z") or "+" in expires_at_str or expires_at_str.count("-") > 2:
expires_at = datetime.fromisoformat(expires_at_str)
else:
# Naive datetime, assume UTC
@@ -103,7 +100,7 @@ class TestApiTokenEndpoints:
async def test_generate_api_token_validation_errors(
self,
authenticated_client: AsyncClient,
):
) -> None:
"""Test API token generation with validation errors."""
# Test minimum validation
response = await authenticated_client.post(
@@ -120,7 +117,7 @@ class TestApiTokenEndpoints:
assert response.status_code == 422
@pytest.mark.asyncio
async def test_generate_api_token_unauthenticated(self, client: AsyncClient):
async def test_generate_api_token_unauthenticated(self, client: AsyncClient) -> None:
"""Test API token generation without authentication."""
response = await client.post(
"/api/v1/auth/api-token",
@@ -132,7 +129,7 @@ class TestApiTokenEndpoints:
async def test_get_api_token_status_no_token(
self,
authenticated_client: AsyncClient,
):
) -> None:
"""Test getting API token status when user has no token."""
response = await authenticated_client.get("/api/v1/auth/api-token/status")
@@ -147,7 +144,7 @@ class TestApiTokenEndpoints:
async def test_get_api_token_status_with_token(
self,
authenticated_client: AsyncClient,
):
) -> None:
"""Test getting API token status when user has a token."""
# First generate a token
await authenticated_client.post(
@@ -170,7 +167,7 @@ class TestApiTokenEndpoints:
self,
authenticated_client: AsyncClient,
authenticated_user: User,
):
) -> None:
"""Test getting API token status with expired token."""
# Mock expired token
with patch("app.utils.auth.TokenUtils.is_token_expired", return_value=True):
@@ -190,7 +187,7 @@ class TestApiTokenEndpoints:
assert data["is_expired"] is True
@pytest.mark.asyncio
async def test_get_api_token_status_unauthenticated(self, client: AsyncClient):
async def test_get_api_token_status_unauthenticated(self, client: AsyncClient) -> None:
"""Test getting API token status without authentication."""
response = await client.get("/api/v1/auth/api-token/status")
assert response.status_code == 401
@@ -199,7 +196,7 @@ class TestApiTokenEndpoints:
async def test_revoke_api_token_success(
self,
authenticated_client: AsyncClient,
):
) -> None:
"""Test successful API token revocation."""
# First generate a token
await authenticated_client.post(
@@ -230,7 +227,7 @@ class TestApiTokenEndpoints:
async def test_revoke_api_token_no_token(
self,
authenticated_client: AsyncClient,
):
) -> None:
"""Test revoking API token when user has no token."""
response = await authenticated_client.delete("/api/v1/auth/api-token")
@@ -239,7 +236,7 @@ class TestApiTokenEndpoints:
assert data["message"] == "API token revoked successfully"
@pytest.mark.asyncio
async def test_revoke_api_token_unauthenticated(self, client: AsyncClient):
async def test_revoke_api_token_unauthenticated(self, client: AsyncClient) -> None:
"""Test revoking API token without authentication."""
response = await client.delete("/api/v1/auth/api-token")
assert response.status_code == 401
@@ -249,7 +246,7 @@ class TestApiTokenEndpoints:
self,
client: AsyncClient,
authenticated_client: AsyncClient,
):
) -> None:
"""Test successful authentication using API token."""
# Generate API token
token_response = await authenticated_client.post(
@@ -268,7 +265,7 @@ class TestApiTokenEndpoints:
assert "email" in data
@pytest.mark.asyncio
async def test_api_token_authentication_invalid_token(self, client: AsyncClient):
async def test_api_token_authentication_invalid_token(self, client: AsyncClient) -> None:
"""Test authentication with invalid API token."""
headers = {"API-TOKEN": "invalid_token"}
response = await client.get("/api/v1/auth/me", headers=headers)
@@ -282,7 +279,7 @@ class TestApiTokenEndpoints:
self,
client: AsyncClient,
authenticated_client: AsyncClient,
):
) -> None:
"""Test authentication with expired API token."""
# Generate API token
token_response = await authenticated_client.post(
@@ -301,7 +298,7 @@ class TestApiTokenEndpoints:
assert "API token has expired" in data["detail"]
@pytest.mark.asyncio
async def test_api_token_authentication_empty_token(self, client: AsyncClient):
async def test_api_token_authentication_empty_token(self, client: AsyncClient) -> None:
"""Test authentication with empty API-TOKEN header."""
# Empty token
headers = {"API-TOKEN": ""}
@@ -325,7 +322,7 @@ class TestApiTokenEndpoints:
client: AsyncClient,
authenticated_client: AsyncClient,
authenticated_user: User,
):
) -> None:
"""Test authentication with API token for inactive user."""
# Generate API token
token_response = await authenticated_client.post(
@@ -351,7 +348,7 @@ class TestApiTokenEndpoints:
client: AsyncClient,
authenticated_client: AsyncClient,
auth_cookies: dict[str, str],
):
) -> None:
"""Test that flexible authentication prefers API token over cookie."""
# Generate API token
token_response = await authenticated_client.post(