Add tests for liquid expressions
This commit is contained in:
parent
e51395ff21
commit
c49ed765b9
|
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:59550/",
|
||||
"sslPort": 44301
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"Elsa.Samples.MassTransitRabbitMq": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"applicationUrl": "https://localhost:5001;http://localhost:5000"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -16,6 +16,7 @@ using Fluid;
|
|||
using Fluid.Values;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Elsa.Scripting.Liquid.Handlers
|
||||
|
|
@ -26,11 +27,11 @@ namespace Elsa.Scripting.Liquid.Handlers
|
|||
private readonly IWorkflowStorageService _workflowStorageService;
|
||||
private readonly LiquidOptions _liquidOptions;
|
||||
|
||||
public ConfigureLiquidEngine(IConfiguration configuration, IWorkflowStorageService workflowStorageService, LiquidOptions liquidOptions)
|
||||
public ConfigureLiquidEngine(IConfiguration configuration, IWorkflowStorageService workflowStorageService, IOptions<LiquidOptions> liquidOptions)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_workflowStorageService = workflowStorageService;
|
||||
_liquidOptions = liquidOptions;
|
||||
_liquidOptions = liquidOptions.Value;
|
||||
}
|
||||
|
||||
public Task Handle(EvaluatingLiquidExpression notification, CancellationToken cancellationToken)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,97 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Elsa.ActivityResults;
|
||||
using Elsa.Builders;
|
||||
using Elsa.Expressions;
|
||||
using Elsa.Scripting.Liquid.Extensions;
|
||||
using Elsa.Services;
|
||||
using Elsa.Services.Models;
|
||||
using Elsa.Testing.Shared.Helpers;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Xunit;
|
||||
|
||||
namespace Elsa.Core.IntegrationTests.Scripting.Liquid
|
||||
{
|
||||
public class LiquidExpressionIntegrationTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task LiquidExpressionsCannotAccessConfigurationByDefault()
|
||||
{
|
||||
var services = new ServiceCollection()
|
||||
.AddSingleton<IConfiguration>(_ => new ConfigurationBuilder()
|
||||
.AddInMemoryCollection(new Dictionary<string, string>
|
||||
{
|
||||
{ "SomeSecret", "I am a secret" },
|
||||
})
|
||||
.Build())
|
||||
.AddSingleton<AssertableActivityState>()
|
||||
.AddElsa(elsa => elsa
|
||||
.AddActivity<WriteConfigActivity>()
|
||||
.AddWorkflow<ConfigurationAccessWorkflow>())
|
||||
.BuildServiceProvider();
|
||||
|
||||
var workflowStarter = services.GetRequiredService<IBuildsAndStartsWorkflow>();
|
||||
var activityState = services.GetRequiredService<AssertableActivityState>();
|
||||
|
||||
await workflowStarter.BuildAndStartWorkflowAsync<ConfigurationAccessWorkflow>();
|
||||
|
||||
Assert.Single(activityState.Messages, "Config secret: ");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ConfigureAccessCanBeEnabledForLiquidExpressions()
|
||||
{
|
||||
var services = new ServiceCollection()
|
||||
.AddSingleton<IConfiguration>(_ => new ConfigurationBuilder()
|
||||
.AddInMemoryCollection(new Dictionary<string, string>
|
||||
{
|
||||
{ "SomeSecret", "I am a secret" },
|
||||
})
|
||||
.Build())
|
||||
.AddSingleton<AssertableActivityState>()
|
||||
.AddElsa(elsa => elsa
|
||||
.AddActivity<WriteConfigActivity>()
|
||||
.AddWorkflow<ConfigurationAccessWorkflow>())
|
||||
.EnableLiquidConfigurationAccess()
|
||||
.BuildServiceProvider();
|
||||
|
||||
var workflowStarter = services.GetRequiredService<IBuildsAndStartsWorkflow>();
|
||||
var activityState = services.GetRequiredService<AssertableActivityState>();
|
||||
|
||||
await workflowStarter.BuildAndStartWorkflowAsync<ConfigurationAccessWorkflow>();
|
||||
|
||||
Assert.Single(activityState.Messages, "Config secret: I am a secret");
|
||||
}
|
||||
|
||||
private class ConfigurationAccessWorkflow : IWorkflow
|
||||
{
|
||||
public void Build(IWorkflowBuilder builder)
|
||||
{
|
||||
builder.StartWith<WriteConfigActivity>();
|
||||
}
|
||||
}
|
||||
|
||||
private class WriteConfigActivity : Activity
|
||||
{
|
||||
private IExpressionEvaluator _expressionEvaluator;
|
||||
private AssertableActivityState _activityState;
|
||||
|
||||
public WriteConfigActivity(IExpressionEvaluator evaluator, AssertableActivityState activityState)
|
||||
{
|
||||
_expressionEvaluator = evaluator;
|
||||
_activityState = activityState;
|
||||
}
|
||||
|
||||
protected override async ValueTask<IActivityExecutionResult> OnExecuteAsync(ActivityExecutionContext context)
|
||||
{
|
||||
var liquidExpression = "{{Configuration.SomeSecret}}";
|
||||
var expressionResult = await _expressionEvaluator.TryEvaluateAsync<string>(liquidExpression, "Liquid", context);
|
||||
|
||||
_activityState.Messages.Add($"Config secret: {expressionResult.Value ?? ""}");
|
||||
|
||||
return Done();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue