mirror of https://github.com/abpframework/abp.git
7 changed files with 133 additions and 171 deletions
@ -1,38 +0,0 @@ |
|||||
using Shouldly; |
|
||||
using Xunit; |
|
||||
|
|
||||
namespace Volo.Abp.MultiTenancy |
|
||||
{ |
|
||||
public class AsyncLocalAmbientTenantScopeProvider_Tests |
|
||||
{ |
|
||||
[Fact] |
|
||||
public void Should_Support_Inner_Scopes() |
|
||||
{ |
|
||||
var scopeProvider = new AsyncLocalTenantScopeProvider(); |
|
||||
|
|
||||
Assert.Null(scopeProvider.CurrentScope?.Tenant); |
|
||||
|
|
||||
using (scopeProvider.EnterScope(new TenantInfo("A"))) |
|
||||
{ |
|
||||
Assert.Equal("A", scopeProvider.CurrentScope?.Tenant?.Id); |
|
||||
|
|
||||
using (scopeProvider.EnterScope(new TenantInfo("B"))) |
|
||||
{ |
|
||||
Assert.Equal("B", scopeProvider.CurrentScope?.Tenant?.Id); |
|
||||
|
|
||||
using (scopeProvider.EnterScope(null)) |
|
||||
{ |
|
||||
Assert.NotNull(scopeProvider.CurrentScope); |
|
||||
Assert.Null(scopeProvider.CurrentScope.Tenant); |
|
||||
} |
|
||||
|
|
||||
Assert.Equal("B", scopeProvider.CurrentScope?.Tenant?.Id); |
|
||||
} |
|
||||
|
|
||||
Assert.Equal("A", scopeProvider.CurrentScope?.Tenant?.Id); |
|
||||
} |
|
||||
|
|
||||
scopeProvider.CurrentScope.ShouldBeNull(); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,9 @@ |
|||||
|
using Volo.Abp.TestBase; |
||||
|
|
||||
|
namespace Volo.Abp.MultiTenancy |
||||
|
{ |
||||
|
public class MultiTenancyTestBase : AbpIntegratedTest<MultiTenancyTestModule> |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,10 @@ |
|||||
|
using Volo.Abp.Modularity; |
||||
|
|
||||
|
namespace Volo.Abp.MultiTenancy |
||||
|
{ |
||||
|
[DependsOn(typeof(AbpMultiTenancyModule))] |
||||
|
public class MultiTenancyTestModule : AbpModule |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
} |
||||
@ -1,133 +0,0 @@ |
|||||
using System; |
|
||||
using Microsoft.Extensions.Options; |
|
||||
using NSubstitute; |
|
||||
using Shouldly; |
|
||||
using Xunit; |
|
||||
|
|
||||
namespace Volo.Abp.MultiTenancy |
|
||||
{ |
|
||||
public class MultiTenantManager_TenantResolver_Tests |
|
||||
{ |
|
||||
[Fact] |
|
||||
public void Should_Get_Current_Tenant_As_Null_If_No_Resolver() |
|
||||
{ |
|
||||
//Arrange
|
|
||||
|
|
||||
var manager = new MultiTenancyManager( |
|
||||
Substitute.For<IServiceProvider>(), |
|
||||
Substitute.For<ITenantScopeProvider>(), |
|
||||
new OptionsWrapper<MultiTenancyOptions>(new MultiTenancyOptions()) |
|
||||
); |
|
||||
|
|
||||
//Assert
|
|
||||
|
|
||||
manager.CurrentTenant.ShouldBeNull(); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public void Should_Get_Current_Tenant_From_Single_Resolver() |
|
||||
{ |
|
||||
//Arrange
|
|
||||
|
|
||||
var fakeTenant = new TenantInfo("A"); |
|
||||
|
|
||||
var manager = new MultiTenancyManager( |
|
||||
Substitute.For<IServiceProvider>(), |
|
||||
Substitute.For<ITenantScopeProvider>(), |
|
||||
new OptionsWrapper<MultiTenancyOptions>(new MultiTenancyOptions |
|
||||
{ |
|
||||
TenantResolvers = |
|
||||
{ |
|
||||
new SimpleTenantResolver(context => |
|
||||
{ |
|
||||
context.Tenant = fakeTenant; |
|
||||
context.Handled = true; |
|
||||
}) |
|
||||
} |
|
||||
} |
|
||||
) |
|
||||
); |
|
||||
|
|
||||
//Assert
|
|
||||
|
|
||||
manager.CurrentTenant.ShouldBe(fakeTenant); |
|
||||
} |
|
||||
|
|
||||
|
|
||||
[Fact] |
|
||||
public void Should_Get_Current_Tenant_From_Multiple_Resolvers() |
|
||||
{ |
|
||||
//Arrange
|
|
||||
|
|
||||
var selectedTenant = new TenantInfo("A"); |
|
||||
|
|
||||
var manager = new MultiTenancyManager( |
|
||||
Substitute.For<IServiceProvider>(), |
|
||||
Substitute.For<ITenantScopeProvider>(), |
|
||||
new OptionsWrapper<MultiTenancyOptions>(new MultiTenancyOptions |
|
||||
{ |
|
||||
TenantResolvers = |
|
||||
{ |
|
||||
new SimpleTenantResolver(context => |
|
||||
{ |
|
||||
context.Tenant = new TenantInfo("B"); |
|
||||
context.Handled = false; //Causes go to the next resolver
|
|
||||
}), |
|
||||
new SimpleTenantResolver(context => |
|
||||
{ |
|
||||
context.Tenant = selectedTenant; |
|
||||
}), |
|
||||
new SimpleTenantResolver(context => |
|
||||
{ |
|
||||
context.Tenant = new TenantInfo("C"); |
|
||||
}) |
|
||||
} |
|
||||
} |
|
||||
) |
|
||||
); |
|
||||
|
|
||||
//Assert
|
|
||||
|
|
||||
manager.CurrentTenant.ShouldBe(selectedTenant); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public void Should_Get_Ambient_Tenant_If_Changed() |
|
||||
{ |
|
||||
//Arrange
|
|
||||
|
|
||||
var oldTenant = new TenantInfo("A"); |
|
||||
|
|
||||
var manager = new MultiTenancyManager( |
|
||||
Substitute.For<IServiceProvider>(), |
|
||||
new AsyncLocalTenantScopeProvider(), |
|
||||
new OptionsWrapper<MultiTenancyOptions>( |
|
||||
new MultiTenancyOptions |
|
||||
{ |
|
||||
TenantResolvers = |
|
||||
{ |
|
||||
new SimpleTenantResolver(context => |
|
||||
{ |
|
||||
context.Tenant = oldTenant; |
|
||||
}) |
|
||||
} |
|
||||
} |
|
||||
) |
|
||||
); |
|
||||
|
|
||||
manager.CurrentTenant.ShouldBe(oldTenant); |
|
||||
|
|
||||
//Act
|
|
||||
|
|
||||
var overridedTenant = new TenantInfo("B"); |
|
||||
using (manager.ChangeTenant(overridedTenant)) |
|
||||
{ |
|
||||
//Assert
|
|
||||
manager.CurrentTenant.ShouldBe(overridedTenant); |
|
||||
} |
|
||||
|
|
||||
//Assert
|
|
||||
manager.CurrentTenant.ShouldBe(oldTenant); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,99 @@ |
|||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Shouldly; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.MultiTenancy |
||||
|
{ |
||||
|
public class MultiTenantManager_Tests : MultiTenancyTestBase |
||||
|
{ |
||||
|
private readonly IMultiTenancyManager _multiTenancyManager; |
||||
|
|
||||
|
private readonly TenantInfo _tenantA = new TenantInfo("A"); |
||||
|
private readonly TenantInfo _tenantB = new TenantInfo("B"); |
||||
|
private TenantInfo _tenantToBeResolved; |
||||
|
|
||||
|
public MultiTenantManager_Tests() |
||||
|
{ |
||||
|
_multiTenancyManager = ServiceProvider.GetRequiredService<IMultiTenancyManager>(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CurrentTenant_Should_Be_Null_As_Default() |
||||
|
{ |
||||
|
//Assert
|
||||
|
|
||||
|
_multiTenancyManager.CurrentTenant.ShouldBeNull(); |
||||
|
} |
||||
|
|
||||
|
protected override void AfterAddApplication(IServiceCollection services) |
||||
|
{ |
||||
|
services.Configure<MultiTenancyOptions>(options => |
||||
|
{ |
||||
|
options.TenantResolvers.Add(new SimpleTenantResolver(context => |
||||
|
{ |
||||
|
if (_tenantToBeResolved == _tenantA) |
||||
|
{ |
||||
|
context.Tenant = _tenantA; |
||||
|
} |
||||
|
})); |
||||
|
|
||||
|
options.TenantResolvers.Add(new SimpleTenantResolver(context => |
||||
|
{ |
||||
|
if (_tenantToBeResolved == _tenantB) |
||||
|
{ |
||||
|
context.Tenant = _tenantB; |
||||
|
} |
||||
|
})); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Get_Current_Tenant_From_Single_Resolver() |
||||
|
{ |
||||
|
//Arrange
|
||||
|
|
||||
|
_tenantToBeResolved = _tenantA; |
||||
|
|
||||
|
//Assert
|
||||
|
|
||||
|
_multiTenancyManager.CurrentTenant.ShouldBe(_tenantA); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Get_Current_Tenant_From_Multiple_Resolvers() |
||||
|
{ |
||||
|
//Arrange
|
||||
|
|
||||
|
_tenantToBeResolved = _tenantB; |
||||
|
|
||||
|
//Assert
|
||||
|
|
||||
|
_multiTenancyManager.CurrentTenant.ShouldBe(_tenantB); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Get_Changed_Tenant_If_Wanted() |
||||
|
{ |
||||
|
_multiTenancyManager.CurrentTenant.ShouldBe(null); |
||||
|
|
||||
|
_tenantToBeResolved = _tenantB; |
||||
|
|
||||
|
_multiTenancyManager.CurrentTenant.ShouldBe(_tenantB); |
||||
|
|
||||
|
using (_multiTenancyManager.ChangeTenant(_tenantA)) |
||||
|
{ |
||||
|
_multiTenancyManager.CurrentTenant.ShouldBe(_tenantA); |
||||
|
|
||||
|
using (_multiTenancyManager.ChangeTenant(_tenantB)) |
||||
|
{ |
||||
|
_multiTenancyManager.CurrentTenant.ShouldBe(_tenantB); |
||||
|
} |
||||
|
|
||||
|
_multiTenancyManager.CurrentTenant.ShouldBe(_tenantA); |
||||
|
} |
||||
|
|
||||
|
_multiTenancyManager.CurrentTenant.ShouldBe(_tenantB); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue