-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring to add test for subject collapsing
- Loading branch information
Showing
3 changed files
with
138 additions
and
58 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
52 changes: 52 additions & 0 deletions
52
src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/Authorization/AuthorizationHelper.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,52 @@ | ||
using Digdir.Domain.Dialogporten.Application.Externals.AltinnAuthorization; | ||
using Digdir.Domain.Dialogporten.Domain.SubjectResources; | ||
|
||
namespace Digdir.Domain.Dialogporten.Infrastructure.Altinn.Authorization; | ||
|
||
internal static class AuthorizationHelper | ||
{ | ||
public static async Task CollapseSubjectResources( | ||
DialogSearchAuthorizationResult dialogSearchAuthorizationResult, | ||
AuthorizedPartiesResult authorizedParties, | ||
List<string> constraintResources, | ||
Func<CancellationToken, Task<List<SubjectResource>>> getAllSubjectResources, | ||
CancellationToken cancellationToken) | ||
{ | ||
var authorizedPartiesWithRoles = authorizedParties.AuthorizedParties | ||
.Where(p => p.AuthorizedRoles.Count != 0) | ||
.ToList(); | ||
|
||
var uniqueSubjects = authorizedPartiesWithRoles | ||
.SelectMany(p => p.AuthorizedRoles) | ||
.ToHashSet(); | ||
|
||
var subjectResources = (await getAllSubjectResources(cancellationToken)) | ||
.Where(x => uniqueSubjects.Contains(x.Subject) && (constraintResources.Count == 0 || constraintResources.Contains(x.Resource))).ToList(); | ||
|
||
var subjectToResources = subjectResources | ||
.GroupBy(sr => sr.Subject) | ||
.ToDictionary(g => g.Key, g => g.Select(sr => sr.Resource).ToHashSet()); | ||
|
||
foreach (var partyEntry in authorizedPartiesWithRoles) | ||
{ | ||
if (!dialogSearchAuthorizationResult.ResourcesByParties.TryGetValue(partyEntry.Party, out var resourceList)) | ||
{ | ||
resourceList = new HashSet<string>(); | ||
dialogSearchAuthorizationResult.ResourcesByParties[partyEntry.Party] = resourceList; | ||
} | ||
|
||
foreach (var subject in partyEntry.AuthorizedRoles) | ||
{ | ||
if (subjectToResources.TryGetValue(subject, out var subjectResourceSet)) | ||
{ | ||
resourceList.UnionWith(subjectResourceSet); | ||
} | ||
} | ||
|
||
if (resourceList.Count == 0) | ||
{ | ||
dialogSearchAuthorizationResult.ResourcesByParties.Remove(partyEntry.Party); | ||
} | ||
} | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
tests/Digdir.Domain.Dialogporten.Infrastructure.Unit.Tests/AuthorizationHelperTests.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,78 @@ | ||
using Digdir.Domain.Dialogporten.Application.Externals.AltinnAuthorization; | ||
using Digdir.Domain.Dialogporten.Domain.SubjectResources; | ||
using Digdir.Domain.Dialogporten.Infrastructure.Altinn.Authorization; | ||
using Xunit; | ||
|
||
namespace Digdir.Domain.Dialogporten.Infrastructure.Unit.Tests; | ||
|
||
public class AuthorizationHelperTests | ||
{ | ||
[Fact] | ||
public async Task CollapseSubjectResources_ShouldCollapseCorrectly() | ||
{ | ||
// Arrange | ||
var dialogSearchAuthorizationResult = new DialogSearchAuthorizationResult | ||
{ | ||
ResourcesByParties = new Dictionary<string, HashSet<string>>() | ||
}; | ||
var authorizedParties = new AuthorizedPartiesResult | ||
{ | ||
AuthorizedParties = new List<AuthorizedParty> | ||
{ | ||
new() | ||
{ | ||
Party = "party1", | ||
AuthorizedRoles = new List<string> { "role1", "role2" } | ||
}, | ||
new() | ||
{ | ||
Party = "party2", | ||
AuthorizedRoles = new List<string> { "role2" } | ||
}, | ||
new() | ||
{ | ||
Party = "party3", | ||
AuthorizedRoles = new List<string> { "role3" } | ||
} | ||
} | ||
}; | ||
var constraintResources = new List<string> { "resource1", "resource2", "resource4" }; | ||
|
||
// Simulate subject resources | ||
var subjectResources = new List<SubjectResource> | ||
{ | ||
new() { Subject = "role1", Resource = "resource1" }, | ||
new() { Subject = "role1", Resource = "resource2" }, | ||
new() { Subject = "role2", Resource = "resource2" }, | ||
new() { Subject = "role2", Resource = "resource3" }, | ||
new() { Subject = "role2", Resource = "resource4" }, | ||
new() { Subject = "role3", Resource = "resource5" }, // Note: not in constraintResources | ||
}; | ||
|
||
Task<List<SubjectResource>> GetSubjectResources(CancellationToken token) | ||
{ | ||
return Task.FromResult(subjectResources); | ||
} | ||
|
||
// Act | ||
await AuthorizationHelper.CollapseSubjectResources( | ||
dialogSearchAuthorizationResult, | ||
authorizedParties, | ||
constraintResources, | ||
GetSubjectResources, | ||
CancellationToken.None); | ||
|
||
// Assert | ||
Assert.Equal(2, dialogSearchAuthorizationResult.ResourcesByParties.Count); | ||
Assert.Contains("party1", dialogSearchAuthorizationResult.ResourcesByParties.Keys); | ||
Assert.Contains("resource1", dialogSearchAuthorizationResult.ResourcesByParties["party1"]); | ||
Assert.Contains("resource2", dialogSearchAuthorizationResult.ResourcesByParties["party1"]); | ||
Assert.Contains("resource4", dialogSearchAuthorizationResult.ResourcesByParties["party1"]); | ||
Assert.Equal(3, dialogSearchAuthorizationResult.ResourcesByParties["party1"].Count); | ||
|
||
Assert.Contains("party2", dialogSearchAuthorizationResult.ResourcesByParties.Keys); | ||
Assert.Contains("resource2", dialogSearchAuthorizationResult.ResourcesByParties["party2"]); | ||
Assert.Contains("resource4", dialogSearchAuthorizationResult.ResourcesByParties["party2"]); | ||
Assert.Equal(2, dialogSearchAuthorizationResult.ResourcesByParties["party2"].Count); | ||
} | ||
} |