Having the clear locations endpoint only clear world locations, and adding endpoint to remove all items
This commit is contained in:
@@ -3,6 +3,7 @@ using GameAuthApi.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using GameAuthApi.Models;
|
||||
|
||||
public class AdminControllerTests
|
||||
{
|
||||
@@ -10,7 +11,7 @@ public class AdminControllerTests
|
||||
public async Task GetStatus_ReturnsOk()
|
||||
{
|
||||
var expected = new Dictionary<string, string> { { "status", "ok" } };
|
||||
var mockService = new Mock<AdminService>();
|
||||
var mockService = new Mock<AdminService>((LocationService)null, (ItemService)null, (PlayerService)null);
|
||||
mockService.Setup(s => s.GetStatusAsync()).ReturnsAsync(expected);
|
||||
var controller = new AdminController(mockService.Object);
|
||||
|
||||
@@ -19,5 +20,37 @@ public class AdminControllerTests
|
||||
var ok = Assert.IsType<OkObjectResult>(result);
|
||||
Assert.Equal(expected, ok.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ResetLocations_ReturnsOk()
|
||||
{
|
||||
var newLoc = new Location { LocationType = "World", X = 0, Y = 0, Z = 0 };
|
||||
var mockService = new Mock<AdminService>((LocationService)null, (ItemService)null, (PlayerService)null);
|
||||
mockService.Setup(s => s.ResetLocationsAsync()).ReturnsAsync(newLoc);
|
||||
var controller = new AdminController(mockService.Object);
|
||||
|
||||
var result = await controller.ResetLocations();
|
||||
|
||||
var ok = Assert.IsType<OkObjectResult>(result);
|
||||
var origin = Assert.IsType<Location>(ok.Value);
|
||||
Assert.Equal("World", origin.LocationType);
|
||||
Assert.Equal(0, origin.X);
|
||||
Assert.Equal(0, origin.Y);
|
||||
Assert.Equal(0, origin.Z);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ClearItems_ReturnsOk()
|
||||
{
|
||||
var mockService = new Mock<AdminService>((LocationService)null, (ItemService)null, (PlayerService)null);
|
||||
mockService.Setup(s => s.ClearItemsAsync()).Returns(Task.CompletedTask).Verifiable();
|
||||
var controller = new AdminController(mockService.Object);
|
||||
|
||||
var result = await controller.ClearItems();
|
||||
|
||||
var ok = Assert.IsType<OkObjectResult>(result);
|
||||
Assert.Equal("All items cleared.", ok.Value);
|
||||
mockService.Verify();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
using GameAuthApi.Services;
|
||||
using GameAuthApi.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
public class AdminServiceTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task ResetLocations_DeletesWorldLocationsOnly()
|
||||
{
|
||||
var worldLocs = new List<Location> { new() { Id = "1", LocationType = "World" } };
|
||||
|
||||
var cfg = new ConfigurationBuilder()
|
||||
.AddInMemoryCollection(new Dictionary<string, string>
|
||||
{
|
||||
{"MongoDB:ConnectionString", "mongodb://localhost"},
|
||||
{"MongoDB:DatabaseName", "test"}
|
||||
}).Build();
|
||||
|
||||
var locSvc = new Mock<LocationService>(cfg);
|
||||
locSvc.Setup(l => l.GetByTypeAsync("World")).ReturnsAsync(worldLocs);
|
||||
locSvc.Setup(l => l.DeleteByTypeAsync("World")).Returns(Task.CompletedTask).Verifiable();
|
||||
locSvc.Setup(l => l.CreateAsync(It.IsAny<Location>())).Returns(Task.CompletedTask);
|
||||
|
||||
var itemSvc = new Mock<ItemService>(cfg);
|
||||
itemSvc.Setup(i => i.DeleteByLocationAsync("1")).Returns(Task.CompletedTask).Verifiable();
|
||||
|
||||
var playerSvc = new Mock<PlayerService>(cfg);
|
||||
playerSvc.Setup(p => p.GetAllAsync()).ReturnsAsync(new List<Player>());
|
||||
|
||||
var adminSvc = new AdminService(locSvc.Object, itemSvc.Object, playerSvc.Object);
|
||||
|
||||
await adminSvc.ResetLocationsAsync();
|
||||
|
||||
locSvc.Verify();
|
||||
itemSvc.Verify();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ClearItems_RemovesAllItemsFromLocations()
|
||||
{
|
||||
var cfg = new ConfigurationBuilder()
|
||||
.AddInMemoryCollection(new Dictionary<string, string>
|
||||
{
|
||||
{"MongoDB:ConnectionString", "mongodb://localhost"},
|
||||
{"MongoDB:DatabaseName", "test"}
|
||||
}).Build();
|
||||
|
||||
var locSvc = new Mock<LocationService>(cfg);
|
||||
locSvc.Setup(l => l.ClearAllItemsAsync()).Returns(Task.CompletedTask).Verifiable();
|
||||
|
||||
var itemSvc = new Mock<ItemService>(cfg);
|
||||
itemSvc.Setup(i => i.DeleteAllAsync()).Returns(Task.CompletedTask).Verifiable();
|
||||
|
||||
var playerSvc = new Mock<PlayerService>(cfg);
|
||||
|
||||
var adminSvc = new AdminService(locSvc.Object, itemSvc.Object, playerSvc.Object);
|
||||
|
||||
await adminSvc.ClearItemsAsync();
|
||||
|
||||
locSvc.Verify();
|
||||
itemSvc.Verify();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user