Browse Source

Add unit tests

pull/16066/head
liangshiwei 3 years ago
parent
commit
036fedd824
  1. 2
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityUserDelegationRepository.cs
  2. 20
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserDelegationManager.cs
  3. 4
      modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserDelegationRepository.cs
  4. 4
      modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserDelegationRepository.cs
  5. 69
      modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityUserDelegationManager_Tests.cs
  6. 6
      modules/identity/test/Volo.Abp.Identity.EntityFrameworkCore.Tests/Volo/Abp/Identity/EntityFrameworkCore/IdentityUserDelegationepository_Tests.cs
  7. 9
      modules/identity/test/Volo.Abp.Identity.MongoDB.Tests/Volo/Abp/Identity/MongoDB/IdentityUserDelegationepository_Tests.cs
  8. 30
      modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/AbpIdentityTestDataBuilder.cs
  9. 55
      modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityUserDelegationepository_Tests.cs

2
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityUserDelegationRepository.cs

@ -14,7 +14,7 @@ public interface IIdentityUserDelegationRepository: IBasicRepository<IdentityUse
CancellationToken cancellationToken = default);
Task<List<IdentityUserDelegation>> GetActiveDelegationsAsync(
Guid sourceUserId,
Guid targetUserId,
CancellationToken cancellationToken = default);
Task<IdentityUserDelegation> FindActiveDelegationByIdAsync(

20
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserDelegationManager.cs

@ -20,9 +20,9 @@ public class IdentityUserDelegationManager : DomainService
return await IdentityUserDelegationRepository.GetListAsync(sourceUserId, targetUserId, cancellationToken: cancellationToken);
}
public virtual async Task<List<IdentityUserDelegation>> GetActiveDelegationsAsync(Guid sourceUserId, CancellationToken cancellationToken = default)
public virtual async Task<List<IdentityUserDelegation>> GetActiveDelegationsAsync(Guid targetUseId, CancellationToken cancellationToken = default)
{
return await IdentityUserDelegationRepository.GetActiveDelegationsAsync(sourceUserId, cancellationToken: cancellationToken);
return await IdentityUserDelegationRepository.GetActiveDelegationsAsync(targetUseId, cancellationToken: cancellationToken);
}
public virtual async Task<IdentityUserDelegation> FindActiveDelegationByIdAsync(Guid id, CancellationToken cancellationToken = default)
@ -30,9 +30,9 @@ public class IdentityUserDelegationManager : DomainService
return await IdentityUserDelegationRepository.FindActiveDelegationByIdAsync(id, cancellationToken: cancellationToken);
}
public virtual async Task DelegateNewUserAsync(Guid sourceUserId, IdentityUser targetUser, DateTime startTime, DateTime endTime, CancellationToken cancellationToken = default)
public virtual async Task DelegateNewUserAsync(Guid sourceUserId, Guid targetUserId, DateTime startTime, DateTime endTime, CancellationToken cancellationToken = default)
{
if (sourceUserId == targetUser.Id)
if (sourceUserId == targetUserId)
{
throw new BusinessException(IdentityErrorCodes.YouCannotDelegateYourself);
}
@ -41,7 +41,7 @@ public class IdentityUserDelegationManager : DomainService
new IdentityUserDelegation(
GuidGenerator.Create(),
sourceUserId,
targetUser.Id,
targetUserId,
startTime,
endTime
),
@ -58,14 +58,4 @@ public class IdentityUserDelegationManager : DomainService
await IdentityUserDelegationRepository.DeleteAsync(delegation, cancellationToken: cancellationToken);
}
}
public virtual Task<bool> IsExpiredAsync(IdentityUserDelegation userDelegation)
{
return Task.FromResult(userDelegation.EndTime <= Clock.Now);
}
public virtual async Task<bool> IsValidAsync(IdentityUserDelegation userDelegation)
{
return userDelegation.StartTime <= Clock.Now && !await IsExpiredAsync(userDelegation);
}
}

4
modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserDelegationRepository.cs

