a159eb2b8b
Punto 7 del documento modifiche del 28/07, terza richiesta di Menoncin.
GET /api/stations ora usa StationWithRecipesResponse, che era gia' definita nello
schema senza che nessun endpoint la usasse. Non costa query in piu':
Station.assignments e' gia' selectin-loaded. In lista compaiono le sole ricette
attive, coerente con cio' che l'operatore vede davvero a quella stazione.
DELETE /api/stations/{id}/recipes azzera le assegnazioni di una stazione senza
eliminarla, cosi' si riassegna da capo. E' idempotente: azzerare una stazione gia'
vuota risponde removed=0, non 404. In tabella e' un pulsante per riga, con
conferma perche' l'azione e' distruttiva.
Il cambio stazione al volo (?station=CODE, override in sessione) serve al
collaudo: senza, provare N stazioni richiede N PC. E' dietro il flag
STATION_SWITCH_ENABLED, default 0. Non e' legato al ruolo admin perche'
/measure/select richiede MeasurementTec, e gatearlo su is_admin avrebbe escluso
proprio chi fa il collaudo. In fabbrica l'identita' della stazione viene
dall'installazione locale, e misurare contro le ricette di un'altra stazione
romperebbe in silenzio la tracciabilita': per questo il codice richiesto e'
validato prima di essere memorizzato - un refuso lascia l'operatore sulla
stazione configurata con un messaggio, invece di incastrarlo su una stazione
inesistente - e quando l'override e' attivo la pagina lo dichiara, con il codice
configurato accanto e un link per ripristinare.
Le traduzioni: pybabel update aveva indovinato dieci voci da stringhe simili
marcandole fuzzy, in entrambi i cataloghi. "Azzera Stazione" era diventato "Crea
Stazione", l'opposto, su un dialogo distruttivo. Non si vedeva perche' la
compilazione salta le fuzzy e ricade sul sorgente italiano, ma sarebbe emerso al
primo build con --use-fuzzy. Tradotte per esteso le 17 stringhe nuove in IT ed EN
e tolti i flag.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
179 lines
6.8 KiB
Python
179 lines
6.8 KiB
Python
"""Tests for the admin Stations management UI and proxy endpoints."""
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_admin_api():
|
|
"""Patch api_client used by the admin blueprint."""
|
|
mock = MagicMock()
|
|
with patch("blueprints.admin.api_client", mock):
|
|
yield mock
|
|
|
|
|
|
def test_station_list_page_requires_admin(logged_in_client, mock_admin_api):
|
|
mock_admin_api.get.return_value = []
|
|
resp = logged_in_client.get("/admin/stations")
|
|
assert resp.status_code == 200
|
|
assert b"Gestione Stazioni" in resp.data or b"stations" in resp.data.lower()
|
|
|
|
|
|
def test_station_list_page_calls_correct_endpoints(logged_in_client, mock_admin_api):
|
|
mock_admin_api.get.side_effect = [
|
|
[{"id": 1, "code": "ST-001", "name": "Linea A", "location": None,
|
|
"notes": None, "active": True, "created_by": 1, "created_at": "2026-04-25T10:00:00"}],
|
|
[{"id": 1, "code": "REC-001", "name": "Test Recipe", "active": True}],
|
|
]
|
|
resp = logged_in_client.get("/admin/stations")
|
|
assert resp.status_code == 200
|
|
calls = [c.args[0] for c in mock_admin_api.get.call_args_list]
|
|
assert "/api/stations" in calls
|
|
assert "/api/recipes" in calls
|
|
|
|
|
|
def test_create_station_proxy(logged_in_client, mock_admin_api):
|
|
mock_admin_api.post.return_value = {
|
|
"id": 5, "code": "ST-005", "name": "Nuova",
|
|
"location": None, "notes": None, "active": True,
|
|
"created_by": 1, "created_at": "2026-04-25T10:00:00",
|
|
}
|
|
resp = logged_in_client.post(
|
|
"/admin/api/stations",
|
|
json={"code": "ST-005", "name": "Nuova", "active": True},
|
|
)
|
|
assert resp.status_code == 201
|
|
body = resp.get_json()
|
|
assert body["code"] == "ST-005"
|
|
mock_admin_api.post.assert_called_once_with(
|
|
"/api/stations",
|
|
data={"code": "ST-005", "name": "Nuova", "active": True},
|
|
)
|
|
|
|
|
|
def test_create_station_propagates_error(logged_in_client, mock_admin_api):
|
|
mock_admin_api.post.return_value = {
|
|
"error": True, "status_code": 409, "detail": "code already exists",
|
|
}
|
|
resp = logged_in_client.post(
|
|
"/admin/api/stations", json={"code": "ST-001", "name": "x"},
|
|
)
|
|
assert resp.status_code == 409
|
|
assert resp.get_json()["detail"] == "code already exists"
|
|
|
|
|
|
def test_update_station_proxy(logged_in_client, mock_admin_api):
|
|
mock_admin_api.put.return_value = {
|
|
"id": 3, "code": "ST-003", "name": "Aggiornata",
|
|
"location": "Reparto B", "notes": None, "active": False,
|
|
"created_by": 1, "created_at": "2026-04-25T10:00:00",
|
|
}
|
|
resp = logged_in_client.put(
|
|
"/admin/api/stations/3",
|
|
json={"name": "Aggiornata", "location": "Reparto B", "active": False},
|
|
)
|
|
assert resp.status_code == 200
|
|
mock_admin_api.put.assert_called_once_with(
|
|
"/api/stations/3",
|
|
data={"name": "Aggiornata", "location": "Reparto B", "active": False},
|
|
)
|
|
|
|
|
|
def test_delete_station_proxy(logged_in_client, mock_admin_api):
|
|
mock_admin_api.delete.return_value = {}
|
|
resp = logged_in_client.delete("/admin/api/stations/7")
|
|
assert resp.status_code == 200
|
|
assert resp.get_json() == {"deleted": True}
|
|
mock_admin_api.delete.assert_called_once_with("/api/stations/7")
|
|
|
|
|
|
def test_assign_recipe_proxy(logged_in_client, mock_admin_api):
|
|
mock_admin_api.post.return_value = {
|
|
"id": 1, "station_id": 2, "recipe_id": 10,
|
|
"assigned_by": 1, "assigned_at": "2026-04-25T10:00:00",
|
|
}
|
|
resp = logged_in_client.post(
|
|
"/admin/api/stations/2/recipes", json={"recipe_id": 10},
|
|
)
|
|
assert resp.status_code == 201
|
|
mock_admin_api.post.assert_called_once_with(
|
|
"/api/stations/2/recipes", data={"recipe_id": 10},
|
|
)
|
|
|
|
|
|
def test_unassign_recipe_proxy(logged_in_client, mock_admin_api):
|
|
mock_admin_api.delete.return_value = {}
|
|
resp = logged_in_client.delete("/admin/api/stations/2/recipes/10")
|
|
assert resp.status_code == 200
|
|
mock_admin_api.delete.assert_called_once_with("/api/stations/2/recipes/10")
|
|
|
|
|
|
def test_list_station_recipes_proxy(logged_in_client, mock_admin_api):
|
|
mock_admin_api.get.return_value = [
|
|
{"id": 1, "code": "REC-001", "name": "R1", "active": True},
|
|
]
|
|
resp = logged_in_client.get("/admin/api/stations/2/recipes")
|
|
assert resp.status_code == 200
|
|
assert resp.get_json() == [{"id": 1, "code": "REC-001", "name": "R1", "active": True}]
|
|
mock_admin_api.get.assert_called_once_with("/api/stations/2/recipes")
|
|
|
|
|
|
def test_non_admin_cannot_access(client, mock_admin_api):
|
|
"""Non-admin user gets redirected away from station management."""
|
|
with client.session_transaction() as sess:
|
|
sess["api_key"] = "test-key"
|
|
sess["user"] = {
|
|
"id": 2,
|
|
"username": "operator",
|
|
"roles": ["MeasurementTec"],
|
|
"is_admin": False,
|
|
"active": True,
|
|
}
|
|
sess["user_id"] = 2
|
|
resp = client.get("/admin/stations", follow_redirects=False)
|
|
assert resp.status_code in (301, 302)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Per-row station reset (point 7)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_reset_station_proxy(logged_in_client, mock_admin_api):
|
|
mock_admin_api.delete.return_value = {"station_id": 3, "removed": 2}
|
|
resp = logged_in_client.delete("/admin/api/stations/3/recipes")
|
|
assert resp.status_code == 200
|
|
assert resp.get_json() == {"station_id": 3, "removed": 2}
|
|
mock_admin_api.delete.assert_called_once_with("/api/stations/3/recipes")
|
|
|
|
|
|
def test_reset_station_propagates_error(logged_in_client, mock_admin_api):
|
|
mock_admin_api.delete.return_value = {
|
|
"error": True, "status_code": 404, "detail": "Station not found",
|
|
}
|
|
resp = logged_in_client.delete("/admin/api/stations/999/recipes")
|
|
assert resp.status_code == 404
|
|
|
|
|
|
def test_reset_station_distinct_from_single_unassign(logged_in_client, mock_admin_api):
|
|
"""The collection route must not swallow the per-recipe delete."""
|
|
mock_admin_api.delete.return_value = {}
|
|
logged_in_client.delete("/admin/api/stations/3/recipes/7")
|
|
mock_admin_api.delete.assert_called_once_with("/api/stations/3/recipes/7")
|
|
|
|
|
|
def test_station_list_page_renders_assigned_recipes(logged_in_client, mock_admin_api):
|
|
"""Recipes assigned to a station reach the page so the row can show them."""
|
|
mock_admin_api.get.side_effect = [
|
|
[{
|
|
"id": 1, "code": "ST-001", "name": "Linea A", "location": "Reparto A",
|
|
"notes": None, "active": True, "created_by": 1,
|
|
"created_at": "2026-04-25T10:00:00",
|
|
"recipes": [{"id": 9, "code": "REC-ABC", "name": "Perno", "active": True}],
|
|
}],
|
|
[{"id": 9, "code": "REC-ABC", "name": "Perno", "active": True}],
|
|
]
|
|
resp = logged_in_client.get("/admin/stations")
|
|
assert resp.status_code == 200
|
|
assert b"REC-ABC" in resp.data
|