From 91329b86531718b510ab923eb1005c600b362718 Mon Sep 17 00:00:00 2001 From: David Barker Date: Wed, 3 Mar 2021 16:04:49 +0800 Subject: [PATCH] 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 --- .gitignore | 8 ++++-- .../MongoIdentitySample.Mvc/appsettings.json | 2 +- ...entity.MongoDbCore.IntegrationTests.csproj | 26 ++++++++++--------- .../MongoDbStoreTestBase.cs | 17 +++++++----- .../UserStoreTest.cs | 12 ++++----- .../IdentitySpecificationTestBase.cs | 24 ++++++++++------- .../appsettings.json | 2 +- .../test.ps1 | 7 +++++ 8 files changed, 59 insertions(+), 39 deletions(-) create mode 100644 test/AspNetCore.Identity.MongoDbCore.IntegrationTests/test.ps1 diff --git a/.gitignore b/.gitignore index 827bbf5..c85705e 100644 --- a/.gitignore +++ b/.gitignore @@ -297,5 +297,9 @@ __pycache__/ *.btm.cs *.odx.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 \ No newline at end of file diff --git a/sample/MongoIdentitySample.Mvc/appsettings.json b/sample/MongoIdentitySample.Mvc/appsettings.json index 5c53f25..1e27be6 100644 --- a/sample/MongoIdentitySample.Mvc/appsettings.json +++ b/sample/MongoIdentitySample.Mvc/appsettings.json @@ -4,7 +4,7 @@ "DatabaseName": "MongoDbTests" }, "Logging": { - "IncludeScopes": false, + "IncludeScopes": {}, "LogLevel": { "Default": "Warning" } diff --git a/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/AspNetCore.Identity.MongoDbCore.IntegrationTests.csproj b/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/AspNetCore.Identity.MongoDbCore.IntegrationTests.csproj index 3c85947..78a7e6b 100644 --- a/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/AspNetCore.Identity.MongoDbCore.IntegrationTests.csproj +++ b/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/AspNetCore.Identity.MongoDbCore.IntegrationTests.csproj @@ -7,23 +7,21 @@ - - - - - - - - - - - - + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -35,4 +33,8 @@ Always + + + + diff --git a/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/AspNetCore.Identity.MongoDbCore.Test/MongoDbStoreTestBase.cs b/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/AspNetCore.Identity.MongoDbCore.Test/MongoDbStoreTestBase.cs index 68a7918..fa1f91e 100644 --- a/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/AspNetCore.Identity.MongoDbCore.Test/MongoDbStoreTestBase.cs +++ b/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/AspNetCore.Identity.MongoDbCore.Test/MongoDbStoreTestBase.cs @@ -6,23 +6,23 @@ using System.Linq; using System.Linq.Expressions; using System.Security.Claims; 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.Identity; using Microsoft.AspNetCore.Identity.Test; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -using Xunit; -using AspNetCore.Identity.MongoDbCore.Models; -using AspNetCore.Identity.MongoDbCore.Extensions; using MongoDB.Driver; -using AspNetCore.Identity.MongoDbCore.IntegrationTests.Infrastructure; using MongoDbGenericRepository; -using Microsoft.AspNetCore.Identity; +using Xunit; namespace AspNetCore.Identity.MongoDbCore.Test { // TODO: Add test variation with non IdentityDbContext - public abstract class MongoDbStoreTestBase : IdentitySpecificationTestBase, + public abstract class MongoDbStoreTestBase : IdentitySpecificationTestBase, IClassFixture> where TUser : MongoIdentityUser, new() where TRole : MongoIdentityRole, new() @@ -39,9 +39,12 @@ namespace AspNetCore.Identity.MongoDbCore.Test { services.AddSingleton(); // configure the default type name - services.ConfigureMongoDbIdentity(Container.MongoDbIdentityConfiguration, Container.MongoRepository.Context); + services.ConfigureMongoDbIdentity(Container.MongoDbIdentityConfiguration, Container.MongoRepository.Context) + .AddDefaultTokenProviders(); + services.AddAuthentication(); services.AddLogging(); + services.AddSingleton>>(new TestLogger>()); services.AddSingleton>>(new TestLogger>()); } diff --git a/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/AspNetCore.Identity.MongoDbCore.Test/UserStoreTest.cs b/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/AspNetCore.Identity.MongoDbCore.Test/UserStoreTest.cs index dd877af..5f8765a 100644 --- a/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/AspNetCore.Identity.MongoDbCore.Test/UserStoreTest.cs +++ b/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/AspNetCore.Identity.MongoDbCore.Test/UserStoreTest.cs @@ -4,13 +4,13 @@ using System; using System.Linq.Expressions; 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.Extensions.DependencyInjection; -using Xunit; -using AspNetCore.Identity.MongoDbCore.Models; -using AspNetCore.Identity.MongoDbCore.IntegrationTests.Infrastructure; using MongoDbGenericRepository; -using Microsoft.AspNetCore.Identity; +using Xunit; namespace AspNetCore.Identity.MongoDbCore.Test { @@ -308,8 +308,8 @@ namespace AspNetCore.Identity.MongoDbCore.Test { // Arrange const string originalEmail = "original@email.com"; - const string newEmail1 = "new1@email.com"; - const string newEmail2 = "new2@email.com"; + string newEmail1 = $"new{DateTime.Now.Ticks}@email.com"; + string newEmail2 = $"new{DateTime.Now.Ticks+1}@email.com"; var user = CreateTestUser(); user.Email = originalEmail; var manager = CreateManager(); diff --git a/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/Specification/IdentitySpecificationTestBase.cs b/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/Specification/IdentitySpecificationTestBase.cs index 64d1078..60479ce 100644 --- a/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/Specification/IdentitySpecificationTestBase.cs +++ b/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/Specification/IdentitySpecificationTestBase.cs @@ -7,14 +7,14 @@ using System.Linq; using System.Linq.Expressions; using System.Security.Claims; 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.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; 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 { @@ -48,7 +48,10 @@ namespace Microsoft.AspNetCore.Identity.Test protected override void SetupIdentityServices(IServiceCollection services) { services.AddSingleton(); - services.ConfigureMongoDbIdentity(Container.MongoDbIdentityConfiguration, Container.MongoRepository.Context); + services.ConfigureMongoDbIdentity(Container.MongoDbIdentityConfiguration, Container.MongoRepository.Context) + .AddDefaultTokenProviders(); + services.AddAuthentication(); + services.AddLogging(); services.AddSingleton>>(new TestLogger>()); services.AddSingleton>>(new TestLogger>()); @@ -64,14 +67,15 @@ namespace Microsoft.AspNetCore.Identity.Test services.AddSingleton(); if (concurrentSetup) { - services.ConfigureMongoDbIdentity(Container.MongoDbIdentityConfiguration, Container.MongoRepositoryConcurrent.Context); + services.ConfigureMongoDbIdentity(Container.MongoDbIdentityConfiguration, Container.MongoRepositoryConcurrent.Context).AddDefaultTokenProviders(); } else { - services.ConfigureMongoDbIdentity(Container.MongoDbIdentityConfiguration, Container.MongoRepository.Context); + services.ConfigureMongoDbIdentity(Container.MongoDbIdentityConfiguration, Container.MongoRepository.Context).AddDefaultTokenProviders(); } - + services.AddLogging(); + services.AddAuthentication(); services.AddSingleton>>(new TestLogger>()); services.AddSingleton>>(new TestLogger>()); } @@ -104,7 +108,7 @@ namespace Microsoft.AspNetCore.Identity.Test services = new ServiceCollection(); } - if(context == null) + if (context == null) { SetupIdentityServices(services); } @@ -112,7 +116,7 @@ namespace Microsoft.AspNetCore.Identity.Test { SetupIdentityServices(services, true); } - + return services.BuildServiceProvider().GetService>(); } diff --git a/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/appsettings.json b/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/appsettings.json index 5c53f25..1e27be6 100644 --- a/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/appsettings.json +++ b/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/appsettings.json @@ -4,7 +4,7 @@ "DatabaseName": "MongoDbTests" }, "Logging": { - "IncludeScopes": false, + "IncludeScopes": {}, "LogLevel": { "Default": "Warning" } diff --git a/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/test.ps1 b/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/test.ps1 new file mode 100644 index 0000000..dbc71a3 --- /dev/null +++ b/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/test.ps1 @@ -0,0 +1,7 @@ + +Write-Output "" +Write-Output "Running Tests with Code Coverage" +Write-Output "" + + +& 'dotnet' test /p:CollectCoverage=true