From 3c970e52adb427eba96845fe799030cd4682c28a Mon Sep 17 00:00:00 2001 From: James Gunn Date: Thu, 5 Oct 2023 15:02:27 +0100 Subject: [PATCH] Use FormFlow for Close Alert journey --- .../JourneyNames.cs | 6 ++ .../Alerts/CloseAlert/CloseAlertState.cs | 13 +++++ .../Pages/Alerts/CloseAlert/Confirm.cshtml | 4 +- .../Pages/Alerts/CloseAlert/Confirm.cshtml.cs | 16 ++++- .../Pages/Alerts/CloseAlert/Index.cshtml | 2 +- .../Pages/Alerts/CloseAlert/Index.cshtml.cs | 12 +++- .../Pages/Persons/PersonDetail/Alerts.cshtml | 2 +- .../TeachingRecordSystem.SupportUi/Program.cs | 5 ++ .../TrsLinkGenerator.cs | 20 +++++-- .../Alerts/CloseAlert/ConfirmTests.cs | 58 ++++++++++++++++--- .../PageTests/Alerts/CloseAlert/IndexTests.cs | 58 +++++++++++++------ 11 files changed, 155 insertions(+), 41 deletions(-) create mode 100644 TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/JourneyNames.cs create mode 100644 TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Alerts/CloseAlert/CloseAlertState.cs diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/JourneyNames.cs b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/JourneyNames.cs new file mode 100644 index 0000000000..c76918ffb3 --- /dev/null +++ b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/JourneyNames.cs @@ -0,0 +1,6 @@ +namespace TeachingRecordSystem.SupportUi; + +public static class JourneyNames +{ + public const string CloseAlert = nameof(CloseAlert); +} diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Alerts/CloseAlert/CloseAlertState.cs b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Alerts/CloseAlert/CloseAlertState.cs new file mode 100644 index 0000000000..c7f6df702b --- /dev/null +++ b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Alerts/CloseAlert/CloseAlertState.cs @@ -0,0 +1,13 @@ +using System.Diagnostics.CodeAnalysis; +using System.Text.Json.Serialization; + +namespace TeachingRecordSystem.SupportUi.Pages.Alerts.CloseAlert; + +public class CloseAlertState +{ + public DateOnly? EndDate { get; set; } + + [JsonIgnore] + [MemberNotNullWhen(true, nameof(EndDate))] + public bool IsComplete => EndDate.HasValue; +} diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Alerts/CloseAlert/Confirm.cshtml b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Alerts/CloseAlert/Confirm.cshtml index 8a0f363306..929c1738fc 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Alerts/CloseAlert/Confirm.cshtml +++ b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Alerts/CloseAlert/Confirm.cshtml @@ -5,14 +5,14 @@ } @section BeforeContent { - Back + Back }

@ViewBag.Title

