feat: CAT-001 Árbol N-ario de Rubros #30

Merged
dmolinari merged 12 commits from feature/CAT-001 into main 2026-04-19 10:49:37 +00:00
7 changed files with 118 additions and 90 deletions
Showing only changes of commit 389dda6e5e - Show all commits

View File

@@ -49,8 +49,9 @@ public class AuthControllerTests
Assert.Equal(JsonValueKind.Array, permisos.ValueKind); Assert.Equal(JsonValueKind.Array, permisos.ValueKind);
// V011 (ADM-001) adds 'administracion:secciones:gestionar' → 22 // V011 (ADM-001) adds 'administracion:secciones:gestionar' → 22
// V013 (ADM-008) adds 'administracion:puntos_de_venta:gestionar' → 23 // V013 (ADM-008) adds 'administracion:puntos_de_venta:gestionar' → 23
// V014 (ADM-009) adds 'administracion:fiscal:gestionar' → 24 total // V014 (ADM-009) adds 'administracion:fiscal:gestionar' → 24
Assert.Equal(24, permisos.GetArrayLength()); // V016 (CAT-001) adds 'catalogo:rubros:gestionar' → 25 total
Assert.Equal(25, permisos.GetArrayLength());
} }
// Scenario: invalid credentials return 401 with opaque error // Scenario: invalid credentials return 401 with opaque error

View File

@@ -129,7 +129,7 @@ public sealed class PermisosEndpointTests : IAsyncLifetime
// ── GET /api/v1/permisos — catalog ─────────────────────────────────────── // ── GET /api/v1/permisos — catalog ───────────────────────────────────────
[Fact] [Fact]
public async Task GetPermisos_WithAdmin_Returns200With24Items() public async Task GetPermisos_WithAdmin_Returns200With25Items()
{ {
var token = await GetBearerTokenAsync(AdminUsername, AdminPassword); var token = await GetBearerTokenAsync(AdminUsername, AdminPassword);
using var req = BuildRequest(HttpMethod.Get, "/api/v1/permisos", bearerToken: token); using var req = BuildRequest(HttpMethod.Get, "/api/v1/permisos", bearerToken: token);
@@ -139,8 +139,9 @@ public sealed class PermisosEndpointTests : IAsyncLifetime
var list = await resp.Content.ReadFromJsonAsync<JsonElement>(); var list = await resp.Content.ReadFromJsonAsync<JsonElement>();
// V011 (ADM-001) adds 'administracion:secciones:gestionar' → 22 // V011 (ADM-001) adds 'administracion:secciones:gestionar' → 22
// V013 (ADM-008) adds 'administracion:puntos_de_venta:gestionar' → 23 // V013 (ADM-008) adds 'administracion:puntos_de_venta:gestionar' → 23
// V014 (ADM-009) adds 'administracion:fiscal:gestionar' → 24 total // V014 (ADM-009) adds 'administracion:fiscal:gestionar' → 24
Assert.Equal(24, list.GetArrayLength()); // V016 (CAT-001) adds 'catalogo:rubros:gestionar' → 25 total
Assert.Equal(25, list.GetArrayLength());
} }
[Fact] [Fact]
@@ -183,7 +184,7 @@ public sealed class PermisosEndpointTests : IAsyncLifetime
// ── GET /api/v1/roles/{codigo}/permisos ────────────────────────────────── // ── GET /api/v1/roles/{codigo}/permisos ──────────────────────────────────
[Fact] [Fact]
public async Task GetRolPermisos_AdminRol_Returns200With24Items() public async Task GetRolPermisos_AdminRol_Returns200With25Items()
{ {
var token = await GetBearerTokenAsync(AdminUsername, AdminPassword); var token = await GetBearerTokenAsync(AdminUsername, AdminPassword);
using var req = BuildRequest(HttpMethod.Get, "/api/v1/roles/admin/permisos", bearerToken: token); using var req = BuildRequest(HttpMethod.Get, "/api/v1/roles/admin/permisos", bearerToken: token);
@@ -193,8 +194,9 @@ public sealed class PermisosEndpointTests : IAsyncLifetime
var list = await resp.Content.ReadFromJsonAsync<JsonElement>(); var list = await resp.Content.ReadFromJsonAsync<JsonElement>();
// V011 (ADM-001) adds 'administracion:secciones:gestionar' → 22 // V011 (ADM-001) adds 'administracion:secciones:gestionar' → 22
// V013 (ADM-008) adds 'administracion:puntos_de_venta:gestionar' → 23 // V013 (ADM-008) adds 'administracion:puntos_de_venta:gestionar' → 23
// V014 (ADM-009) adds 'administracion:fiscal:gestionar' → 24 total // V014 (ADM-009) adds 'administracion:fiscal:gestionar' → 24
Assert.Equal(24, list.GetArrayLength()); // V016 (CAT-001) adds 'catalogo:rubros:gestionar' → 25 total
Assert.Equal(25, list.GetArrayLength());
} }
[Fact] [Fact]

