71 lines
2.4 KiB
C#
71 lines
2.4 KiB
C#
using audit.Model;
|
|
using audit.Repositories.Interfaces;
|
|
using audit_logs.Services;
|
|
|
|
namespace audit_test.Service;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Moq;
|
|
using Xunit;
|
|
|
|
public class AuditServiceTests
|
|
{
|
|
private readonly Mock<IAuditRecordRepository> _mockRepository;
|
|
private readonly AuditService _auditService;
|
|
private readonly AuditRecord _testAuditRecord;
|
|
|
|
public AuditServiceTests()
|
|
{
|
|
// creo el mock del repositorio
|
|
_mockRepository = new Mock<IAuditRecordRepository>();
|
|
|
|
// preparo el servicio con el mock de repositorio
|
|
_auditService = new AuditService(_mockRepository.Object);
|
|
|
|
// configuracion del registro
|
|
_testAuditRecord = new AuditRecord
|
|
{
|
|
EntityType = "User",
|
|
RecordId = "1",
|
|
UserId = "123",
|
|
ActionType = "Create",
|
|
ActionTime = DateTime.UtcNow,
|
|
Changes = new List<AuditRecord.Change>
|
|
{
|
|
new AuditRecord.Change { Field = "Username", OldValue = "oldUser", NewValue = "newUser", ValueType = "String" }
|
|
},
|
|
Reason = "Ingreso de paciente"
|
|
};
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RecordAuditAsync_ValidRecord_InsertsRecord()
|
|
{
|
|
// Arrange
|
|
_mockRepository.Setup(repo => repo.InsertOneAsync(It.IsAny<AuditRecord>()))
|
|
.Returns(Task.CompletedTask);
|
|
|
|
// Act
|
|
await _auditService.RecordAuditAsync(
|
|
_testAuditRecord.EntityType,
|
|
_testAuditRecord.RecordId,
|
|
_testAuditRecord.UserId,
|
|
_testAuditRecord.ActionType,
|
|
_testAuditRecord.Reason,
|
|
_testAuditRecord.ActionTime,
|
|
_testAuditRecord.Changes
|
|
);
|
|
|
|
// Assert
|
|
_mockRepository.Verify(repo => repo.InsertOneAsync(It.IsAny<AuditRecord>()), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RecordAuditAsync_NullRecord_ThrowsArgumentNullException()
|
|
{
|
|
// Act & Assert
|
|
await Assert.ThrowsAsync<ArgumentNullException>(() =>
|
|
_auditService.RecordAuditAsync(null, null, null, null, null,DateTime.MinValue, null));
|
|
}
|
|
} |