From 683d2232aa2f5f2ec12034a25dc7a2969454f886 Mon Sep 17 00:00:00 2001 From: Kevin Joy Date: Thu, 1 Aug 2024 15:24:28 +0100 Subject: [PATCH] add email check --- .../Pages/RequestTrn/Email.cshtml.cs | 1 - .../Pages/RequestTrn/EmailInUse.cshtml.cs | 10 +++++++-- .../PageTests/RequestTrn/EmailInUseTests.cs | 22 +++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 TeachingRecordSystem/tests/TeachingRecordSystem.AuthorizeAccess.Tests/PageTests/RequestTrn/EmailInUseTests.cs diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.AuthorizeAccess/Pages/RequestTrn/Email.cshtml.cs b/TeachingRecordSystem/src/TeachingRecordSystem.AuthorizeAccess/Pages/RequestTrn/Email.cshtml.cs index 3cf5b636aa..90355d76a2 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.AuthorizeAccess/Pages/RequestTrn/Email.cshtml.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.AuthorizeAccess/Pages/RequestTrn/Email.cshtml.cs @@ -40,7 +40,6 @@ public async Task OnPost() return Redirect(linkGenerator.RequestTrnEmailInUse(JourneyInstance!.InstanceId)); } - return FromCheckAnswers == true ? Redirect(linkGenerator.RequestTrnCheckAnswers(JourneyInstance!.InstanceId)) : Redirect(linkGenerator.RequestTrnName(JourneyInstance.InstanceId)); diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.AuthorizeAccess/Pages/RequestTrn/EmailInUse.cshtml.cs b/TeachingRecordSystem/src/TeachingRecordSystem.AuthorizeAccess/Pages/RequestTrn/EmailInUse.cshtml.cs index 913bc6f38f..c17af751a7 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.AuthorizeAccess/Pages/RequestTrn/EmailInUse.cshtml.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.AuthorizeAccess/Pages/RequestTrn/EmailInUse.cshtml.cs @@ -1,15 +1,21 @@ +using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.RazorPages; using TeachingRecordSystem.UiCommon.FormFlow; namespace TeachingRecordSystem.AuthorizeAccess.Pages.RequestTrn; [Journey(RequestTrnJourneyState.JourneyName), RequireJourneyInstance] -public class EmailInUseModel : PageModel +public class EmailInUseModel(AuthorizeAccessLinkGenerator linkGenerator) : PageModel { public JourneyInstance? JourneyInstance { get; set; } - public void OnGet() + public override void OnPageHandlerExecuting(PageHandlerExecutingContext context) { + var state = JourneyInstance!.State; + if (state.Email is null) + { + context.Result = Redirect(linkGenerator.RequestTrnEmail(JourneyInstance.InstanceId)); + } } } diff --git a/TeachingRecordSystem/tests/TeachingRecordSystem.AuthorizeAccess.Tests/PageTests/RequestTrn/EmailInUseTests.cs b/TeachingRecordSystem/tests/TeachingRecordSystem.AuthorizeAccess.Tests/PageTests/RequestTrn/EmailInUseTests.cs new file mode 100644 index 0000000000..6ec551b246 --- /dev/null +++ b/TeachingRecordSystem/tests/TeachingRecordSystem.AuthorizeAccess.Tests/PageTests/RequestTrn/EmailInUseTests.cs @@ -0,0 +1,22 @@ +namespace TeachingRecordSystem.AuthorizeAccess.Tests.PageTests.RequestTrn; + +public class EmailInUseTests(HostFixture hostFixture) : TestBase(hostFixture) +{ + [Fact] + public async Task Get_WithoutEmailAddress_RedirectsToEmail() + { + // Arrange + var state = CreateNewState(); + state.HasPendingTrnRequest = true; + var journeyInstance = await CreateJourneyInstance(state); + + var request = new HttpRequestMessage(HttpMethod.Get, $"/request-trn/emailinuse?{journeyInstance.GetUniqueIdQueryParameter()}"); + + // Act + var response = await HttpClient.SendAsync(request); + + // Assert + Assert.Equal(StatusCodes.Status302Found, (int)response.StatusCode); + Assert.Equal($"/request-trn/email?{journeyInstance.GetUniqueIdQueryParameter()}", response.Headers.Location?.OriginalString); + } +}