Files
2026-04-20 14:47:24 -05:00

114 lines
3.8 KiB
C#

using System.Collections.Generic;
using LCU.API.Collections.RepoApi.Controllers;
using LCU.API.Collections.RepoApi.Models.Filters;
using LCU.API.Collections.RepoApi.Models.Responses;
using LCU.API.Collections.RepoApi.Services;
using LCU.Shared.Collections.RepoApiLib.Models;
using LCU.Shared.Collections.RepoApiLib.Models.Results;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Moq;
using Xunit;
namespace LCU.API.Collections.RepoApiTests.Controllers
{
public class AuditRecordControllerTest
{
private readonly Mock<IAuditRecordService> mockAuditRecordService;
private readonly AuditRecordController controller;
private readonly Mock<IHttpContextAccessor> mockHttpContextAccessor;
public AuditRecordControllerTest()
{
mockAuditRecordService = new Mock<IAuditRecordService>();
mockHttpContextAccessor = new Mock<IHttpContextAccessor>();
controller = new AuditRecordController(mockAuditRecordService.Object,
mockHttpContextAccessor.Object);
}
// Get tests
[Fact]
public async void Get_OK()
{
mockAuditRecordService.Setup(service => service.ListAll(It.IsAny<PageFilter>())).ReturnsAsync(
new PagedResult<AuditRecord>()
{
Total = 1,
Items = new List<AuditRecord>()
{
new AuditRecord()
{
Action = "action",
ClassName = "Something",
Description = "changed something field 1",
PerformedBy = "Developer",
KeyRepresentation = "111"
}
}
});
// arrange
var filter = new PageFilter();
// act
var result = await controller.Get(filter);
Assert.NotNull(result);
var okResult = result as OkObjectResult;
// assert
Assert.NotNull(okResult);
Assert.Equal(200, okResult.StatusCode);
var okValue = okResult.Value as PagedResponse<AuditRecord>;
Assert.NotNull(okValue);
Assert.Equal(1, okValue.TotalItems);
}
// Post tests
[Fact]
public async void Post_OK()
{
// arrange
var postAudit = new AuditRecord()
{
Action = "action",
ClassName = "Something",
Description = "changed something field 1",
PerformedBy = "Developer",
KeyRepresentation = "111"
};
// act
var result = await controller.Post(postAudit);
Assert.NotNull(result);
var okResult = result as OkObjectResult;
// assert
Assert.NotNull(okResult);
Assert.Equal(200, okResult.StatusCode);
}
[Fact]
public async void Post_Bad_Request()
{
// arrange
var postAudit = new AuditRecord()
{
Action = "action",
ClassName = "Something",
Description = "changed something field 1",
KeyRepresentation = "111"
};
controller.ModelState.AddModelError("PerformedBy", "The performed by field is required.");
// act
var result = await controller.Post(postAudit);
Assert.NotNull(result);
var badRequestResult = result as BadRequestObjectResult;
// assert
Assert.NotNull(badRequestResult);
Assert.Equal(StatusCodes.Status400BadRequest, badRequestResult.StatusCode);
}
}
}