Browse Source

Added test for the TokenCleanupService

pull/2545/head
Halil İbrahim Kalkan 7 years ago
parent
commit
aea7eed317
  1. 37
      modules/identityserver/test/Volo.Abp.IdentityServer.Domain.Tests/Volo/Abp/IdentityServer/Tokens/TokenCleanupService_Tests.cs
  2. 52
      modules/identityserver/test/Volo.Abp.IdentityServer.TestBase/Volo/Abp/IdentityServer/AbpIdentityServerTestDataBuilder.cs

37
modules/identityserver/test/Volo.Abp.IdentityServer.Domain.Tests/Volo/Abp/IdentityServer/Tokens/TokenCleanupService_Tests.cs

@ -0,0 +1,37 @@
using System.Threading.Tasks;
using Shouldly;
using Volo.Abp.IdentityServer.Devices;
using Volo.Abp.IdentityServer.Grants;
using Xunit;
namespace Volo.Abp.IdentityServer.Tokens
{
public class TokenCleanupService_Tests : AbpIdentityServerTestBase
{
private readonly IPersistentGrantRepository _persistentGrantRepository;
private readonly IDeviceFlowCodesRepository _deviceFlowCodesRepository;
private readonly TokenCleanupService _tokenCleanupService;
public TokenCleanupService_Tests()
{
_persistentGrantRepository = GetRequiredService<IPersistentGrantRepository>();
_deviceFlowCodesRepository = GetRequiredService<IDeviceFlowCodesRepository>();
_tokenCleanupService = GetRequiredService<TokenCleanupService>();
}
[Fact]
public async Task Should_Clear_Expired_Tokens()
{
var persistentGrantCount = await _persistentGrantRepository.GetCountAsync();
var deviceFlowCodesCount = await _deviceFlowCodesRepository.GetCountAsync();
await _tokenCleanupService.CleanAsync();
(await _persistentGrantRepository.GetCountAsync())
.ShouldBe(persistentGrantCount - 1);
(await _deviceFlowCodesRepository.GetCountAsync())
.ShouldBe(deviceFlowCodesCount - 1);
}
}
}

52
modules/identityserver/test/Volo.Abp.IdentityServer.TestBase/Volo/Abp/IdentityServer/AbpIdentityServerTestDataBuilder.cs

