From 4afa479d487289c6e80369542b1404e1b35548cc Mon Sep 17 00:00:00 2001 From: Kegan Maher Date: Thu, 19 Dec 2024 00:09:09 +0000 Subject: [PATCH] feat(metadata): endpoint returns info as JSON --- eligibility_server/app.py | 12 ++++++++++++ tests/test_app.py | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/eligibility_server/app.py b/eligibility_server/app.py index 3b017c14..022a4464 100644 --- a/eligibility_server/app.py +++ b/eligibility_server/app.py @@ -9,6 +9,7 @@ from flask_restful import Api from eligibility_server import __version__, db, sentry +from eligibility_server.db.models import Metadata from eligibility_server.keypair import get_server_public_key from eligibility_server.settings import Configuration from eligibility_server.verify import Verify @@ -53,6 +54,17 @@ def healthcheck(): return TextResponse("Healthy") +@app.route("/metadata") +def metadata(): + app.logger.info("Request metadata") + + md = Metadata.query.first() + return { + "app": {"version": __version__}, + "db": {"timestamp": md.timestamp, "users": md.users, "eligibility": md.eligibility}, + } + + @app.route("/publickey") def publickey(): app.logger.info("Request public key") diff --git a/tests/test_app.py b/tests/test_app.py index 105d6190..ae21665a 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -4,6 +4,7 @@ import re +from eligibility_server import __version__ from eligibility_server.settings import APP_NAME from eligibility_server.keypair import get_server_public_key @@ -20,6 +21,23 @@ def test_healthcheck(client): assert "Strict-Transport-Security" in response.headers +def test_metadata(client): + response = client.get("metadata") + assert response.status_code == 200 + assert response.mimetype == "application/json" + assert "Strict-Transport-Security" in response.headers + + data = response.json + + assert "app" in data + assert data["app"]["version"] == __version__ + assert "db" in data + assert isinstance(data["db"]["timestamp"], str) + assert isinstance(data["db"]["users"], int) + assert isinstance(data["db"]["eligibility"], list) + assert len(data["db"]["eligibility"]) > 0 + + def test_404(client): response = client.get("/random")