Skip to content

Commit

Permalink
Add supporting test infrastructure for FormFlow journeys
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad committed Oct 5, 2023
1 parent 57b5db3 commit cdae46f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -69,6 +71,7 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
services.AddTestScoped<IUserService>(tss => tss.AzureActiveDirectoryUserServiceMock.Object);
services.AddSingleton<TestData>();
services.AddFakeXrm();
services.AddTransient<ICurrentUserIdProvider, TestUserCurrentUserIdProvider>();
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.");
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -42,6 +46,42 @@ protected TestBase(HostFixture hostFixture)

public IXrmFakedContext XrmFakedContext => HostFixture.Services.GetRequiredService<IXrmFakedContext>();

public async Task<JourneyInstance<TState>> CreateJourneyInstance<TState>(
string journeyName,
TState state,
params KeyValuePair<string, object>[] keys)
where TState : notnull
{
await using var scope = HostFixture.Services.CreateAsyncScope();
var stateProvider = scope.ServiceProvider.GetRequiredService<IUserInstanceStateProvider>();
var options = scope.ServiceProvider.GetRequiredService<IOptions<FormFlowOptions>>();

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<TState>)instance;
}

public async Task<JourneyInstance<TState>> ReloadJourneyInstance<TState>(JourneyInstance<TState> journeyInstance)
{
await using var scope = HostFixture.Services.CreateAsyncScope();
var stateProvider = scope.ServiceProvider.GetRequiredService<IUserInstanceStateProvider>();
var reloadedInstance = await stateProvider.GetInstanceAsync(journeyInstance.InstanceId, typeof(TState));
return (JourneyInstance<TState>)reloadedInstance!;
}

protected Guid GetCurrentUserId()
{
var currentUserProvider = HostFixture.Services.GetRequiredService<CurrentUserProvider>();
Expand Down

0 comments on commit cdae46f

Please sign in to comment.