diff --git a/app/models/usuario.py b/app/models/usuario.py index 3ba594b..d432a3a 100644 --- a/app/models/usuario.py +++ b/app/models/usuario.py @@ -20,6 +20,14 @@ class Usuario(ColumnasBaseMixin, TenantMixin, Base): usuario: Mapped[str] = mapped_column(String(64), nullable=False) rol: Mapped[Rol] = mapped_column( - Enum(Rol, name="rol", native_enum=False, length=20), nullable=False + # Se persiste el valor (`analista`), no el nombre del miembro (`ANALISTA`). + Enum( + Rol, + name="rol", + native_enum=False, + length=20, + values_callable=lambda rol: [miembro.value for miembro in rol], + ), + nullable=False, ) hash: Mapped[str] = mapped_column(String(255), nullable=False) diff --git a/tests/test_auth.py b/tests/test_auth.py index f037675..fb293ac 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -291,6 +291,19 @@ def test_sembrar_es_idempotente_y_actualiza_la_contrasena(fabrica: sessionmaker[ assert verificar_contrasena("clave-nueva", oficial.hash) +def test_el_rol_se_persiste_en_minusculas(fabrica: sessionmaker[Session]) -> None: + with fabrica() as sesion: + crudos = sesion.execute(sa.text("SELECT usuario, rol FROM usuario ORDER BY id")).all() + assert [tuple(fila) for fila in crudos] == [ + ("analista", "analista"), + ("oficial", "oficial"), + ("auditor", "auditor"), + ] + assert sesion.scalar(sa.select(Usuario.rol).where(Usuario.usuario == "oficial")) is ( + Rol.OFICIAL + ) + + def test_sembrar_omite_al_usuario_sin_contrasena_y_no_la_filtra( caplog: pytest.LogCaptureFixture, ) -> None: