Inicio
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
using audit_logs.Utils;
|
||||
|
||||
namespace audit_test.Repository;
|
||||
|
||||
using audit_logs.Repositories;
|
||||
using audit.Model;
|
||||
using audit_logs.Services;
|
||||
|
||||
public class AuditServiceIntegrationTests
|
||||
{
|
||||
private AuditService _service;
|
||||
// private AuditRecordRepository _repository;
|
||||
|
||||
[Order(1)]
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
IntegrationDb.Database.DropCollectionAsync("AuditRecords");
|
||||
//_repository = new AuditRecordRepository(IntegrationDb.Database);
|
||||
_service = new AuditService(IntegrationDb.Database);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task RecordAuditAsync_CreatesAndRetrievesAuditRecord()
|
||||
{
|
||||
// Arrange
|
||||
var changes = new List<AuditRecord.Change>
|
||||
{
|
||||
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;
|
||||
|
||||
// Act
|
||||
await _service.RecordAuditAsync(entityType, recordId, userId, actionType, reason,actionTime, changes );
|
||||
|
||||
|
||||
// Retrieve all records to validate insertion
|
||||
var allRecords = await _service.AuditLogRepository.GetAllAuditRecordsAsync();
|
||||
|
||||
// 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.That(storedRecord.Changes, Is.EquivalentTo(changes).Using(new ChangeComparer()), "Changes should match the input changes.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetAllAuditRecords_ReturnsAllRecords()
|
||||
{
|
||||
// Prepare multiple records
|
||||
var records = new List<AuditRecord>
|
||||
{
|
||||
new AuditRecord
|
||||
{
|
||||
EntityType = "User", RecordId = "1", UserId = "123", ActionType = "Create",
|
||||
ActionTime = DateTime.UtcNow, Reason = "Test Reason 1"
|
||||
},
|
||||
new AuditRecord
|
||||
{
|
||||
EntityType = "Admin", RecordId = "2", UserId = "456", ActionType = "Delete",
|
||||
ActionTime = DateTime.UtcNow, Reason = "Test Reason 2"
|
||||
}
|
||||
};
|
||||
|
||||
foreach (var rec in records)
|
||||
{
|
||||
await _service.AuditLogRepository.InsertOneAsync(rec);
|
||||
}
|
||||
|
||||
// Test retrieval method
|
||||
var allRecords = await _service.AuditLogRepository.GetAllAuditRecordsAsync();
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(2, allRecords.Count, "All records should be retrieved.");
|
||||
Assert.IsTrue(allRecords.Exists(r => r.RecordId == "1" && r.EntityType == "User"));
|
||||
Assert.IsTrue(allRecords.Exists(r => r.RecordId == "2" && r.EntityType == "Admin"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
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";
|
||||
|
||||
// Act - Llamar a DetectChangesAsync para que identifique cambios
|
||||
await _service.CreateAuditLogAsync(entityType, recordId, userId, actionType, reason,actionTime,originalJson, modifiedJson);
|
||||
|
||||
// Act - Registrar la auditoría usando los cambios detectados
|
||||
// List<AuditRecord.Change> changes = new JsonComparer().GetDifferences(originalJson, modifiedJson);
|
||||
//await _service.RecordAuditAsync(entityType, recordId, userId, actionType, reason,actionTime, changes, );
|
||||
|
||||
// Retrieve all records to validate insertion
|
||||
var allRecords = await _service.AuditLogRepository.GetAllAuditRecordsAsync();
|
||||
|
||||
// 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);
|
||||
|
||||
// Verificar que los cambios detectados sean correctos
|
||||
Assert.AreEqual(3, storedRecord.Changes.Count, "Debería detectar dos cambios.");
|
||||
|
||||
var changeUsername = storedRecord.Changes.Find(c => c.Field == "Username");
|
||||
Assert.NotNull(changeUsername, "Debe existir un cambio en el campo 'Username'.");
|
||||
Assert.AreEqual("oldUser", changeUsername.OldValue.AsString);
|
||||
Assert.AreEqual("newUser", changeUsername.NewValue.AsString);
|
||||
Assert.AreEqual("string", changeUsername.ValueType);
|
||||
|
||||
var changePhoneNumber = storedRecord.Changes.Find(c => c.Field == "PhoneNumber");
|
||||
Assert.NotNull(changePhoneNumber, "Debe existir un cambio en el campo 'PhoneNumber'.");
|
||||
Assert.IsTrue(changePhoneNumber.OldValue.IsBsonNull, "El valor anterior de 'PhoneNumber' debe ser nulo.");
|
||||
Assert.AreEqual("123456789", changePhoneNumber.NewValue.AsString);
|
||||
Assert.AreEqual("string", changePhoneNumber.ValueType);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user