-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from bsandmann/dev
Issuing Key Commands Tests
- Loading branch information
Showing
7 changed files
with
581 additions
and
2 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
.idea/.idea.blocktrust.CredentialWorkflow/.idea/dataSources.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
188 changes: 188 additions & 0 deletions
188
...st.CredentialWorkflow.Core.Tests/Commands/IssuingKeyTests/CreateIssuingKeyHandlerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
using Blocktrust.CredentialWorkflow.Core.Commands.Tenant.CreateIssuingKey; | ||
using Blocktrust.CredentialWorkflow.Core.Commands.Tenant.CreateTenant; | ||
using FluentAssertions; | ||
using Microsoft.EntityFrameworkCore; | ||
// using Blocktrust.CredentialWorkflow.Core.Commands.IssuingKey; | ||
// using Blocktrust.CredentialWorkflow.Core.Commands.Tenant; | ||
|
||
namespace Blocktrust.CredentialWorkflow.Core.Tests.Commands.IssuingKeyTests; | ||
|
||
public class CreateIssuingKeyHandlerTests : TestSetup | ||
{ | ||
private readonly CreateIssuingKeyHandler _handler; | ||
private readonly CreateTenantHandler _createTenantHandler; | ||
private readonly DataContext _dataContext; | ||
|
||
public CreateIssuingKeyHandlerTests(TransactionalTestDatabaseFixture fixture) : base(fixture) | ||
{ | ||
_dataContext = fixture.CreateContext(); | ||
_handler = new CreateIssuingKeyHandler(_dataContext); | ||
_createTenantHandler = new CreateTenantHandler(_dataContext); | ||
} | ||
|
||
[Fact] | ||
public async Task Handle_ValidRequest_ShouldSucceed() | ||
{ | ||
// Arrange | ||
var tenantResult = await _createTenantHandler.Handle(new CreateTenantRequest("TestTenant"), CancellationToken.None); | ||
tenantResult.IsSuccess.Should().BeTrue(); | ||
var tenantId = tenantResult.Value; | ||
|
||
var request = new CreateIssuingKeyRequest( | ||
tenantId, | ||
"TestKey", | ||
"did:prism:test123", | ||
"secp256k1", | ||
"publicKeyTest", | ||
"privateKeyTest"); | ||
|
||
// Act | ||
var result = await _handler.Handle(request, CancellationToken.None); | ||
|
||
// Assert | ||
result.IsSuccess.Should().BeTrue(); | ||
result.Value.Should().NotBeNull(); | ||
result.Value.Name.Should().Be("TestKey"); | ||
result.Value.Did.Should().Be("did:prism:test123"); | ||
result.Value.KeyType.Should().Be("secp256k1"); | ||
result.Value.PublicKey.Should().Be("publicKeyTest"); | ||
result.Value.PrivateKey.Should().Be("privateKeyTest"); | ||
|
||
// Verify database state | ||
var issuingKey = await _dataContext.IssuingKeys | ||
.FirstOrDefaultAsync(i => i.TenantEntityId == tenantId); | ||
issuingKey.Should().NotBeNull(); | ||
issuingKey!.Name.Should().Be("TestKey"); | ||
issuingKey.Did.Should().Be("did:prism:test123"); | ||
issuingKey.KeyType.Should().Be("secp256k1"); | ||
issuingKey.PublicKey.Should().Be("publicKeyTest"); | ||
issuingKey.PrivateKey.Should().Be("privateKeyTest"); | ||
} | ||
|
||
[Fact] | ||
public async Task Handle_NonExistentTenant_ShouldFail() | ||
{ | ||
// Arrange | ||
var request = new CreateIssuingKeyRequest( | ||
Guid.NewGuid(), | ||
"TestKey", | ||
"did:prism:test123", | ||
"secp256k1", | ||
"publicKeyTest", | ||
"privateKeyTest"); | ||
|
||
// Act | ||
var result = await _handler.Handle(request, CancellationToken.None); | ||
|
||
// Assert | ||
result.IsFailed.Should().BeTrue(); | ||
result.Errors.Should().ContainSingle() | ||
.Which.Message.Should().Be("The tenant does not exist in the database. The IssuingKey cannot be created."); | ||
} | ||
|
||
[Theory] | ||
[InlineData("", "did:prism:test", "secp256k1", "pub", "priv")] | ||
[InlineData("TestKey", "", "secp256k1", "pub", "priv")] | ||
[InlineData("TestKey", "did:prism:test", "", "pub", "priv")] | ||
[InlineData("TestKey", "did:prism:test", "secp256k1", "", "priv")] | ||
[InlineData("TestKey", "did:prism:test", "secp256k1", "pub", "")] | ||
public async Task Handle_WithEmptyValues_ShouldCreateSuccessfully( | ||
string name, string did, string keyType, string publicKey, string privateKey) | ||
{ | ||
// Arrange | ||
var tenantResult = await _createTenantHandler.Handle(new CreateTenantRequest("TestTenant"), CancellationToken.None); | ||
tenantResult.IsSuccess.Should().BeTrue(); | ||
var tenantId = tenantResult.Value; | ||
|
||
var request = new CreateIssuingKeyRequest( | ||
tenantId, | ||
name, | ||
did, | ||
keyType, | ||
publicKey, | ||
privateKey); | ||
|
||
// Act | ||
var result = await _handler.Handle(request, CancellationToken.None); | ||
|
||
// Assert | ||
result.IsSuccess.Should().BeTrue(); | ||
result.Value.Should().NotBeNull(); | ||
result.Value.Name.Should().Be(name); | ||
result.Value.Did.Should().Be(did); | ||
result.Value.KeyType.Should().Be(keyType); | ||
result.Value.PublicKey.Should().Be(publicKey); | ||
result.Value.PrivateKey.Should().Be(privateKey); | ||
} | ||
|
||
[Fact] | ||
public async Task Handle_MultipleKeysForSameTenant_ShouldSucceed() | ||
{ | ||
// Arrange | ||
var tenantResult = await _createTenantHandler.Handle(new CreateTenantRequest("TestTenant"), CancellationToken.None); | ||
tenantResult.IsSuccess.Should().BeTrue(); | ||
var tenantId = tenantResult.Value; | ||
|
||
var request1 = new CreateIssuingKeyRequest( | ||
tenantId, | ||
"TestKey1", | ||
"did:prism:test1", | ||
"secp256k1", | ||
"publicKey1", | ||
"privateKey1"); | ||
|
||
var request2 = new CreateIssuingKeyRequest( | ||
tenantId, | ||
"TestKey2", | ||
"did:prism:test2", | ||
"secp256k1", | ||
"publicKey2", | ||
"privateKey2"); | ||
|
||
// Act | ||
var result1 = await _handler.Handle(request1, CancellationToken.None); | ||
var result2 = await _handler.Handle(request2, CancellationToken.None); | ||
|
||
// Assert | ||
result1.IsSuccess.Should().BeTrue(); | ||
result2.IsSuccess.Should().BeTrue(); | ||
|
||
var issuingKeys = await _dataContext.IssuingKeys | ||
.Where(i => i.TenantEntityId == tenantId) | ||
.ToListAsync(); | ||
|
||
issuingKeys.Should().HaveCount(2); | ||
issuingKeys.Should().Contain(k => k.Name == "TestKey1" && k.Did == "did:prism:test1"); | ||
issuingKeys.Should().Contain(k => k.Name == "TestKey2" && k.Did == "did:prism:test2"); | ||
} | ||
|
||
// [Fact] | ||
// public async Task Handle_WithLongValues_ShouldSucceed() | ||
// { | ||
// // Arrange | ||
// var tenantResult = await _createTenantHandler.Handle(new CreateTenantRequest("TestTenant"), CancellationToken.None); | ||
// tenantResult.IsSuccess.Should().BeTrue(); | ||
// var tenantId = tenantResult.Value; | ||
// | ||
// var longString = new string('x', 1000); | ||
// var request = new CreateIssuingKeyRequest( | ||
// tenantId, | ||
// longString, | ||
// longString, | ||
// longString, | ||
// longString, | ||
// longString); | ||
// | ||
// // Act | ||
// var result = await _handler.Handle(request, CancellationToken.None); | ||
// | ||
// // Assert | ||
// result.IsSuccess.Should().BeTrue(); | ||
// result.Value.Should().NotBeNull(); | ||
// result.Value.Name.Should().Be(longString); | ||
// result.Value.Did.Should().Be(longString); | ||
// result.Value.KeyType.Should().Be(longString); | ||
// result.Value.PublicKey.Should().Be(longString); | ||
// result.Value.PrivateKey.Should().Be(longString); | ||
// } | ||
} |
183 changes: 183 additions & 0 deletions
183
...t.CredentialWorkflow.Core.Tests/Commands/IssuingKeyTests/GetIssuingKeyByIdHandlerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
using Blocktrust.CredentialWorkflow.Core.Commands.Tenant.CreateIssuingKey; | ||
using Blocktrust.CredentialWorkflow.Core.Commands.Tenant.CreateTenant; | ||
using Blocktrust.CredentialWorkflow.Core.Commands.Tenant.GetIssungKeyById; | ||
using FluentAssertions; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
|
||
namespace Blocktrust.CredentialWorkflow.Core.Tests.Commands.IssuingKeyTests; | ||
|
||
public class GetIssuingKeyByIdHandlerTests : TestSetup | ||
{ | ||
private readonly GetIssuingKeyByIdHandler _handler; | ||
private readonly CreateIssuingKeyHandler _createIssuingKeyHandler; | ||
private readonly CreateTenantHandler _createTenantHandler; | ||
private readonly DataContext _dataContext; | ||
|
||
public GetIssuingKeyByIdHandlerTests(TransactionalTestDatabaseFixture fixture) : base(fixture) | ||
{ | ||
_dataContext = fixture.CreateContext(); | ||
_handler = new GetIssuingKeyByIdHandler(_dataContext); | ||
_createIssuingKeyHandler = new CreateIssuingKeyHandler(_dataContext); | ||
_createTenantHandler = new CreateTenantHandler(_dataContext); | ||
} | ||
|
||
[Fact] | ||
public async Task Handle_NonExistentKeyId_ShouldReturnFailure() | ||
{ | ||
// Arrange | ||
var nonExistentKeyId = Guid.NewGuid(); | ||
var request = new GetIssuingKeyByIdRequest(nonExistentKeyId); | ||
|
||
// Act | ||
var result = await _handler.Handle(request, CancellationToken.None); | ||
|
||
// Assert | ||
result.IsFailed.Should().BeTrue(); | ||
result.Errors.Should().ContainSingle() | ||
.Which.Message.Should().Be("Issuing key not found."); | ||
|
||
var keyInDb = await _dataContext.IssuingKeys.FindAsync(nonExistentKeyId); | ||
keyInDb.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public async Task Handle_MultipleKeysForSameTenant_ShouldReturnCorrectKey() | ||
{ | ||
// Arrange | ||
var tenantResult = await _createTenantHandler.Handle( | ||
new CreateTenantRequest("TestTenant"), | ||
CancellationToken.None); | ||
tenantResult.IsSuccess.Should().BeTrue(); | ||
var tenantId = tenantResult.Value; | ||
|
||
// Create multiple keys for the same tenant | ||
var key1Result = await _createIssuingKeyHandler.Handle( | ||
new CreateIssuingKeyRequest( | ||
tenantId, | ||
"TestKey1", | ||
"did:prism:test1", | ||
"secp256k1", | ||
"publicKey1", | ||
"privateKey1"), | ||
CancellationToken.None); | ||
|
||
var key2Result = await _createIssuingKeyHandler.Handle( | ||
new CreateIssuingKeyRequest( | ||
tenantId, | ||
"TestKey2", | ||
"did:prism:test2", | ||
"secp256k1", | ||
"publicKey2", | ||
"privateKey2"), | ||
CancellationToken.None); | ||
|
||
var key3Result = await _createIssuingKeyHandler.Handle( | ||
new CreateIssuingKeyRequest( | ||
tenantId, | ||
"TestKey3", | ||
"did:prism:test3", | ||
"secp256k1", | ||
"publicKey3", | ||
"privateKey3"), | ||
CancellationToken.None); | ||
|
||
// Verify all keys were created successfully | ||
key1Result.IsSuccess.Should().BeTrue(); | ||
key2Result.IsSuccess.Should().BeTrue(); | ||
key3Result.IsSuccess.Should().BeTrue(); | ||
|
||
// Act - retrieve the second key | ||
var result = await _handler.Handle( | ||
new GetIssuingKeyByIdRequest(key2Result.Value.IssuingKeyId), | ||
CancellationToken.None); | ||
|
||
// Assert | ||
result.IsSuccess.Should().BeTrue(); | ||
result.Value.Should().NotBeNull(); | ||
result.Value.IssuingKeyId.Should().Be(key2Result.Value.IssuingKeyId); | ||
result.Value.Name.Should().Be("TestKey2"); | ||
result.Value.Did.Should().Be("did:prism:test2"); | ||
|
||
// Verify all keys exist in database | ||
var keysInDb = await _dataContext.IssuingKeys | ||
.Where(k => k.TenantEntityId == tenantId) | ||
.ToListAsync(); | ||
keysInDb.Should().HaveCount(3); | ||
} | ||
|
||
[Fact] | ||
public async Task Handle_KeyDataIntegrity_ShouldPreserveAllFields() | ||
{ | ||
// Arrange | ||
var tenantResult = await _createTenantHandler.Handle( | ||
new CreateTenantRequest("TestTenant"), | ||
CancellationToken.None); | ||
tenantResult.IsSuccess.Should().BeTrue(); | ||
|
||
// Create a key with specific test data | ||
var complexData = new | ||
{ | ||
Name = "Complex Key Name with Spaces and Special Chars !@#$", | ||
Did = "did:prism:test:with:multiple:colons:and:special:chars:!@#$", | ||
KeyType = "secp256k1WithSpecialParams!@#$", | ||
PublicKey = new string('A', 1000), // Long public key | ||
PrivateKey = new string('B', 1000) // Long private key | ||
}; | ||
|
||
var createKeyResult = await _createIssuingKeyHandler.Handle( | ||
new CreateIssuingKeyRequest( | ||
tenantResult.Value, | ||
complexData.Name, | ||
complexData.Did, | ||
complexData.KeyType, | ||
complexData.PublicKey, | ||
complexData.PrivateKey), | ||
CancellationToken.None); | ||
createKeyResult.IsSuccess.Should().BeTrue(); | ||
|
||
// Act | ||
var result = await _handler.Handle( | ||
new GetIssuingKeyByIdRequest(createKeyResult.Value.IssuingKeyId), | ||
CancellationToken.None); | ||
|
||
// Assert | ||
result.IsSuccess.Should().BeTrue(); | ||
result.Value.Should().NotBeNull(); | ||
|
||
// Verify all fields maintain exact data integrity | ||
result.Value.Name.Should().Be(complexData.Name); | ||
result.Value.Did.Should().Be(complexData.Did); | ||
result.Value.KeyType.Should().Be(complexData.KeyType); | ||
result.Value.PublicKey.Should().Be(complexData.PublicKey); | ||
result.Value.PrivateKey.Should().Be(complexData.PrivateKey); | ||
|
||
// Verify length and content of long fields | ||
result.Value.PublicKey.Length.Should().Be(1000); | ||
result.Value.PrivateKey.Length.Should().Be(1000); | ||
|
||
// Verify data integrity in database | ||
var keyInDb = await _dataContext.IssuingKeys.FindAsync(result.Value.IssuingKeyId); | ||
keyInDb.Should().NotBeNull(); | ||
keyInDb!.Name.Should().Be(complexData.Name); | ||
keyInDb.Did.Should().Be(complexData.Did); | ||
keyInDb.KeyType.Should().Be(complexData.KeyType); | ||
keyInDb.PublicKey.Should().Be(complexData.PublicKey); | ||
keyInDb.PrivateKey.Should().Be(complexData.PrivateKey); | ||
} | ||
|
||
[Fact] | ||
public async Task Handle_EmptyGuid_ShouldReturnFailure() | ||
{ | ||
// Arrange | ||
var request = new GetIssuingKeyByIdRequest(Guid.Empty); | ||
|
||
// Act | ||
var result = await _handler.Handle(request, CancellationToken.None); | ||
|
||
// Assert | ||
result.IsFailed.Should().BeTrue(); | ||
result.Errors.Should().ContainSingle() | ||
.Which.Message.Should().Be("Issuing key not found."); | ||
} | ||
} |
Oops, something went wrong.