Browse Source

Merge pull request #7414 from abpframework/maliming/ids-patch-test

Remove duplicate data builder of identity server module.
pull/7466/head
Halil İbrahim Kalkan 6 years ago
committed by GitHub
parent
commit
196a2edfaa
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 148
      modules/identityserver/test/Volo.Abp.IdentityServer.EntityFrameworkCore.Tests/Volo/Abp/IdentityServer/AbpIdentityServerTestDataBuilder.cs
  2. 19
      modules/identityserver/test/Volo.Abp.IdentityServer.EntityFrameworkCore.Tests/Volo/Abp/IdentityServer/AbpIdentityServerTestEntityFrameworkCoreModule.cs
  3. 255
      modules/identityserver/test/Volo.Abp.IdentityServer.TestBase/Volo/Abp/IdentityServer/AbpIdentityServerTestDataBuilder.cs

148
modules/identityserver/test/Volo.Abp.IdentityServer.EntityFrameworkCore.Tests/Volo/Abp/IdentityServer/AbpIdentityServerTestDataBuilder.cs

@ -1,148 +0,0 @@
using System.Threading.Tasks;
using IdentityServer4.Models;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids;
using Volo.Abp.IdentityServer.ApiResources;
using Volo.Abp.IdentityServer.ApiScopes;
using Volo.Abp.IdentityServer.Clients;
using Volo.Abp.IdentityServer.Grants;
using Volo.Abp.IdentityServer.IdentityResources;
using ApiResource = Volo.Abp.IdentityServer.ApiResources.ApiResource;
using ApiScope = Volo.Abp.IdentityServer.ApiScopes.ApiScope;
using Client = Volo.Abp.IdentityServer.Clients.Client;
using IdentityResource = Volo.Abp.IdentityServer.IdentityResources.IdentityResource;
using PersistedGrant = Volo.Abp.IdentityServer.Grants.PersistedGrant;
namespace Volo.Abp.IdentityServer
{
//TODO: There are two data builders (see AbpIdentityServerTestDataBuilder in Volo.Abp.IdentityServer.TestBase). It should be somehow unified!
public class AbpIdentityServerTestDataBuilder : ITransientDependency
{
private readonly IGuidGenerator _guidGenerator;
private readonly IClientRepository _clientRepository;
private readonly IPersistentGrantRepository _persistentGrantRepository;
private readonly IApiResourceRepository _apiResourceRepository;
private readonly IApiScopeRepository _apiScopeRepository;
private readonly IIdentityResourceRepository _identityResourceRepository;
public AbpIdentityServerTestDataBuilder(
IClientRepository clientRepository,
IGuidGenerator guidGenerator,
IPersistentGrantRepository persistentGrantRepository,
IApiResourceRepository apiResourceRepository,
IIdentityResourceRepository identityResourceRepository,
IApiScopeRepository apiScopeRepository)
{
_clientRepository = clientRepository;
_guidGenerator = guidGenerator;
_persistentGrantRepository = persistentGrantRepository;
_apiResourceRepository = apiResourceRepository;
_identityResourceRepository = identityResourceRepository;
_apiScopeRepository = apiScopeRepository;
}
public async Task BuildAsync()
{
await AddApiResources();
await AddApiScopes();
await AddIdentityResources();
await AddClients();
await AddPersistentGrants();
}
private async Task AddApiResources()
{
var apiResource = new ApiResource(_guidGenerator.Create(), "Test-ApiResource-Name-1")
{
Enabled = true,
Description = "Test-ApiResource-Description-1",
DisplayName = "Test-ApiResource-DisplayName-1"
};
apiResource.AddSecret("secret".Sha256());
apiResource.AddScope("Test-ApiResource-ApiScope-Name-1");
apiResource.AddScope("Test-ApiResource-ApiScope-DisplayName-1");
apiResource.AddUserClaim("Test-ApiResource-Claim-Type-1");
await _apiResourceRepository.InsertAsync(apiResource);
}
private async Task AddApiScopes()
{
var apiScope = new ApiScope(_guidGenerator.Create(), "Test-ApiScope-Name-1");
apiScope.AddUserClaim("Test-ApiScope-Claim-Type-1");
await _apiScopeRepository.InsertAsync(apiScope);
}
private async Task AddIdentityResources()
{
var identityResource = new IdentityResource(_guidGenerator.Create(), "Test-Identity-Resource-Name-1")
{
Description = "Test-Identity-Resource-Description-1",
DisplayName = "Test-Identity-Resource-DisplayName-1",
Required = true,
Emphasize = true
};
identityResource.AddUserClaim("Test-Identity-Resource-1-IdentityClaim-Type-1");
await _identityResourceRepository.InsertAsync(identityResource);
}
private async Task AddClients()
{
var client42 = new Client(_guidGenerator.Create(), "42")
{
ProtocolType = "TestProtocol-42"
};
client42.AddCorsOrigin("Origin1");
client42.AddScope("Test-ApiScope-Name-1");
await _clientRepository.InsertAsync(client42);
}
private async Task AddPersistentGrants()
{
await _persistentGrantRepository.InsertAsync(new PersistedGrant(_guidGenerator.Create())
{
Key = "38",
ClientId = "TestClientId-38",
Type = "TestType-38",
SubjectId = "TestSubject",
Data = "TestData-38"
});
await _persistentGrantRepository.InsertAsync(new PersistedGrant(_guidGenerator.Create())
{
Key = "37",
ClientId = "TestClientId-37",
Type = "TestType-37",
SubjectId = "TestSubject",
Data = "TestData-37"
});
await _persistentGrantRepository.InsertAsync(new PersistedGrant(_guidGenerator.Create())
{
Key = "36",
ClientId = "TestClientId-X",
Type = "TestType-36",
SubjectId = "TestSubject-X",
Data = "TestData-36"
});
await _persistentGrantRepository.InsertAsync(new PersistedGrant(_guidGenerator.Create())
{
Key = "35",
ClientId = "TestClientId-X",
Type = "TestType-35",
SubjectId = "TestSubject-X",
Data = "TestData-35"
});
}
}
}