@ -6,8 +6,10 @@ using Volo.Abp.Guids;
using Volo.Abp.Identity; using Volo.Abp.Identity;
using Volo.Abp.IdentityServer.ApiResources; using Volo.Abp.IdentityServer.ApiResources;
using Volo.Abp.IdentityServer.Clients; using Volo.Abp.IdentityServer.Clients;
using Volo.Abp.IdentityServer.Devices;
using Volo.Abp.IdentityServer.Grants; using Volo.Abp.IdentityServer.Grants;
using Volo.Abp.IdentityServer.IdentityResources; using Volo.Abp.IdentityServer.IdentityResources;
using Volo.Abp.Timing;
namespace Volo.Abp.IdentityServer namespace Volo.Abp.IdentityServer
{ {
@ -19,7 +21,9 @@ namespace Volo.Abp.IdentityServer
private readonly IIdentityResourceRepository _identityResourceRepository; private readonly IIdentityResourceRepository _identityResourceRepository;
private readonly IIdentityClaimTypeRepository _identityClaimTypeRepository; private readonly IIdentityClaimTypeRepository _identityClaimTypeRepository;
private readonly IPersistentGrantRepository _persistentGrantRepository; private readonly IPersistentGrantRepository _persistentGrantRepository;
private readonly IDeviceFlowCodesRepository _deviceFlowCodesRepository;
private readonly AbpIdentityServerTestData _testData; private readonly AbpIdentityServerTestData _testData;
private readonly IClock _clock;
public AbpIdentityServerTestDataBuilder( public AbpIdentityServerTestDataBuilder(
IGuidGenerator guidGenerator, IGuidGenerator guidGenerator,
@ -28,7 +32,9 @@ namespace Volo.Abp.IdentityServer
IIdentityResourceRepository identityResourceRepository, IIdentityResourceRepository identityResourceRepository,
IIdentityClaimTypeRepository identityClaimTypeRepository, IIdentityClaimTypeRepository identityClaimTypeRepository,
AbpIdentityServerTestData testData, AbpIdentityServerTestData testData,
IPersistentGrantRepository persistentGrantRepository) IPersistentGrantRepository persistentGrantRepository,
IDeviceFlowCodesRepository deviceFlowCodesRepository,
IClock clock)
{ {
_testData = testData; _testData = testData;
_guidGenerator = guidGenerator; _guidGenerator = guidGenerator;
@ -37,10 +43,13 @@ namespace Volo.Abp.IdentityServer
_identityResourceRepository = identityResourceRepository; _identityResourceRepository = identityResourceRepository;
_identityClaimTypeRepository = identityClaimTypeRepository; _identityClaimTypeRepository = identityClaimTypeRepository;
_persistentGrantRepository = persistentGrantRepository; _persistentGrantRepository = persistentGrantRepository;
_clock = clock;
_deviceFlowCodesRepository = deviceFlowCodesRepository;
} }
public async Task BuildAsync() public async Task BuildAsync()
{ {
await AddDeviceFlowCodes().ConfigureAwait(false);
await AddPersistedGrants().ConfigureAwait(false); await AddPersistedGrants().ConfigureAwait(false);
await AddIdentityResources().ConfigureAwait(false); await AddIdentityResources().ConfigureAwait(false);
await AddApiResources().ConfigureAwait(false); await AddApiResources().ConfigureAwait(false);
@ -48,6 +57,34 @@ namespace Volo.Abp.IdentityServer
await AddClaimTypes().ConfigureAwait(false); await AddClaimTypes().ConfigureAwait(false);
} }
private async Task AddDeviceFlowCodes()
{
await _deviceFlowCodesRepository.InsertAsync(
new DeviceFlowCodes(_guidGenerator.Create())
{
ClientId = "c1",
DeviceCode = "DeviceCode1",
Expiration = _clock.Now.AddDays(1),
Data = "",
UserCode = "DeviceFlowCodesUserCode1",
SubjectId = "DeviceFlowCodesSubjectId1"
}
);
await _deviceFlowCodesRepository.InsertAsync(
new DeviceFlowCodes(_guidGenerator.Create())
{
ClientId = "c1",
DeviceCode = "DeviceCode2",
Expiration = _clock.Now.AddDays(-1),
Data = "",
UserCode = "DeviceFlowCodesUserCode2",
SubjectId = "DeviceFlowCodesSubjectId2"
}
);
}
private async Task AddPersistedGrants() private async Task AddPersistedGrants()
{ {
await _persistentGrantRepository.InsertAsync(new PersistedGrant(_guidGenerator.Create()) await _persistentGrantRepository.InsertAsync(new PersistedGrant(_guidGenerator.Create())
@ -74,7 +111,18 @@ namespace Volo.Abp.IdentityServer
SubjectId = "PersistedGrantSubjectId3", SubjectId = "PersistedGrantSubjectId3",
ClientId = "c1", ClientId = "c1",
Type = "c1type", Type = "c1type",
Data = "" Data = "",
Expiration = _clock.Now.AddDays(1),
}).ConfigureAwait(false);
await _persistentGrantRepository.InsertAsync(new PersistedGrant(_guidGenerator.Create())
{
Key = "PersistedGrantKey_Expired1",
SubjectId = "PersistedGrantSubjectId_Expired1",
ClientId = "c1",
Type = "c1type",
Data = "",
Expiration = _clock.Now.AddDays(-1)
}).ConfigureAwait(false); }).ConfigureAwait(false);
} }

Loading…
Cancel
Save