diff --git a/Blocktrust.CredentialWorkflow.Core.Tests/Blocktrust.CredentialWorkflow.Core.Tests.csproj b/Blocktrust.CredentialWorkflow.Core.Tests/Blocktrust.CredentialWorkflow.Core.Tests.csproj index 5f225c1..12ecb19 100644 --- a/Blocktrust.CredentialWorkflow.Core.Tests/Blocktrust.CredentialWorkflow.Core.Tests.csproj +++ b/Blocktrust.CredentialWorkflow.Core.Tests/Blocktrust.CredentialWorkflow.Core.Tests.csproj @@ -30,6 +30,7 @@ + diff --git a/Blocktrust.CredentialWorkflow.Core.Tests/DIDCommTests/GetPeerDIDSecretsHandlerTests.cs b/Blocktrust.CredentialWorkflow.Core.Tests/DIDCommTests/GetPeerDIDSecretsHandlerTests.cs index 75478b5..a232436 100644 --- a/Blocktrust.CredentialWorkflow.Core.Tests/DIDCommTests/GetPeerDIDSecretsHandlerTests.cs +++ b/Blocktrust.CredentialWorkflow.Core.Tests/DIDCommTests/GetPeerDIDSecretsHandlerTests.cs @@ -3,8 +3,6 @@ using Blocktrust.CredentialWorkflow.Core.Commands.DIDComm.GetPeerDIDSecrets; using Blocktrust.CredentialWorkflow.Core.Commands.DIDComm.SavePeerDIDSecrets; using FluentAssertions; -using Microsoft.EntityFrameworkCore; -using Xunit; namespace Blocktrust.CredentialWorkflow.Core.Tests.DIDCommTests { diff --git a/Blocktrust.CredentialWorkflow.Core.Tests/DIDCommTests/SavePeerDIDSecretsHandlerTests.cs b/Blocktrust.CredentialWorkflow.Core.Tests/DIDCommTests/SavePeerDIDSecretsHandlerTests.cs index 793ac96..5dae252 100644 --- a/Blocktrust.CredentialWorkflow.Core.Tests/DIDCommTests/SavePeerDIDSecretsHandlerTests.cs +++ b/Blocktrust.CredentialWorkflow.Core.Tests/DIDCommTests/SavePeerDIDSecretsHandlerTests.cs @@ -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 act = async () => await _handler.Handle(request, CancellationToken.None); + // Act + Func act = async () => await _handler.Handle(request, CancellationToken.None); - // Assert - await act.Should().ThrowAsync("the Secret is null and code does not guard against it"); - } + // Assert + await act.Should().ThrowAsync("the Secret is null and code does not guard against it"); } -} +} \ No newline at end of file diff --git a/Blocktrust.CredentialWorkflow.Core.Tests/Workflow/ExecuteWorkflow/ExecuteWorkflowHandlerTests.cs b/Blocktrust.CredentialWorkflow.Core.Tests/Workflow/ExecuteWorkflow/ExecuteWorkflowHandlerTests.cs index 5dc38e1..62f5083 100644 --- a/Blocktrust.CredentialWorkflow.Core.Tests/Workflow/ExecuteWorkflow/ExecuteWorkflowHandlerTests.cs +++ b/Blocktrust.CredentialWorkflow.Core.Tests/Workflow/ExecuteWorkflow/ExecuteWorkflowHandlerTests.cs @@ -1,151 +1,150 @@ -using FluentAssertions; -using Xunit; +using Blocktrust.CredentialWorkflow.Core.Commands.Workflow.ExecuteWorkflow; +using FluentAssertions; -namespace Blocktrust.CredentialWorkflow.Core.Tests.Workflow.ExecuteWorkflow +namespace Blocktrust.CredentialWorkflow.Core.Tests.Workflow.ExecuteWorkflow; + +public class ExecuteWorkflowHandlerTests { - public class ExecuteWorkflowHandlerTests - { - private readonly ExecuteWorkflowHandler _handler; + private readonly ExecuteWorkflowHandler _handler; - public ExecuteWorkflowHandlerTests() - { - // Since we're only testing ProcessEmailTemplate which doesn't use _mediator, - // we can pass null as it won't be used in our tests - _handler = new ExecuteWorkflowHandler(null!); - } + // public ExecuteWorkflowHandlerTests() + // { + // // Since we're only testing ProcessEmailTemplate which doesn't use _mediator, + // // we can pass null as it won't be used in our tests + // _handler = new ExecuteWorkflowHandler(null!); + // } - [Fact] - public void ProcessEmailTemplate_WithNullTemplate_ShouldReturnEmptyString() - { - // Arrange - string? template = null; - var parameters = new Dictionary - { - { "name", "John" } - }; - - // Act - var result = _handler.ProcessEmailTemplate(template, parameters); - - // Assert - result.Should().BeEmpty(); - } - - [Fact] - public void ProcessEmailTemplate_WithEmptyTemplate_ShouldReturnEmptyString() - { - // Arrange - var template = string.Empty; - var parameters = new Dictionary - { - { "name", "John" } - }; - - // Act - var result = _handler.ProcessEmailTemplate(template, parameters); - - // Assert - result.Should().BeEmpty(); - } - - [Fact] - public void ProcessEmailTemplate_WithNullParameters_ShouldReturnOriginalTemplate() + [Fact] + public void ProcessEmailTemplate_WithNullTemplate_ShouldReturnEmptyString() + { + // Arrange + string? template = null; + var parameters = new Dictionary { - // Arrange - var template = "Hello [name], welcome!"; - Dictionary? parameters = null; + { "name", "John" } + }; - // Act - var result = _handler.ProcessEmailTemplate(template, parameters); + // Act + var result = _handler.ProcessEmailTemplate(template, parameters); - // Assert - result.Should().Be(template); - } + // Assert + result.Should().BeEmpty(); + } - [Fact] - public void ProcessEmailTemplate_WithEmptyParameters_ShouldReturnOriginalTemplate() + [Fact] + public void ProcessEmailTemplate_WithEmptyTemplate_ShouldReturnEmptyString() + { + // Arrange + var template = string.Empty; + var parameters = new Dictionary { - // Arrange - var template = "Hello [name], welcome!"; - var parameters = new Dictionary(); + { "name", "John" } + }; - // Act - var result = _handler.ProcessEmailTemplate(template, parameters); + // Act + var result = _handler.ProcessEmailTemplate(template, parameters); + + // Assert + result.Should().BeEmpty(); + } - // Assert - result.Should().Be(template); - } + [Fact] + public void ProcessEmailTemplate_WithNullParameters_ShouldReturnOriginalTemplate() + { + // Arrange + var template = "Hello [name], welcome!"; + Dictionary? parameters = null; + + // Act + var result = _handler.ProcessEmailTemplate(template, parameters); + + // Assert + result.Should().Be(template); + } + + [Fact] + public void ProcessEmailTemplate_WithEmptyParameters_ShouldReturnOriginalTemplate() + { + // Arrange + var template = "Hello [name], welcome!"; + var parameters = new Dictionary(); + + // Act + var result = _handler.ProcessEmailTemplate(template, parameters); + + // Assert + result.Should().Be(template); + } - [Fact] - public void ProcessEmailTemplate_WithSingleParameter_ShouldReplaceCorrectly() + [Fact] + public void ProcessEmailTemplate_WithSingleParameter_ShouldReplaceCorrectly() + { + // Arrange + var template = "Hello [name], welcome!"; + var parameters = new Dictionary { - // Arrange - var template = "Hello [name], welcome!"; - var parameters = new Dictionary - { - { "name", "John" } - }; - - // Act - var result = _handler.ProcessEmailTemplate(template, parameters); - - // Assert - result.Should().Be("Hello John, welcome!"); - } - - [Fact] - public void ProcessEmailTemplate_WithMultipleParameters_ShouldReplaceAll() + { "name", "John" } + }; + + // Act + var result = _handler.ProcessEmailTemplate(template, parameters); + + // Assert + result.Should().Be("Hello John, welcome!"); + } + + [Fact] + public void ProcessEmailTemplate_WithMultipleParameters_ShouldReplaceAll() + { + // Arrange + var template = "Hello [name], welcome to [company]!"; + var parameters = new Dictionary { - // Arrange - var template = "Hello [name], welcome to [company]!"; - var parameters = new Dictionary - { - { "name", "John" }, - { "company", "Acme Corp" } - }; - - // Act - var result = _handler.ProcessEmailTemplate(template, parameters); - - // Assert - result.Should().Be("Hello John, welcome to Acme Corp!"); - } - - [Fact] - public void ProcessEmailTemplate_WithParameterHavingNullValue_ShouldReplaceWithEmptyString() + { "name", "John" }, + { "company", "Acme Corp" } + }; + + // Act + var result = _handler.ProcessEmailTemplate(template, parameters); + + // Assert + result.Should().Be("Hello John, welcome to Acme Corp!"); + } + + [Fact] + public void ProcessEmailTemplate_WithParameterHavingNullValue_ShouldReplaceWithEmptyString() + { + // Arrange + var template = "Hello [name], welcome!"; + var parameters = new Dictionary { - // Arrange - var template = "Hello [name], welcome!"; - var parameters = new Dictionary - { - { "name", null! } - }; - - // Act - var result = _handler.ProcessEmailTemplate(template, parameters); - - // Assert - result.Should().Be("Hello , welcome!"); - } - - [Fact] - public void ProcessEmailTemplate_WithUnmatchedParameters_ShouldNotReplace() + { "name", null! } + }; + + // Act + var result = _handler.ProcessEmailTemplate(template, parameters); + + // Assert + result.Should().Be("Hello , welcome!"); + } + + [Fact] + public void ProcessEmailTemplate_WithUnmatchedParameters_ShouldNotReplace() + { + // Arrange + var template = "Hello [name], welcome to [company]!"; + var parameters = new Dictionary { - // Arrange - var template = "Hello [name], welcome to [company]!"; - var parameters = new Dictionary - { - { "name", "John" }, - { "location", "New York" } // This parameter doesn't exist in template - }; + { "name", "John" }, + { "location", "New York" } // This parameter doesn't exist in template + }; - // Act - var result = _handler.ProcessEmailTemplate(template, parameters); + // Act + var result = _handler.ProcessEmailTemplate(template, parameters); - // Assert - result.Should().Be("Hello John, welcome to [company]!"); - } + // Assert + result.Should().Be("Hello John, welcome to [company]!"); + } - } } \ No newline at end of file diff --git a/Blocktrust.CredentialWorkflow.Core/Blocktrust.CredentialWorkflow.Core.csproj b/Blocktrust.CredentialWorkflow.Core/Blocktrust.CredentialWorkflow.Core.csproj index 6d3539f..1a33a4f 100644 --- a/Blocktrust.CredentialWorkflow.Core/Blocktrust.CredentialWorkflow.Core.csproj +++ b/Blocktrust.CredentialWorkflow.Core/Blocktrust.CredentialWorkflow.Core.csproj @@ -59,6 +59,7 @@ + diff --git a/Blocktrust.CredentialWorkflow.Core/Commands/Workflow/ExecuteWorkflow/ExecuteWorkflowHandler.cs b/Blocktrust.CredentialWorkflow.Core/Commands/Workflow/ExecuteWorkflow/ExecuteWorkflowHandler.cs index 6e6848c..212e917 100644 --- a/Blocktrust.CredentialWorkflow.Core/Commands/Workflow/ExecuteWorkflow/ExecuteWorkflowHandler.cs +++ b/Blocktrust.CredentialWorkflow.Core/Commands/Workflow/ExecuteWorkflow/ExecuteWorkflowHandler.cs @@ -1,5 +1,4 @@ using System.Text.Json; -using Blocktrust.Common.Resolver; using Blocktrust.CredentialWorkflow.Core.Commands.IssueCredentials.IssueW3cCredential.CreateW3cCredential; using Blocktrust.CredentialWorkflow.Core.Commands.IssueCredentials.IssueW3cCredential.SignW3cCredential; using Blocktrust.CredentialWorkflow.Core.Commands.Tenant.GetIssuingKeys; @@ -14,8 +13,6 @@ using Blocktrust.CredentialWorkflow.Core.Domain.ProcessFlow.Actions.Outgoing; using Blocktrust.CredentialWorkflow.Core.Domain.ProcessFlow.Actions.Verification; using Blocktrust.CredentialWorkflow.Core.Domain.ProcessFlow.Triggers; -using Blocktrust.Mediator.Client.Commands.SendMessage; -using Blocktrust.Mediator.Common.Commands.CreatePeerDid; using Blocktrust.Mediator.Common.Protocols; using Blocktrust.PeerDID.DIDDoc; using Blocktrust.PeerDID.PeerDIDCreateResolve; @@ -28,14 +25,16 @@ using System.Text; +using System.Text.RegularExpressions; +using Blocktrust.Common.Resolver; +using Blocktrust.CredentialWorkflow.Core.Commands.DIDComm.GetPeerDIDs; +using Blocktrust.CredentialWorkflow.Core.Commands.Workflow.SendEmailAction; using Blocktrust.DIDComm.Message.Attachments; using Blocktrust.DIDComm.Message.Messages; -using DIDComm.GetPeerDIDs; -using Mediator.Client.Commands.ForwardMessage; -using Mediator.Client.Commands.TrustPing; -using Mediator.Common; -using Mediator.Common.Models.CredentialOffer; -using Workflow = Domain.Workflow.Workflow; +using Blocktrust.Mediator.Client.Commands.ForwardMessage; +using Blocktrust.Mediator.Client.Commands.TrustPing; +using Blocktrust.Mediator.Common; + namespace Blocktrust.CredentialWorkflow.Core.Commands.Workflow.ExecuteWorkflow; public class ExecuteWorkflowHandler : IRequestHandler> @@ -50,7 +49,7 @@ public ExecuteWorkflowHandler(IMediator mediator, ISecretResolver secretResolver _secretResolver = secretResolver; _didDocResolver = didDocResolver; } - + public async Task> Handle(ExecuteWorkflowRequest request, CancellationToken cancellationToken) { var workflowId = request.WorkflowOutcome.WorkflowId; @@ -206,7 +205,7 @@ private async Task> ProcessIssueW3CCredentialAction( Guid workflowOutcomeId, ExecutionContext executionContext, List actionOutcomes, - Workflow workflow, + Domain.Workflow.Workflow workflow, CancellationToken cancellationToken ) { @@ -285,7 +284,7 @@ private async Task> ProcessVerifyW3CCredentialAction( Guid workflowOutcomeId, ExecutionContext executionContext, List actionOutcomes, - Workflow workflow, + Domain.Workflow.Workflow workflow, CancellationToken cancellationToken ) { @@ -320,7 +319,7 @@ private async Task> ProcessEmailAction( Guid workflowOutcomeId, ExecutionContext executionContext, List actionOutcomes, - Workflow workflow, + Domain.Workflow.Workflow workflow, CancellationToken cancellationToken ) { @@ -373,7 +372,7 @@ private async Task> ProcessDIDCommAction( Guid workflowOutcomeId, ExecutionContext executionContext, List actionOutcomes, - Workflow workflow, + Domain.Workflow.Workflow workflow, CancellationToken cancellationToken ) { @@ -503,7 +502,7 @@ CancellationToken cancellationToken return Result.Ok(true); } - private string ProcessEmailTemplate(string template, Dictionary parameters) + public string ProcessEmailTemplate(string template, Dictionary parameters) { if (string.IsNullOrEmpty(template)) return string.Empty; @@ -626,7 +625,7 @@ private static Message BuildIssuingMessage(PeerDid localPeerDid, PeerDid prismPe private async Task GetParameterFromExecutionContext( ParameterReference parameterReference, ExecutionContext executionContext, - Workflow workflow, + Domain.Workflow.Workflow workflow, List actionOutcomes, EActionType? actionType = null) { diff --git a/Blocktrust.CredentialWorkflow.Core/Domain/ProcessFlow/Actions/Outgoing/EmailAction.cs b/Blocktrust.CredentialWorkflow.Core/Domain/ProcessFlow/Actions/Outgoing/EmailAction.cs index 4a2d1e0..ca6710c 100644 --- a/Blocktrust.CredentialWorkflow.Core/Domain/ProcessFlow/Actions/Outgoing/EmailAction.cs +++ b/Blocktrust.CredentialWorkflow.Core/Domain/ProcessFlow/Actions/Outgoing/EmailAction.cs @@ -6,14 +6,27 @@ namespace Blocktrust.CredentialWorkflow.Core.Domain.ProcessFlow.Actions.Outgoing public class EmailAction : ActionInput { [JsonPropertyName("to")] - public ParameterReference To { get; set; } = new(); + public ParameterReference To { get; set; } [JsonPropertyName("subject")] - public ParameterReference Subject { get; set; } = new(); + public string Subject { get; set; } [JsonPropertyName("body")] - public ParameterReference Body { get; set; } = new(); + public string Body { get; set; } + + [JsonPropertyName("parameters")] + public Dictionary Parameters { get; set; } [JsonPropertyName("attachments")] - public List Attachments { get; set; } = new(); + public List Attachments { get; set; } + + public EmailAction() + { + Id = Guid.NewGuid(); + To = new ParameterReference { Source = ParameterSource.Static }; + Subject = string.Empty; + Body = string.Empty; + Parameters = new Dictionary(); + Attachments = new List(); + } } \ No newline at end of file diff --git a/Blocktrust.CredentialWorkflow.Web/Blocktrust.CredentialWorkflow.Web.csproj b/Blocktrust.CredentialWorkflow.Web/Blocktrust.CredentialWorkflow.Web.csproj index 979e44b..df3cca2 100644 --- a/Blocktrust.CredentialWorkflow.Web/Blocktrust.CredentialWorkflow.Web.csproj +++ b/Blocktrust.CredentialWorkflow.Web/Blocktrust.CredentialWorkflow.Web.csproj @@ -28,6 +28,10 @@ + + + + true true diff --git a/Blocktrust.CredentialWorkflow.Web/Controllers/WorkflowController.cs b/Blocktrust.CredentialWorkflow.Web/Controllers/WorkflowController.cs index 505e626..8b04296 100644 --- a/Blocktrust.CredentialWorkflow.Web/Controllers/WorkflowController.cs +++ b/Blocktrust.CredentialWorkflow.Web/Controllers/WorkflowController.cs @@ -1,18 +1,10 @@ -using System; -using System.IO; -using System.Linq; using System.Text; -using System.Text.Json; -using System.Threading.Tasks; using Blocktrust.CredentialWorkflow.Core.Commands.Workflow.GetWorkflowById; using Blocktrust.CredentialWorkflow.Core.Commands.WorkflowOutcome.CreateWorkflowOutcome; using Blocktrust.CredentialWorkflow.Core.Domain.Common; using Blocktrust.CredentialWorkflow.Core.Domain.Enums; using Blocktrust.CredentialWorkflow.Core.Domain.ProcessFlow.Triggers; using Blocktrust.CredentialWorkflow.Core.Services; -using Core.Commands.WorkflowOutcome.CreateWorkflowOutcome; -using Core.Domain.ProcessFlow.Triggers; -using Core.Services; using MediatR; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; diff --git a/Blocktrust.CredentialWorkflow.Web/Program.cs b/Blocktrust.CredentialWorkflow.Web/Program.cs index 509ab65..6537a68 100644 --- a/Blocktrust.CredentialWorkflow.Web/Program.cs +++ b/Blocktrust.CredentialWorkflow.Web/Program.cs @@ -10,7 +10,6 @@ using Blocktrust.CredentialWorkflow.Web.Common; using Blocktrust.CredentialWorkflow.Web.Components.Account; using Blocktrust.CredentialWorkflow.Web.Services; -using Blocktrust.DIDComm.Secrets; using Blocktrust.Mediator.Client.Commands.TrustPing; using Blocktrust.Mediator.Common; using Blocktrust.Mediator.Common.Commands.CreatePeerDid;