feat(domain): V008 migration + Usuario with-methods + DomainException hierarchy [UDT-008]
This commit is contained in:
@@ -2,6 +2,8 @@ using SIGCM2.Domain.Entities;
|
||||
|
||||
namespace SIGCM2.Application.Tests.Domain;
|
||||
|
||||
// ── UDT-008 tests ────────────────────────────────────────────────────────────
|
||||
|
||||
public class UsuarioTests
|
||||
{
|
||||
// Happy path: constructor sets all properties correctly
|
||||
@@ -69,4 +71,172 @@ public class UsuarioTests
|
||||
var usuario = new Usuario(2, "inactive", "$2a$12$hash", "Old", "User", null, "consulta", "[]", false);
|
||||
Assert.False(usuario.Activo);
|
||||
}
|
||||
|
||||
// ── UDT-008: new properties ───────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void ForCreation_Defaults_MustChangePassword_False()
|
||||
{
|
||||
var u = Usuario.ForCreation("user", "hash", "A", "B", null, "cajero");
|
||||
Assert.False(u.MustChangePassword);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ForCreation_Defaults_FechaModificacion_Null()
|
||||
{
|
||||
var u = Usuario.ForCreation("user", "hash", "A", "B", null, "cajero");
|
||||
Assert.Null(u.FechaModificacion);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ForCreation_Defaults_UltimoLogin_Null()
|
||||
{
|
||||
var u = Usuario.ForCreation("user", "hash", "A", "B", null, "cajero");
|
||||
Assert.Null(u.UltimoLogin);
|
||||
}
|
||||
|
||||
// ── UDT-008: WithUpdatedProfile ──────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void WithUpdatedProfile_Returns_NewInstance()
|
||||
{
|
||||
var u = MakeUsuario();
|
||||
var updated = u.WithUpdatedProfile("NuevoNombre", "NuevoApellido", "new@x.com", "cajero", true);
|
||||
Assert.NotSame(u, updated);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithUpdatedProfile_Sets_Fields_Correctly()
|
||||
{
|
||||
var u = MakeUsuario();
|
||||
var updated = u.WithUpdatedProfile("Pedro", "Gómez", "p@g.com", "cajero", false);
|
||||
Assert.Equal("Pedro", updated.Nombre);
|
||||
Assert.Equal("Gómez", updated.Apellido);
|
||||
Assert.Equal("p@g.com", updated.Email);
|
||||
Assert.Equal("cajero", updated.Rol);
|
||||
Assert.False(updated.Activo);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithUpdatedProfile_Sets_FechaModificacion_To_UtcNow()
|
||||
{
|
||||
var before = DateTime.UtcNow.AddSeconds(-1);
|
||||
var u = MakeUsuario();
|
||||
var updated = u.WithUpdatedProfile("A", "B", null, "admin", true);
|
||||
Assert.NotNull(updated.FechaModificacion);
|
||||
Assert.True(updated.FechaModificacion >= before);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithUpdatedProfile_Preserves_Immutable_Fields()
|
||||
{
|
||||
var u = MakeUsuario();
|
||||
var updated = u.WithUpdatedProfile("X", "Y", null, "cajero", true);
|
||||
Assert.Equal(u.Id, updated.Id);
|
||||
Assert.Equal(u.Username, updated.Username);
|
||||
Assert.Equal(u.PasswordHash, updated.PasswordHash);
|
||||
}
|
||||
|
||||
// ── UDT-008: WithNewPasswordHash ─────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void WithNewPasswordHash_Returns_NewInstance()
|
||||
{
|
||||
var u = MakeUsuario();
|
||||
var updated = u.WithNewPasswordHash("newhash", mustChangePassword: false);
|
||||
Assert.NotSame(u, updated);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithNewPasswordHash_Sets_Hash_And_MustChange()
|
||||
{
|
||||
var u = MakeUsuario();
|
||||
var updated = u.WithNewPasswordHash("newhash", mustChangePassword: true);
|
||||
Assert.Equal("newhash", updated.PasswordHash);
|
||||
Assert.True(updated.MustChangePassword);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithNewPasswordHash_Clears_MustChange_When_False()
|
||||
{
|
||||
var u = MakeUsuario(mustChangePassword: true);
|
||||
var updated = u.WithNewPasswordHash("hash2", mustChangePassword: false);
|
||||
Assert.False(updated.MustChangePassword);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithNewPasswordHash_Sets_FechaModificacion()
|
||||
{
|
||||
var before = DateTime.UtcNow.AddSeconds(-1);
|
||||
var u = MakeUsuario();
|
||||
var updated = u.WithNewPasswordHash("hash2", false);
|
||||
Assert.NotNull(updated.FechaModificacion);
|
||||
Assert.True(updated.FechaModificacion >= before);
|
||||
}
|
||||
|
||||
// ── UDT-008: WithUltimoLogin ──────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void WithUltimoLogin_Returns_NewInstance()
|
||||
{
|
||||
var u = MakeUsuario();
|
||||
var updated = u.WithUltimoLogin(DateTime.UtcNow);
|
||||
Assert.NotSame(u, updated);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithUltimoLogin_Sets_UltimoLogin()
|
||||
{
|
||||
var ts = new DateTime(2026, 1, 15, 10, 0, 0, DateTimeKind.Utc);
|
||||
var u = MakeUsuario();
|
||||
var updated = u.WithUltimoLogin(ts);
|
||||
Assert.Equal(ts, updated.UltimoLogin);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithUltimoLogin_Does_NOT_Touch_FechaModificacion()
|
||||
{
|
||||
var u = MakeUsuario();
|
||||
var originalFecha = u.FechaModificacion;
|
||||
var updated = u.WithUltimoLogin(DateTime.UtcNow);
|
||||
Assert.Equal(originalFecha, updated.FechaModificacion);
|
||||
}
|
||||
|
||||
// ── UDT-008: WithMustChangePassword ──────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void WithMustChangePassword_Sets_Value_True()
|
||||
{
|
||||
var u = MakeUsuario(mustChangePassword: false);
|
||||
var updated = u.WithMustChangePassword(true);
|
||||
Assert.True(updated.MustChangePassword);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithMustChangePassword_Sets_FechaModificacion()
|
||||
{
|
||||
var before = DateTime.UtcNow.AddSeconds(-1);
|
||||
var u = MakeUsuario();
|
||||
var updated = u.WithMustChangePassword(true);
|
||||
Assert.NotNull(updated.FechaModificacion);
|
||||
Assert.True(updated.FechaModificacion >= before);
|
||||
}
|
||||
|
||||
// ── helpers ───────────────────────────────────────────────────────────────
|
||||
|
||||
private static Usuario MakeUsuario(bool mustChangePassword = false)
|
||||
=> new(
|
||||
id: 1,
|
||||
username: "testuser",
|
||||
passwordHash: "$2a$12$hash",
|
||||
nombre: "Test",
|
||||
apellido: "User",
|
||||
email: "test@x.com",
|
||||
rol: "admin",
|
||||
permisosJson: "[]",
|
||||
activo: true,
|
||||
fechaModificacion: null,
|
||||
ultimoLogin: null,
|
||||
mustChangePassword: mustChangePassword
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user