19
modules/identityserver/test/Volo.Abp.IdentityServer.EntityFrameworkCore.Tests/Volo/Abp/IdentityServer/AbpIdentityServerTestEntityFrameworkCoreModule.cs

@ -1,4 +1,4 @@
using Microsoft.Data.Sqlite; using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage; using Microsoft.EntityFrameworkCore.Storage;
@ -33,11 +33,6 @@ namespace Volo.Abp.IdentityServer
}); });
} }
public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
SeedTestData(context);
}
private static SqliteConnection CreateDatabaseAndGetConnection() private static SqliteConnection CreateDatabaseAndGetConnection()
{ {
var connection = new SqliteConnection("Data Source=:memory:"); var connection = new SqliteConnection("Data Source=:memory:");
@ -53,15 +48,5 @@ namespace Volo.Abp.IdentityServer
return connection; return connection;
} }
private static void SeedTestData(ApplicationInitializationContext context)
{
using (var scope = context.ServiceProvider.CreateScope())
{
AsyncHelper.RunSync(() => scope.ServiceProvider
.GetRequiredService<AbpIdentityServerTestDataBuilder>()
.BuildAsync());
}
}
} }
} }

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

@ -1,5 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids; using Volo.Abp.Guids;
@ -11,6 +10,13 @@ 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; using Volo.Abp.Timing;
using IdentityServer4.Models;
using ApiResource = Volo.Abp.IdentityServer.ApiResources.ApiResource;
using ApiScope = Volo.Abp.IdentityServer.ApiScopes.ApiScope;
using Client = Volo.Abp.IdentityServer.Clients.Client;
using ClientClaim = Volo.Abp.IdentityServer.Clients.ClientClaim;
using IdentityResource = Volo.Abp.IdentityServer.IdentityResources.IdentityResource;
using PersistedGrant = Volo.Abp.IdentityServer.Grants.PersistedGrant;
namespace Volo.Abp.IdentityServer namespace Volo.Abp.IdentityServer
{ {
@ -18,6 +24,7 @@ namespace Volo.Abp.IdentityServer
{ {
private readonly IGuidGenerator _guidGenerator; private readonly IGuidGenerator _guidGenerator;
private readonly IApiResourceRepository _apiResourceRepository; private readonly IApiResourceRepository _apiResourceRepository;
private readonly IApiScopeRepository _apiScopeRepository;
private readonly IClientRepository _clientRepository; private readonly IClientRepository _clientRepository;
private readonly IIdentityResourceRepository _identityResourceRepository; private readonly IIdentityResourceRepository _identityResourceRepository;
private readonly IIdentityClaimTypeRepository _identityClaimTypeRepository; private readonly IIdentityClaimTypeRepository _identityClaimTypeRepository;
@ -29,6 +36,7 @@ namespace Volo.Abp.IdentityServer
public AbpIdentityServerTestDataBuilder( public AbpIdentityServerTestDataBuilder(
IGuidGenerator guidGenerator, IGuidGenerator guidGenerator,
IApiResourceRepository apiResourceRepository, IApiResourceRepository apiResourceRepository,
IApiScopeRepository apiScopeRepository,
IClientRepository clientRepository, IClientRepository clientRepository,
IIdentityResourceRepository identityResourceRepository, IIdentityResourceRepository identityResourceRepository,
IIdentityClaimTypeRepository identityClaimTypeRepository, IIdentityClaimTypeRepository identityClaimTypeRepository,
@ -40,125 +48,91 @@ namespace Volo.Abp.IdentityServer
_testData = testData; _testData = testData;
_guidGenerator = guidGenerator; _guidGenerator = guidGenerator;
_apiResourceRepository = apiResourceRepository; _apiResourceRepository = apiResourceRepository;
_apiScopeRepository = apiScopeRepository;
_clientRepository = clientRepository; _clientRepository = clientRepository;
_identityResourceRepository = identityResourceRepository; _identityResourceRepository = identityResourceRepository;
_identityClaimTypeRepository = identityClaimTypeRepository; _identityClaimTypeRepository = identityClaimTypeRepository;
_persistentGrantRepository = persistentGrantRepository; _persistentGrantRepository = persistentGrantRepository;
_clock = clock;
_deviceFlowCodesRepository = deviceFlowCodesRepository; _deviceFlowCodesRepository = deviceFlowCodesRepository;
_clock = clock;
} }
public async Task BuildAsync() public async Task BuildAsync()
{ {
await AddDeviceFlowCodes(); await AddApiScopes();
await AddPersistedGrants();
await AddIdentityResources();
await AddApiResources(); await AddApiResources();
await AddIdentityResources();
await AddClients(); await AddClients();
await AddPersistentGrants();
await AddDeviceFlowCodes();
await AddPersistedGrants();
await AddClaimTypes(); await AddClaimTypes();
} }
private async Task AddDeviceFlowCodes() private async Task AddApiScopes()
{ {
await _deviceFlowCodesRepository.InsertAsync( var apiScope = new ApiScope(_guidGenerator.Create(), "Test-ApiScope-Name-1");
new DeviceFlowCodes(_guidGenerator.Create())
{
ClientId = "c1",
DeviceCode = "DeviceCode1",
Expiration = _clock.Now.AddDays(1),
Data = "{\"Lifetime\":\"42\"}",
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"
}
);
apiScope.AddUserClaim("Test-ApiScope-Claim-Type-1");
await _apiScopeRepository.InsertAsync(apiScope);
} }
private async Task AddPersistedGrants() private async Task AddApiResources()
{ {
await _persistentGrantRepository.InsertAsync(new PersistedGrant(_guidGenerator.Create()) var apiResource = new ApiResource(_testData.ApiResource1Id, "NewApiResource1");
{ apiResource.Description = nameof(apiResource.Description);
Key = "PersistedGrantKey1", apiResource.DisplayName = nameof(apiResource.DisplayName);
SubjectId = "PersistedGrantSubjectId1",
SessionId = "PersistedGrantSessionId1",
ClientId = "PersistedGrantClientId1",
Type = "PersistedGrantType1",
Data = ""
});
await _persistentGrantRepository.InsertAsync(new PersistedGrant(_guidGenerator.Create()) apiResource.AddScope(nameof(ApiResourceScope.Scope));
{ apiResource.AddUserClaim(nameof(ApiResourceClaim.Type));
Key = "PersistedGrantKey2", apiResource.AddSecret(nameof(ApiResourceSecret.Value));
SubjectId = "PersistedGrantSubjectId2",
ClientId = "c1",
Type = "c1type",
Data = ""
});
await _persistentGrantRepository.InsertAsync(new PersistedGrant(_guidGenerator.Create()) await _apiResourceRepository.InsertAsync(apiResource);
{ await _apiResourceRepository.InsertAsync(new ApiResource(_guidGenerator.Create(), "NewApiResource2"));
Key = "PersistedGrantKey3", await _apiResourceRepository.InsertAsync(new ApiResource(_guidGenerator.Create(), "NewApiResource3"));
SubjectId = "PersistedGrantSubjectId3",
ClientId = "c1",
Type = "c1type",
Data = "",
Expiration = _clock.Now.AddDays(1),
});
await _persistentGrantRepository.InsertAsync(new PersistedGrant(_guidGenerator.Create()) var apiResource2 = new ApiResource(_guidGenerator.Create(), "Test-ApiResource-Name-1")
{ {
Key = "PersistedGrantKey_Expired1", Enabled = true,
SubjectId = "PersistedGrantSubjectId_Expired1", Description = "Test-ApiResource-Description-1",
ClientId = "c1", DisplayName = "Test-ApiResource-DisplayName-1"
Type = "c1type", };
Data = "",
Expiration = _clock.Now.AddDays(-1) apiResource2.AddSecret("secret".Sha256());
}); apiResource2.AddScope("Test-ApiResource-ApiScope-Name-1");
apiResource2.AddScope("Test-ApiResource-ApiScope-DisplayName-1");
apiResource2.AddUserClaim("Test-ApiResource-Claim-Type-1");
await _apiResourceRepository.InsertAsync(apiResource2);
} }
private async Task AddIdentityResources() private async Task AddIdentityResources()
{ {
var identityResource = new IdentityResource(_testData.IdentityResource1Id, "NewIdentityResource1") var identityResource1 = new IdentityResource(_testData.IdentityResource1Id, "NewIdentityResource1")
{ {
Description = nameof(Client.Description), Description = nameof(Client.Description),
DisplayName = nameof(IdentityResource.DisplayName) DisplayName = nameof(IdentityResource.DisplayName)
}; };
identityResource.AddUserClaim(nameof(ApiResourceClaim.Type)); identityResource1.AddUserClaim(nameof(ApiResourceClaim.Type));
await _identityResourceRepository.InsertAsync(identityResource); await _identityResourceRepository.InsertAsync(identityResource1);
await _identityResourceRepository.InsertAsync(new IdentityResource(_guidGenerator.Create(), "NewIdentityResource2")); await _identityResourceRepository.InsertAsync(new IdentityResource(_guidGenerator.Create(), "NewIdentityResource2"));
await _identityResourceRepository.InsertAsync(new IdentityResource(_guidGenerator.Create(), "NewIdentityResource3")); await _identityResourceRepository.InsertAsync(new IdentityResource(_guidGenerator.Create(), "NewIdentityResource3"));
}
private async Task AddApiResources() var identityResource2 = new IdentityResource(_guidGenerator.Create(), "Test-Identity-Resource-Name-1")
{ {
var apiResource = new ApiResource(_testData.ApiResource1Id, "NewApiResource1"); Description = "Test-Identity-Resource-Description-1",
apiResource.Description = nameof(apiResource.Description); DisplayName = "Test-Identity-Resource-DisplayName-1",
apiResource.DisplayName = nameof(apiResource.DisplayName); Required = true,
Emphasize = true
apiResource.AddScope(nameof(ApiResourceScope.Scope)); };
apiResource.AddUserClaim(nameof(ApiResourceClaim.Type));
apiResource.AddSecret(nameof(ApiResourceSecret.Value));
await _apiResourceRepository.InsertAsync(apiResource); identityResource2.AddUserClaim("Test-Identity-Resource-1-IdentityClaim-Type-1");
await _apiResourceRepository.InsertAsync(new ApiResource(_guidGenerator.Create(), "NewApiResource2")); await _identityResourceRepository.InsertAsync(identityResource2);
await _apiResourceRepository.InsertAsync(new ApiResource(_guidGenerator.Create(), "NewApiResource3"));
} }
private async Task AddClients() private async Task AddClients()
{ {
var client = new Client(_testData.Client1Id, "ClientId1") var client = new Client(_testData.Client1Id, "ClientId1")
{ {
@ -184,6 +158,125 @@ namespace Volo.Abp.IdentityServer
await _clientRepository.InsertAsync(new Client(_guidGenerator.Create(), "ClientId2")); await _clientRepository.InsertAsync(new Client(_guidGenerator.Create(), "ClientId2"));
await _clientRepository.InsertAsync(new Client(_guidGenerator.Create(), "ClientId3")); await _clientRepository.InsertAsync(new Client(_guidGenerator.Create(), "ClientId3"));
var client42 = new Client(_guidGenerator.Create(), "42")
{
ProtocolType = "TestProtocol-42"
};
client42.AddCorsOrigin("Origin1");
client42.AddScope("Test-ApiScope-Name-1");
await _clientRepository.InsertAsync(client42);
}
private async Task AddPersistentGrants()
{
await _persistentGrantRepository.InsertAsync(new PersistedGrant(_guidGenerator.Create())
{
Key = "38",
ClientId = "TestClientId-38",
Type = "TestType-38",
SubjectId = "TestSubject",
Data = "TestData-38"
});
await _persistentGrantRepository.InsertAsync(new PersistedGrant(_guidGenerator.Create())
{
Key = "37",
ClientId = "TestClientId-37",
Type = "TestType-37",
SubjectId = "TestSubject",
Data = "TestData-37"
});
await _persistentGrantRepository.InsertAsync(new PersistedGrant(_guidGenerator.Create())
{
Key = "36",
ClientId = "TestClientId-X",
Type = "TestType-36",
SubjectId = "TestSubject-X",
Data = "TestData-36"
});
await _persistentGrantRepository.InsertAsync(new PersistedGrant(_guidGenerator.Create())
{
Key = "35",
ClientId = "TestClientId-X",
Type = "TestType-35",
SubjectId = "TestSubject-X",
Data = "TestData-35"
});
}
private async Task AddPersistedGrants()
{
await _persistentGrantRepository.InsertAsync(new PersistedGrant(_guidGenerator.Create())
{
Key = "PersistedGrantKey1",
SubjectId = "PersistedGrantSubjectId1",
SessionId = "PersistedGrantSessionId1",
ClientId = "PersistedGrantClientId1",
Type = "PersistedGrantType1",
Data = ""
});
await _persistentGrantRepository.InsertAsync(new PersistedGrant(_guidGenerator.Create())
{
Key = "PersistedGrantKey2",
SubjectId = "PersistedGrantSubjectId2",
ClientId = "c1",
Type = "c1type",
Data = ""
});
await _persistentGrantRepository.InsertAsync(new PersistedGrant(_guidGenerator.Create())
{
Key = "PersistedGrantKey3",
SubjectId = "PersistedGrantSubjectId3",
ClientId = "c1",
Type = "c1type",
Data = "",
Expiration = _clock.Now.AddDays(1),
});
await _persistentGrantRepository.InsertAsync(new PersistedGrant(_guidGenerator.Create())
{
Key = "PersistedGrantKey_Expired1",
SubjectId = "PersistedGrantSubjectId_Expired1",
ClientId = "c1",
Type = "c1type",
Data = "",
Expiration = _clock.Now.AddDays(-1)
});
}
private async Task AddDeviceFlowCodes()
{
await _deviceFlowCodesRepository.InsertAsync(
new DeviceFlowCodes(_guidGenerator.Create())
{
ClientId = "c1",
DeviceCode = "DeviceCode1",
Expiration = _clock.Now.AddDays(1),
Data = "{\"Lifetime\":\"42\"}",
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 AddClaimTypes() private async Task AddClaimTypes()

Loading…
Cancel
Save