mirror of https://github.com/abpframework/abp.git
4 changed files with 77 additions and 14 deletions
@ -0,0 +1,28 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Domain.Services; |
|||
|
|||
namespace Volo.Abp.TenantManagement; |
|||
|
|||
public class DefaultTenantNameValidator : ITenantNameValidator, ITransientDependency |
|||
{ |
|||
protected ITenantRepository TenantRepository { get; } |
|||
|
|||
public DefaultTenantNameValidator(ITenantRepository tenantRepository) |
|||
{ |
|||
TenantRepository = tenantRepository; |
|||
} |
|||
|
|||
public virtual async Task ValidateAsync(string normalizedTenantName, Guid? expectedTenantId = null) |
|||
{ |
|||
Check.NotNullOrWhiteSpace(normalizedTenantName, nameof(normalizedTenantName)); |
|||
|
|||
var existingTenant = await TenantRepository.FindByNameAsync(normalizedTenantName); |
|||
if (existingTenant != null && existingTenant.Id != expectedTenantId) |
|||
{ |
|||
throw new BusinessException("Volo.Abp.TenantManagement:DuplicateTenantName") |
|||
.WithData("Name", normalizedTenantName); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.TenantManagement; |
|||
|
|||
public interface ITenantNameValidator |
|||
{ |
|||
Task ValidateAsync(string normalizedTenantName, Guid? expectedTenantId = null); |
|||
} |
|||
@ -0,0 +1,35 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Guids; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.TenantManagement; |
|||
|
|||
public class TenantNameValidator_Tests : AbpTenantManagementDomainTestBase |
|||
{ |
|||
private readonly ITenantNameValidator _tenantNameValidator; |
|||
private readonly ITenantRepository _tenantRepository; |
|||
public TenantNameValidator_Tests() |
|||
{ |
|||
_tenantRepository = GetRequiredService<ITenantRepository>(); |
|||
_tenantNameValidator = GetRequiredService<ITenantNameValidator>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Throw_If_Name_Is_Null() |
|||
{ |
|||
await Assert.ThrowsAsync<ArgumentException>(() => _tenantNameValidator.ValidateAsync(null)); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Throw_If_Duplicate_Name() |
|||
{ |
|||
await Assert.ThrowsAsync<BusinessException>(() => _tenantNameValidator.ValidateAsync("VOLOSOFT")); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Not_Throw_For_Unique_Name() |
|||
{ |
|||
await _tenantNameValidator.ValidateAsync("UNIQUE-TENANT-NAME"); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue