From 57d037def5c680a9b2c7535ab4e5a01231d5b396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 8 Dec 2016 22:44:35 +0300 Subject: [PATCH] Added tests. --- Volo.Abp.sln | 7 ++ .../AbpAspNetMultiTenancyModule.cs | 15 ++++ .../project.json | 3 +- .../Abp/MultiTenancy/AbpMultiTenancyModule.cs | 13 +++ .../Abp/MultiTenancy/MultiTenancyManager.cs | 3 +- src/Volo.Abp/Volo/Abp/Json/JsonExtensions.cs | 31 +++++++ src/Volo.Abp/project.json | 3 +- .../Properties/AssemblyInfo.cs | 19 +++++ ...lo.Abp.AspNetCore.MultiTenancy.Tests.xproj | 23 ++++++ .../Volo/Abp/AspNetCore/App/AppModule.cs | 33 ++++++++ .../Volo/Abp/AspNetCore/App/AppTestBase.cs | 35 ++++++++ .../Volo/Abp/AspNetCore/App/Startup.cs | 20 +++++ .../AspNetCoreMultiTenancy_Tests.cs | 22 +++++ .../AbpAspNetCoreIntegratedTestBase.cs | 80 +++++++++++++++++++ .../project.json | 23 ++++++ 15 files changed, 327 insertions(+), 3 deletions(-) create mode 100644 src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AbpAspNetMultiTenancyModule.cs create mode 100644 src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/AbpMultiTenancyModule.cs create mode 100644 src/Volo.Abp/Volo/Abp/Json/JsonExtensions.cs create mode 100644 test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Properties/AssemblyInfo.cs create mode 100644 test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo.Abp.AspNetCore.MultiTenancy.Tests.xproj create mode 100644 test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo/Abp/AspNetCore/App/AppModule.cs create mode 100644 test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo/Abp/AspNetCore/App/AppTestBase.cs create mode 100644 test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo/Abp/AspNetCore/App/Startup.cs create mode 100644 test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo/Abp/AspNetCore/MultiTenancy/AspNetCoreMultiTenancy_Tests.cs create mode 100644 test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo/Abp/AspNetCore/TestBase/AbpAspNetCoreIntegratedTestBase.cs create mode 100644 test/Volo.Abp.AspNetCore.MultiTenancy.Tests/project.json diff --git a/Volo.Abp.sln b/Volo.Abp.sln index d6a627317f..502d2dde2b 100644 --- a/Volo.Abp.sln +++ b/Volo.Abp.sln @@ -48,6 +48,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Volo.Abp.MultiTenancy.Tests EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Volo.Abp.AspNetCore.MultiTenancy", "src\Volo.Abp.AspNetCore.MultiTenancy\Volo.Abp.AspNetCore.MultiTenancy.xproj", "{7CC7946B-E026-4F66-8D4F-4F78F4801D43}" EndProject +Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Volo.Abp.AspNetCore.MultiTenancy.Tests", "test\Volo.Abp.AspNetCore.MultiTenancy.Tests\Volo.Abp.AspNetCore.MultiTenancy.Tests.xproj", "{2C282467-2CD5-4750-BE1F-CA8BD8ECC6EA}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -106,6 +108,10 @@ Global {7CC7946B-E026-4F66-8D4F-4F78F4801D43}.Debug|Any CPU.Build.0 = Debug|Any CPU {7CC7946B-E026-4F66-8D4F-4F78F4801D43}.Release|Any CPU.ActiveCfg = Release|Any CPU {7CC7946B-E026-4F66-8D4F-4F78F4801D43}.Release|Any CPU.Build.0 = Release|Any CPU + {2C282467-2CD5-4750-BE1F-CA8BD8ECC6EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2C282467-2CD5-4750-BE1F-CA8BD8ECC6EA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2C282467-2CD5-4750-BE1F-CA8BD8ECC6EA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2C282467-2CD5-4750-BE1F-CA8BD8ECC6EA}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -129,5 +135,6 @@ Global {58FA9F8F-216D-4C93-8929-D40D22B11CA7} = {4C753F64-0C93-4D65-96C2-A40893AFC1E8} {05271341-7A15-484C-9FD6-802A4193F4DE} = {37087D1B-3693-4E96-983D-A69F210BDE53} {7CC7946B-E026-4F66-8D4F-4F78F4801D43} = {4C753F64-0C93-4D65-96C2-A40893AFC1E8} + {2C282467-2CD5-4750-BE1F-CA8BD8ECC6EA} = {37087D1B-3693-4E96-983D-A69F210BDE53} EndGlobalSection EndGlobal diff --git a/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AbpAspNetMultiTenancyModule.cs b/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AbpAspNetMultiTenancyModule.cs new file mode 100644 index 0000000000..230cb125dc --- /dev/null +++ b/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AbpAspNetMultiTenancyModule.cs @@ -0,0 +1,15 @@ +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Modularity; +using Volo.Abp.MultiTenancy; + +namespace Volo.Abp.AspNetCore.MultiTenancy +{ + [DependsOn(typeof(AbpMultiTenancyModule), typeof(AbpAspNetCoreModule))] + public class AbpAspNetMultiTenancyModule : AbpModule + { + public override void ConfigureServices(IServiceCollection services) + { + services.AddAssemblyOf(); + } + } +} diff --git a/src/Volo.Abp.AspNetCore.MultiTenancy/project.json b/src/Volo.Abp.AspNetCore.MultiTenancy/project.json index 4690ae6dda..71b48923a7 100644 --- a/src/Volo.Abp.AspNetCore.MultiTenancy/project.json +++ b/src/Volo.Abp.AspNetCore.MultiTenancy/project.json @@ -1,8 +1,9 @@ -{ +{ "version": "1.0.0-*", "dependencies": { "NETStandard.Library": "1.6.1", + "Volo.Abp.AspNetCore": "1.0.0-*", "Volo.Abp.MultiTenancy": "1.0.0-*" }, diff --git a/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/AbpMultiTenancyModule.cs b/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/AbpMultiTenancyModule.cs new file mode 100644 index 0000000000..d92f07bcf6 --- /dev/null +++ b/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/AbpMultiTenancyModule.cs @@ -0,0 +1,13 @@ +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Modularity; + +namespace Volo.Abp.MultiTenancy +{ + public class AbpMultiTenancyModule : AbpModule + { + public override void ConfigureServices(IServiceCollection services) + { + services.AddAssemblyOf(); + } + } +} diff --git a/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/MultiTenancyManager.cs b/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/MultiTenancyManager.cs index 3bf70d6b45..7e793e1718 100644 --- a/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/MultiTenancyManager.cs +++ b/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/MultiTenancyManager.cs @@ -1,9 +1,10 @@ using System; using System.Collections.Generic; +using Volo.DependencyInjection; namespace Volo.Abp.MultiTenancy { - public class MultiTenancyManager : IMultiTenancyManager + public class MultiTenancyManager : IMultiTenancyManager, IScopedDependency { public TenantInfo CurrentTenant => GetCurrentTenant(); diff --git a/src/Volo.Abp/Volo/Abp/Json/JsonExtensions.cs b/src/Volo.Abp/Volo/Abp/Json/JsonExtensions.cs new file mode 100644 index 0000000000..e2f02eea04 --- /dev/null +++ b/src/Volo.Abp/Volo/Abp/Json/JsonExtensions.cs @@ -0,0 +1,31 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; + +namespace Volo.Abp.Json +{ + public static class JsonExtensions + { + /// + /// Converts given object to JSON string. + /// + /// + public static string ToJsonString(this object obj, bool camelCase = false, bool indented = false) + { + var options = new JsonSerializerSettings(); + + if (camelCase) + { + options.ContractResolver = new CamelCasePropertyNamesContractResolver(); + } + + if (indented) + { + options.Formatting = Formatting.Indented; + } + + //options.Converters.Insert(0, new AbpDateTimeConverter()); //TODO: AbpDateTimeConverter? + + return JsonConvert.SerializeObject(obj, options); + } + } +} diff --git a/src/Volo.Abp/project.json b/src/Volo.Abp/project.json index b1e94f0b63..98e162e1d4 100644 --- a/src/Volo.Abp/project.json +++ b/src/Volo.Abp/project.json @@ -5,7 +5,8 @@ "NETStandard.Library": "1.6.1", "Volo.DependencyInjection": "1.0.0-*", "System.Collections.Immutable": "1.3.0", - "Volo.ExtensionMethods": "1.0.0-*" + "Volo.ExtensionMethods": "1.0.0-*", + "Newtonsoft.Json": "9.0.1" }, "frameworks": { diff --git a/test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Properties/AssemblyInfo.cs b/test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000..d1aa43f6ce --- /dev/null +++ b/test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Properties/AssemblyInfo.cs @@ -0,0 +1,19 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Volo.Abp.AspNetCore.MultiTenancy.Tests")] +[assembly: AssemblyTrademark("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("2c282467-2cd5-4750-be1f-ca8bd8ecc6ea")] diff --git a/test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo.Abp.AspNetCore.MultiTenancy.Tests.xproj b/test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo.Abp.AspNetCore.MultiTenancy.Tests.xproj new file mode 100644 index 0000000000..d3adbc9bb8 --- /dev/null +++ b/test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo.Abp.AspNetCore.MultiTenancy.Tests.xproj @@ -0,0 +1,23 @@ + + + + 14.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + 2c282467-2cd5-4750-be1f-ca8bd8ecc6ea + + + .\obj + .\bin\ + v4.6.1 + + + 2.0 + + + + + + \ No newline at end of file diff --git a/test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo/Abp/AspNetCore/App/AppModule.cs b/test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo/Abp/AspNetCore/App/AppModule.cs new file mode 100644 index 0000000000..459db93b88 --- /dev/null +++ b/test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo/Abp/AspNetCore/App/AppModule.cs @@ -0,0 +1,33 @@ +using System.Collections.Generic; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.AspNetCore.Modularity; +using Volo.Abp.AspNetCore.MultiTenancy; +using Volo.Abp.Json; +using Volo.Abp.Modularity; +using Volo.Abp.MultiTenancy; + +namespace Volo.Abp.AspNetCore.App +{ + [DependsOn(typeof(AbpAspNetMultiTenancyModule))] + public class AppModule : AbpModule + { + public override void OnApplicationInitialization(ApplicationInitializationContext context) + { + var app = context.GetApplicationBuilder(); + + app.Run(async (ctx) => + { + var manager = ctx.RequestServices.GetRequiredService(); + + var dictionary = new Dictionary + { + ["TenantName"] = manager.CurrentTenant?.Name ?? "" + }; + + await ctx.Response.WriteAsync(dictionary.ToJsonString()); + }); + } + } +} diff --git a/test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo/Abp/AspNetCore/App/AppTestBase.cs b/test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo/Abp/AspNetCore/App/AppTestBase.cs new file mode 100644 index 0000000000..3f45334e3e --- /dev/null +++ b/test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo/Abp/AspNetCore/App/AppTestBase.cs @@ -0,0 +1,35 @@ +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; +using Shouldly; +using Volo.Abp.AspNetCore.TestBase; + +namespace Volo.Abp.AspNetCore.App +{ + public abstract class AppTestBase : AbpAspNetCoreIntegratedTestBase + { + protected virtual async Task GetResponseAsObjectAsync(string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK) + { + var strResponse = await GetResponseAsStringAsync(url, expectedStatusCode); + return JsonConvert.DeserializeObject(strResponse, new JsonSerializerSettings + { + ContractResolver = new CamelCasePropertyNamesContractResolver() + }); + } + + protected virtual async Task GetResponseAsStringAsync(string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK) + { + var response = await GetResponseAsync(url, expectedStatusCode); + return await response.Content.ReadAsStringAsync(); + } + + protected virtual async Task GetResponseAsync(string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK) + { + var response = await Client.GetAsync(url); + response.StatusCode.ShouldBe(expectedStatusCode); + return response; + } + } +} \ No newline at end of file diff --git a/test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo/Abp/AspNetCore/App/Startup.cs b/test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo/Abp/AspNetCore/App/Startup.cs new file mode 100644 index 0000000000..1af71d75e8 --- /dev/null +++ b/test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo/Abp/AspNetCore/App/Startup.cs @@ -0,0 +1,20 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +namespace Volo.Abp.AspNetCore.App +{ + public class Startup + { + public void ConfigureServices(IServiceCollection services) + { + services.AddApplication(); + } + + public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) + { + app.InitializeApplication(); + } + } +} diff --git a/test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo/Abp/AspNetCore/MultiTenancy/AspNetCoreMultiTenancy_Tests.cs b/test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo/Abp/AspNetCore/MultiTenancy/AspNetCoreMultiTenancy_Tests.cs new file mode 100644 index 0000000000..f868b48567 --- /dev/null +++ b/test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo/Abp/AspNetCore/MultiTenancy/AspNetCoreMultiTenancy_Tests.cs @@ -0,0 +1,22 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Shouldly; +using Volo.Abp.AspNetCore.App; +using Xunit; + +namespace Volo.Abp.AspNetCore.MultiTenancy +{ + public class AspNetCoreMultiTenancy_Tests : AppTestBase + { + private const string FakeUrl = "http://abp.io"; + + [Fact] + public async Task Should_Use_Host_If_Tenant_Is_Not_Specified() + { + var result = await GetResponseAsObjectAsync>(FakeUrl); + result["TenantName"].ShouldBe(""); + } + + //TODO: Specify tenant in the header, cookie, url, querystring and so on... + } +} diff --git a/test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo/Abp/AspNetCore/TestBase/AbpAspNetCoreIntegratedTestBase.cs b/test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo/Abp/AspNetCore/TestBase/AbpAspNetCoreIntegratedTestBase.cs new file mode 100644 index 0000000000..4dac052314 --- /dev/null +++ b/test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo/Abp/AspNetCore/TestBase/AbpAspNetCoreIntegratedTestBase.cs @@ -0,0 +1,80 @@ +using System; +using System.Linq; +using System.Net.Http; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Routing; +using Microsoft.AspNetCore.TestHost; +using Volo.ExtensionMethods; +using Volo.ExtensionMethods.Collections.Generic; + +namespace Volo.Abp.AspNetCore.TestBase +{ + public abstract class AbpAspNetCoreIntegratedTestBase + where TStartup : class + { + protected TestServer Server { get; } + + protected HttpClient Client { get; } + + protected IServiceProvider ServiceProvider { get; } + + protected AbpAspNetCoreIntegratedTestBase() + { + var builder = CreateWebHostBuilder(); + Server = CreateTestServer(builder); + Client = Server.CreateClient(); + + ServiceProvider = Server.Host.Services; + } + + protected virtual IWebHostBuilder CreateWebHostBuilder() + { + return new WebHostBuilder() + .UseStartup(); + } + + protected virtual TestServer CreateTestServer(IWebHostBuilder builder) + { + return new TestServer(builder); + } + + #region GetUrl + + /// + /// Gets default URL for given controller type. + /// + /// The type of the controller. + protected virtual string GetUrl() + { + return "/" + typeof(TController).Name.RemovePostFix("Controller", "AppService", "ApplicationService", "Service"); + } + + /// + /// Gets default URL for given controller type's given action. + /// + /// The type of the controller. + protected virtual string GetUrl(string actionName) + { + return GetUrl() + "/" + actionName; + } + + /// + /// Gets default URL for given controller type's given action with query string parameters (as anonymous object). + /// + /// The type of the controller. + protected virtual string GetUrl(string actionName, object queryStringParamsAsAnonymousObject) + { + var url = GetUrl(actionName); + + var dictionary = new RouteValueDictionary(queryStringParamsAsAnonymousObject); + if (dictionary.Any()) + { + url += "?" + dictionary.Select(d => $"{d.Key}={d.Value}").JoinAsString("&"); + } + + return url; + } + + #endregion + } +} diff --git a/test/Volo.Abp.AspNetCore.MultiTenancy.Tests/project.json b/test/Volo.Abp.AspNetCore.MultiTenancy.Tests/project.json new file mode 100644 index 0000000000..d8078e4e8f --- /dev/null +++ b/test/Volo.Abp.AspNetCore.MultiTenancy.Tests/project.json @@ -0,0 +1,23 @@ +{ + "version": "1.0.0-*", + + "testRunner": "xunit", + + "dependencies": { + "AbpTestBase": "1.0.0-*", + "Volo.Abp.AspNetCore.MultiTenancy": "1.0.0-*", + "Microsoft.AspNetCore.TestHost": "1.1.0", + "Microsoft.AspNetCore.Routing.Abstractions": "1.1.0" + }, + + "frameworks": { + "netcoreapp1.1": { + "dependencies": { + "Microsoft.NETCore.App": { + "type": "platform", + "version": "1.1.0" + } + } + } + } +}