generating openapi from backend and adding ci pipeline
This commit is contained in:
@ -1,11 +1,11 @@
|
||||
# app/main.py
|
||||
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
from pathlib import Path
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
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
|
||||
@ -68,28 +68,38 @@ app.include_router(sample.router, prefix="/samples", tags=["samples"])
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
import os
|
||||
|
||||
# Get environment from an environment variable
|
||||
environment = os.getenv('ENVIRONMENT', 'dev')
|
||||
|
||||
# Paths for SSL certificates
|
||||
cert_path = "ssl/cert.pem"
|
||||
key_path = "ssl/key.pem"
|
||||
|
||||
if environment == 'test':
|
||||
cert_path = "ssl/mx-aare-test.psi.ch.pem"
|
||||
key_path = "ssl/mx-aare-test.psi.ch.key"
|
||||
host = "0.0.0.0" # Bind to all interfaces
|
||||
# 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:
|
||||
host = "127.0.0.1" # Default for other environments
|
||||
# Default behavior: Run the FastAPI server
|
||||
import os
|
||||
|
||||
# Run the application with appropriate SSL setup
|
||||
uvicorn.run(
|
||||
app,
|
||||
host=host,
|
||||
port=8000,
|
||||
log_level="debug",
|
||||
ssl_keyfile=key_path,
|
||||
ssl_certfile=cert_path,
|
||||
)
|
||||
# Get environment from an environment variable
|
||||
environment = os.getenv('ENVIRONMENT', 'dev')
|
||||
|
||||
# Paths for SSL certificates
|
||||
cert_path = "ssl/cert.pem"
|
||||
key_path = "ssl/key.pem"
|
||||
|
||||
if environment == 'test':
|
||||
cert_path = "ssl/mx-aare-test.psi.ch.pem"
|
||||
key_path = "ssl/mx-aare-test.psi.ch.key"
|
||||
host = "0.0.0.0" # Bind to all interfaces
|
||||
else:
|
||||
host = "127.0.0.1" # Default for other environments
|
||||
|
||||
# Run the application with appropriate SSL setup
|
||||
uvicorn.run(
|
||||
app,
|
||||
host=host,
|
||||
port=8000,
|
||||
log_level="debug",
|
||||
ssl_keyfile=key_path,
|
||||
ssl_certfile=cert_path,
|
||||
)
|
30
backend/tests/test_auth.py
Normal file
30
backend/tests/test_auth.py
Normal 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]}
|
Reference in New Issue
Block a user