126 lines
5.0 KiB
C#

using Manager.Services;
using ManagerService.Controllers;
using ManagerService.Data;
using ManagerService.DTOs;
using ManagerService.Services;
using ManagerService.Tests.Infrastructure;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging.Abstractions;
using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace ManagerService.Tests.Controllers
{
public class SectionControllerTests
{
private static IConfiguration BuildConfig() =>
new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?>
{
["ConnectionStrings:TabletDb"] = "mongodb://localhost:27017",
["SupportedLanguages:0"] = "FR",
["SupportedLanguages:1"] = "EN"
})
.Build();
private static SectionController BuildController(MyInfoMateDbContext db)
{
var cfg = FakeMongoConfig.Create();
var controller = new SectionController(
BuildConfig(),
NullLogger<SectionController>.Instance,
new SectionDatabaseService(cfg),
new ConfigurationDatabaseService(cfg),
db);
FakeUser.SetUser(controller, FakeUser.Create("Manager.contenteditor", "inst-test"));
return controller;
}
// ── GET ──────────────────────────────────────────────────────────────
[Fact]
public void Get_FiltersToInstance()
{
using var db = DbContextFactory.Create();
db.Sections.AddRange(
new Section { Id = "s1", InstanceId = "inst-test", Label = "A", Type = SectionType.Article, ConfigurationId = "c1", Title = new List<TranslationDTO>(), Description = new List<TranslationDTO>() },
new Section { Id = "s2", InstanceId = "other-inst", Label = "B", Type = SectionType.Article, ConfigurationId = "c2", Title = new List<TranslationDTO>(), Description = new List<TranslationDTO>() }
);
db.SaveChanges();
var result = BuildController(db).Get("inst-test");
var ok = Assert.IsType<OkObjectResult>(result);
var sections = (ok.Value as System.Collections.IEnumerable)!.Cast<object>().ToList();
Assert.Single(sections);
}
// ── CREATE ───────────────────────────────────────────────────────────
[Fact]
public void Create_ValidDto_Persists()
{
using var db = DbContextFactory.Create();
db.Configurations.Add(new Configuration { Id = "c1", InstanceId = "inst-test", Label = "Conf", Title = new List<TranslationDTO>() });
db.SaveChanges();
var result = BuildController(db).Create(new SectionDTO
{
instanceId = "inst-test",
configurationId = "c1",
label = "Section 1",
type = SectionType.Video
});
Assert.IsType<OkObjectResult>(result);
Assert.Equal(1, db.Sections.Count());
}
[Fact]
public void Create_UnknownConfiguration_ReturnsError()
{
using var db = DbContextFactory.Create();
// KeyNotFoundException → caught by catch(Exception) → 500
var result = BuildController(db).Create(new SectionDTO
{
instanceId = "inst-test",
configurationId = "nonexistent",
label = "Section 1",
type = SectionType.Video
});
var obj = Assert.IsType<ObjectResult>(result);
Assert.Equal(500, obj.StatusCode);
}
// ── DELETE ───────────────────────────────────────────────────────────
[Fact]
public void Delete_ExistingSection_Returns202()
{
using var db = DbContextFactory.Create();
db.Sections.Add(new Section { Id = "s1", InstanceId = "inst-test", Label = "A", Type = SectionType.Article, ConfigurationId = "c1", Title = new List<TranslationDTO>(), Description = new List<TranslationDTO>() });
db.SaveChanges();
var result = BuildController(db).Delete("s1");
var obj = Assert.IsType<ObjectResult>(result);
Assert.Equal(202, obj.StatusCode);
Assert.Equal(0, db.Sections.Count());
}
[Fact]
public void Delete_UnknownSection_Returns404()
{
using var db = DbContextFactory.Create();
var result = BuildController(db).Delete("unknown");
Assert.IsType<NotFoundObjectResult>(result);
}
}
}