2026-04-15 21:33:39 -03:00
|
|
|
using Dapper;
|
|
|
|
|
using SIGCM2.Infrastructure.Persistence;
|
2026-04-18 21:44:36 -03:00
|
|
|
using SIGCM2.TestSupport;
|
2026-04-15 21:33:39 -03:00
|
|
|
|
|
|
|
|
namespace SIGCM2.Application.Tests.Integration;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Integration tests for IUsuarioRepository.UpdatePermisosJsonAsync (UDT-009).
|
2026-04-18 21:44:36 -03:00
|
|
|
/// Uses SIGCM2_Test_App database via shared SqlTestFixture.
|
2026-04-15 21:33:39 -03:00
|
|
|
/// </summary>
|
|
|
|
|
[Collection("Database")]
|
|
|
|
|
public sealed class UsuarioRepository_PermisosTests : IAsyncLifetime
|
|
|
|
|
{
|
2026-04-18 21:44:36 -03:00
|
|
|
private readonly SqlTestFixture _db;
|
2026-04-15 21:33:39 -03:00
|
|
|
private UsuarioRepository _repository = null!;
|
|
|
|
|
|
2026-04-18 21:44:36 -03:00
|
|
|
public UsuarioRepository_PermisosTests(SqlTestFixture db)
|
|
|
|
|
{
|
|
|
|
|
_db = db;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-15 21:33:39 -03:00
|
|
|
public async Task InitializeAsync()
|
|
|
|
|
{
|
2026-04-18 21:44:36 -03:00
|
|
|
await _db.ResetAndSeedAsync();
|
|
|
|
|
|
|
|
|
|
var factory = new SqlConnectionFactory(TestConnectionStrings.AppTestDb);
|
2026-04-15 21:33:39 -03:00
|
|
|
_repository = new UsuarioRepository(factory);
|
|
|
|
|
|
|
|
|
|
// Seed a test user
|
2026-04-18 21:44:36 -03:00
|
|
|
await _db.Connection.ExecuteAsync("""
|
2026-04-15 21:33:39 -03:00
|
|
|
INSERT INTO dbo.Usuario (Username, PasswordHash, Nombre, Apellido, Rol, PermisosJson, Activo, MustChangePassword)
|
|
|
|
|
VALUES ('testuser', '$2a$12$hash', 'Test', 'User', 'cajero', '{"grant":[],"deny":[]}', 1, 0)
|
|
|
|
|
""");
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-18 21:44:36 -03:00
|
|
|
public Task DisposeAsync() => Task.CompletedTask;
|
2026-04-15 21:33:39 -03:00
|
|
|
|
|
|
|
|
// UPJ-01: UpdatePermisosJsonAsync persists PermisosJson and FechaModificacion
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task UpdatePermisosJsonAsync_PersistsJsonAndFechaModificacion()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
2026-04-18 21:44:36 -03:00
|
|
|
var userId = await _db.Connection.QuerySingleAsync<int>(
|
2026-04-15 21:33:39 -03:00
|
|
|
"SELECT Id FROM dbo.Usuario WHERE Username = 'testuser'");
|
|
|
|
|
var newJson = """{"grant":["textos:editar"],"deny":[]}""";
|
|
|
|
|
var fechaMod = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
await _repository.UpdatePermisosJsonAsync(userId, newJson, fechaMod);
|
|
|
|
|
|
|
|
|
|
// Assert
|
2026-04-18 21:44:36 -03:00
|
|
|
var row = await _db.Connection.QuerySingleAsync<(string PermisosJson, DateTime? FechaModificacion)>(
|
2026-04-15 21:33:39 -03:00
|
|
|
"SELECT PermisosJson, FechaModificacion FROM dbo.Usuario WHERE Id = @Id",
|
|
|
|
|
new { Id = userId });
|
|
|
|
|
|
|
|
|
|
Assert.Equal(newJson, row.PermisosJson);
|
|
|
|
|
Assert.NotNull(row.FechaModificacion);
|
|
|
|
|
// Allow 2-second tolerance for DB round-trip
|
|
|
|
|
Assert.True(
|
|
|
|
|
Math.Abs((row.FechaModificacion!.Value - fechaMod).TotalSeconds) < 2,
|
|
|
|
|
$"FechaModificacion {row.FechaModificacion} is too far from {fechaMod}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UPJ-02: UpdatePermisosJsonAsync with non-existent id → no throw (UPDATE affects 0 rows)
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task UpdatePermisosJsonAsync_NonExistentId_NoThrow()
|
|
|
|
|
{
|
|
|
|
|
// Should not throw — UPDATE with 0 rows affected is a no-op
|
|
|
|
|
await _repository.UpdatePermisosJsonAsync(99999, """{"grant":[],"deny":[]}""", DateTime.UtcNow);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UPJ-03: GetByIdAsync after update reflects new PermisosJson
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task UpdatePermisosJsonAsync_GetByIdReflectsChange()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
2026-04-18 21:44:36 -03:00
|
|
|
var userId = await _db.Connection.QuerySingleAsync<int>(
|
2026-04-15 21:33:39 -03:00
|
|
|
"SELECT Id FROM dbo.Usuario WHERE Username = 'testuser'");
|
|
|
|
|
var newJson = """{"grant":["pauta:azanu:ver"],"deny":["ventas:contado:cobrar"]}""";
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
await _repository.UpdatePermisosJsonAsync(userId, newJson, DateTime.UtcNow);
|
|
|
|
|
|
|
|
|
|
// Assert — read back through the repo
|
|
|
|
|
var usuario = await _repository.GetByIdAsync(userId);
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(usuario);
|
|
|
|
|
Assert.Equal(newJson, usuario!.PermisosJson);
|
|
|
|
|
}
|
|
|
|
|
}
|