Updated tests based on changes to AspNet Identity Core

Added .AddDefaultTokenProviders() after the call to ConfigureMongoDbIdentity
Note: There are four places in the test project
Added .AddAuthentication() to register all the authentication types
Fixed aspnetsettings.json IncludeScopes to empty object, not false
Updated unit test packages to latest version
Added project reference for unit test rather than package reference
Removed all references to AspNet framework assemblies as these are now part of the netcoreapp3.1
This commit is contained in:
David Barker
2021-03-03 16:04:49 +08:00
parent c39a8b3109
commit 91329b8653
8 changed files with 59 additions and 39 deletions
+6 -2
View File
@@ -297,5 +297,9 @@ __pycache__/
*.btm.cs *.btm.cs
*.odx.cs *.odx.cs
*.xsd.cs *.xsd.cs
sample/MongoIdentitySample.Mvc/appsettings.local.json
test/AspNetCore.Identity.MongoDbCore.IntegrationTests/appsettings.local.json # Local app settings for development
**/**/appsettings.local.json
# Output from the code coverage analysis
**/**/coverage.json
@@ -4,7 +4,7 @@
"DatabaseName": "MongoDbTests" "DatabaseName": "MongoDbTests"
}, },
"Logging": { "Logging": {
"IncludeScopes": false, "IncludeScopes": {},
"LogLevel": { "LogLevel": {
"Default": "Warning" "Default": "Warning"
} }
@@ -7,23 +7,21 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="AspNetCore.Identity.MongoDbCore" Version="2.1.1" /> <PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.12" />
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.2.0" /> <PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="3.1.12" />
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.7" /> <PackageReference Include="Microsoft.Extensions.Identity.Core" Version="3.1.12" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" /> <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.1.12" />
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" /> <PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.1.12" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
<PackageReference Include="MongoDB.Driver" Version="2.10.1" /> <PackageReference Include="MongoDB.Driver" Version="2.10.1" />
<PackageReference Include="MongoDbGenericRepository" Version="1.4.3" /> <PackageReference Include="MongoDbGenericRepository" Version="1.4.3" />
<PackageReference Include="Moq" Version="4.13.1" /> <PackageReference Include="Moq" Version="4.13.1" />
<PackageReference Include="xunit" Version="2.4.1" /> <PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="coverlet.msbuild" Version="2.9.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="dotnet-reportgenerator-cli" Version="4.6.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1"> <PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
@@ -35,4 +33,8 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content> </Content>
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\AspNetCore.Identity.MongoDbCore.csproj" />
</ItemGroup>
</Project> </Project>
@@ -6,17 +6,17 @@ using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Security.Claims; using System.Security.Claims;
using System.Threading.Tasks; using System.Threading.Tasks;
using AspNetCore.Identity.MongoDbCore.Extensions;
using AspNetCore.Identity.MongoDbCore.IntegrationTests.Infrastructure;
using AspNetCore.Identity.MongoDbCore.Models;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.Test; using Microsoft.AspNetCore.Identity.Test;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Xunit;
using AspNetCore.Identity.MongoDbCore.Models;
using AspNetCore.Identity.MongoDbCore.Extensions;
using MongoDB.Driver; using MongoDB.Driver;
using AspNetCore.Identity.MongoDbCore.IntegrationTests.Infrastructure;
using MongoDbGenericRepository; using MongoDbGenericRepository;
using Microsoft.AspNetCore.Identity; using Xunit;
namespace AspNetCore.Identity.MongoDbCore.Test namespace AspNetCore.Identity.MongoDbCore.Test
{ {
@@ -39,9 +39,12 @@ namespace AspNetCore.Identity.MongoDbCore.Test
{ {
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
// configure the default type name // configure the default type name
services.ConfigureMongoDbIdentity<TUser, TRole, TKey>(Container.MongoDbIdentityConfiguration, Container.MongoRepository.Context); services.ConfigureMongoDbIdentity<TUser, TRole, TKey>(Container.MongoDbIdentityConfiguration, Container.MongoRepository.Context)
.AddDefaultTokenProviders();
services.AddAuthentication();
services.AddLogging(); services.AddLogging();
services.AddSingleton<ILogger<UserManager<TUser>>>(new TestLogger<UserManager<TUser>>()); services.AddSingleton<ILogger<UserManager<TUser>>>(new TestLogger<UserManager<TUser>>());
services.AddSingleton<ILogger<RoleManager<TRole>>>(new TestLogger<RoleManager<TRole>>()); services.AddSingleton<ILogger<RoleManager<TRole>>>(new TestLogger<RoleManager<TRole>>());
} }
@@ -4,13 +4,13 @@
using System; using System;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using AspNetCore.Identity.MongoDbCore.IntegrationTests.Infrastructure;
using AspNetCore.Identity.MongoDbCore.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.Test; using Microsoft.AspNetCore.Identity.Test;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Xunit;
using AspNetCore.Identity.MongoDbCore.Models;
using AspNetCore.Identity.MongoDbCore.IntegrationTests.Infrastructure;
using MongoDbGenericRepository; using MongoDbGenericRepository;
using Microsoft.AspNetCore.Identity; using Xunit;
namespace AspNetCore.Identity.MongoDbCore.Test namespace AspNetCore.Identity.MongoDbCore.Test
{ {
@@ -308,8 +308,8 @@ namespace AspNetCore.Identity.MongoDbCore.Test
{ {
// Arrange // Arrange
const string originalEmail = "original@email.com"; const string originalEmail = "original@email.com";
const string newEmail1 = "new1@email.com"; string newEmail1 = $"new{DateTime.Now.Ticks}@email.com";
const string newEmail2 = "new2@email.com"; string newEmail2 = $"new{DateTime.Now.Ticks+1}@email.com";
var user = CreateTestUser(); var user = CreateTestUser();
user.Email = originalEmail; user.Email = originalEmail;
var manager = CreateManager(); var manager = CreateManager();
@@ -7,14 +7,14 @@ using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Security.Claims; using System.Security.Claims;
using System.Threading.Tasks; using System.Threading.Tasks;
using AspNetCore.Identity.MongoDbCore.Extensions;
using AspNetCore.Identity.MongoDbCore.Infrastructure;
using AspNetCore.Identity.MongoDbCore.IntegrationTests.Infrastructure;
using AspNetCore.Identity.MongoDbCore.Models;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Xunit; using Xunit;
using AspNetCore.Identity.MongoDbCore.Models;
using AspNetCore.Identity.MongoDbCore.Extensions;
using AspNetCore.Identity.MongoDbCore.Infrastructure;
using AspNetCore.Identity.MongoDbCore.IntegrationTests.Infrastructure;
namespace Microsoft.AspNetCore.Identity.Test namespace Microsoft.AspNetCore.Identity.Test
{ {
@@ -48,7 +48,10 @@ namespace Microsoft.AspNetCore.Identity.Test
protected override void SetupIdentityServices(IServiceCollection services) protected override void SetupIdentityServices(IServiceCollection services)
{ {
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.ConfigureMongoDbIdentity<TUser, TRole, TKey>(Container.MongoDbIdentityConfiguration, Container.MongoRepository.Context); services.ConfigureMongoDbIdentity<TUser, TRole, TKey>(Container.MongoDbIdentityConfiguration, Container.MongoRepository.Context)
.AddDefaultTokenProviders();
services.AddAuthentication();
services.AddLogging(); services.AddLogging();
services.AddSingleton<ILogger<UserManager<TUser>>>(new TestLogger<UserManager<TUser>>()); services.AddSingleton<ILogger<UserManager<TUser>>>(new TestLogger<UserManager<TUser>>());
services.AddSingleton<ILogger<RoleManager<TRole>>>(new TestLogger<RoleManager<TRole>>()); services.AddSingleton<ILogger<RoleManager<TRole>>>(new TestLogger<RoleManager<TRole>>());
@@ -64,14 +67,15 @@ namespace Microsoft.AspNetCore.Identity.Test
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
if (concurrentSetup) if (concurrentSetup)
{ {
services.ConfigureMongoDbIdentity<TUser, TRole, TKey>(Container.MongoDbIdentityConfiguration, Container.MongoRepositoryConcurrent.Context); services.ConfigureMongoDbIdentity<TUser, TRole, TKey>(Container.MongoDbIdentityConfiguration, Container.MongoRepositoryConcurrent.Context).AddDefaultTokenProviders();
} }
else else
{ {
services.ConfigureMongoDbIdentity<TUser, TRole, TKey>(Container.MongoDbIdentityConfiguration, Container.MongoRepository.Context); services.ConfigureMongoDbIdentity<TUser, TRole, TKey>(Container.MongoDbIdentityConfiguration, Container.MongoRepository.Context).AddDefaultTokenProviders();
} }
services.AddLogging(); services.AddLogging();
services.AddAuthentication();
services.AddSingleton<ILogger<UserManager<TUser>>>(new TestLogger<UserManager<TUser>>()); services.AddSingleton<ILogger<UserManager<TUser>>>(new TestLogger<UserManager<TUser>>());
services.AddSingleton<ILogger<RoleManager<TRole>>>(new TestLogger<RoleManager<TRole>>()); services.AddSingleton<ILogger<RoleManager<TRole>>>(new TestLogger<RoleManager<TRole>>());
} }
@@ -104,7 +108,7 @@ namespace Microsoft.AspNetCore.Identity.Test
services = new ServiceCollection(); services = new ServiceCollection();
} }
if(context == null) if (context == null)
{ {
SetupIdentityServices(services); SetupIdentityServices(services);
} }
@@ -4,7 +4,7 @@
"DatabaseName": "MongoDbTests" "DatabaseName": "MongoDbTests"
}, },
"Logging": { "Logging": {
"IncludeScopes": false, "IncludeScopes": {},
"LogLevel": { "LogLevel": {
"Default": "Warning" "Default": "Warning"
} }
@@ -0,0 +1,7 @@
Write-Output ""
Write-Output "Running Tests with Code Coverage"
Write-Output ""
& 'dotnet' test /p:CollectCoverage=true