- Added debug middleware for /connect/* endpoints to log request and response details for better troubleshooting. - Updated OAuth2 configuration to include "offline_access" scope and disabled access token encryption for development. - Improved DbContext registration in tests by removing all related registrations and ensuring in-memory database setup for testing purposes. - Addressed issues with the /connect/token endpoint not responding, outlining next steps for debugging and fixing the OpenIddict configuration.
57 lines
2.2 KiB
C#
57 lines
2.2 KiB
C#
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Mvc.Testing;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
using IamService.Infrastructure;
|
|
|
|
namespace IamService.FunctionalTests;
|
|
|
|
/// <summary>
|
|
/// EN: Custom WebApplicationFactory for functional tests.
|
|
/// VI: WebApplicationFactory tùy chỉnh cho functional tests.
|
|
/// </summary>
|
|
public class CustomWebApplicationFactory : WebApplicationFactory<Program>
|
|
{
|
|
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
|
{
|
|
builder.UseEnvironment("Testing");
|
|
|
|
builder.ConfigureServices(services =>
|
|
{
|
|
// EN: Remove ALL DbContext-related registrations
|
|
// VI: Xóa TẤT CẢ các đăng ký liên quan đến DbContext
|
|
var descriptorsToRemove = services.Where(
|
|
d => d.ServiceType == typeof(DbContextOptions<IamServiceContext>) ||
|
|
d.ServiceType == typeof(IamServiceContext) ||
|
|
d.ServiceType.FullName?.Contains("EntityFrameworkCore") == true ||
|
|
d.ImplementationType?.FullName?.Contains("Npgsql") == true)
|
|
.ToList();
|
|
|
|
foreach (var descriptor in descriptorsToRemove)
|
|
{
|
|
services.Remove(descriptor);
|
|
}
|
|
|
|
// EN: Remove the DbContextOptions generic
|
|
// VI: Xóa DbContextOptions generic
|
|
services.RemoveAll(typeof(DbContextOptions));
|
|
|
|
// EN: Add in-memory database for testing
|
|
// VI: Thêm in-memory database để test
|
|
services.AddDbContext<IamServiceContext>(options =>
|
|
{
|
|
options.UseInMemoryDatabase("TestDatabase_" + Guid.NewGuid().ToString());
|
|
options.EnableSensitiveDataLogging();
|
|
});
|
|
|
|
// EN: Ensure database is created with seed data
|
|
// VI: Đảm bảo database được tạo với seed data
|
|
var sp = services.BuildServiceProvider();
|
|
using var scope = sp.CreateScope();
|
|
var db = scope.ServiceProvider.GetRequiredService<IamServiceContext>();
|
|
db.Database.EnsureCreated();
|
|
});
|
|
}
|
|
}
|