fixes the current date at testing phase

This commit is contained in:
Paul Schneider 2026-04-19 19:14:37 +01:00
commit 65db349c0a
5 changed files with 46 additions and 22 deletions

View file

@ -33,6 +33,7 @@
<PackageVersion Include="Microsoft.AspNetCore.StaticFiles" Version="2.3.9" /> <PackageVersion Include="Microsoft.AspNetCore.StaticFiles" Version="2.3.9" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.7" /> <PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.7" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.7" /> <PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.7" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.7" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.7" /> <PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.7" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.6" /> <PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.6" />
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="10.0.6" /> <PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="10.0.6" />

View file

@ -152,7 +152,7 @@ public static class HostingExtensions
if (useInMemory) if (useInMemory)
{ {
services.AddDbContext<ApplicationDbContext>(options => services.AddDbContext<ApplicationDbContext>(options =>
options.UseInMemoryDatabase("YavscInMemory")); options.UseSqlite("Data Source=file::memory:?cache=shared"));
} }
else else
{ {
@ -281,7 +281,7 @@ public static class HostingExtensions
var migrationsAssembly = typeof(Program).GetTypeInfo().Assembly.GetName().Name; var migrationsAssembly = typeof(Program).GetTypeInfo().Assembly.GetName().Name;
var connectionString = builder.Configuration.GetConnectionString(Constants.YavscConnectionStringName); var connectionString = builder.Configuration.GetConnectionString(Constants.YavscConnectionStringName);
bool useInMemory = builder.Configuration.GetValue<bool>("UseInMemoryDatabase", false); bool useInMemory = builder.Configuration.GetValue<bool>("UseInMemoryDatabase", false);
string inMemoryDatabaseName = "YavscInMemory"; string sqliteInMemoryConnectionString = "Data Source=file::memory:?cache=shared";
var identityServerBuilder = builder.Services.AddIdentityServer(options => var identityServerBuilder = builder.Services.AddIdentityServer(options =>
{ {
@ -303,7 +303,7 @@ public static class HostingExtensions
{ {
if (useInMemory) if (useInMemory)
{ {
options.ConfigureDbContext = b => b.UseInMemoryDatabase(inMemoryDatabaseName); options.ConfigureDbContext = b => b.UseSqlite(sqliteInMemoryConnectionString);
} }
else else
{ {
@ -328,7 +328,7 @@ public static class HostingExtensions
{ {
if (useInMemory) if (useInMemory)
{ {
options.ConfigureDbContext = b => b.UseInMemoryDatabase(inMemoryDatabaseName); options.ConfigureDbContext = b => b.UseSqlite(sqliteInMemoryConnectionString);
} }
else else
{ {

View file

@ -32,6 +32,7 @@
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" /> <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" />
<PackageReference Include="Google.Apis.Compute.v1" /> <PackageReference Include="Google.Apis.Compute.v1" />

View file

@ -42,18 +42,30 @@ namespace Yavsc.Models
} }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{ {
if (Database.IsRelational())
{
Database.SetCommandTimeout(180);
}
} }
protected override void OnModelCreating(ModelBuilder builder) protected override void OnModelCreating(ModelBuilder builder)
{ {
base.OnModelCreating(builder); base.OnModelCreating(builder);
if (Database.IsNpgsql())
{
NOW_SQL="LOCALTIMESTAMP";
}
else
{
NOW_SQL="CURRENT_TIMESTAMP";
}
builder.UseIdentityByDefaultColumns(); builder.UseIdentityByDefaultColumns();
// Customize the ASP.NET Identity model and override the defaults if needed. // Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more. // For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder); // Add your customizations after calling base.OnModelCreating(builder);
builder.Entity<Contact>().HasKey(x => new { x.OwnerId, x.UserId }); builder.Entity<Contact>().HasKey(x => new { x.OwnerId, x.UserId });
builder.Entity<DeviceDeclaration>().Property(x => x.DeclarationDate).HasDefaultValueSql("LOCALTIMESTAMP"); builder.Entity<DeviceDeclaration>().Property(x => x.DeclarationDate).HasDefaultValueSql(NOW_SQL);
builder.Entity<BlogTag>().HasKey(x => new { x.PostId, x.TagId }); builder.Entity<BlogTag>().HasKey(x => new { x.PostId, x.TagId });
builder.Entity<ApplicationUser>().Property(u => u.FullName).IsRequired(false); builder.Entity<ApplicationUser>().Property(u => u.FullName).IsRequired(false);
@ -337,6 +349,6 @@ namespace Yavsc.Models
public DbSet<DeviceFlowCodes> DeviceFlowCodes { get; set; } public DbSet<DeviceFlowCodes> DeviceFlowCodes { get; set; }
public DbSet<YavscApiScope> YavscApiScopes { get; set; } public DbSet<YavscApiScope> YavscApiScopes { get; set; }
public string NOW_SQL { get; private set; }
} }
} }

View file

@ -1,3 +1,4 @@
using IdentityServer8.EntityFramework.DbContexts;
using IdentityServer8.EntityFramework.Entities; using IdentityServer8.EntityFramework.Entities;
using IdentityServer8.Models; using IdentityServer8.Models;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
@ -136,6 +137,12 @@ namespace isnd.tests
var builder = WebApplication.CreateBuilder(); var builder = WebApplication.CreateBuilder();
builder.Environment.EnvironmentName = "Development"; builder.Environment.EnvironmentName = "Development";
// Set ContentRoot to the Yavsc.Org project directory so WebRootPath resolves correctly
var testAssemblyLocation = AppDomain.CurrentDomain.BaseDirectory;
var yavscOrgPath = Path.GetFullPath(Path.Combine(testAssemblyLocation, "../../src/Yavsc.Org"));
builder.Environment.ContentRootPath = yavscOrgPath;
ConfigureLogger(); ConfigureLogger();
builder.Configuration builder.Configuration
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: false) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: false)
@ -260,8 +267,10 @@ namespace isnd.tests
private void AddAuthorizedClient(IServiceScope scope, string testClientId, string testClientSecret) private void AddAuthorizedClient(IServiceScope scope, string testClientId, string testClientSecret)
{ {
var configDb = scope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
if (configDb == null)
throw new InvalidOperationException("ConfigurationDbContext is not available for IdentityServer client seeding.");
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
Client testingClient = new Client Client testingClient = new Client
{ {
ClientId = testClientId, ClientId = testClientId,
@ -269,57 +278,58 @@ namespace isnd.tests
AccessTokenType = 1, AccessTokenType = 1,
BackChannelLogoutUri = SiteSettings!.Audience, BackChannelLogoutUri = SiteSettings!.Audience,
ClientName = "Testing client", ClientName = "Testing client",
Enabled = true Enabled = true,
RequireClientSecret = true
}; };
db.Clients.Add(testingClient); configDb.Clients.Add(testingClient);
db.SaveChanges(); configDb.SaveChanges();
ClientSecret secret = new ClientSecret ClientSecret secret = new ClientSecret
{ {
Value = testClientSecret.Sha256(), Value = testClientSecret.Sha256(),
Type = IdentityServer8.IdentityServerConstants.SecretTypes.SharedSecret,
ClientId = testingClient.Id ClientId = testingClient.Id
}; };
db.ClientSecrets.Add(secret); configDb.Set<ClientSecret>().Add(secret);
var testOrigin = new ClientCorsOrigin configDb.Set<ClientCorsOrigin>().Add(new ClientCorsOrigin
{ {
ClientId = testingClient.Id, ClientId = testingClient.Id,
Origin = SiteSettings!.Audience Origin = SiteSettings!.Audience
});
}; configDb.Set<ClientGrantType>().Add(new ClientGrantType
db.ClientCorsOrigins.Add(testOrigin);
db.ClientGrantTypes.Add(new ClientGrantType
{ {
ClientId = testingClient.Id, ClientId = testingClient.Id,
GrantType = "client_credentials" GrantType = "client_credentials"
}); });
db.ClientGrantTypes.Add(new ClientGrantType configDb.Set<ClientGrantType>().Add(new ClientGrantType
{ {
ClientId = testingClient.Id, ClientId = testingClient.Id,
GrantType = "password" GrantType = "password"
}); });
db.ClientGrantTypes.Add(new ClientGrantType configDb.Set<ClientGrantType>().Add(new ClientGrantType
{ {
ClientId = testingClient.Id, ClientId = testingClient.Id,
GrantType = "code" GrantType = "code"
}); });
db.ClientScopes.Add(new ClientScope configDb.Set<ClientScope>().Add(new ClientScope
{ {
ClientId = testingClient.Id, ClientId = testingClient.Id,
Scope = "test" Scope = "test"
}); });
db.ApiScopes.Add(new IdentityServer8.EntityFramework.Entities.ApiScope configDb.Set<IdentityServer8.EntityFramework.Entities.ApiScope>().Add(new IdentityServer8.EntityFramework.Entities.ApiScope
{ {
Name = "test", Name = "test",
Enabled = true Enabled = true
}); });
db.ClientRedirectUris.Add(new ClientRedirectUri configDb.Set<ClientRedirectUri>().Add(new ClientRedirectUri
{ {
ClientId = testingClient.Id, ClientId = testingClient.Id,
RedirectUri = SiteSettings!.Audience RedirectUri = SiteSettings!.Audience
}); });
db.SaveChanges(); configDb.SaveChanges();
} }
public void EnsureUser(string testingUserName, string password, string email, IServiceScope scope) public void EnsureUser(string testingUserName, string password, string email, IServiceScope scope)