mirror of https://github.com/abpframework/abp.git
17 changed files with 417 additions and 19 deletions
@ -0,0 +1,111 @@ |
|||||
|
using Microsoft.AspNetCore.Identity; |
||||
|
using Shouldly; |
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.Identity.Organizations; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.Identity |
||||
|
{ |
||||
|
public class OrganizationUnitManager_Tests : AbpIdentityDomainTestBase |
||||
|
{ |
||||
|
private readonly OrganizationUnitManager _organizationUnitManager; |
||||
|
private readonly IOrganizationUnitRepository _organizationUnitRepository; |
||||
|
private readonly IdentityTestData _testData; |
||||
|
private readonly IIdentityRoleRepository _identityRoleRepository; |
||||
|
private readonly ILookupNormalizer _lookupNormalizer; |
||||
|
public OrganizationUnitManager_Tests() |
||||
|
{ |
||||
|
_organizationUnitManager = GetRequiredService<OrganizationUnitManager>(); |
||||
|
_organizationUnitRepository = GetRequiredService<IOrganizationUnitRepository>(); |
||||
|
_identityRoleRepository = GetRequiredService<IIdentityRoleRepository>(); |
||||
|
_lookupNormalizer = GetRequiredService<ILookupNormalizer>(); |
||||
|
_testData = GetRequiredService<IdentityTestData>(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task CreateAsnyc() |
||||
|
{ |
||||
|
await _organizationUnitManager.CreateAsync(new OrganizationUnit(null, "Root 1")); |
||||
|
|
||||
|
var root1 = await _organizationUnitRepository.GetOrganizationUnit("Root 1").ConfigureAwait(false); |
||||
|
root1.ShouldNotBeNull(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task UpdateAsync() |
||||
|
{ |
||||
|
var ou = await _organizationUnitRepository.GetOrganizationUnit("OU111").ConfigureAwait(false); |
||||
|
ou.Code = OrganizationUnit.CreateCode(123); |
||||
|
await _organizationUnitManager.UpdateAsync(ou); |
||||
|
|
||||
|
var ouAfterChange = await _organizationUnitRepository.GetOrganizationUnit("OU111").ConfigureAwait(false); |
||||
|
ouAfterChange.Code.ShouldContain("123"); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task DeleteAsync() |
||||
|
{ |
||||
|
var ou = await _organizationUnitRepository.GetOrganizationUnit("OU11").ConfigureAwait(false); |
||||
|
await _organizationUnitManager.DeleteAsync(ou.Id); |
||||
|
|
||||
|
(await _organizationUnitRepository.GetOrganizationUnit("OU11").ConfigureAwait(false)).ShouldBeNull(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task MoveAsync() |
||||
|
{ |
||||
|
var ou1 = await _organizationUnitRepository.GetOrganizationUnit("OU1").ConfigureAwait(false); |
||||
|
var ou2 = await _organizationUnitRepository.GetOrganizationUnit("OU2").ConfigureAwait(false); |
||||
|
|
||||
|
await _organizationUnitManager.MoveAsync(ou1.Id, ou2.Id); |
||||
|
|
||||
|
ou1 = await _organizationUnitRepository.GetOrganizationUnit("OU1").ConfigureAwait(false); |
||||
|
ou1.ParentId.ShouldBe(ou2.Id); |
||||
|
ou1.Code.ShouldBe(OrganizationUnit.CreateCode(2, 2)); |
||||
|
|
||||
|
var ou11 = await _organizationUnitRepository.GetOrganizationUnit("OU11").ConfigureAwait(false); |
||||
|
ou11.ParentId.ShouldBe(ou1.Id); |
||||
|
ou11.Code.ShouldBe(OrganizationUnit.CreateCode(2, 2, 1)); |
||||
|
|
||||
|
var ou111 = await _organizationUnitRepository.GetOrganizationUnit("OU111").ConfigureAwait(false); |
||||
|
ou111.ParentId.ShouldBe(ou11.Id); |
||||
|
ou111.Code.ShouldBe(OrganizationUnit.CreateCode(2, 2, 1, 1)); |
||||
|
|
||||
|
var ou112 = await _organizationUnitRepository.GetOrganizationUnit("OU112").ConfigureAwait(false); |
||||
|
ou112.ParentId.ShouldBe(ou11.Id); |
||||
|
ou112.Code.ShouldBe(OrganizationUnit.CreateCode(2, 2, 1, 2)); |
||||
|
|
||||
|
var ou12 = await _organizationUnitRepository.GetOrganizationUnit("OU12").ConfigureAwait(false); |
||||
|
ou12.ParentId.ShouldBe(ou1.Id); |
||||
|
ou12.Code.ShouldBe(OrganizationUnit.CreateCode(2, 2, 2)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task AddRoleToOrganizationUnitAsync() |
||||
|
{ |
||||
|
var ou = await _organizationUnitRepository.GetOrganizationUnit("OU1", true).ConfigureAwait(false); |
||||
|
var adminRole = await _identityRoleRepository.FindByNormalizedNameAsync(_lookupNormalizer.NormalizeName("admin")).ConfigureAwait(false); |
||||
|
await _organizationUnitManager.AddRoleToOrganizationUnitAsync(adminRole.Id, ou.Id); |
||||
|
|
||||
|
//TODO: This method has a bug: includeDetails not work
|
||||
|
ou = await _organizationUnitRepository.GetOrganizationUnit("OU1", includeDetails: true).ConfigureAwait(false); |
||||
|
ou.Roles.FirstOrDefault().Id.ShouldBe(adminRole.Id); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task RemoveRoleFromOrganizationUnitAsync() |
||||
|
{ |
||||
|
var ou = await _organizationUnitRepository.GetOrganizationUnit("OU1", true).ConfigureAwait(false); |
||||
|
var adminRole = await _identityRoleRepository.FindByNormalizedNameAsync(_lookupNormalizer.NormalizeName("admin")).ConfigureAwait(false); |
||||
|
await _organizationUnitManager.AddRoleToOrganizationUnitAsync(adminRole.Id, ou.Id); |
||||
|
|
||||
|
await _organizationUnitManager.RemoveRoleFromOrganizationUnitAsync(adminRole.Id, ou.Id); |
||||
|
ou = await _organizationUnitRepository.GetOrganizationUnit("OU1", includeDetails: true).ConfigureAwait(false); |
||||
|
ou.Roles.FirstOrDefault(r => r.RoleId == adminRole.Id).ShouldBeNull(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,10 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Text; |
||||
|
|
||||
|
namespace Volo.Abp.Identity.EntityFrameworkCore |
||||
|
{ |
||||
|
public class OrganizationUnitRepository_Tests : OrganizationUnitRepository_Tests<AbpIdentityEntityFrameworkCoreTestModule> |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,76 @@ |
|||||
|
using Microsoft.AspNetCore.Identity; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Shouldly; |
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.Guids; |
||||
|
using Volo.Abp.Identity.Organizations; |
||||
|
using Volo.Abp.Modularity; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.Identity |
||||
|
{ |
||||
|
public abstract class OrganizationUnitRepository_Tests<TStartupModule> : AbpIdentityTestBase<TStartupModule> |
||||
|
where TStartupModule : IAbpModule |
||||
|
{ |
||||
|
protected IOrganizationUnitRepository OrganizationUnitRepository { get; } |
||||
|
protected ILookupNormalizer LookupNormalizer { get; } |
||||
|
private readonly IdentityTestData _testData; |
||||
|
private readonly IGuidGenerator _guidGenerator; |
||||
|
|
||||
|
public OrganizationUnitRepository_Tests() |
||||
|
{ |
||||
|
OrganizationUnitRepository = ServiceProvider.GetRequiredService<IOrganizationUnitRepository>(); |
||||
|
LookupNormalizer = ServiceProvider.GetRequiredService<ILookupNormalizer>(); |
||||
|
_testData = GetRequiredService<IdentityTestData>(); |
||||
|
_guidGenerator = GetRequiredService<IGuidGenerator>(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task GetChildrenAsync() |
||||
|
{ |
||||
|
(await OrganizationUnitRepository.GetChildrenAsync(_testData.RoleModeratorId).ConfigureAwait(false)).ShouldNotBeNull(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task GetAllChildrenWithParentCodeAsync() |
||||
|
{ |
||||
|
(await OrganizationUnitRepository.GetAllChildrenWithParentCodeAsync(OrganizationUnit.CreateCode(0), _guidGenerator.Create()).ConfigureAwait(false)).ShouldNotBeNull(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task GetListAsync() |
||||
|
{ |
||||
|
var ouIds = (await OrganizationUnitRepository.GetListAsync().ConfigureAwait(false)) |
||||
|
.Select(ou => ou.Id).Take(2); |
||||
|
var ous = await OrganizationUnitRepository.GetListAsync(ouIds).ConfigureAwait(false); |
||||
|
ous.Count.ShouldBe(2); |
||||
|
ous.ShouldContain(ou => ou.Id == ouIds.First()); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task GetOrganizationUnit() |
||||
|
{ |
||||
|
var organizationUnit = await OrganizationUnitRepository.GetOrganizationUnit("OU111").ConfigureAwait(false); |
||||
|
organizationUnit.ShouldNotBeNull(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task GetCountAsync() |
||||
|
{ |
||||
|
(await OrganizationUnitRepository.GetCountAsync().ConfigureAwait(false)).ShouldBeGreaterThan(0); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_Eager_Load_OrganizationUnit_Collections() |
||||
|
{ |
||||
|
var ou = (await OrganizationUnitRepository.GetListAsync(true).ConfigureAwait(false)) |
||||
|
.FirstOrDefault(ou => ou.DisplayName == "OU111"); |
||||
|
ou.Roles.ShouldNotBeNull(); |
||||
|
ou.Roles.Any().ShouldBeTrue(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue