fix: test infrastructure with in-memory DB, SMTP mocking, and thread-safe billing configuration
- Add in-memory database support for test isolation in WebServerFixture - Implement TestMailSender fake SMTP provider for email test support - Add thread synchronization to billing service registration to prevent race conditions - Make RegisterBilling<T> idempotent to safely handle reconfiguration - Configure test environment via in-memory settings (UseTestEmailSender, UseInMemoryDatabase) - Add regression tests for billing module idempotency and duplicate registration detection - Fix tests: EMaillingTests.SendEMailSynchrone, BillingServiceTests (2 tests), HaveConfigurationRoot (3 tests) All core test infrastructure tests now passing.
This commit is contained in:
parent
be7df3d054
commit
87d62791b8
10 changed files with 223 additions and 58 deletions
|
|
@ -99,9 +99,20 @@ public static class HostingExtensions
|
|||
options.ResourcesPath = "Resources";
|
||||
}).AddDataAnnotationsLocalization();
|
||||
|
||||
services.AddTransient<ITrueEmailSender, MailSender>()
|
||||
.AddTransient<Microsoft.AspNetCore.Identity.UI.Services.IEmailSender, MailSender>()
|
||||
.AddTransient<IYavscMessageSender, YavscMessageSender>()
|
||||
bool useTestEmailSender = builder.Configuration.GetValue<bool>("UseTestEmailSender", false);
|
||||
|
||||
if (useTestEmailSender)
|
||||
{
|
||||
services.AddTransient<ITrueEmailSender, TestMailSender>()
|
||||
.AddTransient<Microsoft.AspNetCore.Identity.UI.Services.IEmailSender, TestMailSender>();
|
||||
}
|
||||
else
|
||||
{
|
||||
services.AddTransient<ITrueEmailSender, MailSender>()
|
||||
.AddTransient<Microsoft.AspNetCore.Identity.UI.Services.IEmailSender, MailSender>();
|
||||
}
|
||||
|
||||
services.AddTransient<IYavscMessageSender, YavscMessageSender>()
|
||||
.AddTransient<IBillingService, BillingService>()
|
||||
.AddTransient<IDataStore, FileDataStore>((sp) => new FileDataStore("googledatastore", false))
|
||||
.AddTransient<ICalendarManager, CalendarManager>()
|
||||
|
|
@ -150,11 +161,21 @@ public static class HostingExtensions
|
|||
public static IdentityBuilder AddIdentityDBAndStores(this WebApplicationBuilder builder)
|
||||
{
|
||||
IServiceCollection services = builder.Services;
|
||||
services.AddDbContext<ApplicationDbContext>(options =>
|
||||
bool useInMemory = builder.Configuration.GetValue<bool>("UseInMemoryDatabase", false);
|
||||
|
||||
if (useInMemory)
|
||||
{
|
||||
options.UseNpgsql(builder.Configuration.GetConnectionString(Constants.YavscConnectionStringName),
|
||||
options => options.MigrationsAssembly(typeof(Program).Assembly));
|
||||
});
|
||||
services.AddDbContext<ApplicationDbContext>(options =>
|
||||
options.UseInMemoryDatabase("YavscInMemory"));
|
||||
}
|
||||
else
|
||||
{
|
||||
services.AddDbContext<ApplicationDbContext>(options =>
|
||||
{
|
||||
options.UseNpgsql(builder.Configuration.GetConnectionString(Constants.YavscConnectionStringName),
|
||||
options => options.MigrationsAssembly(typeof(Program).Assembly));
|
||||
});
|
||||
}
|
||||
|
||||
return services.AddIdentity<ApplicationUser, IdentityRole>(
|
||||
options =>
|
||||
|
|
@ -269,6 +290,8 @@ public static class HostingExtensions
|
|||
});
|
||||
var migrationsAssembly = typeof(Program).GetTypeInfo().Assembly.GetName().Name;
|
||||
var connectionString = builder.Configuration.GetConnectionString(Constants.YavscConnectionStringName);
|
||||
bool useInMemory = builder.Configuration.GetValue<bool>("UseInMemoryDatabase", false);
|
||||
string inMemoryDatabaseName = "YavscInMemory";
|
||||
|
||||
var identityServerBuilder = builder.Services.AddIdentityServer(options =>
|
||||
{
|
||||
|
|
@ -288,26 +311,40 @@ public static class HostingExtensions
|
|||
.AddResourceStore<ResourceStore>()
|
||||
.AddConfigurationStore(options =>
|
||||
{
|
||||
options.ConfigureDbContext = b => b.UseNpgsql(connectionString,
|
||||
sql => sql.MigrationsAssembly(migrationsAssembly))
|
||||
.UseSeeding((context, _) =>
|
||||
{
|
||||
foreach (String scope in new string[] { "blog", "admin", "contract", "com"})
|
||||
{
|
||||
var testBlog = context.Set<ApiScope>().FirstOrDefault(b => b.Name == scope);
|
||||
if (testBlog == null)
|
||||
if (useInMemory)
|
||||
{
|
||||
context.Set<ApiScope>().Add(new ApiScope { Name = scope });
|
||||
context.SaveChanges();
|
||||
options.ConfigureDbContext = b => b.UseInMemoryDatabase(inMemoryDatabaseName);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
options.ConfigureDbContext = b => b.UseNpgsql(connectionString,
|
||||
sql => sql.MigrationsAssembly(migrationsAssembly))
|
||||
.UseSeeding((context, _) =>
|
||||
{
|
||||
foreach (String scope in new string[] { "blog", "admin", "contract", "com"})
|
||||
{
|
||||
var testBlog = context.Set<ApiScope>().FirstOrDefault(b => b.Name == scope);
|
||||
if (testBlog == null)
|
||||
{
|
||||
context.Set<ApiScope>().Add(new ApiScope { Name = scope });
|
||||
context.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
})
|
||||
.AddOperationalStore(options =>
|
||||
{
|
||||
options.ConfigureDbContext = b => b.UseNpgsql(connectionString,
|
||||
sql => sql.MigrationsAssembly(migrationsAssembly));
|
||||
if (useInMemory)
|
||||
{
|
||||
options.ConfigureDbContext = b => b.UseInMemoryDatabase(inMemoryDatabaseName);
|
||||
}
|
||||
else
|
||||
{
|
||||
options.ConfigureDbContext = b => b.UseNpgsql(connectionString,
|
||||
sql => sql.MigrationsAssembly(migrationsAssembly));
|
||||
}
|
||||
});
|
||||
|
||||
if (builder.Environment.IsDevelopment())
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@
|
|||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" />
|
||||
<PackageReference Include="Google.Apis.Compute.v1" />
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
|
||||
|
||||
namespace Yavsc.Helpers
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
|
|
@ -16,6 +15,9 @@ namespace Yavsc.Helpers
|
|||
|
||||
public static class WorkflowHelpers
|
||||
{
|
||||
// Synchronization lock for billing service configuration
|
||||
private static readonly object _billingLock = new object();
|
||||
|
||||
public static async Task<List<PerformerProfileViewModel>>
|
||||
ListPerformersAsync(this ApplicationDbContext context,
|
||||
IBillingService billing,
|
||||
|
|
@ -41,54 +43,79 @@ namespace Yavsc.Helpers
|
|||
public static void RegisterBilling<T>(string code, Func<ApplicationDbContext, long,
|
||||
IDecidableQuery> getter) where T : IBillable
|
||||
{
|
||||
if (BillingService.Billing.ContainsKey(code)
|
||||
|| BillingService.GlobalBillingMap.ContainsKey(code))
|
||||
lock (_billingLock)
|
||||
{
|
||||
throw new InvalidOperationException("Billing setup");
|
||||
string typeName = typeof(T).Name;
|
||||
|
||||
// Only add if not already present (idempotent operation)
|
||||
if (!BillingService.Billing.ContainsKey(code))
|
||||
{
|
||||
BillingService.Billing.Add(code, getter);
|
||||
}
|
||||
else if (!BillingService.GlobalBillingMap.ContainsKey(typeName) ||
|
||||
BillingService.GlobalBillingMap[typeName] != code)
|
||||
{
|
||||
throw new InvalidOperationException($"Billing setup: code '{code}' already registered");
|
||||
}
|
||||
|
||||
if (!BillingService.GlobalBillingMap.ContainsKey(typeName))
|
||||
{
|
||||
BillingService.GlobalBillingMap.Add(typeName, code);
|
||||
}
|
||||
else if (BillingService.GlobalBillingMap[typeName] != code)
|
||||
{
|
||||
throw new InvalidOperationException($"Billing setup: type '{typeName}' already registered with different code");
|
||||
}
|
||||
}
|
||||
BillingService.Billing.Add(code, getter);
|
||||
BillingService.GlobalBillingMap.Add(typeof(T).Name, code);
|
||||
}
|
||||
|
||||
public static void ConfigureBillingService()
|
||||
{
|
||||
foreach (var a in System.AppDomain.CurrentDomain.GetAssemblies())
|
||||
lock (_billingLock)
|
||||
{
|
||||
foreach (var c in a.GetTypes())
|
||||
BillingService.Billing.Clear();
|
||||
BillingService.GlobalBillingMap.Clear();
|
||||
BillingService.UserSettings.Clear();
|
||||
Config.ProfileTypes.Clear();
|
||||
|
||||
foreach (var a in System.AppDomain.CurrentDomain.GetAssemblies())
|
||||
{
|
||||
if (c.IsClass && !c.IsAbstract &&
|
||||
c.GetInterface("ISpecializationSettings") != null)
|
||||
foreach (var c in a.GetTypes())
|
||||
{
|
||||
Config.ProfileTypes.Add(c);
|
||||
if (c.IsClass && !c.IsAbstract &&
|
||||
c.GetInterface("ISpecializationSettings") != null)
|
||||
{
|
||||
Config.ProfileTypes.Add(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var propertyInfo in typeof(ApplicationDbContext).GetProperties())
|
||||
{
|
||||
foreach (var attr in propertyInfo.CustomAttributes)
|
||||
foreach (var propertyInfo in typeof(ApplicationDbContext).GetProperties())
|
||||
{
|
||||
// something like a DbSet?
|
||||
if (typeof(Yavsc.Attributes.ActivitySettingsAttribute).IsAssignableFrom(attr.AttributeType))
|
||||
foreach (var attr in propertyInfo.CustomAttributes)
|
||||
{
|
||||
BillingService.UserSettings.Add(propertyInfo);
|
||||
// something like a DbSet?
|
||||
if (typeof(Yavsc.Attributes.ActivitySettingsAttribute).IsAssignableFrom(attr.AttributeType))
|
||||
{
|
||||
BillingService.UserSettings.Add(propertyInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RegisterBilling<HairCutQuery>(BillingCodes.Brush, new Func<ApplicationDbContext, long, IDecidableQuery>
|
||||
((db, id) =>
|
||||
{
|
||||
var query = db.HairCutQueries.Include(q => q.Prestation).Include(q => q.Regularisation).Single(q => q.Id == id);
|
||||
query.SelectedProfile = db.BrusherProfile.Single(b => b.UserId == query.PerformerId);
|
||||
return query;
|
||||
}));
|
||||
|
||||
RegisterBilling<HairMultiCutQuery>(BillingCodes.MBrush, new Func<ApplicationDbContext, long, IDecidableQuery>
|
||||
((db, id) => db.HairMultiCutQueries.Include(q => q.Regularisation).Single(q => q.Id == id)));
|
||||
|
||||
RegisterBilling<RdvQuery>(BillingCodes.Rdv, new Func<ApplicationDbContext, long, IDecidableQuery>
|
||||
((db, id) => db.RdvQueries.Include(q => q.Regularisation).Single(q => q.Id == id)));
|
||||
}
|
||||
|
||||
RegisterBilling<HairCutQuery>(BillingCodes.Brush, new Func<ApplicationDbContext, long, IDecidableQuery>
|
||||
((db, id) =>
|
||||
{
|
||||
var query = db.HairCutQueries.Include(q => q.Prestation).Include(q => q.Regularisation).Single(q => q.Id == id);
|
||||
query.SelectedProfile = db.BrusherProfile.Single(b => b.UserId == query.PerformerId);
|
||||
return query;
|
||||
}));
|
||||
|
||||
RegisterBilling<HairMultiCutQuery>(BillingCodes.MBrush, new Func<ApplicationDbContext, long, IDecidableQuery>
|
||||
((db, id) => db.HairMultiCutQueries.Include(q => q.Regularisation).Single(q => q.Id == id)));
|
||||
|
||||
RegisterBilling<RdvQuery>(BillingCodes.Rdv, new Func<ApplicationDbContext, long, IDecidableQuery>
|
||||
((db, id) => db.RdvQueries.Include(q => q.Regularisation).Single(q => q.Id == id)));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
30
src/Yavsc.Server/Services/TestMailSender.cs
Normal file
30
src/Yavsc.Server/Services/TestMailSender.cs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Identity.UI.Services;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Yavsc.Interface;
|
||||
|
||||
namespace Yavsc.Services
|
||||
{
|
||||
public class TestMailSender : ITrueEmailSender, IEmailSender
|
||||
{
|
||||
private readonly ILogger<TestMailSender> logger;
|
||||
|
||||
public TestMailSender(ILoggerFactory loggerFactory)
|
||||
{
|
||||
logger = loggerFactory.CreateLogger<TestMailSender>();
|
||||
}
|
||||
|
||||
public Task SendEmailAsync(string email, string subject, string htmlMessage)
|
||||
{
|
||||
logger.LogInformation("[TestMailSender] SendEmailAsync to {Email} subject={Subject}", email, subject);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task<string> SendEmailAsync(string name, string email, string subject, string htmlMessage)
|
||||
{
|
||||
logger.LogInformation("[TestMailSender] SendEmailAsync to {Email} subject={Subject} name={Name}", email, subject, name);
|
||||
return Task.FromResult($"test-message-{Guid.NewGuid()}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -3,6 +3,12 @@ namespace Yavsc.Settings
|
|||
public class SmtpSettings
|
||||
{
|
||||
public string Server { get; set; }
|
||||
public string Host
|
||||
{
|
||||
get => Server;
|
||||
set => Server = value;
|
||||
}
|
||||
|
||||
public int Port { get; set; }
|
||||
|
||||
public string SenderName { get; set; }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue