Skip to content

Commit

Permalink
Tester for ProxyManagementService
Browse files Browse the repository at this point in the history
  • Loading branch information
larsolavk committed Nov 20, 2024
1 parent e3ce13d commit b5ef156
Show file tree
Hide file tree
Showing 6 changed files with 310 additions and 43 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: CI

on:
push:
branches-ignore:
- master
pull_request:
branches:
- master
env:
DOTNET_VERSION: '8.x' # Global environment variables

jobs:
build:
name: Build and Test
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v3

- name: GitHub metadata
run: |
printf "%s on %s by %s\n" "$GITHUB_REPOSITORY" "$GITHUB_REF_NAME" "$GITHUB_ACTOR"
- name: Set up .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ env.DOTNET_VERSION }}

- name: Restore dependencies
run: dotnet restore

- name: Build
run: dotnet build -c Release

- name: Run Tests
run: dotnet test -c Release --no-build --verbosity normal
25 changes: 0 additions & 25 deletions src/oed-authz-old.sln

This file was deleted.

60 changes: 43 additions & 17 deletions src/oed-authz/Services/ProxyManagementService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,35 +57,50 @@ public async Task UpdateProxyRoleAssigments(string estateSsn)
// If so, that recipient should be given the collective proxy role from the estate it self.
var roleAssignmentsForEstate = await _oedRoleRepositoryService.GetRoleAssignmentsForEstate(estateSsn);

var heirsWithProbateRole = roleAssignmentsForEstate.Where(x => x.RoleCode == Constants.ProbateRoleCode).ToList();
var individualProxyAssigments = roleAssignmentsForEstate.Where(x => x.RoleCode == Constants.IndividualProxyRoleCode).ToList();
var heirsWithProbateRole = roleAssignmentsForEstate
.Where(x => x.RoleCode == Constants.ProbateRoleCode)
.ToList();

var individualProxyAssigments = roleAssignmentsForEstate
.Where(x => x.RoleCode == Constants.IndividualProxyRoleCode)
.ToList();

// If there are any individual proxy assignments from heirs without probate role, those assignments are no longer valid and must be removed
var invalidIndividualProxyAssignments = individualProxyAssigments.Where(x =>
heirsWithProbateRole.All(y => y.RecipientSsn != x.HeirSsn)).ToList();
var invalidIndividualProxyAssignments = individualProxyAssigments
.Where(x => heirsWithProbateRole
.All(y => y.RecipientSsn != x.HeirSsn))
.ToList();

foreach (var invalidIndividualProxyAssignment in invalidIndividualProxyAssignments)
{
_logger.LogInformation("Removing no longer valid individual proxy assignment: {InvalidIndividualProxyAssignment}",
JsonSerializer.Serialize(invalidIndividualProxyAssignment));

await _oedRoleRepositoryService.RemoveRoleAssignment(
new RoleAssignment
{
EstateSsn = estateSsn,
HeirSsn = invalidIndividualProxyAssignment.HeirSsn,
RecipientSsn = invalidIndividualProxyAssignment.RecipientSsn,
RoleCode = Constants.IndividualProxyRoleCode
});
}
// Update the list of individual proxy assignments to only contain valid assignments
individualProxyAssigments = individualProxyAssigments.Where(x =>
heirsWithProbateRole.Any(y => y.RecipientSsn == x.HeirSsn)).ToList();
individualProxyAssigments = individualProxyAssigments
.Where(x => heirsWithProbateRole
.Any(y => y.RecipientSsn == x.HeirSsn))
.ToList();

var collectiveProxyAssigments = roleAssignmentsForEstate
.Where(x => x.RoleCode == Constants.CollectiveProxyRoleCode)
.ToList();

var collectiveProxyAssigments = roleAssignmentsForEstate.Where(x => x.RoleCode == Constants.CollectiveProxyRoleCode).ToList();
var eligibleCollectiveProxyRecipients = GetEligibleCollectiveProxyRecipients(heirsWithProbateRole, individualProxyAssigments);

