137 lines
5.5 KiB
C#
137 lines
5.5 KiB
C#
using Manager.Services;
|
|
using ManagerService.Controllers;
|
|
using ManagerService.Data;
|
|
using ManagerService.DTOs;
|
|
using ManagerService.Tests.Infrastructure;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using System.Linq;
|
|
using Xunit;
|
|
|
|
namespace ManagerService.Tests.Controllers
|
|
{
|
|
public class DeviceControllerTests
|
|
{
|
|
private static DeviceController BuildController(MyInfoMateDbContext db)
|
|
{
|
|
var cfg = FakeMongoConfig.Create();
|
|
var controller = new DeviceController(
|
|
NullLogger<DeviceController>.Instance,
|
|
new DeviceDatabaseService(cfg),
|
|
new ConfigurationDatabaseService(cfg),
|
|
db);
|
|
FakeUser.SetUser(controller, FakeUser.Create("Manager.instanceadmin", "inst-test"));
|
|
return controller;
|
|
}
|
|
|
|
private static void SeedPrerequisites(MyInfoMateDbContext db)
|
|
{
|
|
db.Configurations.Add(new Configuration { Id = "conf-1", InstanceId = "inst-test", Label = "Conf", Title = new System.Collections.Generic.List<TranslationDTO>() });
|
|
db.ApplicationInstances.Add(new ApplicationInstance { Id = "ai-1", InstanceId = "inst-test", AppType = AppType.Tablet });
|
|
db.SaveChanges();
|
|
}
|
|
|
|
// ── CREATE ───────────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void Create_NewIdentifier_CreatesDevice()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
SeedPrerequisites(db);
|
|
|
|
var result = BuildController(db).Create(new DeviceDetailDTO
|
|
{
|
|
identifier = "device-abc",
|
|
instanceId = "inst-test",
|
|
configurationId = "conf-1",
|
|
name = "Tablet 1"
|
|
});
|
|
|
|
Assert.IsType<OkObjectResult>(result);
|
|
Assert.Equal(1, db.Devices.Count());
|
|
Assert.Equal("Tablet 1", db.Devices.First().Name);
|
|
}
|
|
|
|
[Fact]
|
|
public void Create_ExistingIdentifier_UpdatesDeviceNoDuplicate()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
SeedPrerequisites(db);
|
|
db.Devices.Add(new Device { Id = "d1", Identifier = "device-abc", InstanceId = "inst-test", ConfigurationId = "conf-1" });
|
|
db.SaveChanges();
|
|
|
|
var result = BuildController(db).Create(new DeviceDetailDTO
|
|
{
|
|
identifier = "device-abc",
|
|
instanceId = "inst-test",
|
|
configurationId = "conf-1",
|
|
name = "Updated Name"
|
|
});
|
|
|
|
Assert.IsType<OkObjectResult>(result);
|
|
Assert.Equal(1, db.Devices.Count()); // pas de doublon
|
|
Assert.Equal("Updated Name", db.Devices.First().Name);
|
|
}
|
|
|
|
[Fact]
|
|
public void Create_NewDevice_AutoCreatesAppConfigurationLink()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
SeedPrerequisites(db);
|
|
|
|
BuildController(db).Create(new DeviceDetailDTO
|
|
{
|
|
identifier = "device-abc",
|
|
instanceId = "inst-test",
|
|
configurationId = "conf-1",
|
|
name = "Tablet 1"
|
|
});
|
|
|
|
Assert.Equal(1, db.AppConfigurationLinks.Count());
|
|
}
|
|
|
|
// ── UPDATE ───────────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void Update_ExistingDevice_UpdatesName()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
db.Configurations.Add(new Configuration { Id = "conf-1", InstanceId = "inst-test", Label = "Conf", Title = new System.Collections.Generic.List<TranslationDTO>() });
|
|
db.Devices.Add(new Device { Id = "d1", Identifier = "dev-1", InstanceId = "inst-test", Name = "Old Name", ConfigurationId = "conf-1" });
|
|
db.SaveChanges();
|
|
|
|
var result = BuildController(db).Update(new DeviceDetailDTO { id = "d1", name = "New Name", instanceId = "inst-test", identifier = "dev-1" });
|
|
|
|
Assert.IsType<OkObjectResult>(result);
|
|
Assert.Equal("New Name", db.Devices.First().Name);
|
|
}
|
|
|
|
// ── DELETE ───────────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void Delete_ExistingDevice_Returns202()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
db.Configurations.Add(new Configuration { Id = "conf-1", InstanceId = "inst-test", Label = "Conf", Title = new System.Collections.Generic.List<TranslationDTO>() });
|
|
db.Devices.Add(new Device { Id = "d1", Identifier = "dev-1", InstanceId = "inst-test", ConfigurationId = "conf-1" });
|
|
db.SaveChanges();
|
|
|
|
var result = BuildController(db).Delete("d1");
|
|
|
|
var obj = Assert.IsType<ObjectResult>(result);
|
|
Assert.Equal(202, obj.StatusCode);
|
|
Assert.Equal(0, db.Devices.Count());
|
|
}
|
|
|
|
[Fact]
|
|
public void Delete_UnknownId_Returns404()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
|
|
var result = BuildController(db).Delete("unknown");
|
|
|
|
Assert.IsType<NotFoundObjectResult>(result);
|
|
}
|
|
}
|
|
}
|