diff --git a/TeachingRecordSystem/tests/TeachingRecordSystem.SupportUi.Tests/HostFixture.cs b/TeachingRecordSystem/tests/TeachingRecordSystem.SupportUi.Tests/HostFixture.cs index 2ef5fdc60e..bb1d05671e 100644 --- a/TeachingRecordSystem/tests/TeachingRecordSystem.SupportUi.Tests/HostFixture.cs +++ b/TeachingRecordSystem/tests/TeachingRecordSystem.SupportUi.Tests/HostFixture.cs @@ -8,8 +8,10 @@ using TeachingRecordSystem.Core.DataStore.Postgres; using TeachingRecordSystem.Core.Dqt; using TeachingRecordSystem.Core.Events.Processing; +using TeachingRecordSystem.SupportUi.Infrastructure.FormFlow; using TeachingRecordSystem.SupportUi.Services.AzureActiveDirectory; using TeachingRecordSystem.SupportUi.Tests.Infrastructure; +using TeachingRecordSystem.SupportUi.Tests.Infrastructure.FormFlow; using TeachingRecordSystem.SupportUi.Tests.Infrastructure.Security; using TeachingRecordSystem.TestCommon.Infrastructure; @@ -69,6 +71,7 @@ protected override void ConfigureWebHost(IWebHostBuilder builder) services.AddTestScoped(tss => tss.AzureActiveDirectoryUserServiceMock.Object); services.AddSingleton(); services.AddFakeXrm(); + services.AddTransient(); }); } diff --git a/TeachingRecordSystem/tests/TeachingRecordSystem.SupportUi.Tests/Infrastructure/FormFlow/TestUserCurrentUserIdProvider.cs b/TeachingRecordSystem/tests/TeachingRecordSystem.SupportUi.Tests/Infrastructure/FormFlow/TestUserCurrentUserIdProvider.cs new file mode 100644 index 0000000000..2e6a6e8c1d --- /dev/null +++ b/TeachingRecordSystem/tests/TeachingRecordSystem.SupportUi.Tests/Infrastructure/FormFlow/TestUserCurrentUserIdProvider.cs @@ -0,0 +1,17 @@ +using TeachingRecordSystem.SupportUi.Infrastructure.FormFlow; +using TeachingRecordSystem.SupportUi.Tests.Infrastructure.Security; + +namespace TeachingRecordSystem.SupportUi.Tests.Infrastructure.FormFlow; + +public class TestUserCurrentUserIdProvider : ICurrentUserIdProvider +{ + private readonly CurrentUserProvider _currentUserProvider; + + public TestUserCurrentUserIdProvider(CurrentUserProvider currentUserProvider) + { + _currentUserProvider = currentUserProvider; + } + + public Guid GetCurrentUserId() => + _currentUserProvider.CurrentUser?.UserId ?? throw new InvalidOperationException("No current user."); +} diff --git a/TeachingRecordSystem/tests/TeachingRecordSystem.SupportUi.Tests/JourneyInstanceExtensions.cs b/TeachingRecordSystem/tests/TeachingRecordSystem.SupportUi.Tests/JourneyInstanceExtensions.cs new file mode 100644 index 0000000000..191bd2e1f2 --- /dev/null +++ b/TeachingRecordSystem/tests/TeachingRecordSystem.SupportUi.Tests/JourneyInstanceExtensions.cs @@ -0,0 +1,11 @@ +using FormFlow; + +namespace TeachingRecordSystem.SupportUi.Tests; + +public static class JourneyInstanceExtensions +{ + public static string GetUniqueIdQueryParameter(this JourneyInstance journeyInstance) => + journeyInstance.InstanceId.UniqueKey is string uniqueKey ? + $"{Constants.UniqueKeyQueryParameterName}={Uri.EscapeDataString(uniqueKey)}" : + string.Empty; +} diff --git a/TeachingRecordSystem/tests/TeachingRecordSystem.SupportUi.Tests/TestBase.cs b/TeachingRecordSystem/tests/TeachingRecordSystem.SupportUi.Tests/TestBase.cs index 1bcfe4eb2c..e00ec809d7 100644 --- a/TeachingRecordSystem/tests/TeachingRecordSystem.SupportUi.Tests/TestBase.cs +++ b/TeachingRecordSystem/tests/TeachingRecordSystem.SupportUi.Tests/TestBase.cs @@ -1,5 +1,9 @@ using FakeXrmEasy.Abstractions; +using FormFlow; +using FormFlow.State; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Options; +using Microsoft.Extensions.Primitives; using TeachingRecordSystem.Core.DataStore.Postgres; using TeachingRecordSystem.Core.DataStore.Postgres.Models; using TeachingRecordSystem.Core.Dqt; @@ -42,6 +46,42 @@ protected TestBase(HostFixture hostFixture) public IXrmFakedContext XrmFakedContext => HostFixture.Services.GetRequiredService(); + public async Task> CreateJourneyInstance( + string journeyName, + TState state, + params KeyValuePair[] keys) + where TState : notnull + { + await using var scope = HostFixture.Services.CreateAsyncScope(); + var stateProvider = scope.ServiceProvider.GetRequiredService(); + var options = scope.ServiceProvider.GetRequiredService>(); + + var journeyDescriptor = options.Value.JourneyRegistry.GetJourneyByName(journeyName) ?? + throw new ArgumentException("Journey not found.", nameof(journeyName)); + + var keysDict = keys.ToDictionary(k => k.Key, k => new StringValues(k.Value.ToString())); + + if (journeyDescriptor.AppendUniqueKey) + { + keysDict.Add(Constants.UniqueKeyQueryParameterName, new StringValues(Guid.NewGuid().ToString())); + } + + var instanceId = new JourneyInstanceId(journeyDescriptor.JourneyName, keysDict); + + var stateType = typeof(TState); + + var instance = await stateProvider.CreateInstanceAsync(instanceId, stateType, state, properties: null); + return (JourneyInstance)instance; + } + + public async Task> ReloadJourneyInstance(JourneyInstance journeyInstance) + { + await using var scope = HostFixture.Services.CreateAsyncScope(); + var stateProvider = scope.ServiceProvider.GetRequiredService(); + var reloadedInstance = await stateProvider.GetInstanceAsync(journeyInstance.InstanceId, typeof(TState)); + return (JourneyInstance)reloadedInstance!; + } + protected Guid GetCurrentUserId() { var currentUserProvider = HostFixture.Services.GetRequiredService();