-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent resubmitting trn request for same email
- Loading branch information
Showing
17 changed files
with
297 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...gRecordSystem/src/TeachingRecordSystem.AuthorizeAccess/Pages/RequestTrn/EmailInUse.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
@page "/request-trn/emailinuse" | ||
@model TeachingRecordSystem.AuthorizeAccess.Pages.RequestTrn.EmailInUseModel | ||
@{ | ||
ViewBag.Title = "Email Address in use"; | ||
} | ||
|
||
@section BeforeContent { | ||
<govuk-back-link href="@(LinkGenerator.RequestTrnEmail(Model.JourneyInstance!.InstanceId))" /> | ||
} | ||
|
||
<div class="govuk-grid-row"> | ||
<div class="govuk-grid-column-two-thirds-from-desktop"> | ||
<h1 class="govuk-heading-l">You’ve already submitted a request for a TRN</h1> | ||
</div> | ||
</div> |
21 changes: 21 additions & 0 deletions
21
...cordSystem/src/TeachingRecordSystem.AuthorizeAccess/Pages/RequestTrn/EmailInUse.cshtml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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(AuthorizeAccessLinkGenerator linkGenerator) : PageModel | ||
{ | ||
public JourneyInstance<RequestTrnJourneyState>? JourneyInstance { get; set; } | ||
|
||
public override void OnPageHandlerExecuting(PageHandlerExecutingContext context) | ||
{ | ||
var state = JourneyInstance!.State; | ||
if (state.Email is null) | ||
{ | ||
context.Result = Redirect(linkGenerator.RequestTrnEmail(JourneyInstance.InstanceId)); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
...ecordSystem/src/TeachingRecordSystem.Core/Dqt/Queries/GetOpenTasksForEmailAddressQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace TeachingRecordSystem.Core.Dqt.Queries; | ||
|
||
public record GetOpenTasksForEmailAddressQuery(string EmailAddress) : ICrmQuery<CrmTask[]>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...tem/src/TeachingRecordSystem.Core/Dqt/QueryHandlers/GetOpenTasksForEmailAddressHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Microsoft.PowerPlatform.Dataverse.Client; | ||
using Microsoft.Xrm.Sdk.Messages; | ||
using Microsoft.Xrm.Sdk.Query; | ||
using TeachingRecordSystem.Core.Dqt.Queries; | ||
|
||
namespace TeachingRecordSystem.Core.Dqt.QueryHandlers; | ||
|
||
public class GetOpenTasksForEmailAddressHandler : ICrmQueryHandler<GetOpenTasksForEmailAddressQuery, CrmTask[]> | ||
{ | ||
public async Task<CrmTask[]> Execute(GetOpenTasksForEmailAddressQuery query, IOrganizationServiceAsync organizationService) | ||
{ | ||
var queryExpression = new QueryExpression() | ||
{ | ||
EntityName = CrmTask.EntityLogicalName, | ||
ColumnSet = new ColumnSet( | ||
CrmTask.Fields.dfeta_EmailAddress, CrmTask.Fields.StateCode) | ||
}; | ||
queryExpression.Criteria.AddCondition(CrmTask.Fields.dfeta_EmailAddress, ConditionOperator.Equal, query.EmailAddress); | ||
queryExpression.Criteria.AddCondition(CrmTask.Fields.StateCode, ConditionOperator.Equal, (int)TaskState.Open); | ||
|
||
var request = new RetrieveMultipleRequest() | ||
{ | ||
Query = queryExpression | ||
}; | ||
|
||
var response = await organizationService.RetrieveMultipleAsync(queryExpression); | ||
return response.Entities.Select(e => e.ToEntity<CrmTask>()).ToArray(); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
.../tests/TeachingRecordSystem.AuthorizeAccess.Tests/PageTests/RequestTrn/EmailInUseTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
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); | ||
} | ||
|
||
[Fact] | ||
public async Task Get_WithEmailAddress_RendersExpectedContent() | ||
{ | ||
// Arrange | ||
var email = Faker.Internet.Email(); | ||
var state = CreateNewState(email); | ||
var journeyInstance = await CreateJourneyInstance(state); | ||
var person = await TestData.CreatePerson(); | ||
await TestData.CreateCrmTask(x => | ||
{ | ||
x.WithPersonId(person.ContactId); | ||
x.WithEmailAddress(email); | ||
}); | ||
|
||
var request = new HttpRequestMessage(HttpMethod.Get, $"/request-trn/emailinuse?{journeyInstance.GetUniqueIdQueryParameter()}"); | ||
|
||
// Act | ||
var response = await HttpClient.SendAsync(request); | ||
|
||
// Assert | ||
var doc = await AssertEx.HtmlResponse(response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,7 +116,6 @@ public async Task Post_InvalidFormatEmailAddress_ReturnsError() | |
await AssertEx.HtmlResponseHasError(response, "Email", "Enter an email address in the correct format, like [email protected]"); | ||
} | ||
|
||
|
||
[Fact] | ||
public async Task Post_ValidRequestWithValidData_RedirectsToNamePage() | ||
{ | ||
|
@@ -140,4 +139,34 @@ public async Task Post_ValidRequestWithValidData_RedirectsToNamePage() | |
Assert.Equal(StatusCodes.Status302Found, (int)response.StatusCode); | ||
Assert.Equal($"/request-trn/name?{journeyInstance.GetUniqueIdQueryParameter()}", response.Headers.Location?.OriginalString); | ||
} | ||
|
||
[Fact] | ||
public async Task Post_RequestForEmailWithOpenTasks_RedirectsToEmailInUse() | ||
{ | ||
// Arrange | ||
var email = Faker.Internet.Email(); | ||
var state = CreateNewState(); | ||
var journeyInstance = await CreateJourneyInstance(state); | ||
var person = await TestData.CreatePerson(); | ||
await TestData.CreateCrmTask(x => | ||
{ | ||
x.WithPersonId(person.ContactId); | ||
x.WithEmailAddress(email); | ||
}); | ||
|
||
var request = new HttpRequestMessage(HttpMethod.Post, $"/request-trn/email?{journeyInstance.GetUniqueIdQueryParameter()}") | ||
{ | ||
Content = new FormUrlEncodedContent(new Dictionary<string, string> | ||
{ | ||
{ "Email", email } | ||
}) | ||
}; | ||
|
||
// Act | ||
var response = await HttpClient.SendAsync(request); | ||
|
||
// Assert | ||
Assert.Equal(StatusCodes.Status302Found, (int)response.StatusCode); | ||
Assert.Equal($"/request-trn/emailinuse?{journeyInstance.GetUniqueIdQueryParameter()}", response.Headers.Location?.OriginalString); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...gRecordSystem.Core.Dqt.CrmIntegrationTests/QueryTests/GetOpenTasksForEmailAddressTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
namespace TeachingRecordSystem.Core.Dqt.CrmIntegrationTests.QueryTests; | ||
|
||
public class GetOpenTasksForEmailAddressTests : IAsyncLifetime | ||
{ | ||
private readonly CrmClientFixture.TestDataScope _dataScope; | ||
private readonly CrmQueryDispatcher _crmQueryDispatcher; | ||
|
||
public GetOpenTasksForEmailAddressTests(CrmClientFixture crmClientFixture) | ||
{ | ||
_dataScope = crmClientFixture.CreateTestDataScope(); | ||
_crmQueryDispatcher = crmClientFixture.CreateQueryDispatcher(); | ||
} | ||
|
||
public Task InitializeAsync() => Task.CompletedTask; | ||
|
||
public async Task DisposeAsync() => await _dataScope.DisposeAsync(); | ||
|
||
[Fact] | ||
public async Task WhenCalled_WithEmailWithNoOpenTasks_ReturnsEmpty() | ||
{ | ||
// Arrange | ||
var emailWithNoOpenTasks = Faker.Internet.Email(); | ||
|
||
// Act | ||
var result = await _crmQueryDispatcher.ExecuteQuery(new GetOpenTasksForEmailAddressQuery(emailWithNoOpenTasks)); | ||
|
||
// Assert | ||
Assert.Empty(result); | ||
} | ||
|
||
[Fact] | ||
public async Task WhenCalled_WithEmailWithOpenTasks_ReturnsTasks() | ||
{ | ||
// Arrange | ||
var person = await _dataScope.TestData.CreatePerson(); | ||
var emailWithOpenTasks = Faker.Internet.Email(); | ||
await _dataScope.TestData.CreateCrmTask(x => | ||
{ | ||
x.WithPersonId(person.ContactId); | ||
x.WithEmailAddress(emailWithOpenTasks); | ||
}); | ||
|
||
// Act | ||
var result = await _crmQueryDispatcher.ExecuteQuery(new GetOpenTasksForEmailAddressQuery(emailWithOpenTasks)); | ||
|
||
// Assert | ||
Assert.NotEmpty(result); | ||
Assert.Collection(result, item1 => | ||
{ | ||
Assert.Equal(emailWithOpenTasks, item1.dfeta_EmailAddress); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public async Task WhenCalled_WithEmailWithCompletedTasks_ReturnsEmpty() | ||
{ | ||
// Arrange | ||
var person = await _dataScope.TestData.CreatePerson(); | ||
var emailWithOpenTasks = Faker.Internet.Email(); | ||
await _dataScope.TestData.CreateCrmTask(x => | ||
{ | ||
x.WithPersonId(person.ContactId); | ||
x.WithEmailAddress(emailWithOpenTasks); | ||
x.WithCompletedStatus(); | ||
}); | ||
|
||
// Act | ||
var result = await _crmQueryDispatcher.ExecuteQuery(new GetOpenTasksForEmailAddressQuery(emailWithOpenTasks)); | ||
|
||
// Assert | ||
Assert.Empty(result); | ||
} | ||
|
||
[Fact] | ||
public async Task WhenCalled_WithEmailWithCancelledTasks_ReturnsEmpty() | ||
{ | ||
// Arrange | ||
var person = await _dataScope.TestData.CreatePerson(); | ||
var emailWithOpenTasks = Faker.Internet.Email(); | ||
await _dataScope.TestData.CreateCrmTask(x => | ||
{ | ||
x.WithPersonId(person.ContactId); | ||
x.WithEmailAddress(emailWithOpenTasks); | ||
x.WithCanceledStatus(); | ||
}); | ||
|
||
// Act | ||
var result = await _crmQueryDispatcher.ExecuteQuery(new GetOpenTasksForEmailAddressQuery(emailWithOpenTasks)); | ||
|
||
// Assert | ||
Assert.Empty(result); | ||
} | ||
} |
Oops, something went wrong.