diff --git a/client/blueprints/measure.py b/client/blueprints/measure.py index b0eae1d..e7466c2 100644 --- a/client/blueprints/measure.py +++ b/client/blueprints/measure.py @@ -22,8 +22,18 @@ measure_bp = Blueprint("measure", __name__) @role_required("MeasurementTec") def select_recipe(): """Recipe selection page with search and barcode support.""" - # Load recipes from API - resp = api_client.get("/api/recipes", params={"per_page": 100}) + # Fail-fast if STATION_CODE is not configured + if not Config.STATION_CODE: + return render_template("errors/station_not_configured.html"), 503 + + # Load recipes filtered by station + try: + resp = api_client.get_station_recipes(Config.STATION_CODE) + except Exception as e: + return render_template( + "errors/station_not_configured.html", error=str(e), + ), 502 + if isinstance(resp, dict) and resp.get("error"): flash( _("Errore nel caricamento delle ricette: %(detail)s", @@ -43,6 +53,7 @@ def select_recipe(): return render_template( "measure/select_recipe.html", recipes=recipes, + station_code=Config.STATION_CODE, auto_recipe_code=auto_recipe_code, auto_lot=auto_lot, auto_serial=auto_serial, diff --git a/client/templates/errors/station_not_configured.html b/client/templates/errors/station_not_configured.html new file mode 100644 index 0000000..d19e6d8 --- /dev/null +++ b/client/templates/errors/station_not_configured.html @@ -0,0 +1,33 @@ +{% extends "base.html" %} +{% block title %}{{ _('Stazione non configurata') }} — TieMeasureFlow{% endblock %} + +{% block content %} +
+ {{ _('Questo client non ha impostato la variabile di ambiente STATION_CODE.') }} +
+ ++ {{ _('Contattare il responsabile IT: il file .env del container deve contenere STATION_CODE con il codice della stazione assegnata.') }} +
+ + {% if error %} +{{ error }}
+ {% endif %}
+ {{ _('Scegli la ricetta di misura da eseguire') }}
++ {{ _('Stazione') }}: {{ station_code }} +
diff --git a/client/tests/test_measure.py b/client/tests/test_measure.py index 3309737..9992c4b 100644 --- a/client/tests/test_measure.py +++ b/client/tests/test_measure.py @@ -8,15 +8,18 @@ import pytest class TestSelectRecipe: """GET /measure/select tests.""" - def test_select_recipe_renders(self, logged_in_client, mock_api_client): + def test_select_recipe_renders(self, logged_in_client, mock_api_client, monkeypatch): """Recipe selection page renders for MeasurementTec role.""" - mock_api_client.get.return_value = { - "items": [ - {"id": 1, "code": "REC-001", "name": "Test Recipe"}, - ], - "total": 1, - "pages": 1, - } + monkeypatch.setenv("STATION_CODE", "ST-TEST") + import config + import importlib + importlib.reload(config) + import blueprints.measure + importlib.reload(blueprints.measure) + + mock_api_client.get_station_recipes.return_value = [ + {"id": 1, "code": "REC-001", "name": "Test Recipe"}, + ] resp = logged_in_client.get("/measure/select") assert resp.status_code == 200 diff --git a/client/tests/test_measure_station_filter.py b/client/tests/test_measure_station_filter.py new file mode 100644 index 0000000..59efe10 --- /dev/null +++ b/client/tests/test_measure_station_filter.py @@ -0,0 +1,37 @@ +"""Verify that /measure/select reads STATION_CODE and filters recipes via the server.""" +import importlib +from unittest.mock import patch, MagicMock + + +def _reload_measure(monkeypatch, station_code=None): + """Reload config and measure module under the given STATION_CODE env.""" + if station_code is None: + monkeypatch.delenv("STATION_CODE", raising=False) + else: + monkeypatch.setenv("STATION_CODE", station_code) + import config + importlib.reload(config) + import blueprints.measure + importlib.reload(blueprints.measure) + + +def test_select_recipe_calls_station_endpoint(logged_in_client, monkeypatch): + _reload_measure(monkeypatch, station_code="ST-TEST") + from blueprints import measure as measure_bp_mod + with patch.object(measure_bp_mod, "api_client") as mock_api: + mock_api.get_station_recipes.return_value = [ + {"id": 1, "code": "R1", "name": "Recipe 1", "active": True}, + ] + resp = logged_in_client.get("/measure/select") + assert resp.status_code == 200 + mock_api.get_station_recipes.assert_called_once() + args, kwargs = mock_api.get_station_recipes.call_args + assert args[0] == "ST-TEST" or kwargs.get("station_code") == "ST-TEST" + + +def test_select_recipe_without_station_code_shows_error(logged_in_client, monkeypatch): + _reload_measure(monkeypatch, station_code=None) + resp = logged_in_client.get("/measure/select") + assert resp.status_code == 503 + body = resp.data.lower() + assert b"station_code" in body or b"stazione" in body