Implementacion del metodo de listar y cambios en los modelos e interfaces
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using audit_logs.Models;
|
||||
using audit_logs.Utils;
|
||||
|
||||
namespace audit_test.Repository;
|
||||
@@ -29,15 +30,18 @@ public class AuditServiceIntegrationTests
|
||||
new AuditRecord.Change
|
||||
{ Field = "Username", OldValue = "oldUser", NewValue = "newUser", ValueType = "String" }
|
||||
};
|
||||
string entityType = "User",
|
||||
recordId = "1",
|
||||
userId = "123",
|
||||
actionType = "Create",
|
||||
reason = "Testing create functionality";
|
||||
DateTime actionTime = DateTime.UtcNow;
|
||||
AuditRecord auditLogData = new();
|
||||
auditLogData.EntityType = "User";
|
||||
auditLogData.RecordId = "1";
|
||||
auditLogData.UserId = "123";
|
||||
auditLogData.ActionType = "Update";
|
||||
auditLogData.ActionTime = DateTime.UtcNow;
|
||||
auditLogData.Reason = "Testing change detection";
|
||||
auditLogData.UserIpAddress = "127.0.0.1";
|
||||
auditLogData.Changes = changes;
|
||||
|
||||
// Act
|
||||
await _service.RecordAuditAsync(entityType, recordId, userId, actionType, reason,actionTime, changes );
|
||||
await _service.RecordAuditAsync(auditLogData);
|
||||
|
||||
|
||||
// Retrieve all records to validate insertion
|
||||
@@ -46,11 +50,11 @@ public class AuditServiceIntegrationTests
|
||||
// Assert
|
||||
Assert.AreEqual(1, allRecords.Count, "There should be exactly three audit record in the database.");
|
||||
var storedRecord = allRecords[0];
|
||||
Assert.AreEqual(entityType, storedRecord.EntityType);
|
||||
Assert.AreEqual(recordId, storedRecord.RecordId);
|
||||
Assert.AreEqual(userId, storedRecord.UserId);
|
||||
Assert.AreEqual(actionType, storedRecord.ActionType);
|
||||
Assert.AreEqual(reason, storedRecord.Reason);
|
||||
Assert.AreEqual(auditLogData.EntityType, storedRecord.EntityType);
|
||||
Assert.AreEqual(auditLogData.RecordId, storedRecord.RecordId);
|
||||
Assert.AreEqual(auditLogData.UserId, storedRecord.UserId);
|
||||
Assert.AreEqual(auditLogData.ActionType, storedRecord.ActionType);
|
||||
Assert.AreEqual(auditLogData.Reason, storedRecord.Reason);
|
||||
// Assert.That(storedRecord.Changes, Is.EquivalentTo(changes).Using(new ChangeComparer()), "Changes should match the input changes.");
|
||||
}
|
||||
|
||||
@@ -90,20 +94,22 @@ public class AuditServiceIntegrationTests
|
||||
public async Task DetectChangesAsync_DetectsAndRecordsChanges()
|
||||
{
|
||||
// Arrange
|
||||
string originalJson = "{ \"Username\": \"oldUser\", \"Email\": \"old@example.com\", \"address\": 15 }";
|
||||
string modifiedJson =
|
||||
"{ \"Username\": \"newUser\", \"Email\": \"old@example.com\", \"PhoneNumber\": \"123456789\" }";
|
||||
|
||||
|
||||
// Variables de auditoría
|
||||
string entityType = "User";
|
||||
string recordId = "1";
|
||||
string userId = "123";
|
||||
string actionType = "Update";
|
||||
DateTime actionTime = DateTime.UtcNow;
|
||||
string reason = "Testing change detection";
|
||||
AuditLogData auditLogData = new();
|
||||
auditLogData.OriginalJson = "{ \"Username\": \"oldUser\", \"Email\": \"old@example.com\", \"address\": 15 }";
|
||||
auditLogData.ModifiedJson =
|
||||
"{ \"Username\": \"newUser\", \"Email\": \"old@example.com\", \"PhoneNumber\": \"123456789\" }";
|
||||
auditLogData.EntityType = "User";
|
||||
auditLogData.RecordId = "1";
|
||||
auditLogData.UserId = "123";
|
||||
auditLogData.ActionType = "Update";
|
||||
auditLogData.ActionTime = DateTime.UtcNow;
|
||||
auditLogData.Reason = "Testing change detection";
|
||||
auditLogData.UserIpAddress = "127.0.0.1";
|
||||
|
||||
// Act - Llamar a DetectChangesAsync para que identifique cambios
|
||||
await _service.CreateAuditLogAsync(entityType, recordId, userId, actionType, reason,actionTime,originalJson, modifiedJson);
|
||||
await _service.CreateAuditLogAsync(auditLogData);
|
||||
|
||||
// Act - Registrar la auditoría usando los cambios detectados
|
||||
// List<AuditRecord.Change> changes = new JsonComparer().GetDifferences(originalJson, modifiedJson);
|
||||
@@ -115,14 +121,15 @@ public class AuditServiceIntegrationTests
|
||||
// Assert
|
||||
Assert.AreEqual(1, allRecords.Count, "Debe haber exactamente un registro de auditoría en la base de datos.");
|
||||
var storedRecord = allRecords[0];
|
||||
Assert.AreEqual(entityType, storedRecord.EntityType);
|
||||
Assert.AreEqual(recordId, storedRecord.RecordId);
|
||||
Assert.AreEqual(userId, storedRecord.UserId);
|
||||
Assert.AreEqual(actionType, storedRecord.ActionType);
|
||||
Assert.AreEqual(reason, storedRecord.Reason);
|
||||
Assert.AreEqual(auditLogData.EntityType, storedRecord.EntityType);
|
||||
Assert.AreEqual(auditLogData.RecordId, storedRecord.RecordId);
|
||||
Assert.AreEqual(auditLogData.UserId, storedRecord.UserId);
|
||||
Assert.AreEqual(auditLogData.ActionType, storedRecord.ActionType);
|
||||
Assert.AreEqual(auditLogData.Reason, storedRecord.Reason);
|
||||
|
||||
// Verificar que los cambios detectados sean correctos
|
||||
Assert.AreEqual(3, storedRecord.Changes.Count, "Debería detectar dos cambios.");
|
||||
Assert.That(storedRecord.Changes.Count, Is.EqualTo(3), "Debería detectar tres cambios.");
|
||||
|
||||
|
||||
var changeUsername = storedRecord.Changes.Find(c => c.Field == "Username");
|
||||
Assert.NotNull(changeUsername, "Debe existir un cambio en el campo 'Username'.");
|
||||
@@ -136,4 +143,65 @@ public class AuditServiceIntegrationTests
|
||||
Assert.AreEqual("123456789", changePhoneNumber.NewValue.AsString);
|
||||
Assert.AreEqual("string", changePhoneNumber.ValueType);
|
||||
}
|
||||
[Test]
|
||||
public async Task GetAuditLogsAsync_ReturnsPaginatedRecords()
|
||||
{
|
||||
// Arrange - Insert multiple audit records to test pagination
|
||||
var records = new List<AuditRecord>();
|
||||
for (int i = 1; i <= 20; i++)
|
||||
{
|
||||
records.Add(new AuditRecord
|
||||
{
|
||||
EntityType = "TestEntity",
|
||||
RecordId = i.ToString(),
|
||||
UserId = "User" + i,
|
||||
ActionType = "Create",
|
||||
ActionTime = DateTime.UtcNow,
|
||||
Reason = "Test Reason " + i,
|
||||
Changes = new List<AuditRecord.Change>
|
||||
{
|
||||
new AuditRecord.Change
|
||||
{
|
||||
Field = "Field" + i,
|
||||
OldValue = "OldValue" + i,
|
||||
NewValue = "NewValue" + i,
|
||||
ValueType = "String"
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
foreach (var record in records)
|
||||
{
|
||||
await _service.AuditLogRepository.InsertOneAsync(record);
|
||||
}
|
||||
|
||||
// Act - Retrieve the first page with a page size of 5
|
||||
int pageNumber = 1;
|
||||
int pageSize = 5;
|
||||
var paginatedResult = await _service.GetAuditLogsAsync(pageNumber, pageSize);
|
||||
|
||||
// Assert - Check that the pagination returns the correct number of records and page information
|
||||
Assert.AreEqual(pageSize, paginatedResult.Records.Count(), "The number of records should match the page size.");
|
||||
Assert.AreEqual(20, paginatedResult.TotalRecords, "The total number of records should match the inserted records.");
|
||||
Assert.AreEqual(4, paginatedResult.TotalPages, "The total pages should be calculated correctly.");
|
||||
Assert.AreEqual(pageNumber, paginatedResult.CurrentPage, "The current page should be correct.");
|
||||
|
||||
// Further assertions to check that the records and changes are the expected ones
|
||||
var firstRecord = paginatedResult.Records.First();
|
||||
Assert.AreEqual("TestEntity", firstRecord.EntityType);
|
||||
Assert.AreEqual("1", firstRecord.RecordId);
|
||||
Assert.AreEqual("User1", firstRecord.UserId);
|
||||
Assert.AreEqual("Create", firstRecord.ActionType);
|
||||
|
||||
// Verify the changes for the first record
|
||||
Assert.IsNotNull(firstRecord.Changes);
|
||||
Assert.AreEqual(1, firstRecord.Changes.Count, "There should be exactly one change in the first record.");
|
||||
var firstChange = firstRecord.Changes.First();
|
||||
Assert.AreEqual("Field1", firstChange.Field);
|
||||
Assert.AreEqual("OldValue1", firstChange.OldValue.AsString);
|
||||
Assert.AreEqual("NewValue1", firstChange.NewValue.AsString);
|
||||
Assert.AreEqual("String", firstChange.ValueType);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user