From 421ca9a38ef4fb04c2a1fcd1f8a34f9031688694 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 16 Feb 2018 11:24:18 +0300 Subject: [PATCH] Refactored Identity tests and fixed ProxyHelper --- .../Volo/Abp/DynamicProxy/ProxyHelper.cs | 7 +-- ...Volo.Abp.Identity.Application.Tests.csproj | 1 + .../AbpIdentityApplicationTestBase.cs | 11 +--- .../AbpIdentityApplicationTestModule.cs | 41 +------------- .../Volo.Abp.Identity.Tests.csproj | 1 + .../Abp/Identity/AbpIdentityCommonTestBase.cs | 14 +++++ .../Abp/Identity/AbpIdentityDomainTestBase.cs | 7 +++ .../Identity/AbpIdentityDomainTestModule.cs | 55 +++++++++++++++++++ .../Identity/AbpIdentityTestDataBuilder.cs | 0 .../Abp/Identity/AbpIdentityTestModule.cs | 32 ----------- ...s => Identity_Repository_Resolve_Tests.cs} | 11 ++-- .../Abp/Identity/PermissionManager_Tests.cs | 10 ++++ 12 files changed, 103 insertions(+), 87 deletions(-) create mode 100644 test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/AbpIdentityCommonTestBase.cs create mode 100644 test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/AbpIdentityDomainTestBase.cs create mode 100644 test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/AbpIdentityDomainTestModule.cs rename test/{Volo.Abp.Identity.Application.Tests => Volo.Abp.Identity.Tests}/Volo/Abp/Identity/AbpIdentityTestDataBuilder.cs (100%) delete mode 100644 test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/AbpIdentityTestModule.cs rename test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/{Initialize_Tests.cs => Identity_Repository_Resolve_Tests.cs} (59%) create mode 100644 test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/PermissionManager_Tests.cs diff --git a/src/Volo.Abp.Core/Volo/Abp/DynamicProxy/ProxyHelper.cs b/src/Volo.Abp.Core/Volo/Abp/DynamicProxy/ProxyHelper.cs index 06a2ae264b..27c839b023 100644 --- a/src/Volo.Abp.Core/Volo/Abp/DynamicProxy/ProxyHelper.cs +++ b/src/Volo.Abp.Core/Volo/Abp/DynamicProxy/ProxyHelper.cs @@ -7,18 +7,17 @@ namespace Volo.Abp.DynamicProxy { /// /// Returns dynamic proxy target object if this is a proxied object, otherwise returns the given object. + /// It supports Castle Dynamic Proxies. /// public static object UnProxy(object obj) { - //TODO: This code depends on Castle, so we should find a better way. - if (obj.GetType().Namespace != "Castle.Proxies") { return obj; } - var targetField = obj.GetType().GetTypeInfo() - .GetFields() + var targetField = obj.GetType() + .GetFields(BindingFlags.Instance | BindingFlags.NonPublic) .FirstOrDefault(f => f.Name == "__target"); if (targetField == null) diff --git a/test/Volo.Abp.Identity.Application.Tests/Volo.Abp.Identity.Application.Tests.csproj b/test/Volo.Abp.Identity.Application.Tests/Volo.Abp.Identity.Application.Tests.csproj index 5ac951c81d..9bb754c33e 100644 --- a/test/Volo.Abp.Identity.Application.Tests/Volo.Abp.Identity.Application.Tests.csproj +++ b/test/Volo.Abp.Identity.Application.Tests/Volo.Abp.Identity.Application.Tests.csproj @@ -16,6 +16,7 @@ + diff --git a/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/AbpIdentityApplicationTestBase.cs b/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/AbpIdentityApplicationTestBase.cs index 2bf77ad26d..efaa376f26 100644 --- a/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/AbpIdentityApplicationTestBase.cs +++ b/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/AbpIdentityApplicationTestBase.cs @@ -1,12 +1,7 @@ -using Volo.Abp.TestBase; - -namespace Volo.Abp.Identity +namespace Volo.Abp.Identity { - public class AbpIdentityApplicationTestBase : AbpIntegratedTest + public class AbpIdentityApplicationTestBase : AbpIdentityCommonTestBase { - protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) - { - options.UseAutofac(); - } + } } diff --git a/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/AbpIdentityApplicationTestModule.cs b/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/AbpIdentityApplicationTestModule.cs index 34a28327f2..5977aebf42 100644 --- a/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/AbpIdentityApplicationTestModule.cs +++ b/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/AbpIdentityApplicationTestModule.cs @@ -1,55 +1,20 @@ -using System; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Autofac; -using Volo.Abp.EntityFrameworkCore; using Volo.Abp.Identity.EntityFrameworkCore; using Volo.Abp.Modularity; -using Volo.Abp.Uow; namespace Volo.Abp.Identity { [DependsOn( typeof(AbpIdentityApplicationModule), typeof(AbpIdentityEntityFrameworkCoreModule), - typeof(AbpAutofacModule))] + typeof(AbpAutofacModule), + typeof(AbpIdentityDomainTestModule))] public class AbpIdentityApplicationTestModule : AbpModule { public override void ConfigureServices(IServiceCollection services) { services.AddAssemblyOf(); - - services.AddEntityFrameworkInMemoryDatabase(); - - var databaseName = Guid.NewGuid().ToString(); - - services.Configure(options => - { - options.Configure(context => - { - context.DbContextOptions.UseInMemoryDatabase(databaseName); - }); - }); - - services.Configure(options => - { - options.TransactionBehavior = UnitOfWorkTransactionBehavior.Disabled; //EF in-memory database does not support transactions - }); - } - - public override void OnApplicationInitialization(ApplicationInitializationContext context) - { - SeedTestData(context); - } - - private static void SeedTestData(ApplicationInitializationContext context) - { - using (var scope = context.ServiceProvider.CreateScope()) - { - scope.ServiceProvider - .GetRequiredService() - .Build(); - } } } } diff --git a/test/Volo.Abp.Identity.Tests/Volo.Abp.Identity.Tests.csproj b/test/Volo.Abp.Identity.Tests/Volo.Abp.Identity.Tests.csproj index 322b551671..db72b07307 100644 --- a/test/Volo.Abp.Identity.Tests/Volo.Abp.Identity.Tests.csproj +++ b/test/Volo.Abp.Identity.Tests/Volo.Abp.Identity.Tests.csproj @@ -12,6 +12,7 @@ + diff --git a/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/AbpIdentityCommonTestBase.cs b/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/AbpIdentityCommonTestBase.cs new file mode 100644 index 0000000000..92412904eb --- /dev/null +++ b/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/AbpIdentityCommonTestBase.cs @@ -0,0 +1,14 @@ +using Volo.Abp.Modularity; +using Volo.Abp.TestBase; + +namespace Volo.Abp.Identity +{ + public abstract class AbpIdentityCommonTestBase : AbpIntegratedTest + where TStartupModule : IAbpModule + { + protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) + { + options.UseAutofac(); + } + } +} diff --git a/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/AbpIdentityDomainTestBase.cs b/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/AbpIdentityDomainTestBase.cs new file mode 100644 index 0000000000..9a6f55b24d --- /dev/null +++ b/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/AbpIdentityDomainTestBase.cs @@ -0,0 +1,7 @@ +namespace Volo.Abp.Identity +{ + public abstract class AbpIdentityDomainTestBase : AbpIdentityCommonTestBase + { + + } +} \ No newline at end of file diff --git a/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/AbpIdentityDomainTestModule.cs b/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/AbpIdentityDomainTestModule.cs new file mode 100644 index 0000000000..f19bdded46 --- /dev/null +++ b/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/AbpIdentityDomainTestModule.cs @@ -0,0 +1,55 @@ +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Autofac; +using Volo.Abp.EntityFrameworkCore; +using Volo.Abp.Identity.EntityFrameworkCore; +using Volo.Abp.Modularity; +using Volo.Abp.Uow; + +namespace Volo.Abp.Identity +{ + [DependsOn( + typeof(AbpIdentityDomainModule), + typeof(AbpIdentityEntityFrameworkCoreModule), + typeof(AbpAutofacModule))] + public class AbpIdentityDomainTestModule : AbpModule + { + public override void ConfigureServices(IServiceCollection services) + { + services.AddEntityFrameworkInMemoryDatabase(); + + var databaseName = Guid.NewGuid().ToString(); + + services.Configure(options => + { + options.Configure(context => + { + context.DbContextOptions.UseInMemoryDatabase(databaseName); + }); + }); + + services.Configure(options => + { + options.TransactionBehavior = UnitOfWorkTransactionBehavior.Disabled; //EF in-memory database does not support transactions + }); + + services.AddAssemblyOf(); + } + + public override void OnApplicationInitialization(ApplicationInitializationContext context) + { + SeedTestData(context); + } + + private static void SeedTestData(ApplicationInitializationContext context) + { + using (var scope = context.ServiceProvider.CreateScope()) + { + scope.ServiceProvider + .GetRequiredService() + .Build(); + } + } + } +} diff --git a/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/AbpIdentityTestDataBuilder.cs b/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/AbpIdentityTestDataBuilder.cs similarity index 100% rename from test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/AbpIdentityTestDataBuilder.cs rename to test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/AbpIdentityTestDataBuilder.cs diff --git a/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/AbpIdentityTestModule.cs b/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/AbpIdentityTestModule.cs deleted file mode 100644 index a236b22779..0000000000 --- a/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/AbpIdentityTestModule.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; -using Volo.Abp.Data; -using Volo.Abp.EntityFrameworkCore; -using Volo.Abp.Identity.EntityFrameworkCore; -using Volo.Abp.Modularity; - -namespace Volo.Abp.Identity -{ - [DependsOn(typeof(AbpIdentityDomainModule), typeof(AbpIdentityEntityFrameworkCoreModule))] - public class AbpIdentityTestModule : AbpModule - { - public override void ConfigureServices(IServiceCollection services) - { - services.AddEntityFrameworkInMemoryDatabase(); - - services.Configure(options => - { - options.ConnectionStrings.Default = Guid.NewGuid().ToString(); - }); - - services.Configure(options => - { - options.Configure(context => - { - context.DbContextOptions.UseInMemoryDatabase(context.ConnectionString); - }); - }); - } - } -} diff --git a/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/Initialize_Tests.cs b/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/Identity_Repository_Resolve_Tests.cs similarity index 59% rename from test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/Initialize_Tests.cs rename to test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/Identity_Repository_Resolve_Tests.cs index 69b50be7f7..7c043c0532 100644 --- a/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/Initialize_Tests.cs +++ b/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/Identity_Repository_Resolve_Tests.cs @@ -2,12 +2,12 @@ using Microsoft.Extensions.DependencyInjection; using Shouldly; using Volo.Abp.Domain.Repositories; -using Volo.Abp.TestBase; +using Volo.Abp.DynamicProxy; using Xunit; namespace Volo.Abp.Identity { - public class Initialize_Tests : AbpIntegratedTest + public class Identity_Repository_Resolve_Tests : AbpIdentityDomainTestBase { [Fact] public void Should_Resolve_UserManager() @@ -24,12 +24,13 @@ namespace Volo.Abp.Identity [Fact] //Move this test to Volo.Abp.EntityFrameworkCore.Tests since it's actually testing the EF Core repository registration! public void Should_Resolve_Repositories() { - (ServiceProvider.GetRequiredService() is EfCoreIdentityUserRepository).ShouldBeTrue(); + var x = ServiceProvider.GetRequiredService(); + (ProxyHelper.UnProxy(ServiceProvider.GetRequiredService()) is EfCoreIdentityUserRepository).ShouldBeTrue(); - (ServiceProvider.GetRequiredService>() is EfCoreIdentityUserRepository).ShouldBeTrue(); + (ProxyHelper.UnProxy(ServiceProvider.GetRequiredService>()) is EfCoreIdentityUserRepository).ShouldBeTrue(); //(ServiceProvider.GetRequiredService>() is EfCoreIdentityUserRepository).ShouldBeTrue(); - (ServiceProvider.GetRequiredService>() is EfCoreIdentityUserRepository).ShouldBeTrue(); + (ProxyHelper.UnProxy(ServiceProvider.GetRequiredService>()) is EfCoreIdentityUserRepository).ShouldBeTrue(); //(ServiceProvider.GetRequiredService>() is EfCoreIdentityUserRepository).ShouldBeTrue(); } } diff --git a/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/PermissionManager_Tests.cs b/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/PermissionManager_Tests.cs new file mode 100644 index 0000000000..0e74cc6f5b --- /dev/null +++ b/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/PermissionManager_Tests.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Volo.Abp.Identity +{ + public class PermissionManager_Tests + { + } +}