View File

@@ -17,8 +17,7 @@ namespace SIGCM2.Api.Tests.Rubros;
[Collection("ApiIntegration")] [Collection("ApiIntegration")]
public sealed class RubrosControllerTests : IAsyncLifetime public sealed class RubrosControllerTests : IAsyncLifetime
{ {
private const string TestConnectionString = private const string TestConnectionString = TestConnectionStrings.ApiTestDb;
"Server=TECNICA3;Database=SIGCM2_Test;User Id=desarrollo;Password=desarrollo2026;TrustServerCertificate=True;";
private const string ReadEndpoint = "/api/v1/rubros"; private const string ReadEndpoint = "/api/v1/rubros";
private const string AdminEndpoint = "/api/v1/admin/rubros"; private const string AdminEndpoint = "/api/v1/admin/rubros";

View File

@@ -73,15 +73,16 @@ public class PermisoRepositoryTests : IAsyncLifetime
// ── ListAsync ──────────────────────────────────────────────────────────── // ── ListAsync ────────────────────────────────────────────────────────────
[Fact] [Fact]
public async Task ListAsync_Returns23CanonicalSeeds() public async Task ListAsync_Returns25CanonicalSeeds()
{ {
var list = await _repository.ListAsync(); var list = await _repository.ListAsync();
// V005 seeds 18 canonical permisos + V007 (UDT-006) adds 3 admin permisos // V005 seeds 18 canonical permisos + V007 (UDT-006) adds 3 admin permisos
// + V011 (ADM-001) adds 'administracion:secciones:gestionar' // + V011 (ADM-001) adds 'administracion:secciones:gestionar'
// + V013 (ADM-008) adds 'administracion:puntos_de_venta:gestionar' // + V013 (ADM-008) adds 'administracion:puntos_de_venta:gestionar'
// + V014 (ADM-009) adds 'administracion:fiscal:gestionar' = 24 total // + V014 (ADM-009) adds 'administracion:fiscal:gestionar'
Assert.Equal(24, list.Count); // + V016 (CAT-001) adds 'catalogo:rubros:gestionar' = 25 total
Assert.Equal(25, list.Count);
} }
[Fact] [Fact]

View File

@@ -173,15 +173,16 @@ public class RolPermisoRepositoryTests : IAsyncLifetime
// ── GetByRolCodigoAsync ────────────────────────────────────────────────── // ── GetByRolCodigoAsync ──────────────────────────────────────────────────
[Fact] [Fact]
public async Task GetByRolCodigoAsync_Admin_Returns23Permisos() public async Task GetByRolCodigoAsync_Admin_Returns25Permisos()
{ {
// admin has 18 permisos from V006 + 3 new admin permisos from V007 (UDT-006) // admin has 18 permisos from V006 + 3 new admin permisos from V007 (UDT-006)
// + 1 from V011 (ADM-001): 'administracion:secciones:gestionar' // + 1 from V011 (ADM-001): 'administracion:secciones:gestionar'
// + 1 from V013 (ADM-008): 'administracion:puntos_de_venta:gestionar' // + 1 from V013 (ADM-008): 'administracion:puntos_de_venta:gestionar'
// + 1 from V014 (ADM-009): 'administracion:fiscal:gestionar' = 24 total // + 1 from V014 (ADM-009): 'administracion:fiscal:gestionar'
// + 1 from V016 (CAT-001): 'catalogo:rubros:gestionar' = 25 total
var permisos = await _repository.GetByRolCodigoAsync("admin"); var permisos = await _repository.GetByRolCodigoAsync("admin");
Assert.Equal(24, permisos.Count); Assert.Equal(25, permisos.Count);
} }
[Fact] [Fact]

View File

@@ -1,73 +1,37 @@
using Dapper; using Dapper;
using Microsoft.Data.SqlClient;
using Respawn;
using SIGCM2.Domain.Entities; using SIGCM2.Domain.Entities;
using SIGCM2.Infrastructure.Persistence; using SIGCM2.Infrastructure.Persistence;
using SIGCM2.TestSupport;
namespace SIGCM2.Application.Tests.Rubros; namespace SIGCM2.Application.Tests.Rubros;
/// <summary> /// <summary>
/// Integration tests for RubroRepository against SIGCM2_Test. /// Integration tests for RubroRepository against SIGCM2_Test_App.
/// TDD: RED written before implementation, GREEN after RubroRepository was created. /// Uses shared SqlTestFixture via [Collection("Database")] — fixture maneja Respawn + seeds.
/// Temporal: after UpdateAsync, dbo.Rubro_History MUST have ≥1 row for that Id. /// Temporal: after UpdateAsync, dbo.Rubro_History MUST have ≥1 row for that Id.
/// </summary> /// </summary>
[Collection("Database")] [Collection("Database")]
public class RubroRepositoryTests : IAsyncLifetime public class RubroRepositoryTests : IAsyncLifetime
{ {
private const string ConnectionString = private readonly SqlTestFixture _db;
"Server=TECNICA3;Database=SIGCM2_Test;User Id=desarrollo;Password=desarrollo2026;TrustServerCertificate=True;";
private SqlConnection _connection = null!;
private Respawner _respawner = null!;
private RubroRepository _repository = null!; private RubroRepository _repository = null!;
private TimeProvider _timeProvider = null!; private TimeProvider _timeProvider = null!;
public RubroRepositoryTests(SqlTestFixture db)
{
_db = db;
}
public async Task InitializeAsync() public async Task InitializeAsync()
{ {
_connection = new SqlConnection(ConnectionString); await _db.ResetAndSeedAsync();
await _connection.OpenAsync();
_respawner = await Respawner.CreateAsync(_connection, new RespawnerOptions var factory = new SqlConnectionFactory(TestConnectionStrings.AppTestDb);
{
DbAdapter = DbAdapter.SqlServer,
TablesToIgnore =
[
new Respawn.Graph.Table("dbo", "Rol"),
new Respawn.Graph.Table("dbo", "Permiso"),
new Respawn.Graph.Table("dbo", "RolPermiso"),
// *_History tables are system-versioned — engine rejects direct DELETE.
new Respawn.Graph.Table("dbo", "Usuario_History"),
new Respawn.Graph.Table("dbo", "Rol_History"),
new Respawn.Graph.Table("dbo", "Permiso_History"),
new Respawn.Graph.Table("dbo", "RolPermiso_History"),
// ADM-001 (V011): Medio + Seccion are temporal — history tables cannot be directly deleted.
new Respawn.Graph.Table("dbo", "Medio_History"),
new Respawn.Graph.Table("dbo", "Seccion_History"),
// ADM-008 (V013): PuntoDeVenta is temporal.
new Respawn.Graph.Table("dbo", "PuntoDeVenta_History"),
// ADM-009 (V014): TipoDeIva + IngresosBrutos son temporales.
new Respawn.Graph.Table("dbo", "TipoDeIva_History"),
new Respawn.Graph.Table("dbo", "IngresosBrutos_History"),
new Respawn.Graph.Table("dbo", "TipoDeIva"),
new Respawn.Graph.Table("dbo", "IngresosBrutos"),
// CAT-001 (V016): Rubro es temporal — history no puede deletearse directo.
new Respawn.Graph.Table("dbo", "Rubro_History"),
]
});
await _respawner.ResetAsync(_connection);
await SeedRolCanonicalAsync();
var factory = new SqlConnectionFactory(ConnectionString);
_repository = new RubroRepository(factory); _repository = new RubroRepository(factory);
_timeProvider = TimeProvider.System; _timeProvider = TimeProvider.System;
} }
public async Task DisposeAsync() public Task DisposeAsync() => Task.CompletedTask;
{
await _connection.CloseAsync();
await _connection.DisposeAsync();
}
// ── AddAsync + GetByIdAsync roundtrip ───────────────────────────────────── // ── AddAsync + GetByIdAsync roundtrip ─────────────────────────────────────
@@ -381,7 +345,7 @@ public class RubroRepositoryTests : IAsyncLifetime
Assert.Equal("Actualizado", result!.Nombre); Assert.Equal("Actualizado", result!.Nombre);
Assert.NotNull(result.FechaModificacion); Assert.NotNull(result.FechaModificacion);
var historyCount = await _connection.ExecuteScalarAsync<int>( var historyCount = await _db.Connection.ExecuteScalarAsync<int>(
"SELECT COUNT(*) FROM dbo.Rubro_History WHERE Id = @Id", new { Id = id }); "SELECT COUNT(*) FROM dbo.Rubro_History WHERE Id = @Id", new { Id = id });
Assert.True(historyCount >= 1, $"Expected ≥1 history row for Rubro Id={id}, got {historyCount}"); Assert.True(historyCount >= 1, $"Expected ≥1 history row for Rubro Id={id}, got {historyCount}");
@@ -423,28 +387,4 @@ public class RubroRepositoryTests : IAsyncLifetime
Assert.NotNull(result.FechaModificacion); Assert.NotNull(result.FechaModificacion);
} }
// ── helpers ───────────────────────────────────────────────────────────────
private async Task SeedRolCanonicalAsync()
{
const string sql = """
SET QUOTED_IDENTIFIER ON;
MERGE dbo.Rol AS t
USING (VALUES
('admin', N'Administrador', N'Supervisor total'),
('cajero', N'Cajero', N'Mostrador contado'),
('operador_ctacte', N'Operador Cta Cte', N'Cuenta corriente'),
('picadora', N'Picadora/Correctora', N'Edición de textos'),
('jefe_publicidad', N'Jefe de Publicidad', N'Supervisión de pauta'),
('productor', N'Productor', N'Carga restringida'),
('diagramacion', N'Diagramación/Taller', N'Solo lectura pauta'),
('reportes', N'Reportes', N'Solo lectura reportes')
) AS s (Codigo, Nombre, Descripcion)
ON t.Codigo = s.Codigo
WHEN NOT MATCHED BY TARGET THEN
INSERT (Codigo, Nombre, Descripcion, Activo)
VALUES (s.Codigo, s.Nombre, s.Descripcion, 1);
""";
await _connection.ExecuteAsync(sql);
}
} }