-
+ Alert type diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Alerts/CloseAlert/Confirm.cshtml.cs b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Alerts/CloseAlert/Confirm.cshtml.cs index f01f21e8d3..3edc8304ea 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Alerts/CloseAlert/Confirm.cshtml.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Alerts/CloseAlert/Confirm.cshtml.cs @@ -5,6 +5,7 @@ namespace TeachingRecordSystem.SupportUi.Pages.Alerts.CloseAlert; +[Journey(JourneyNames.CloseAlert), RequireJourneyInstance] public class ConfirmModel : PageModel { private readonly TrsLinkGenerator _linkGenerator; @@ -18,19 +19,21 @@ public ConfirmModel( _crmQueryDispatcher = crmQueryDispatcher; } + public JourneyInstance? JourneyInstance { get; set; } + [FromRoute] public Guid AlertId { get; set; } - [FromQuery(Name = "endDate")] - public DateOnly EndDate { get; set; } - public string? AlertType { get; set; } public Guid? PersonId { get; set; } + public DateOnly EndDate { get; set; } + public async Task OnPost() { await _crmQueryDispatcher.ExecuteQuery(new CloseSanctionQuery(AlertId, EndDate)); + await JourneyInstance!.CompleteAsync(); TempData.SetFlashSuccess("Alert closed"); @@ -48,8 +51,15 @@ public override async Task OnPageHandlerExecutionAsync(PageHandlerExecutingConte return; } + if (!JourneyInstance!.State.IsComplete) + { + context.Result = Redirect(_linkGenerator.AlertClose(AlertId, JourneyInstance!.InstanceId)); + return; + } + AlertType = alert.Description; PersonId = alert.Sanction.dfeta_PersonId.Id; + EndDate = JourneyInstance!.State.EndDate.Value; await next(); } diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Alerts/CloseAlert/Index.cshtml b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Alerts/CloseAlert/Index.cshtml index 1bbd01f233..c1466d19df 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Alerts/CloseAlert/Index.cshtml +++ b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Alerts/CloseAlert/Index.cshtml @@ -12,7 +12,7 @@
- + diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Alerts/CloseAlert/Index.cshtml.cs b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Alerts/CloseAlert/Index.cshtml.cs index 8434ede83d..286783022c 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Alerts/CloseAlert/Index.cshtml.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Alerts/CloseAlert/Index.cshtml.cs @@ -6,6 +6,7 @@ namespace TeachingRecordSystem.SupportUi.Pages.Alerts.CloseAlert; +[Journey(JourneyNames.CloseAlert), ActivatesJourney, RequireJourneyInstance] public class IndexModel : PageModel { private readonly TrsLinkGenerator _linkGenerator; @@ -19,10 +20,12 @@ public IndexModel( _crmQueryDispatcher = crmQueryDispatcher; } + public JourneyInstance? JourneyInstance { get; set; } + [FromRoute] public Guid AlertId { get; set; } - [BindProperty(SupportsGet = true)] + [BindProperty] [Display(Name = "End date")] public DateOnly? EndDate { get; set; } @@ -30,7 +33,7 @@ public IndexModel( public DateOnly? StartDate { get; set; } - public IActionResult OnPost() + public async Task OnPost() { if (EndDate is null) { @@ -47,7 +50,9 @@ public IActionResult OnPost() return this.PageWithErrors(); } - return Redirect(_linkGenerator.AlertCloseConfirm(AlertId, EndDate!.Value)); + await JourneyInstance!.UpdateStateAsync(s => s.EndDate = EndDate); + + return Redirect(_linkGenerator.AlertCloseConfirm(AlertId, JourneyInstance!.InstanceId)); } public override async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next) @@ -63,6 +68,7 @@ public override async Task OnPageHandlerExecutionAsync(PageHandlerExecutingConte PersonId = alert.Sanction.dfeta_PersonId.Id; StartDate = alert.Sanction.dfeta_StartDate.ToDateOnlyWithDqtBstFix(isLocalTime: true); + EndDate ??= JourneyInstance!.State.EndDate; await next(); } diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Persons/PersonDetail/Alerts.cshtml b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Persons/PersonDetail/Alerts.cshtml index 638412ff22..f1afda793d 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Persons/PersonDetail/Alerts.cshtml +++ b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Persons/PersonDetail/Alerts.cshtml @@ -24,7 +24,7 @@ else @alert.Description - Close + Close diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Program.cs b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Program.cs index 0d623b290e..7c5a7f558f 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Program.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Program.cs @@ -187,6 +187,11 @@ .AddTransient() .AddFormFlow(options => { + options.JourneyRegistry.RegisterJourney(new JourneyDescriptor( + JourneyNames.CloseAlert, + typeof(TeachingRecordSystem.SupportUi.Pages.Alerts.CloseAlert.CloseAlertState), + requestDataKeys: new[] { "alertId" }, + appendUniqueKey: true)); }); builder.Services diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/TrsLinkGenerator.cs b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/TrsLinkGenerator.cs index d508b7eac7..71fe5f1d83 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/TrsLinkGenerator.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/TrsLinkGenerator.cs @@ -1,3 +1,4 @@ +using Microsoft.AspNetCore.WebUtilities; using TeachingRecordSystem.Core.Dqt.Models; using TeachingRecordSystem.SupportUi.Infrastructure.ModelBinding; @@ -22,9 +23,11 @@ public TrsLinkGenerator(LinkGenerator linkGenerator) public string Alert(Guid alertId) => GetRequiredPathByPage("/Alerts/Alert/Index", routeValues: new { alertId }); - public string AlertClose(Guid alertId, DateOnly? endDate) => GetRequiredPathByPage("/Alerts/CloseAlert/Index", routeValues: new { alertId, endDate = endDate?.ToString(DateOnlyFormat) }); + public string AlertClose(Guid alertId, JourneyInstanceId? journeyInstanceId) => + GetRequiredPathByPage("/Alerts/CloseAlert/Index", routeValues: new { alertId }, journeyInstanceId: journeyInstanceId); - public string AlertCloseConfirm(Guid alertId, DateOnly endDate) => GetRequiredPathByPage("/Alerts/CloseAlert/Confirm", routeValues: new { alertId, endDate = endDate.ToString(DateOnlyFormat) }); + public string AlertCloseConfirm(Guid alertId, JourneyInstanceId journeyInstanceId) => + GetRequiredPathByPage("/Alerts/CloseAlert/Confirm", routeValues: new { alertId }, journeyInstanceId: journeyInstanceId); public string Cases() => GetRequiredPathByPage("/Cases/Index"); @@ -58,6 +61,15 @@ public string PersonChangeLog(Guid personId, string? search = null, ContactSearc public string EditUser(Guid userId) => GetRequiredPathByPage("/Users/EditUser", routeValues: new { userId }); - private string GetRequiredPathByPage(string page, string? handler = null, object? routeValues = null) => - _linkGenerator.GetPathByPage(page, handler, values: routeValues) ?? throw new InvalidOperationException("Page was not found."); + private string GetRequiredPathByPage(string page, string? handler = null, object? routeValues = null, JourneyInstanceId? journeyInstanceId = null) + { + var url = _linkGenerator.GetPathByPage(page, handler, values: routeValues) ?? throw new InvalidOperationException("Page was not found."); + + if (journeyInstanceId?.UniqueKey is string journeyInstanceUniqueKey) + { + url = QueryHelpers.AddQueryString(url, FormFlow.Constants.UniqueKeyQueryParameterName, journeyInstanceUniqueKey); + } + + return url; + } } diff --git a/TeachingRecordSystem/tests/TeachingRecordSystem.SupportUi.Tests/PageTests/Alerts/CloseAlert/ConfirmTests.cs b/TeachingRecordSystem/tests/TeachingRecordSystem.SupportUi.Tests/PageTests/Alerts/CloseAlert/ConfirmTests.cs index 07dbc23264..918922b03b 100644 --- a/TeachingRecordSystem/tests/TeachingRecordSystem.SupportUi.Tests/PageTests/Alerts/CloseAlert/ConfirmTests.cs +++ b/TeachingRecordSystem/tests/TeachingRecordSystem.SupportUi.Tests/PageTests/Alerts/CloseAlert/ConfirmTests.cs @@ -1,3 +1,6 @@ +using FormFlow; +using TeachingRecordSystem.SupportUi.Pages.Alerts.CloseAlert; + namespace TeachingRecordSystem.SupportUi.Tests.PageTests.Alerts.CloseAlert; public class ConfirmTests : TestBase @@ -11,9 +14,12 @@ public ConfirmTests(HostFixture hostFixture) public async Task Get_WithAlertIdForNonExistentAlert_ReturnsNotFound() { // Arrange - var nonExistentAlertId = Guid.NewGuid().ToString(); + var endDate = new DateOnly(2020, 01, 01); + var alertId = Guid.NewGuid(); + + var journeyInstance = await CreateJourneyInstance(alertId, new CloseAlertState() { EndDate = endDate }); - var request = new HttpRequestMessage(HttpMethod.Get, $"/alerts/{nonExistentAlertId}/close/confirm"); + var request = new HttpRequestMessage(HttpMethod.Get, $"/alerts/{alertId}/close/confirm?{journeyInstance.GetUniqueIdQueryParameter()}"); // Act var response = await HttpClient.SendAsync(request); @@ -22,6 +28,29 @@ public async Task Get_WithAlertIdForNonExistentAlert_ReturnsNotFound() Assert.Equal(StatusCodes.Status404NotFound, (int)response.StatusCode); } + [Fact] + public async Task Get_StateHasNoEndDate_RedirectsToIndex() + { + // Arrange + var sanctionCode = "G1"; + var sanctionCodeName = (await TestData.ReferenceDataCache.GetSanctionCodeByValue(sanctionCode)).dfeta_name; + var startDate = new DateOnly(2021, 01, 01); + var endDate = new DateOnly(2020, 01, 01); + var person = await TestData.CreatePerson(x => x.WithSanction(sanctionCode, startDate: startDate)); + var alertId = person.Sanctions.Single().SanctionId; + + var journeyInstance = await CreateJourneyInstance(alertId, new CloseAlertState() { EndDate = null }); + + var request = new HttpRequestMessage(HttpMethod.Get, $"/alerts/{alertId}/close/confirm?{journeyInstance.GetUniqueIdQueryParameter()}"); + + // Act + var response = await HttpClient.SendAsync(request); + + // Assert + Assert.Equal(StatusCodes.Status302Found, (int)response.StatusCode); + Assert.Equal($"/alerts/{alertId}/close?{journeyInstance.GetUniqueIdQueryParameter()}", response.Headers.Location?.OriginalString); + } + [Fact] public async Task Get_ValidRequest_RendersExpectedContent() { @@ -29,10 +58,13 @@ public async Task Get_ValidRequest_RendersExpectedContent() var sanctionCode = "G1"; var sanctionCodeName = (await TestData.ReferenceDataCache.GetSanctionCodeByValue(sanctionCode)).dfeta_name; var startDate = new DateOnly(2021, 01, 01); - var endDate = new DateOnly(2022, 03, 05); + var endDate = new DateOnly(2020, 01, 01); var person = await TestData.CreatePerson(x => x.WithSanction(sanctionCode, startDate: startDate)); + var alertId = person.Sanctions.Single().SanctionId; + + var journeyInstance = await CreateJourneyInstance(alertId, new CloseAlertState() { EndDate = endDate }); - var request = new HttpRequestMessage(HttpMethod.Get, $"/alerts/{person.Sanctions.Single().SanctionId}/close/confirm?endDate={endDate:yyyy-MM-dd}"); + var request = new HttpRequestMessage(HttpMethod.Get, $"/alerts/{alertId}/close/confirm?{journeyInstance.GetUniqueIdQueryParameter()}"); // Act var response = await HttpClient.SendAsync(request); @@ -46,17 +78,18 @@ public async Task Get_ValidRequest_RendersExpectedContent() } [Fact] - public async Task Post_ValidRequest_ClosesAlert() + public async Task Post_ValidRequest_ClosesAlertAndCompletesJourney() { // Arrange var sanctionCode = "G1"; var startDate = new DateOnly(2021, 01, 01); var endDate = new DateOnly(2023, 08, 02); var person = await TestData.CreatePerson(x => x.WithSanction(sanctionCode, startDate: startDate)); + var alertId = person.Sanctions.Single().SanctionId; + + var journeyInstance = await CreateJourneyInstance(alertId, new CloseAlertState() { EndDate = endDate }); - var request = new HttpRequestMessage( - HttpMethod.Post, - $"/alerts/{person.Sanctions.Single().SanctionId}/close/confirm?endDate={endDate:yyyy-MM-dd}") + var request = new HttpRequestMessage(HttpMethod.Post, $"/alerts/{alertId}/close/confirm?{journeyInstance.GetUniqueIdQueryParameter()}") { Content = new FormUrlEncodedContentBuilder() }; @@ -70,5 +103,14 @@ public async Task Post_ValidRequest_ClosesAlert() var redirectResponse = await response.FollowRedirect(HttpClient); var redirectDoc = await redirectResponse.GetDocument(); AssertEx.HtmlDocumentHasFlashSuccess(redirectDoc, "Alert closed"); + + journeyInstance = await ReloadJourneyInstance(journeyInstance); + Assert.True(journeyInstance.Completed); } + + private async Task> CreateJourneyInstance(Guid alertId, CloseAlertState state) => + await CreateJourneyInstance( + JourneyNames.CloseAlert, + state, + new KeyValuePair("alertId", alertId)); } diff --git a/TeachingRecordSystem/tests/TeachingRecordSystem.SupportUi.Tests/PageTests/Alerts/CloseAlert/IndexTests.cs b/TeachingRecordSystem/tests/TeachingRecordSystem.SupportUi.Tests/PageTests/Alerts/CloseAlert/IndexTests.cs index 3761528803..eb3f3b2a9e 100644 --- a/TeachingRecordSystem/tests/TeachingRecordSystem.SupportUi.Tests/PageTests/Alerts/CloseAlert/IndexTests.cs +++ b/TeachingRecordSystem/tests/TeachingRecordSystem.SupportUi.Tests/PageTests/Alerts/CloseAlert/IndexTests.cs @@ -1,5 +1,7 @@ -namespace TeachingRecordSystem.SupportUi.Tests.PageTests.Alerts.CloseAlert; +using FormFlow; +using TeachingRecordSystem.SupportUi.Pages.Alerts.CloseAlert; +namespace TeachingRecordSystem.SupportUi.Tests.PageTests.Alerts.CloseAlert; public class IndexTests : TestBase { @@ -12,9 +14,11 @@ public IndexTests(HostFixture hostFixture) public async Task Get_WithAlertIdForNonExistentAlert_ReturnsNotFound() { // Arrange - var nonExistentActivityId = Guid.NewGuid().ToString(); + var alertId = Guid.NewGuid(); + + var journeyInstance = await CreateJourneyInstance(alertId); - var request = new HttpRequestMessage(HttpMethod.Get, $"/alerts/{nonExistentActivityId}/close"); + var request = new HttpRequestMessage(HttpMethod.Get, $"/alerts/{alertId}/close?{journeyInstance.GetUniqueIdQueryParameter()}"); // Act var response = await HttpClient.SendAsync(request); @@ -28,8 +32,11 @@ public async Task Get_WithAlertIdForEndedAlert_ReturnsNotFound() { // Arrange var person = await TestData.CreatePerson(x => x.WithSanction("G1", endDate: new DateOnly(2023, 09, 21))); + var alertId = person.Sanctions.Single().SanctionId; - var request = new HttpRequestMessage(HttpMethod.Get, $"/alerts/{person.Sanctions.Single().SanctionId}/close"); + var journeyInstance = await CreateJourneyInstance(alertId); + + var request = new HttpRequestMessage(HttpMethod.Get, $"/alerts/{alertId}/close?{journeyInstance.GetUniqueIdQueryParameter()}"); // Act var response = await HttpClient.SendAsync(request); @@ -43,8 +50,11 @@ public async Task Get_WithAlertIdForActiveAlert_ReturnsOk() { // Arrange var person = await TestData.CreatePerson(x => x.WithSanction("G1")); + var alertId = person.Sanctions.Single().SanctionId; + + var journeyInstance = await CreateJourneyInstance(alertId); - var request = new HttpRequestMessage(HttpMethod.Get, $"/alerts/{person.Sanctions.Single().SanctionId}/close"); + var request = new HttpRequestMessage(HttpMethod.Get, $"/alerts/{alertId}/close?{journeyInstance.GetUniqueIdQueryParameter()}"); // Act var response = await HttpClient.SendAsync(request); @@ -56,16 +66,17 @@ public async Task Get_WithAlertIdForActiveAlert_ReturnsOk() } [Fact] - public async Task Get_ValidRequestWithEndDateInQueryParam_PopulatesEndDateFromQueryParam() + public async Task Get_ValidRequestWithEndDateInJourneyState_PopulatesEndDateFromJourneyState() { // Arrange var startDate = new DateOnly(2021, 01, 01); var endDate = new DateOnly(2020, 01, 01); var person = await TestData.CreatePerson(x => x.WithSanction("G1", startDate: startDate)); + var alertId = person.Sanctions.Single().SanctionId; - var request = new HttpRequestMessage( - HttpMethod.Get, - $"/alerts/{person.Sanctions.Single().SanctionId}/close?endDate={endDate:yyyy-MM-dd}"); + var journeyInstance = await CreateJourneyInstance(alertId, new CloseAlertState() { EndDate = endDate }); + + var request = new HttpRequestMessage(HttpMethod.Get, $"/alerts/{alertId}/close?{journeyInstance.GetUniqueIdQueryParameter()}"); // Act var response = await HttpClient.SendAsync(request); @@ -82,10 +93,11 @@ public async Task Post_EmptyEndDate_ReturnsError() { // Arrange var person = await TestData.CreatePerson(x => x.WithSanction("G1")); + var alertId = person.Sanctions.Single().SanctionId; + + var journeyInstance = await CreateJourneyInstance(alertId); - var request = new HttpRequestMessage( - HttpMethod.Post, - $"/alerts/{person.Sanctions.Single().SanctionId}/close") + var request = new HttpRequestMessage(HttpMethod.Post, $"/alerts/{alertId}/close?{journeyInstance.GetUniqueIdQueryParameter()}") { Content = new FormUrlEncodedContentBuilder() }; @@ -104,10 +116,11 @@ public async Task Post_EndDateBeforeStartDate_ReturnsError() var startDate = new DateOnly(2021, 01, 01); var endDate = new DateOnly(2020, 01, 01); var person = await TestData.CreatePerson(x => x.WithSanction("G1", startDate: startDate)); + var alertId = person.Sanctions.Single().SanctionId; + + var journeyInstance = await CreateJourneyInstance(alertId); - var request = new HttpRequestMessage( - HttpMethod.Post, - $"/alerts/{person.Sanctions.Single().SanctionId}/close") + var request = new HttpRequestMessage(HttpMethod.Post, $"/alerts/{alertId}/close?{journeyInstance.GetUniqueIdQueryParameter()}") { Content = new FormUrlEncodedContentBuilder() { @@ -131,10 +144,11 @@ public async Task Post_ValidEndDate_RedirectsToConfirmPage() var startDate = new DateOnly(2021, 01, 01); var endDate = new DateOnly(2022, 08, 03); var person = await TestData.CreatePerson(x => x.WithSanction("G1", startDate: startDate)); + var alertId = person.Sanctions.Single().SanctionId; - var request = new HttpRequestMessage( - HttpMethod.Post, - $"/alerts/{person.Sanctions.Single().SanctionId}/close") + var journeyInstance = await CreateJourneyInstance(alertId); + + var request = new HttpRequestMessage(HttpMethod.Post, $"/alerts/{alertId}/close?{journeyInstance.GetUniqueIdQueryParameter()}") { Content = new FormUrlEncodedContentBuilder() { @@ -149,6 +163,12 @@ public async Task Post_ValidEndDate_RedirectsToConfirmPage() // Assert Assert.Equal(StatusCodes.Status302Found, (int)response.StatusCode); - Assert.Equal($"/alerts/{person.Sanctions.Single().SanctionId}/close/confirm?endDate={endDate:yyyy-MM-dd}", response.Headers.Location?.OriginalString); + Assert.Equal($"/alerts/{alertId}/close/confirm?{journeyInstance.GetUniqueIdQueryParameter()}", response.Headers.Location?.OriginalString); } + + private async Task> CreateJourneyInstance(Guid alertId, CloseAlertState? state = null) => + await CreateJourneyInstance( + JourneyNames.CloseAlert, + state ?? new CloseAlertState(), + new KeyValuePair("alertId", alertId)); }