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 { {"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(cfg); itemSvc.Setup(i => i.GetByIdAsync("bad")).ReturnsAsync((Item?)null); var controller = new AuctionController(new Mock(cfg).Object, itemSvc.Object) { ControllerContext = Ctx() }; var result = await controller.ListItem(new CreateAuctionRequest { ItemId="bad", StartingPrice=1 }); Assert.IsType(result); } [Fact] public async Task PlaceBid_AuctionNotFound() { var aucSvc = new Mock(cfg); aucSvc.Setup(a => a.GetByIdAsync("a")).ReturnsAsync((Auction?)null); var controller = new AuctionController(aucSvc.Object, new Mock(cfg).Object) { ControllerContext = Ctx() }; var result = await controller.PlaceBid(new BidRequest { AuctionId="a", BidAmount=5 }); Assert.IsType(result); } [Fact] public async Task GetActive_ReturnsOk() { var aucSvc = new Mock(cfg); var list = new List(); aucSvc.Setup(a => a.GetActiveAsync()).ReturnsAsync(list); var controller = new AuctionController(aucSvc.Object, new Mock(cfg).Object) { ControllerContext = Ctx() }; var result = await controller.GetActive(); var ok = Assert.IsType(result); Assert.Equal(list, ok.Value); } }