@ -29,11 +29,11 @@ public class EfCoreIdentityUserDelegationRepository : EfCoreRepository<IIdentity
.ToListAsync(cancellationToken: cancellationToken);
}
public async Task<List<IdentityUserDelegation>> GetActiveDelegationsAsync(Guid sourceUserId, CancellationToken cancellationToken = default)
public async Task<List<IdentityUserDelegation>> GetActiveDelegationsAsync(Guid targetUserId, CancellationToken cancellationToken = default)
{
return await (await GetDbSetAsync())
.AsNoTracking()
.Where(x => x.SourceUserId == sourceUserId &&
.Where(x => x.TargetUserId == targetUserId &&
x.StartTime <= Clock.Now &&
x.EndTime >= Clock.Now)
.ToListAsync(cancellationToken: cancellationToken);

4
modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserDelegationRepository.cs

@ -31,10 +31,10 @@ public class MongoIdentityUserDelegationRepository : MongoDbRepository<IAbpIdent
.ToListAsync(cancellationToken: cancellationToken);
}
public async Task<List<IdentityUserDelegation>> GetActiveDelegationsAsync(Guid sourceUserId, CancellationToken cancellationToken = default)
public async Task<List<IdentityUserDelegation>> GetActiveDelegationsAsync(Guid targetUserId, CancellationToken cancellationToken = default)
{
return await (await GetMongoQueryableAsync(cancellationToken))
.Where(x => x.SourceUserId == sourceUserId)
.Where(x => x.TargetUserId == targetUserId)
.Where(x => x.StartTime <= Clock.Now && x.EndTime >= Clock.Now)
.As<IMongoQueryable<IdentityUserDelegation>>()
.ToListAsync(cancellationToken: cancellationToken);

69
modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityUserDelegationManager_Tests.cs

@ -0,0 +1,69 @@
using System;
using System.Threading.Tasks;
using Shouldly;
using Xunit;
namespace Volo.Abp.Identity;
public class IdentityUserDelegationManager_Tests : AbpIdentityDomainTestBase
{
protected IdentityUserDelegationManager IdentityUserDelegationManager { get; }
protected IdentityTestData TestData { get; }
public IdentityUserDelegationManager_Tests()
{
IdentityUserDelegationManager = GetRequiredService<IdentityUserDelegationManager>();
TestData = GetRequiredService<IdentityTestData>();
}
[Fact]
public async Task GetListAsync()
{
(await IdentityUserDelegationManager.GetListAsync(Guid.NewGuid(), Guid.NewGuid())).Count.ShouldBe(0);
(await IdentityUserDelegationManager.GetListAsync(TestData.UserJohnId, null)).Count.ShouldBe(2);
(await IdentityUserDelegationManager.GetListAsync(null, TestData.UserDavidId)).Count.ShouldBe(3);
(await IdentityUserDelegationManager.GetListAsync(TestData.UserNeoId, TestData.UserDavidId)).Count.ShouldBe(1);
}
[Fact]
public async Task GetActiveDelegationsAsync()
{
var activeDelegations = await IdentityUserDelegationManager.GetActiveDelegationsAsync(TestData.UserDavidId);
activeDelegations.Count.ShouldBe(2);
activeDelegations[0].SourceUserId.ShouldBe(TestData.UserJohnId);
activeDelegations[0].TargetUserId.ShouldBe(TestData.UserDavidId);
activeDelegations[1].SourceUserId.ShouldBe(TestData.UserNeoId);
activeDelegations[1].TargetUserId.ShouldBe(TestData.UserDavidId);
}
[Fact]
public async Task FindActiveDelegationByIdAsync()
{
var activeDelegations = await IdentityUserDelegationManager.GetActiveDelegationsAsync(TestData.UserDavidId);
var activeDelegation = await IdentityUserDelegationManager.FindActiveDelegationByIdAsync(activeDelegations[0].Id);
activeDelegation.ShouldNotBeNull();
activeDelegation.SourceUserId.ShouldBe(TestData.UserJohnId);
activeDelegation.TargetUserId.ShouldBe(TestData.UserDavidId);
}
[Fact]
public async Task DelegateNewUserAsync()
{
await Should.ThrowAsync<BusinessException>(IdentityUserDelegationManager.DelegateNewUserAsync(
TestData.UserJohnId,
TestData.UserJohnId,
DateTime.Now.AddDays(-1),
DateTime.Now));
await IdentityUserDelegationManager.DelegateNewUserAsync(
TestData.UserJohnId,
TestData.UserBobId,
DateTime.Now.AddDays(-1),
DateTime.Now.AddDays(1));
(await IdentityUserDelegationManager.GetActiveDelegationsAsync(TestData.UserBobId)).Count.ShouldBe(1);
}
}

6
modules/identity/test/Volo.Abp.Identity.EntityFrameworkCore.Tests/Volo/Abp/Identity/EntityFrameworkCore/IdentityUserDelegationepository_Tests.cs

@ -0,0 +1,6 @@
namespace Volo.Abp.Identity.EntityFrameworkCore;
public class IdentityUserDelegationepository_Tests : IdentityUserDelegationepository_Tests<AbpIdentityEntityFrameworkCoreTestModule>
{
}

9
modules/identity/test/Volo.Abp.Identity.MongoDB.Tests/Volo/Abp/Identity/MongoDB/IdentityUserDelegationepository_Tests.cs

@ -0,0 +1,9 @@
using Xunit;
namespace Volo.Abp.Identity.MongoDB;
[Collection(MongoTestCollection.Name)]
public class IdentityUserDelegationepository_Tests: IdentityUserDelegationepository_Tests<AbpIdentityMongoDbTestModule>
{
}

30
modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/AbpIdentityTestDataBuilder.cs

@ -22,6 +22,7 @@ public class AbpIdentityTestDataBuilder : ITransientDependency
private readonly OrganizationUnitManager _organizationUnitManager;
private readonly IIdentityLinkUserRepository _identityLinkUserRepository;
private readonly IdentityLinkUserManager _identityLinkUserManager;
private readonly IIdentityUserDelegationRepository _identityUserDelegationRepository;
private IdentityRole _adminRole;
private IdentityRole _moderatorRole;
@ -42,7 +43,8 @@ public class AbpIdentityTestDataBuilder : ITransientDependency
IdentityTestData testData,
OrganizationUnitManager organizationUnitManager,
IIdentityLinkUserRepository identityLinkUserRepository,
IdentityLinkUserManager identityLinkUserManager)
IdentityLinkUserManager identityLinkUserManager,
IIdentityUserDelegationRepository identityUserDelegationRepository)
{
_guidGenerator = guidGenerator;
_userRepository = userRepository;
@ -55,6 +57,7 @@ public class AbpIdentityTestDataBuilder : ITransientDependency
_organizationUnitManager = organizationUnitManager;
_identityLinkUserRepository = identityLinkUserRepository;
_identityLinkUserManager = identityLinkUserManager;
_identityUserDelegationRepository = identityUserDelegationRepository;
_identitySecurityLogRepository = identitySecurityLogRepository;
}
@ -66,6 +69,7 @@ public class AbpIdentityTestDataBuilder : ITransientDependency
await AddLinkUsers();
await AddClaimTypes();
await AddSecurityLogs();
await AddUserDelegations();
}
private async Task AddRoles()
@ -197,4 +201,28 @@ public class AbpIdentityTestDataBuilder : ITransientDependency
CreationTime = new DateTime(2020, 01, 02, 10, 0, 0)
}));
}
private async Task AddUserDelegations()
{
await _identityUserDelegationRepository.InsertAsync(
new IdentityUserDelegation(_guidGenerator.Create(),
_testData.UserJohnId,
_testData.UserDavidId,
DateTime.Now.AddDays(-2),
DateTime.Now.AddDays(-1)));
await _identityUserDelegationRepository.InsertAsync(
new IdentityUserDelegation(_guidGenerator.Create(),
_testData.UserJohnId,
_testData.UserDavidId,
DateTime.Now.AddDays(-1),
DateTime.Now.AddDays(1)));
await _identityUserDelegationRepository.InsertAsync(
new IdentityUserDelegation(_guidGenerator.Create(),
_testData.UserNeoId,
_testData.UserDavidId,
DateTime.Now.AddDays(-1),
DateTime.Now.AddDays(1)));
}
}

