Add appsettings config load for tests
Enables the config to be changed outside of the test appsettings.local.json is not checked into the repo
This commit is contained in:
@@ -298,3 +298,4 @@ __pycache__/
|
|||||||
*.odx.cs
|
*.odx.cs
|
||||||
*.xsd.cs
|
*.xsd.cs
|
||||||
sample/MongoIdentitySample.Mvc/appsettings.local.json
|
sample/MongoIdentitySample.Mvc/appsettings.local.json
|
||||||
|
test/AspNetCore.Identity.MongoDbCore.IntegrationTests/appsettings.local.json
|
||||||
|
|||||||
+5
@@ -33,4 +33,9 @@
|
|||||||
<ProjectReference Include="..\..\src\AspNetCore.Identity.MongoDbCore.csproj" />
|
<ProjectReference Include="..\..\src\AspNetCore.Identity.MongoDbCore.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Content Include="appsettings*.json">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using AspNetCore.Identity.MongoDbCore.Extensions;
|
using AspNetCore.Identity.MongoDbCore.Extensions;
|
||||||
using AspNetCore.Identity.MongoDbCore.Infrastructure;
|
using AspNetCore.Identity.MongoDbCore.Infrastructure;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace AspNetCore.Identity.MongoDbCore.IntegrationTests.Infrastructure
|
namespace AspNetCore.Identity.MongoDbCore.IntegrationTests.Infrastructure
|
||||||
@@ -12,22 +13,47 @@ namespace AspNetCore.Identity.MongoDbCore.IntegrationTests.Infrastructure
|
|||||||
|
|
||||||
public static class Container
|
public static class Container
|
||||||
{
|
{
|
||||||
public static MongoDbIdentityConfiguration MongoDbIdentityConfiguration = new MongoDbIdentityConfiguration
|
public static IConfiguration Configuration { get; set; }
|
||||||
|
|
||||||
|
static Container()
|
||||||
{
|
{
|
||||||
MongoDbSettings = new MongoDbSettings
|
var builder = new ConfigurationBuilder()
|
||||||
|
.SetBasePath(System.Environment.CurrentDirectory)
|
||||||
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
||||||
|
//per user config that is not committed to repo, use this to override settings (e.g. connection string) based on your local environment.
|
||||||
|
.AddJsonFile($"appsettings.local.json", optional: true);
|
||||||
|
|
||||||
|
//builder.AddEnvironmentVariables();
|
||||||
|
|
||||||
|
Configuration = builder.Build();
|
||||||
|
|
||||||
|
var databaseSettings = Configuration.Load<MongoDbSettings>("MongoDbSettings");
|
||||||
|
|
||||||
|
MongoDbIdentityConfiguration = new MongoDbIdentityConfiguration()
|
||||||
{
|
{
|
||||||
ConnectionString = "mongodb://localhost:27017",
|
MongoDbSettings = databaseSettings,
|
||||||
DatabaseName = "MongoDbTests"
|
IdentityOptionsAction = (options) =>
|
||||||
},
|
{
|
||||||
IdentityOptionsAction = options =>
|
options.Password.RequireDigit = false;
|
||||||
|
options.Password.RequireLowercase = false;
|
||||||
|
options.Password.RequireNonAlphanumeric = false;
|
||||||
|
options.Password.RequireUppercase = false;
|
||||||
|
options.User.AllowedUserNameCharacters = null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
lock (Locks.MongoInitLock)
|
||||||
{
|
{
|
||||||
options.Password.RequireDigit = false;
|
_mongoDbRepository = new MongoRepository(
|
||||||
options.Password.RequireLowercase = false;
|
databaseSettings.ConnectionString,
|
||||||
options.Password.RequireNonAlphanumeric = false;
|
databaseSettings.DatabaseName);
|
||||||
options.Password.RequireUppercase = false;
|
_mongoDbRepository2 = new MongoRepository(
|
||||||
options.User.AllowedUserNameCharacters = null;
|
databaseSettings.ConnectionString,
|
||||||
|
databaseSettings.DatabaseName);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
|
public static MongoDbIdentityConfiguration MongoDbIdentityConfiguration { get; set; }
|
||||||
|
|
||||||
public static IServiceProvider Instance { get; set; }
|
public static IServiceProvider Instance { get; set; }
|
||||||
|
|
||||||
@@ -36,19 +62,6 @@ namespace AspNetCore.Identity.MongoDbCore.IntegrationTests.Infrastructure
|
|||||||
|
|
||||||
private static readonly IMongoRepository _mongoDbRepository2;
|
private static readonly IMongoRepository _mongoDbRepository2;
|
||||||
|
|
||||||
static Container()
|
|
||||||
{
|
|
||||||
lock (Locks.MongoInitLock)
|
|
||||||
{
|
|
||||||
_mongoDbRepository = new MongoRepository(
|
|
||||||
MongoDbIdentityConfiguration.MongoDbSettings.ConnectionString,
|
|
||||||
MongoDbIdentityConfiguration.MongoDbSettings.DatabaseName);
|
|
||||||
_mongoDbRepository2 = new MongoRepository(
|
|
||||||
MongoDbIdentityConfiguration.MongoDbSettings.ConnectionString,
|
|
||||||
MongoDbIdentityConfiguration.MongoDbSettings.DatabaseName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static IMongoRepository MongoRepository
|
public static IMongoRepository MongoRepository
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
@@ -65,4 +78,20 @@ namespace AspNetCore.Identity.MongoDbCore.IntegrationTests.Infrastructure
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class ConfigurationExtensions
|
||||||
|
{
|
||||||
|
public static T Load<T>(this IConfiguration configuration, string key) where T : new()
|
||||||
|
{
|
||||||
|
var instance = new T();
|
||||||
|
configuration.GetSection(key).Bind(instance);
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static T Load<T>(this IConfiguration configuration, string key, T instance) where T : new()
|
||||||
|
{
|
||||||
|
configuration.GetSection(key).Bind(instance);
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"MongoDbSettings": {
|
||||||
|
"ConnectionString": "mongodb://localhost:27017",
|
||||||
|
"DatabaseName": "MongoDbTests"
|
||||||
|
},
|
||||||
|
"Logging": {
|
||||||
|
"IncludeScopes": false,
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Warning"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user