View File

@@ -57,6 +57,9 @@ public sealed class SqlTestFixture : IAsyncLifetime
// V015 (UDT-011): ensure dbo.v_AuditEvent_Local + dbo.v_SecurityEvent_Local views exist. // V015 (UDT-011): ensure dbo.v_AuditEvent_Local + dbo.v_SecurityEvent_Local views exist.
await EnsureV015SchemaAsync(); await EnsureV015SchemaAsync();
// V016 (CAT-001): ensure dbo.Rubro + temporal + permiso 'catalogo:rubros:gestionar'.
await EnsureV016SchemaAsync();
_respawner = await Respawner.CreateAsync(_connection, new RespawnerOptions _respawner = await Respawner.CreateAsync(_connection, new RespawnerOptions
{ {
DbAdapter = DbAdapter.SqlServer, DbAdapter = DbAdapter.SqlServer,
@@ -81,6 +84,8 @@ public sealed class SqlTestFixture : IAsyncLifetime
// Seed de TipoDeIva e IngresosBrutos son datos de referencia — no limpiar con Respawn. // Seed de TipoDeIva e IngresosBrutos son datos de referencia — no limpiar con Respawn.
new Respawn.Graph.Table("dbo", "TipoDeIva"), new Respawn.Graph.Table("dbo", "TipoDeIva"),
new Respawn.Graph.Table("dbo", "IngresosBrutos"), new Respawn.Graph.Table("dbo", "IngresosBrutos"),
// CAT-001 (V016): Rubro es temporal — history no puede deletearse directo.
new Respawn.Graph.Table("dbo", "Rubro_History"),
] ]
}); });
@@ -201,7 +206,9 @@ public sealed class SqlTestFixture : IAsyncLifetime
-- V013 (ADM-008): permiso para CRUD de Puntos de Venta -- V013 (ADM-008): permiso para CRUD de Puntos de Venta
('administracion:puntos_de_venta:gestionar', N'Gestionar puntos de venta', N'Crear, editar y desactivar puntos de venta AFIP','administracion'), ('administracion:puntos_de_venta:gestionar', N'Gestionar puntos de venta', N'Crear, editar y desactivar puntos de venta AFIP','administracion'),
-- V014 (ADM-009): permiso para tablas fiscales -- V014 (ADM-009): permiso para tablas fiscales
('administracion:fiscal:gestionar', N'Gestionar tablas fiscales', N'Gestionar tablas fiscales (IVA, IIBB)', 'administracion') ('administracion:fiscal:gestionar', N'Gestionar tablas fiscales', N'Gestionar tablas fiscales (IVA, IIBB)', 'administracion'),
-- V016 (CAT-001): permiso para gestionar árbol de rubros
('catalogo:rubros:gestionar', N'Gestionar rubros del catálogo', N'Crear, editar, mover y desactivar rubros del árbol de catálogo comercial', 'catalogo')
) AS s (Codigo, Nombre, Descripcion, Modulo) ) AS s (Codigo, Nombre, Descripcion, Modulo)
ON t.Codigo = s.Codigo ON t.Codigo = s.Codigo
WHEN NOT MATCHED BY TARGET THEN WHEN NOT MATCHED BY TARGET THEN
@@ -247,6 +254,8 @@ public sealed class SqlTestFixture : IAsyncLifetime
('admin', 'administracion:puntos_de_venta:gestionar'), ('admin', 'administracion:puntos_de_venta:gestionar'),
-- V014 (ADM-009) -- V014 (ADM-009)
('admin', 'administracion:fiscal:gestionar'), ('admin', 'administracion:fiscal:gestionar'),
-- V016 (CAT-001)
('admin', 'catalogo:rubros:gestionar'),
('cajero', 'ventas:contado:crear'), ('cajero', 'ventas:contado:crear'),
('cajero', 'ventas:contado:modificar'), ('cajero', 'ventas:contado:modificar'),
('cajero', 'ventas:contado:cobrar'), ('cajero', 'ventas:contado:cobrar'),
@@ -849,4 +858,79 @@ public sealed class SqlTestFixture : IAsyncLifetime
await _connection.ExecuteAsync(createAuditEventLocal); await _connection.ExecuteAsync(createAuditEventLocal);
await _connection.ExecuteAsync(createSecurityEventLocal); await _connection.ExecuteAsync(createSecurityEventLocal);
} }
/// <summary>
/// CAT-001 (V016): applies dbo.Rubro schema + temporal + filtered unique index + covering index
/// idempotentemente. Mirrors V016__create_rubro.sql.
/// Nota: COLLATE debe ir ANTES de NOT NULL — parser de SQL Server 2019 es estricto con ese orden.
/// Permiso 'catalogo:rubros:gestionar' y asignación a admin se siembran
/// desde SeedPermisosCanonicalAsync / SeedRolPermisosCanonicalAsync (post-respawn).
/// </summary>
private async Task EnsureV016SchemaAsync()
{
const string createRubro = """
IF OBJECT_ID(N'dbo.Rubro', N'U') IS NULL
BEGIN
CREATE TABLE dbo.Rubro (
Id INT IDENTITY(1,1) NOT NULL CONSTRAINT PK_Rubro PRIMARY KEY,
ParentId INT NULL,
Nombre NVARCHAR(200) COLLATE SQL_Latin1_General_CP1_CI_AI NOT NULL,
Orden INT NOT NULL CONSTRAINT DF_Rubro_Orden DEFAULT(0),
Activo BIT NOT NULL CONSTRAINT DF_Rubro_Activo DEFAULT(1),
TarifarioBaseId INT NULL,
FechaCreacion DATETIME2(3) NOT NULL CONSTRAINT DF_Rubro_FechaCreacion DEFAULT(SYSUTCDATETIME()),
FechaModificacion DATETIME2(3) NULL,
CONSTRAINT FK_Rubro_Parent FOREIGN KEY (ParentId) REFERENCES dbo.Rubro(Id) ON DELETE NO ACTION
);
END
""";
const string addRubroPeriod = """
IF COL_LENGTH('dbo.Rubro', 'ValidFrom') IS NULL
BEGIN
ALTER TABLE dbo.Rubro
ADD
ValidFrom DATETIME2(3) GENERATED ALWAYS AS ROW START HIDDEN NOT NULL
CONSTRAINT DF_Rubro_ValidFrom DEFAULT(SYSUTCDATETIME()),
ValidTo DATETIME2(3) GENERATED ALWAYS AS ROW END HIDDEN NOT NULL
CONSTRAINT DF_Rubro_ValidTo DEFAULT(CONVERT(DATETIME2(3), '9999-12-31 23:59:59.999')),
PERIOD FOR SYSTEM_TIME (ValidFrom, ValidTo);
END
""";
const string setRubroVersioning = """
IF NOT EXISTS (SELECT 1 FROM sys.tables WHERE object_id = OBJECT_ID('dbo.Rubro') AND temporal_type = 2)
BEGIN
ALTER TABLE dbo.Rubro
SET (SYSTEM_VERSIONING = ON (
HISTORY_TABLE = dbo.Rubro_History,
HISTORY_RETENTION_PERIOD = 10 YEARS
));
END
""";
const string createUqIndex = """
IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'UQ_Rubro_ParentId_Nombre_Activo' AND object_id = OBJECT_ID('dbo.Rubro'))
BEGIN
CREATE UNIQUE INDEX UQ_Rubro_ParentId_Nombre_Activo
ON dbo.Rubro(ParentId, Nombre)
WHERE Activo = 1 AND ParentId IS NOT NULL;
END
""";
const string createCoveringIndex = """
IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'IX_Rubro_ParentId_Activo' AND object_id = OBJECT_ID('dbo.Rubro'))
BEGIN
CREATE INDEX IX_Rubro_ParentId_Activo
ON dbo.Rubro(ParentId, Activo)
INCLUDE (Nombre, Orden);
END
""";
await _connection.ExecuteAsync(createRubro);
await _connection.ExecuteAsync(addRubroPeriod);
await _connection.ExecuteAsync(setRubroVersioning);
await _connection.ExecuteAsync(createUqIndex);
await _connection.ExecuteAsync(createCoveringIndex);
}
} }