55
modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityUserDelegationepository_Tests.cs

@ -0,0 +1,55 @@
using System;
using System.Threading.Tasks;
using Shouldly;
using Volo.Abp.Modularity;
using Xunit;
namespace Volo.Abp.Identity;
public abstract class IdentityUserDelegationepository_Tests<TStartupModule> : AbpIdentityTestBase<TStartupModule>
where TStartupModule : IAbpModule
{
protected IIdentityUserRepository UserRepository { get; }
protected IIdentityUserDelegationRepository IdentityUserDelegationRepository { get; }
protected IdentityTestData TestData { get; }
public IdentityUserDelegationepository_Tests()
{
UserRepository = GetRequiredService<IIdentityUserRepository>();
IdentityUserDelegationRepository = GetRequiredService<IIdentityUserDelegationRepository>();
TestData = GetRequiredService<IdentityTestData>();
}
[Fact]
public async Task GetListAsync()
{
(await IdentityUserDelegationRepository.GetListAsync(Guid.NewGuid(), Guid.NewGuid())).Count.ShouldBe(0);
(await IdentityUserDelegationRepository.GetListAsync(TestData.UserJohnId, null)).Count.ShouldBe(2);
(await IdentityUserDelegationRepository.GetListAsync(null, TestData.UserDavidId)).Count.ShouldBe(3);
(await IdentityUserDelegationRepository.GetListAsync(TestData.UserNeoId, TestData.UserDavidId)).Count.ShouldBe(1);
}
[Fact]
public async Task GetActiveDelegationsAsync()
{
var activeDelegations = await IdentityUserDelegationRepository.GetActiveDelegationsAsync(TestData.UserDavidId);
activeDelegations.Count.ShouldBe(2);
activeDelegations[0].SourceUserId.ShouldBe(TestData.UserJohnId);
activeDelegations[0].TargetUserId.ShouldBe(TestData.UserDavidId);
activeDelegations[1].SourceUserId.ShouldBe(TestData.UserNeoId);
activeDelegations[1].TargetUserId.ShouldBe(TestData.UserDavidId);
}
[Fact]
public async Task GetActiveDelegationOrNullAsync()
{
var activeDelegations = await IdentityUserDelegationRepository.GetActiveDelegationsAsync(TestData.UserDavidId);
var activeDelegation = await IdentityUserDelegationRepository.FindActiveDelegationByIdAsync(activeDelegations[0].Id);
activeDelegation.ShouldNotBeNull();
activeDelegation.SourceUserId.ShouldBe(TestData.UserJohnId);
activeDelegation.TargetUserId.ShouldBe(TestData.UserDavidId);
}
}
Loading…
Cancel
Save