// Find the eligible recipients that does not already have the collective proxy role
var eligibleRecipientsWithoutCollectiveProxyRole = eligibleCollectiveProxyRecipients.Where(x =>
collectiveProxyAssigments.All(y => y.RecipientSsn != x)).ToList();
var eligibleRecipientsWithoutCollectiveProxyRole = eligibleCollectiveProxyRecipients
.Where(x => collectiveProxyAssigments
.All(y => y.RecipientSsn != x))
.ToList();

// Grant the collective proxy role to the eligible recipients
foreach (var eligibleRecipient in eligibleRecipientsWithoutCollectiveProxyRole)
Expand All @@ -102,14 +117,17 @@ await _oedRoleRepositoryService.AddRoleAssignment(
}

// Find any recipients that have the collective proxy role, but should not have it anymore
var noLongerEligbleRecipientsWithCollectiveProxyRole = collectiveProxyAssigments.Where(x =>
!eligibleCollectiveProxyRecipients.Contains(x.RecipientSsn)).ToList();
var noLongerEligbleRecipientsWithCollectiveProxyRole = collectiveProxyAssigments
.Where(x => !eligibleCollectiveProxyRecipients
.Contains(x.RecipientSsn))
.ToList();

// Revoke the collective proxy role from the recipients that should no longer have it
foreach (var noLongerEligbleRecipient in noLongerEligbleRecipientsWithCollectiveProxyRole)
{
_logger.LogInformation("Revoking collective proxy role from no longer eligible recipient: {NoLongerEligbleRecipient}",
JsonSerializer.Serialize(noLongerEligbleRecipient));

await _oedRoleRepositoryService.RemoveRoleAssignment(
new RoleAssignment
{
Expand Down Expand Up @@ -146,7 +164,7 @@ private async Task ValidateRequest(ProxyManagementRequest proxyManagementRequest
var heirRoles = await _oedRoleRepositoryService.GetRoleAssignmentsForPerson(
proxyManagementRequest.EstateSsn, proxyManagementRequest.ProxyRoleAssignment.HeirSsn);

if (!heirRoles.Any(ra => ra.RoleCode == Constants.ProbateRoleCode))
if (heirRoles.All(ra => ra.RoleCode != Constants.ProbateRoleCode))
{
throw new InvalidOperationException("The heir does not have the probate role for the given estate");
}
Expand All @@ -157,10 +175,14 @@ private static List<string> GetEligibleCollectiveProxyRecipients(
List<RoleAssignment> individualProxyRoleAssignments)
{
// Get the recipients that have been given a proxy role by all heir with the probate role
var heirsWithProbateRoles = heirRoleAssignments.Select(x => x.RecipientSsn).Distinct();
var heirsWithProbateRoles = heirRoleAssignments
.Select(x => x.RecipientSsn)
.Distinct()
.ToList();

var eligibleCollectiveProxyRecipients = individualProxyRoleAssignments
.GroupBy(x => x.RecipientSsn)
.Where(x => x.Count() == heirsWithProbateRoles.Count())
.Where(x => x.Count() == heirsWithProbateRoles.Count)
.Select(x => x.Key)
.ToList();

Expand All @@ -169,9 +191,13 @@ private static List<string> GetEligibleCollectiveProxyRecipients(
// other than themselves have given them a proxy role
foreach (var heirWithProbateRole in heirsWithProbateRoles)
{
var otherHeirsWithProbateRoles = heirsWithProbateRoles.Where(x => x != heirWithProbateRole).ToList();
var otherHeirsWithProbateRoles = heirsWithProbateRoles
.Where(x => x != heirWithProbateRole)
.ToList();

var otherHeirsWithProbateRolesThatHaveGivenProxyRoleToHeir = individualProxyRoleAssignments
.Where(x => otherHeirsWithProbateRoles.Contains(x.RecipientSsn))
.Where(x => otherHeirsWithProbateRoles
.Contains(x.RecipientSsn))
.Select(x => x.RecipientSsn)
.Distinct()
.ToList();
Expand Down
187 changes: 187 additions & 0 deletions test/oed-authz.UnitTests/Services/ProxyManagementServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
using FakeItEasy;
using Microsoft.Extensions.Logging;
using oed_authz.Interfaces;
using oed_authz.Models;
using oed_authz.Services;
using oed_authz.UnitTests.TestUtils;

namespace oed_authz.UnitTests.Services
{
public class ProxyManagementServiceTests
{
private readonly IRoleAssignmentsRepository _fakeRoleAssignmentRepository = A.Fake<IRoleAssignmentsRepository>();
private readonly ILogger<ProxyManagementService> _fakeLogger = A.Fake<ILogger<ProxyManagementService>>();

[Fact]
public async Task UpdateProxyRoleAssignments_EstateWithThreeHeirs_When_AllHeirsHaveFormuesfullmaktRole_ShouldNot_AddOrRemoveRoles()
{
// Arrange
A.CallTo(() => _fakeRoleAssignmentRepository.GetRoleAssignmentsForEstate(A<string>._))
.ReturnsLazily((call) =>
{
var estateSsn = call.Arguments.Get<string>("estateSsn")!;
var factory = new RoleAssignmentFactory(estateSsn);
return Task.FromResult(new List<RoleAssignment>
{
factory.FormuesfulmaktRole("12345678901"),
factory.FormuesfulmaktRole("12345678902"),
factory.FormuesfulmaktRole("12345678903"),
});
});

var sut = new ProxyManagementService(_fakeRoleAssignmentRepository, _fakeLogger);

// Act
await sut.UpdateProxyRoleAssigments("11111111111");

// Assert
A.CallTo(() => _fakeRoleAssignmentRepository.AddRoleAssignment(A<RoleAssignment>._)).MustNotHaveHappened();
A.CallTo(() => _fakeRoleAssignmentRepository.RemoveRoleAssignment(A<RoleAssignment>._)).MustNotHaveHappened();
}

[Fact]
public async Task UpdateProxyRoleAssignments_EstateWithThreeHeirs_When_AllHeirsHaveProbateRole_ShouldNot_AddOrRemoveRoles()
{
// Arrange
A.CallTo(() => _fakeRoleAssignmentRepository.GetRoleAssignmentsForEstate(A<string>._))
.ReturnsLazily((call) =>
{
var estateSsn = call.Arguments.Get<string>("estateSsn")!;
var factory = new RoleAssignmentFactory(estateSsn);
return Task.FromResult(new List<RoleAssignment>
{
factory.ProbateRole("12345678901"),
factory.ProbateRole("12345678902"),
factory.ProbateRole("12345678903"),
});
});

var sut = new ProxyManagementService(_fakeRoleAssignmentRepository, _fakeLogger);

// Act
await sut.UpdateProxyRoleAssigments("11111111111");

// Assert
A.CallTo(() => _fakeRoleAssignmentRepository.AddRoleAssignment(A<RoleAssignment>._)).MustNotHaveHappened();
A.CallTo(() => _fakeRoleAssignmentRepository.RemoveRoleAssignment(A<RoleAssignment>._)).MustNotHaveHappened();
}

[Fact]
public async Task UpdateProxyRoleAssignments_EstateWithThreeHeirs_When_AllHeirsHaveProbateOrFormuesfullmaktRoles_ShouldNot_AddOrRemoveRoles()
{
// Arrange
A.CallTo(() => _fakeRoleAssignmentRepository.GetRoleAssignmentsForEstate(A<string>._))
.ReturnsLazily((call) =>
{
var estateSsn = call.Arguments.Get<string>("estateSsn")!;
var factory = new RoleAssignmentFactory(estateSsn);
return Task.FromResult(new List<RoleAssignment>
{
factory.ProbateRole("12345678901"),
factory.ProbateRole("12345678902"),
factory.FormuesfulmaktRole("12345678903"),
});
});

var sut = new ProxyManagementService(_fakeRoleAssignmentRepository, _fakeLogger);

// Act
await sut.UpdateProxyRoleAssigments("11111111111");

// Assert
A.CallTo(() => _fakeRoleAssignmentRepository.AddRoleAssignment(A<RoleAssignment>._)).MustNotHaveHappened();
A.CallTo(() => _fakeRoleAssignmentRepository.RemoveRoleAssignment(A<RoleAssignment>._)).MustNotHaveHappened();
}

[Fact]
public async Task UpdateProxyRoleAssignments_EstateWithThreeHeirs_When_AllHeirsHaveProbateRole_And_OneHeirHasAppointedAnotherHeirAsIndividualProxy_ShouldNot_AddOrRemoveRoles()
{
// Arrange
A.CallTo(() => _fakeRoleAssignmentRepository.GetRoleAssignmentsForEstate(A<string>._))
.ReturnsLazily((call) =>
{
var estateSsn = call.Arguments.Get<string>("estateSsn")!;
var factory = new RoleAssignmentFactory(estateSsn);
return Task.FromResult(new List<RoleAssignment>
{
factory.ProbateRole("12345678901"),
factory.ProbateRole("12345678902"),
factory.ProbateRole("12345678903"),
factory.IndividualProxyRole("12345678902", "12345678901")
});
});

var sut = new ProxyManagementService(_fakeRoleAssignmentRepository, _fakeLogger);

// Act
await sut.UpdateProxyRoleAssigments("11111111111");

// Assert
A.CallTo(() => _fakeRoleAssignmentRepository.AddRoleAssignment(A<RoleAssignment>._)).MustNotHaveHappened();
A.CallTo(() => _fakeRoleAssignmentRepository.RemoveRoleAssignment(A<RoleAssignment>._)).MustNotHaveHappened();
}

[Fact]
public async Task UpdateProxyRoleAssignments_EstateWithThreeHeirs_When_TwoHeirsHaveProbateRole_And_OneHeirHasAppointedTheOtherHeirAsIndividualProxy_Should_AddCollectiveProxyRole()
{
// Arrange
A.CallTo(() => _fakeRoleAssignmentRepository.GetRoleAssignmentsForEstate(A<string>._))
.ReturnsLazily((call) =>
{
var estateSsn = call.Arguments.Get<string>("estateSsn")!;
var factory = new RoleAssignmentFactory(estateSsn);
return Task.FromResult(new List<RoleAssignment>
{
factory.ProbateRole("12345678901"),
factory.ProbateRole("12345678902"),
factory.FormuesfulmaktRole("12345678903"),
factory.IndividualProxyRole("12345678902", "12345678901")
});
});

var sut = new ProxyManagementService(_fakeRoleAssignmentRepository, _fakeLogger);

// Act
await sut.UpdateProxyRoleAssigments("11111111111");

// Assert
A.CallTo(() => _fakeRoleAssignmentRepository.AddRoleAssignment(A<RoleAssignment>._)).MustHaveHappened();
A.CallTo(() => _fakeRoleAssignmentRepository.RemoveRoleAssignment(A<RoleAssignment>._)).MustNotHaveHappened();
}

[Fact]
public async Task UpdateProxyRoleAssignments_EstateWithThreeHeirs_When_AllHeirsHaveProbateRole_And_TwoHeirsHaveBothAppointedTheThirdHeirAsIndividualProxy_Should_AddCollectiveProxyRole()
{
// Arrange
A.CallTo(() => _fakeRoleAssignmentRepository.GetRoleAssignmentsForEstate(A<string>._))
.ReturnsLazily((call) =>
{
var estateSsn = call.Arguments.Get<string>("estateSsn")!;
var factory = new RoleAssignmentFactory(estateSsn);
return Task.FromResult(new List<RoleAssignment>
{
factory.ProbateRole("12345678901"),
factory.ProbateRole("12345678902"),
factory.ProbateRole("12345678903"),
factory.IndividualProxyRole("12345678902", "12345678901"),
factory.IndividualProxyRole("12345678903", "12345678901")
});
});

var sut = new ProxyManagementService(_fakeRoleAssignmentRepository, _fakeLogger);

// Act
await sut.UpdateProxyRoleAssigments("11111111111");

// Assert
A.CallTo(() => _fakeRoleAssignmentRepository.AddRoleAssignment(A<RoleAssignment>._)).MustHaveHappened();
A.CallTo(() => _fakeRoleAssignmentRepository.RemoveRoleAssignment(A<RoleAssignment>._)).MustNotHaveHappened();
}
}
}
Loading

0 comments on commit b5ef156

Please sign in to comment.