generating openapi from backend and adding ci pipeline

This commit is contained in:
GotthardG 2024-12-13 10:45:04 +01:00
parent 530c9efae0
commit 20f341a9a9
4 changed files with 93 additions and 27 deletions

View File

@ -1,11 +1,11 @@
# app/main.py # app/main.py
import sys
import os
import json
from pathlib import Path
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from app import ssl_heidi from app import ssl_heidi
from pathlib import Path
import os
import json
from app.routers import address, contact, proposal, dewar, shipment, puck, spreadsheet, logistics, auth, sample from app.routers import address, contact, proposal, dewar, shipment, puck, spreadsheet, logistics, auth, sample
@ -68,6 +68,16 @@ app.include_router(sample.router, prefix="/samples", tags=["samples"])
if __name__ == "__main__": if __name__ == "__main__":
import uvicorn import uvicorn
# Check if the user has passed "generate-openapi" as the first CLI argument
if len(sys.argv) > 1 and sys.argv[1] == "generate-openapi":
# Generate and save the OpenAPI schema
openapi_schema = app.openapi()
with open("openapi.json", "w") as openapi_file:
json.dump(openapi_schema, openapi_file, indent=2)
print("OpenAPI schema has been generated and saved to 'openapi.json'.")
else:
# Default behavior: Run the FastAPI server
import os import os
# Get environment from an environment variable # Get environment from an environment variable

View File

@ -0,0 +1,30 @@
# tests/test_auth.py
from fastapi.testclient import TestClient
from app.main import app
client = TestClient(app)
def test_login_success():
response = client.post("/auth/token/login", data={"username": "testuser", "password": "testpass"})
assert response.status_code == 200
assert "access_token" in response.json()
def test_login_failure():
response = client.post("/auth/token/login", data={"username": "wrong", "password": "wrongpass"})
assert response.status_code == 401
assert response.json() == {"detail": "Incorrect username or password"}
def test_protected_route():
# Step 1: Login
response = client.post("/auth/token/login", data={"username": "testuser", "password": "testpass"})
token = response.json()["access_token"]
# Step 2: Access protected route
headers = {"Authorization": f"Bearer {token}"}
response = client.get("/auth/protected-route", headers=headers)
assert response.status_code == 200
assert response.json() == {"username": "testuser", "pgroups": [20000, 20001, 20003]}

23
make_openapi_client.sh Executable file
View File

@ -0,0 +1,23 @@
#!/bin/bash
VERSION={0.1.0}
# Run whataver you need to generate FastAPI .json file
pushd backend
python -m main generate-openapi
popd
# Download OpenAPI generator
OPENAPI_VERSION=7.8.0
wget https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/${OPENAPI_VERSION}/openapi-generator-cli-${OPENAPI_VERSION}.jar -O openapi-generator-cli.jar
# Run OpenAPI generator (requires java to be installed - this is already configured on mx-aare-test)
java -jar openapi-generator-cli.jar generate -i /backend/openapi.json -o python-client/ -g python --additional-properties=packageName=aare-test,packageVersion=${VERSION}
# build python package
cd python-client
python3.11 -m venv .venv
source .venv/bin/activate
pip install twine build
python -m build
# upload the package into the package repository - this uses variables from CI pipeline, so it won't work offline
twine upload --repository-url ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/pypi dist/*

View File

@ -8,3 +8,6 @@ qrcode~=8.0
pillow~=11.0.0 pillow~=11.0.0
reportlab~=4.2.5 reportlab~=4.2.5
cryptography~=44.0.0 cryptography~=44.0.0
pytest~=7.4.1
pytest-cov~=4.1.0
httpx~=0.24.1