Files
2025-06-04 22:03:01 -05:00

75 lines
2.4 KiB
C#

using GameAuthApi.Controllers;
using GameAuthApi.Models;
using GameAuthApi.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System.Security.Claims;
public class AuctionControllerTests
{
private static IConfiguration cfg = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string,string>
{
{"MongoDB:ConnectionString","mongodb://localhost"},
{"MongoDB:DatabaseName","test"}
}).Build();
private static ControllerContext Ctx(string id="u")
{
var ctx = new DefaultHttpContext();
ctx.User = new ClaimsPrincipal(new ClaimsIdentity(new[]
{
new Claim(ClaimTypes.NameIdentifier,id)
}, "mock"));
return new ControllerContext { HttpContext = ctx };
}
[Fact]
public async Task ListItem_InvalidItem_ReturnsBadRequest()
{
var itemSvc = new Mock<ItemService>(cfg);
itemSvc.Setup(i => i.GetByIdAsync("bad")).ReturnsAsync((Item?)null);
var controller = new AuctionController(new Mock<AuctionService>(cfg).Object, itemSvc.Object)
{
ControllerContext = Ctx()
};
var result = await controller.ListItem(new CreateAuctionRequest { ItemId="bad", StartingPrice=1 });
Assert.IsType<BadRequestObjectResult>(result);
}
[Fact]
public async Task PlaceBid_AuctionNotFound()
{
var aucSvc = new Mock<AuctionService>(cfg);
aucSvc.Setup(a => a.GetByIdAsync("a")).ReturnsAsync((Auction?)null);
var controller = new AuctionController(aucSvc.Object, new Mock<ItemService>(cfg).Object)
{
ControllerContext = Ctx()
};
var result = await controller.PlaceBid(new BidRequest { AuctionId="a", BidAmount=5 });
Assert.IsType<NotFoundObjectResult>(result);
}
[Fact]
public async Task GetActive_ReturnsOk()
{
var aucSvc = new Mock<AuctionService>(cfg);
var list = new List<Auction>();
aucSvc.Setup(a => a.GetActiveAsync()).ReturnsAsync(list);
var controller = new AuctionController(aucSvc.Object, new Mock<ItemService>(cfg).Object)
{
ControllerContext = Ctx()
};
var result = await controller.GetActive();
var ok = Assert.IsType<OkObjectResult>(result);
Assert.Equal(list, ok.Value);
}
}