formatting fixes
This commit is contained in:
@@ -29,6 +29,7 @@ async def lifespan(app_instance: FastAPI) -> AsyncIterator[None]:
|
|||||||
yield
|
yield
|
||||||
# Shutdown: Add cleanup code here if needed
|
# Shutdown: Add cleanup code here if needed
|
||||||
|
|
||||||
|
|
||||||
# Instantiate app
|
# Instantiate app
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
docs_url=docs_url,
|
docs_url=docs_url,
|
||||||
|
|||||||
@@ -58,9 +58,7 @@ def _initialize_token_repository() -> TokenRepositoryInterface:
|
|||||||
f"Ensure Redis is running and accessible. Error: {e}"
|
f"Ensure Redis is running and accessible. Error: {e}"
|
||||||
) from e
|
) from e
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise RuntimeError(
|
raise RuntimeError(f"Failed to initialize Redis token repository: {e}") from e
|
||||||
f"Failed to initialize Redis token repository: {e}"
|
|
||||||
) from e
|
|
||||||
|
|
||||||
|
|
||||||
def get_token_repository() -> TokenRepositoryInterface:
|
def get_token_repository() -> TokenRepositoryInterface:
|
||||||
|
|||||||
@@ -25,16 +25,14 @@ def test_api_root_with_auth(app_test_client: TestClient) -> None:
|
|||||||
"""Test authenticated API root endpoint returns expected message."""
|
"""Test authenticated API root endpoint returns expected message."""
|
||||||
# First login to get token
|
# First login to get token
|
||||||
login_response = app_test_client.post(
|
login_response = app_test_client.post(
|
||||||
"/api/login",
|
"/api/login", json={"username": "admin", "password": "password"}
|
||||||
json={"username": "admin", "password": "password"}
|
|
||||||
)
|
)
|
||||||
assert login_response.status_code == 200
|
assert login_response.status_code == 200
|
||||||
token = login_response.json()["access_token"]
|
token = login_response.json()["access_token"]
|
||||||
|
|
||||||
# Then access API root with token
|
# Then access API root with token
|
||||||
response = app_test_client.get(
|
response = app_test_client.get(
|
||||||
"/api/",
|
"/api/", headers={"Authorization": f"Bearer {token}"}
|
||||||
headers={"Authorization": f"Bearer {token}"}
|
|
||||||
)
|
)
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert response.json() == {"message": "Hello World", "authenticated": True}
|
assert response.json() == {"message": "Hello World", "authenticated": True}
|
||||||
@@ -91,14 +89,12 @@ def test_api_root_endpoint_content_type(app_test_client: TestClient) -> None:
|
|||||||
"""Test API root endpoint returns JSON content type."""
|
"""Test API root endpoint returns JSON content type."""
|
||||||
# Login first to get token
|
# Login first to get token
|
||||||
login_response = app_test_client.post(
|
login_response = app_test_client.post(
|
||||||
"/api/login",
|
"/api/login", json={"username": "admin", "password": "password"}
|
||||||
json={"username": "admin", "password": "password"}
|
|
||||||
)
|
)
|
||||||
token = login_response.json()["access_token"]
|
token = login_response.json()["access_token"]
|
||||||
|
|
||||||
response = app_test_client.get(
|
response = app_test_client.get(
|
||||||
"/api/",
|
"/api/", headers={"Authorization": f"Bearer {token}"}
|
||||||
headers={"Authorization": f"Bearer {token}"}
|
|
||||||
)
|
)
|
||||||
assert response.headers["content-type"] == "application/json"
|
assert response.headers["content-type"] == "application/json"
|
||||||
|
|
||||||
@@ -121,8 +117,7 @@ def test_ready_endpoint_content_type(app_test_client: TestClient) -> None:
|
|||||||
def test_login_success(app_test_client: TestClient) -> None:
|
def test_login_success(app_test_client: TestClient) -> None:
|
||||||
"""Test successful login returns access token."""
|
"""Test successful login returns access token."""
|
||||||
response = app_test_client.post(
|
response = app_test_client.post(
|
||||||
"/api/login",
|
"/api/login", json={"username": "admin", "password": "password"}
|
||||||
json={"username": "admin", "password": "password"}
|
|
||||||
)
|
)
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
data = response.json()
|
data = response.json()
|
||||||
@@ -134,8 +129,7 @@ def test_login_success(app_test_client: TestClient) -> None:
|
|||||||
def test_login_invalid_credentials(app_test_client: TestClient) -> None:
|
def test_login_invalid_credentials(app_test_client: TestClient) -> None:
|
||||||
"""Test login with invalid credentials returns 401."""
|
"""Test login with invalid credentials returns 401."""
|
||||||
response = app_test_client.post(
|
response = app_test_client.post(
|
||||||
"/api/login",
|
"/api/login", json={"username": "admin", "password": "wrongpassword"}
|
||||||
json={"username": "admin", "password": "wrongpassword"}
|
|
||||||
)
|
)
|
||||||
assert response.status_code == 401
|
assert response.status_code == 401
|
||||||
assert response.json() == {"detail": "Invalid username or password"}
|
assert response.json() == {"detail": "Invalid username or password"}
|
||||||
@@ -144,10 +138,7 @@ def test_login_invalid_credentials(app_test_client: TestClient) -> None:
|
|||||||
@pytest.mark.integration
|
@pytest.mark.integration
|
||||||
def test_login_missing_fields(app_test_client: TestClient) -> None:
|
def test_login_missing_fields(app_test_client: TestClient) -> None:
|
||||||
"""Test login with missing fields returns validation error."""
|
"""Test login with missing fields returns validation error."""
|
||||||
response = app_test_client.post(
|
response = app_test_client.post("/api/login", json={"username": "admin"})
|
||||||
"/api/login",
|
|
||||||
json={"username": "admin"}
|
|
||||||
)
|
|
||||||
assert response.status_code == 422
|
assert response.status_code == 422
|
||||||
|
|
||||||
|
|
||||||
@@ -156,15 +147,13 @@ def test_logout(app_test_client: TestClient) -> None:
|
|||||||
"""Test logout endpoint."""
|
"""Test logout endpoint."""
|
||||||
# Login first
|
# Login first
|
||||||
login_response = app_test_client.post(
|
login_response = app_test_client.post(
|
||||||
"/api/login",
|
"/api/login", json={"username": "admin", "password": "password"}
|
||||||
json={"username": "admin", "password": "password"}
|
|
||||||
)
|
)
|
||||||
token = login_response.json()["access_token"]
|
token = login_response.json()["access_token"]
|
||||||
|
|
||||||
# Logout
|
# Logout
|
||||||
response = app_test_client.post(
|
response = app_test_client.post(
|
||||||
"/api/logout",
|
"/api/logout", headers={"Authorization": f"Bearer {token}"}
|
||||||
headers={"Authorization": f"Bearer {token}"}
|
|
||||||
)
|
)
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert response.json() == {"message": "Logged out successfully"}
|
assert response.json() == {"message": "Logged out successfully"}
|
||||||
|
|||||||
Reference in New Issue
Block a user