-
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.
- Loading branch information
1 parent
4a6e68d
commit bc2e98f
Showing
10 changed files
with
220 additions
and
218 deletions.
There are no files selected for viewing
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
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
110 changes: 53 additions & 57 deletions
110
Blocktrust.CredentialWorkflow.Core.Tests/DIDCommTests/SavePeerDIDSecretsHandlerTests.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 |
---|---|---|
@@ -1,67 +1,63 @@ | ||
namespace Blocktrust.CredentialWorkflow.Core.Tests.DIDCommTests | ||
using Blocktrust.Common.Models.DidDoc; | ||
using Blocktrust.Common.Models.Secrets; | ||
using Blocktrust.CredentialWorkflow.Core.Commands.DIDComm.SavePeerDIDSecrets; | ||
using FluentAssertions; | ||
|
||
namespace Blocktrust.CredentialWorkflow.Core.Tests.DIDCommTests; | ||
|
||
public class SavePeerDIDSecretsHandlerTests : TestSetup | ||
{ | ||
using Blocktrust.Common.Models.DidDoc; | ||
using Blocktrust.Common.Models.Secrets; | ||
using Blocktrust.CredentialWorkflow.Core; | ||
using Blocktrust.CredentialWorkflow.Core.Commands.DIDComm.SavePeerDIDSecrets; | ||
using Blocktrust.CredentialWorkflow.Core.Tests; | ||
using FluentAssertions; | ||
using Xunit; | ||
private readonly DataContext _dataContext; | ||
private readonly SavePeerDIDSecretsHandler _handler; | ||
|
||
public class SavePeerDIDSecretsHandlerTests : TestSetup | ||
public SavePeerDIDSecretsHandlerTests(TransactionalTestDatabaseFixture fixture) : base(fixture) | ||
{ | ||
private readonly DataContext _dataContext; | ||
private readonly SavePeerDIDSecretsHandler _handler; | ||
|
||
public SavePeerDIDSecretsHandlerTests(TransactionalTestDatabaseFixture fixture) : base(fixture) | ||
{ | ||
_dataContext = fixture.CreateContext(); | ||
_handler = new SavePeerDIDSecretsHandler(_dataContext); | ||
} | ||
_dataContext = fixture.CreateContext(); | ||
_handler = new SavePeerDIDSecretsHandler(_dataContext); | ||
} | ||
|
||
[Fact] | ||
public async Task Handle_ValidRequest_ShouldSavePeerDIDSecret() | ||
[Fact] | ||
public async Task Handle_ValidRequest_ShouldSavePeerDIDSecret() | ||
{ | ||
// Arrange | ||
var secret = new Secret | ||
{ | ||
// Arrange | ||
var secret = new Secret | ||
Type = VerificationMethodType.JsonWebKey2020, | ||
VerificationMaterial = new VerificationMaterial | ||
{ | ||
Type = VerificationMethodType.JsonWebKey2020, | ||
VerificationMaterial = new VerificationMaterial | ||
{ | ||
Format = VerificationMaterialFormat.Jwk, | ||
Value = "{\"kty\":\"EC\",\"crv\":\"secp256k1\",\"x\":\"abc\",\"y\":\"123\"}" // Example JSON | ||
} | ||
}; | ||
|
||
var kid = "did:example:123#key-1"; | ||
var request = new SavePeerDIDSecretRequest(kid, secret); | ||
|
||
// Act | ||
var result = await _handler.Handle(request, CancellationToken.None); | ||
|
||
// Assert | ||
result.IsSuccess.Should().BeTrue("the request is valid, so saving should succeed"); | ||
|
||
// Verify that the secret was saved in the database | ||
var savedSecret = _dataContext.PeerDIDSecrets.FirstOrDefault(x => x.Kid == kid); | ||
savedSecret.Should().NotBeNull("we expect an entry to be created in PeerDIDSecretEntities table"); | ||
savedSecret!.Kid.Should().Be(kid); | ||
savedSecret.Value.Should().Be(secret.VerificationMaterial.Value); | ||
savedSecret.VerificationMaterialFormat.Should().Be((int)secret.VerificationMaterial.Format); | ||
savedSecret.VerificationMethodType.Should().Be((int)secret.Type); | ||
} | ||
Format = VerificationMaterialFormat.Jwk, | ||
Value = "{\"kty\":\"EC\",\"crv\":\"secp256k1\",\"x\":\"abc\",\"y\":\"123\"}" // Example JSON | ||
} | ||
}; | ||
|
||
var kid = "did:example:123#key-1"; | ||
var request = new SavePeerDIDSecretRequest(kid, secret); | ||
|
||
// Act | ||
var result = await _handler.Handle(request, CancellationToken.None); | ||
|
||
// Assert | ||
result.IsSuccess.Should().BeTrue("the request is valid, so saving should succeed"); | ||
|
||
// Verify that the secret was saved in the database | ||
var savedSecret = _dataContext.PeerDIDSecrets.FirstOrDefault(x => x.Kid == kid); | ||
savedSecret.Should().NotBeNull("we expect an entry to be created in PeerDIDSecretEntities table"); | ||
savedSecret!.Kid.Should().Be(kid); | ||
savedSecret.Value.Should().Be(secret.VerificationMaterial.Value); | ||
savedSecret.VerificationMaterialFormat.Should().Be((int)secret.VerificationMaterial.Format); | ||
savedSecret.VerificationMethodType.Should().Be((int)secret.Type); | ||
} | ||
|
||
[Fact] | ||
public async Task Handle_NullSecret_ShouldThrowOrFail() | ||
{ | ||
// Arrange | ||
var request = new SavePeerDIDSecretRequest("someKid", null!); | ||
[Fact] | ||
public async Task Handle_NullSecret_ShouldThrowOrFail() | ||
{ | ||
// Arrange | ||
var request = new SavePeerDIDSecretRequest("someKid", null!); | ||
|
||
// Act | ||
Func<Task> act = async () => await _handler.Handle(request, CancellationToken.None); | ||
// Act | ||
Func<Task> act = async () => await _handler.Handle(request, CancellationToken.None); | ||
|
||
// Assert | ||
await act.Should().ThrowAsync<NullReferenceException>("the Secret is null and code does not guard against it"); | ||
} | ||
// Assert | ||
await act.Should().ThrowAsync<NullReferenceException>("the Secret is null and code does not guard against it"); | ||
} | ||
} | ||
} |
Oops, something went wrong.