-
Notifications
You must be signed in to change notification settings - Fork 45
Unit tests middleware cicd #130
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
Merged
NiveditJain
merged 17 commits into
FailproofAI:main
from
namidanam:unit-tests-middleware-cicd
Aug 2, 2025
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
21aff4b
Add separated middleware tests and CI workflow
namidanam 18902ab
Update middleware tests, dependencies, and CI workflow
namidanam ddd9953
Update .github/workflows/ci.yml
namidanam 80ba9f9
Update .github/workflows/ci.yml
namidanam a0fa269
Update api-server/pyproject.toml
namidanam f596ced
Update api-server/pyproject.toml
namidanam 1aac48d
Update pyproject.toml
namidanam 55f1ec1
Update .github/workflows/ci.yml
namidanam 9e82282
Update pyproject.toml
namidanam e46b6d3
Update .github/workflows/ci.yml
namidanam ae1aa93
Update api-server/tests/test_request_id_middleware.py
namidanam 9f185e2
Update api-server/tests/test_request_id_middleware.py
namidanam 624d2ca
Update api-server/tests/test_unhandled_exceptions_middleware.py
namidanam 77d09e7
Address reviewer feedback and CI errors: Python version, error respon…
namidanam 4c6031b
Enable coverage reporting via Codecov and cleanup nits
namidanam 9a1fb6b
Enable coverage reporting via Codecov and cleanup nits remaining changes
namidanam d4a6a1d
Fix YAML structure/codecov step; cleanup pyproject formatting
namidanam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| name: Python API Server Tests | ||
|
|
||
| on: | ||
| push: | ||
| branches: [main] | ||
| pull_request: | ||
| branches: [main] | ||
|
|
||
| jobs: | ||
| test: | ||
| runs-on: ubuntu-latest | ||
| services: | ||
| redis: | ||
| image: redis:7 | ||
| ports: | ||
| - 6379:6379 | ||
| options: >- | ||
| --health-cmd "redis-cli ping" | ||
| --health-interval 10s | ||
| --health-timeout 5s | ||
| --health-retries 5 | ||
|
|
||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Set up Python | ||
| uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: '3.12' | ||
|
|
||
| - name: Install uv | ||
| uses: astral-sh/setup-uv@v2 | ||
| with: | ||
| cache: true | ||
|
|
||
| - name: Install dev dependencies with uv | ||
| working-directory: api-server | ||
| run: | | ||
| uv sync --group dev | ||
|
|
||
| - name: Run tests with pytest and coverage | ||
| working-directory: api-server | ||
| run: | | ||
| uv run pytest --cov=app --cov-report=xml --cov-report=term-missing -v --junitxml=pytest-report.xml | ||
|
|
||
| - name: Upload coverage reports to Codecov | ||
| uses: codecov/codecov-action@v5 | ||
| with: | ||
| token: ${{ secrets.CODECOV_TOKEN }} | ||
| slug: exospherehost/exospherehost | ||
| files: api-server/coverage.xml | ||
| flags: unittests | ||
| name: codecov-coverage-report | ||
| fail_ci_if_error: true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import uuid | ||
| import pytest | ||
| from fastapi import FastAPI, Request | ||
| from fastapi.responses import JSONResponse | ||
| from fastapi.testclient import TestClient | ||
| from app.middlewares.request_id_middleware import RequestIdMiddleware | ||
|
|
||
| # Minimal endpoint to test request ID logic | ||
| async def handler(request: Request): | ||
| return JSONResponse({"msg": "ok"}) | ||
|
|
||
| @pytest.fixture | ||
| def client(): | ||
| app = FastAPI() | ||
| app.add_middleware(RequestIdMiddleware) # Only the request ID middleware | ||
| app.add_api_route("/test", handler, methods=["GET"]) | ||
| return TestClient(app) | ||
|
|
||
| def test_generates_uuid_when_missing(client): | ||
| resp = client.get("/test") | ||
| assert resp.status_code == 200 | ||
| rid = resp.headers["x-exosphere-request-id"] | ||
| # Validate that a proper UUID was generated | ||
| parsed_uuid = uuid.UUID(rid) | ||
| assert str(parsed_uuid) == rid | ||
| assert resp.json() == {"msg": "ok"} | ||
|
|
||
| def test_preserves_valid_id(client): | ||
| valid_id = str(uuid.uuid4()) | ||
| resp = client.get("/test", headers={"x-exosphere-request-id": valid_id}) | ||
| assert resp.status_code == 200 | ||
| assert resp.headers["x-exosphere-request-id"] == valid_id | ||
|
|
||
| def test_replaces_invalid_id(client): | ||
| resp = client.get("/test", headers={"x-exosphere-request-id": "bad-id"}) | ||
| assert resp.status_code == 200 | ||
| new_id = resp.headers["x-exosphere-request-id"] | ||
| assert new_id != "bad-id" | ||
| # Validate that a proper UUID was generated | ||
| parsed_uuid = uuid.UUID(new_id) | ||
| assert str(parsed_uuid) == new_id |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import pytest | ||
| from fastapi import FastAPI, Request | ||
| from fastapi.responses import JSONResponse | ||
| from fastapi.testclient import TestClient | ||
| from app.middlewares.unhandled_exceptions_middleware import UnhandledExceptionsMiddleware | ||
|
|
||
| # Exception handlers to trigger different exceptions | ||
|
|
||
| async def fail(request: Request): | ||
| raise RuntimeError("boom") | ||
|
|
||
| async def fail_value_error(request: Request): | ||
| raise ValueError("Invalid value test") | ||
|
|
||
| async def fail_key_error(request: Request): | ||
| raise KeyError("Missing key test") | ||
|
|
||
| async def ok(request: Request): | ||
| return JSONResponse({"ok": True}) | ||
|
|
||
| @pytest.fixture | ||
| def client(): | ||
| app = FastAPI() | ||
| app.add_middleware(UnhandledExceptionsMiddleware) | ||
| app.add_api_route("/fail", fail, methods=["GET"]) | ||
| app.add_api_route("/fail_value_error", fail_value_error, methods=["GET"]) | ||
| app.add_api_route("/fail_key_error", fail_key_error, methods=["GET"]) | ||
| app.add_api_route("/ok", ok, methods=["GET"]) | ||
| return TestClient(app) | ||
|
|
||
| ERROR_RESPONSE = { | ||
| "success": False, | ||
| "detail": "internal server error, please reach out to support team at nivedit@exosphere.host" | ||
| } | ||
|
|
||
| def test_runtime_error_returns_expected_json(client): | ||
| resp = client.get("/fail") | ||
| assert resp.status_code == 500 | ||
| assert resp.json() == ERROR_RESPONSE | ||
|
|
||
| def test_value_error_returns_expected_json(client): | ||
| resp = client.get("/fail_value_error") | ||
| assert resp.status_code == 500 | ||
| assert resp.json() == ERROR_RESPONSE | ||
|
|
||
| def test_key_error_returns_expected_json(client): | ||
| resp = client.get("/fail_key_error") | ||
| assert resp.status_code == 500 | ||
| assert resp.json() == ERROR_RESPONSE | ||
|
|
||
| def test_normal_request_passes_through(client): | ||
| resp = client.get("/ok") | ||
| assert resp.status_code == 200 | ||
| assert resp.json() == {"ok": True} | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.