140 lines
6.1 KiB
C#
140 lines
6.1 KiB
C#
using Manager.Services;
|
|
using ManagerService.Controllers;
|
|
using ManagerService.Data;
|
|
using ManagerService.DTOs;
|
|
using ManagerService.Helpers;
|
|
using ManagerService.Tests.Infrastructure;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Xunit;
|
|
|
|
namespace ManagerService.Tests.Controllers
|
|
{
|
|
public class ApplicationInstanceControllerTests
|
|
{
|
|
private static ApplicationInstanceController BuildController(MyInfoMateDbContext db)
|
|
{
|
|
var cfg = FakeMongoConfig.Create();
|
|
var controller = new ApplicationInstanceController(
|
|
NullLogger<ApplicationInstanceController>.Instance,
|
|
new InstanceDatabaseService(cfg),
|
|
new UserDatabaseService(cfg),
|
|
new ProfileLogic(NullLogger<ProfileLogic>.Instance),
|
|
db);
|
|
FakeUser.SetUser(controller, FakeUser.Create("Manager.instanceadmin", "inst-test"));
|
|
return controller;
|
|
}
|
|
|
|
// ── CREATE ───────────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void Create_ValidDto_Persists()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
|
|
var result = BuildController(db).Create(new ApplicationInstanceDTO
|
|
{
|
|
instanceId = "inst-test",
|
|
appType = AppType.Tablet
|
|
});
|
|
|
|
Assert.IsType<OkObjectResult>(result);
|
|
Assert.Equal(1, db.ApplicationInstances.Count());
|
|
}
|
|
|
|
// ── UPDATE ───────────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void Update_ExistingAppInstance_UpdatesAppType()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
db.ApplicationInstances.Add(new ApplicationInstance { Id = "ai-1", InstanceId = "inst-test", AppType = AppType.Tablet });
|
|
db.SaveChanges();
|
|
|
|
var result = BuildController(db).Update(new ApplicationInstanceDTO
|
|
{
|
|
id = "ai-1",
|
|
instanceId = "inst-test",
|
|
appType = AppType.Web
|
|
});
|
|
|
|
Assert.IsType<OkObjectResult>(result);
|
|
Assert.Equal(AppType.Web, db.ApplicationInstances.First().AppType);
|
|
}
|
|
|
|
[Fact]
|
|
public void Update_UnknownId_Returns404()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
|
|
var result = BuildController(db).Update(new ApplicationInstanceDTO { id = "unknown" });
|
|
|
|
Assert.IsType<NotFoundObjectResult>(result);
|
|
}
|
|
|
|
// ── DELETE ───────────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void Delete_ExistingAppInstance_Returns202()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
db.ApplicationInstances.Add(new ApplicationInstance { Id = "ai-1", InstanceId = "inst-test", AppType = AppType.Tablet });
|
|
db.SaveChanges();
|
|
|
|
var result = BuildController(db).Delete("ai-1");
|
|
|
|
var obj = Assert.IsType<ObjectResult>(result);
|
|
Assert.Equal(202, obj.StatusCode);
|
|
Assert.Equal(0, db.ApplicationInstances.Count());
|
|
}
|
|
|
|
// ── CONFIG LINK ──────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void AddConfigLink_DuplicatePrevented_Returns409()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
db.ApplicationInstances.Add(new ApplicationInstance { Id = "ai-1", InstanceId = "inst-test", AppType = AppType.Tablet, Configurations = new List<AppConfigurationLink>() });
|
|
db.Configurations.Add(new Configuration { Id = "c1", InstanceId = "inst-test", Label = "C", Title = new List<TranslationDTO>() });
|
|
db.AppConfigurationLinks.Add(new AppConfigurationLink { Id = "lnk-1", ApplicationInstanceId = "ai-1", ConfigurationId = "c1" });
|
|
db.SaveChanges();
|
|
|
|
var result = BuildController(db).AddConfigurationToApplicationInstance("ai-1", new AppConfigurationLinkDTO
|
|
{
|
|
applicationInstanceId = "ai-1",
|
|
configurationId = "c1"
|
|
});
|
|
|
|
Assert.IsType<ConflictObjectResult>(result);
|
|
}
|
|
|
|
[Fact]
|
|
public void UpdateApplicationLinkOrder_ReordersCorrectly()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
db.Configurations.AddRange(
|
|
new Configuration { Id = "c1", InstanceId = "inst-test", Label = "C1", Title = new List<TranslationDTO>() },
|
|
new Configuration { Id = "c2", InstanceId = "inst-test", Label = "C2", Title = new List<TranslationDTO>() }
|
|
);
|
|
db.ApplicationInstances.Add(new ApplicationInstance { Id = "ai-1", InstanceId = "inst-test", AppType = AppType.Tablet });
|
|
db.AppConfigurationLinks.AddRange(
|
|
new AppConfigurationLink { Id = "lnk-1", ApplicationInstanceId = "ai-1", ConfigurationId = "c1", Order = 0 },
|
|
new AppConfigurationLink { Id = "lnk-2", ApplicationInstanceId = "ai-1", ConfigurationId = "c2", Order = 1 }
|
|
);
|
|
db.SaveChanges();
|
|
|
|
var result = BuildController(db).UpdateApplicationLinkOrder(new List<AppConfigurationLinkDTO>
|
|
{
|
|
new AppConfigurationLinkDTO { id = "lnk-1", order = 1, configurationId = "c1" },
|
|
new AppConfigurationLinkDTO { id = "lnk-2", order = 0, configurationId = "c2" }
|
|
});
|
|
|
|
Assert.IsType<OkObjectResult>(result);
|
|
Assert.Equal(1, db.AppConfigurationLinks.First(l => l.Id == "lnk-1").Order);
|
|
Assert.Equal(0, db.AppConfigurationLinks.First(l => l.Id == "lnk-2").Order);
|
|
}
|
|
}
|
|
}
|