Skip to content

Commit

Permalink
Merge branch 'test'
Browse files Browse the repository at this point in the history
  • Loading branch information
james-hollinger committed Aug 29, 2024
2 parents bfc5756 + 9326b60 commit 1db1ed4
Show file tree
Hide file tree
Showing 16 changed files with 1,932 additions and 120 deletions.
122 changes: 95 additions & 27 deletions backend/webapi.tests/Features/EndorsementRequests/Approve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ namespace PidpTests.Features.EndorsementRequests;

using PidpTests.TestingExtensions;

public class EndorsementApproveTests : InMemoryDbTest
public class EndorsementRequestApproveTests : InMemoryDbTest
{
private static readonly int RequestingPartyId = 1;
private static readonly int ReceivingPartyId = 2;
private const int RequestingPartyId = 1;
private const int ReceivingPartyId = 2;

public EndorsementApproveTests()
public EndorsementRequestApproveTests()
{
this.TestDb.HasAParty(party =>
{
Expand All @@ -41,7 +41,7 @@ public EndorsementApproveTests()

[Theory]
[MemberData(nameof(StatusCases))]
public async void Approve_AsRequestingParty_SuccessOnApproved(EndorsementRequestStatus status)
public async Task Approve_AsRequestingParty_SuccessOnApproved(EndorsementRequestStatus status)
{
var request = this.TestDb.Has(new EndorsementRequest
{
Expand All @@ -55,21 +55,20 @@ public async void Approve_AsRequestingParty_SuccessOnApproved(EndorsementRequest

var result = await handler.HandleAsync(new Approve.Command { EndorsementRequestId = request.Id, PartyId = RequestingPartyId });

Assert.Equal(expected, result.IsSuccess);
if (expected)
{
Assert.True(result.IsSuccess);
Assert.Equal(EndorsementRequestStatus.Completed, request.Status);
}
else
{
Assert.False(result.IsSuccess);
Assert.Equal(status, request.Status);
}
}

[Theory]
[MemberData(nameof(StatusCases))]
public async void Approve_AsRecievingParty_SuccessOnRecieved(EndorsementRequestStatus status)
public async Task Approve_AsRecievingPartyNotPreApproved_SuccessOnRecieved(EndorsementRequestStatus status)
{
var request = this.TestDb.Has(new EndorsementRequest
{
Expand All @@ -82,29 +81,66 @@ public async void Approve_AsRecievingParty_SuccessOnRecieved(EndorsementRequestS

var result = await handler.HandleAsync(new Approve.Command { EndorsementRequestId = request.Id, PartyId = ReceivingPartyId });

Assert.Equal(expected, result.IsSuccess);
if (expected)
{
Assert.True(result.IsSuccess);
Assert.Equal(EndorsementRequestStatus.Approved, request.Status);
}
else
{
Assert.False(result.IsSuccess);
Assert.Equal(status, request.Status);
}
}

public static IEnumerable<object[]> StatusCases()
[Theory]
[MemberData(nameof(StatusCases))]
public async Task Approve_AsRecievingPartyPreApproved_CompleteOnRecieved(EndorsementRequestStatus status)
{
foreach (var status in TestData.AllValuesOf<EndorsementRequestStatus>())
var request = this.TestDb.Has(new EndorsementRequest
{
RequestingPartyId = RequestingPartyId,
ReceivingPartyId = ReceivingPartyId,
Status = status,
PreApproved = true
});
var expected = status == EndorsementRequestStatus.Received; // Reciever can only approve ER after recieving.
var handler = this.MockDependenciesFor<Approve.CommandHandler>();

var result = await handler.HandleAsync(new Approve.Command { EndorsementRequestId = request.Id, PartyId = ReceivingPartyId });

Assert.Equal(expected, result.IsSuccess);
if (expected)
{
yield return new object[] { status };
Assert.Equal(EndorsementRequestStatus.Completed, request.Status);
}
else
{
Assert.Equal(status, request.Status);
}
}

[Fact]
public async Task Approve_AsReceivingParty_SendEmailToRequestingParty()
{
var request = this.TestDb.Has(new EndorsementRequest
{
RequestingPartyId = RequestingPartyId,
ReceivingPartyId = ReceivingPartyId,
Status = EndorsementRequestStatus.Received,
RecipientEmail = "[email protected]"
});

var emailService = AMock.EmailService();
var handler = this.MockDependenciesFor<Approve.CommandHandler>(emailService);

var result = await handler.HandleAsync(new Approve.Command { EndorsementRequestId = request.Id, PartyId = ReceivingPartyId });
Assert.True(result.IsSuccess);
A.CallTo(() => emailService.SendAsync(An<Email>._)).MustHaveHappenedOnceExactly();
}

[Theory]
[MemberData(nameof(MoaRoleTestCases))]
public async void Approve_AsRequester_MoaRoleAssigned(IEnumerable<int> licencedParties, int? expectedRoleAssigned)
public async Task Approve_AsRequester_MoaRoleAssignedEmailSentToReceiver(IEnumerable<int> licencedParties, int? expectedRoleAssigned)
{
foreach (var partyId in licencedParties)
{
Expand All @@ -122,7 +158,8 @@ public async void Approve_AsRequester_MoaRoleAssigned(IEnumerable<int> licencedP
.ReturningTrueWhenAssigingClientRoles();
var plrClient = A.Fake<IPlrClient>()
.ReturningAStandingsDigestWhenCalledWithCpn("cpn", true);
var handler = this.MockDependenciesFor<Approve.CommandHandler>(keycloakClient, plrClient);
var emailService = AMock.EmailService();
var handler = this.MockDependenciesFor<Approve.CommandHandler>(keycloakClient, plrClient, emailService);

var result = await handler.HandleAsync(new Approve.Command { EndorsementRequestId = request.Id, PartyId = RequestingPartyId });

Expand All @@ -139,32 +176,63 @@ public async void Approve_AsRequester_MoaRoleAssigned(IEnumerable<int> licencedP
.Single();
A.CallTo(() => keycloakClient.AssignAccessRoles(expectedUserId, MohKeycloakEnrolment.MoaLicenceStatus)).MustHaveHappened();
}
Assert.Single(emailService.SentEmails);
var sentTo = emailService.SentEmails.Single().To.Single();
Assert.Equal(this.TestDb.Parties.Single(party => party.Id == ReceivingPartyId).Email, sentTo);
}

[Fact]
public async void Approve_AsReceivingParty_Send_Email_to_RequestingParty()
[Theory]
[MemberData(nameof(MoaRoleTestCases))]
public async Task Approve_AsPreApprovedReciever_MoaRoleAssignedEmailSentToRequester(IEnumerable<int> licencedParties, int? expectedRoleAssigned)
{
foreach (var partyId in licencedParties)
{
var party = this.TestDb.Parties.Single(party => party.Id == partyId);
party.Cpn = "cpn";
}
var request = this.TestDb.Has(new EndorsementRequest
{
RequestingPartyId = RequestingPartyId,
ReceivingPartyId = ReceivingPartyId,
Status = EndorsementRequestStatus.Received,
RecipientEmail = "[email protected]"
RecipientEmail = "[email protected]",
PreApproved = true
});

var keycloakClient = A.Fake<IKeycloakAdministrationClient>()
.ReturningTrueWhenAssigingClientRoles();
var plrClient = A.Fake<IPlrClient>()
.ReturningAStandingsDigestWhenCalledWithCpn("cpn", true);
var emailService = AMock.EmailService();
var handler = this.MockDependenciesFor<Approve.CommandHandler>(emailService);
var handler = this.MockDependenciesFor<Approve.CommandHandler>(keycloakClient, plrClient, emailService);

var result = await handler.HandleAsync(new Approve.Command { EndorsementRequestId = request.Id, PartyId = ReceivingPartyId });

Assert.True(result.IsSuccess);
A.CallTo(() => emailService.SendAsync(An<Email>._)).MustHaveHappenedOnceExactly();
if (expectedRoleAssigned == null)
{
keycloakClient.AssertNoRolesAssigned();
}
else
{
var expectedUserId = this.TestDb.Parties
.Where(party => party.Id == expectedRoleAssigned)
.Select(party => party.PrimaryUserId)
.Single();
A.CallTo(() => keycloakClient.AssignAccessRoles(expectedUserId, MohKeycloakEnrolment.MoaLicenceStatus)).MustHaveHappened();
}
Assert.Single(emailService.SentEmails);
var sentTo = emailService.SentEmails.Single().To.Single();
Assert.Equal(this.TestDb.Parties.Single(party => party.Id == RequestingPartyId).Email, sentTo);
}

public static IEnumerable<object?[]> MoaRoleTestCases()
public static TheoryData<EndorsementRequestStatus> StatusCases => new(TestData.AllValuesOf<EndorsementRequestStatus>());

public static TheoryData<int[], int?> MoaRoleTestCases => new()
{
yield return new object[] { Enumerable.Empty<int>(), null! }; // neither are licenced, no role assigned.
yield return new object[] { new[] { RequestingPartyId }, ReceivingPartyId };
yield return new object[] { new[] { ReceivingPartyId }, RequestingPartyId };
yield return new object[] { new[] { RequestingPartyId, ReceivingPartyId }, null! }; // Both are licenced, no role assigned
}
// { [ Licenced PartyIds ], PartyIds expected to have MOA role assigned }
{ [], null }, // neither are licenced, no role assigned.
{ [ RequestingPartyId ], ReceivingPartyId },
{ [ ReceivingPartyId ], RequestingPartyId },
{ [ RequestingPartyId, ReceivingPartyId ], null }, // Both are licenced, no role assigned
};
}
121 changes: 121 additions & 0 deletions backend/webapi.tests/Features/EndorsementRequests/Create.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
namespace PidpTests.Features.EndorsementRequests;

using FakeItEasy;
using Xunit;

using Pidp.Features.EndorsementRequests;
using Pidp.Infrastructure.HttpClients.Mail;
using Pidp.Models;
using PidpTests.TestingExtensions;

public class EndorsementRequestCreateTests : InMemoryDbTest
{
[Fact]
public async Task Create_NotPreApproved_CreatedEmailSentToRecipient()
{
var requester = this.TestDb.HasAParty();
var command = new Create.Command
{
PartyId = requester.Id,
RecipientEmail = "[email protected]",
AdditionalInformation = "addditional info",
PreApproved = false
};
var emailService = AMock.EmailService();
var handler = this.MockDependenciesFor<Create.CommandHandler>(emailService);

var result = await handler.HandleAsync(command);

Assert.False(result.DuplicateEndorsementRequest);

var endorsementRequest = this.TestDb.EndorsementRequests.SingleOrDefault();
Assert.NotNull(endorsementRequest);
Assert.Equal(requester.Id, endorsementRequest.RequestingPartyId);
Assert.Equal(command.RecipientEmail, endorsementRequest.RecipientEmail);
Assert.Equal(command.AdditionalInformation, endorsementRequest.AdditionalInformation);
Assert.NotEqual(Guid.Empty, endorsementRequest.Token);
Assert.Equal(EndorsementRequestStatus.Created, endorsementRequest.Status);
Assert.False(endorsementRequest.PreApproved);

A.CallTo(() => emailService.SendAsync(An<Email>._)).MustHaveHappenedOnceExactly();
var email = emailService.SentEmails.Single();
Assert.Single(email.To);
Assert.Equal(command.RecipientEmail, email.To.Single());
Assert.Contains(endorsementRequest.Token.ToString(), email.Body);
}

[Fact]
public async Task Create_PreApprovedOneMatchingEmailDifferentCase_RecievedEmailSentToRecipient()
{
var requester = this.TestDb.HasAParty();
var reciever = this.TestDb.HasAParty(party => party.Email = "[email protected]");
var command = new Create.Command
{
PartyId = requester.Id,
RecipientEmail = "[email protected]",
PreApproved = true
};
var emailService = AMock.EmailService();
var handler = this.MockDependenciesFor<Create.CommandHandler>(emailService);

var result = await handler.HandleAsync(command);

Assert.False(result.DuplicateEndorsementRequest);

var endorsementRequest = this.TestDb.EndorsementRequests.SingleOrDefault();
Assert.NotNull(endorsementRequest);
Assert.Equal(requester.Id, endorsementRequest.RequestingPartyId);
Assert.Equal(reciever.Id, endorsementRequest.ReceivingPartyId);
Assert.Equal(command.RecipientEmail, endorsementRequest.RecipientEmail);
Assert.NotEqual(Guid.Empty, endorsementRequest.Token);
Assert.Equal(EndorsementRequestStatus.Received, endorsementRequest.Status);
Assert.True(endorsementRequest.PreApproved);

A.CallTo(() => emailService.SendAsync(An<Email>._)).MustHaveHappenedOnceExactly();
var email = emailService.SentEmails.Single();
Assert.Single(email.To);
Assert.Equal(command.RecipientEmail, email.To.Single());
Assert.DoesNotContain(endorsementRequest.Token.ToString(), email.Body);
}

[Theory]
[InlineData(0)]
[InlineData(2)]
[InlineData(3)]
public async Task Create_PreApprovedZeroOrManyMatchingEmails_CreatedEmailSentToRecipient(int numberOfMatchingEmails)
{
var requester = this.TestDb.HasAParty();
var recipientEmail = "[email protected]";
for (var i = 0; i < numberOfMatchingEmails; i++)
{
this.TestDb.HasAParty(party => party.Email = recipientEmail);
}
var command = new Create.Command
{
PartyId = requester.Id,
RecipientEmail = recipientEmail,
PreApproved = true
};
var emailService = AMock.EmailService();
var handler = this.MockDependenciesFor<Create.CommandHandler>(emailService);

var result = await handler.HandleAsync(command);

Assert.False(result.DuplicateEndorsementRequest);

var endorsementRequest = this.TestDb.EndorsementRequests.SingleOrDefault();
Assert.NotNull(endorsementRequest);
Assert.Equal(requester.Id, endorsementRequest.RequestingPartyId);
Assert.Null(endorsementRequest.ReceivingPartyId);
Assert.Equal(command.RecipientEmail, endorsementRequest.RecipientEmail);
Assert.NotEqual(Guid.Empty, endorsementRequest.Token);
Assert.Equal(EndorsementRequestStatus.Created, endorsementRequest.Status);
Assert.False(endorsementRequest.PreApproved);

A.CallTo(() => emailService.SendAsync(An<Email>._)).MustHaveHappenedOnceExactly();
var email = emailService.SentEmails.Single();
Assert.Single(email.To);
Assert.Equal(command.RecipientEmail, email.To.Single());
Assert.Contains(endorsementRequest.Token.ToString(), email.Body);
}
}
Loading

0 comments on commit 1db1ed4

Please sign in to comment.