Skip to content

Commit

Permalink
Adjusts UserHelper and test method based on research/status quo
Browse files Browse the repository at this point in the history
  • Loading branch information
danielskovli committed Nov 29, 2024
1 parent ab1458b commit 5b449df
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
59 changes: 28 additions & 31 deletions src/Altinn.App.Core/Helpers/UserHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,32 @@ public UserHelper(
public async Task<UserContext> GetUserContext(HttpContext context)
{
using var activity = _telemetry?.StartGetUserContextActivity();
string? cookieValue = context.Request.Cookies[_settings.GetAltinnPartyCookieName];
string? partyCookieValue = context.Request.Cookies[_settings.GetAltinnPartyCookieName];
Dictionary<string, string> tokenClaims = context.User.Claims.ToDictionary(
x => x.Type,
y => y.Value,
StringComparer.Ordinal
);

UserContext userContext = new()
{
User = context.User,
UserName = GetClaim(context.User.Claims, AltinnCoreClaimTypes.UserName),
UserId = GetClaim(context.User.Claims, AltinnCoreClaimTypes.UserId),
PartyId = GetClaim(context.User.Claims, AltinnCoreClaimTypes.PartyID),
AuthenticationLevel = GetClaim(context.User.Claims, AltinnCoreClaimTypes.AuthenticationLevel),
UserName = tokenClaims[AltinnCoreClaimTypes.UserName],
UserId = tokenClaims[AltinnCoreClaimTypes.UserId] switch
{
{ } value => Convert.ToInt32(value, CultureInfo.InvariantCulture),
_ => default,
},
PartyId = tokenClaims[AltinnCoreClaimTypes.PartyID] switch
{
{ } value => Convert.ToInt32(value, CultureInfo.InvariantCulture),
_ => default,
},
AuthenticationLevel = tokenClaims[AltinnCoreClaimTypes.AuthenticationLevel] switch
{
{ } value => Convert.ToInt32(value, CultureInfo.InvariantCulture),
_ => default,
},
};

if (userContext.UserId == default)
Expand All @@ -72,37 +89,17 @@ await _profileClient.GetUserProfile(userContext.UserId)

userContext.UserParty = userProfile.Party;

userContext.PartyId = cookieValue is not null
? Convert.ToInt32(cookieValue, CultureInfo.InvariantCulture)
userContext.PartyId = partyCookieValue is not null
? Convert.ToInt32(partyCookieValue, CultureInfo.InvariantCulture)
: userContext.PartyId;

userContext.Party = userContext.PartyId.Equals(userProfile.Party?.PartyId)
? userContext.Party = userProfile.Party
userContext.Party = userContext.PartyId.Equals(userProfile.PartyId)
? userProfile.Party
: await _altinnPartyClientService.GetParty(userContext.PartyId);

userContext.SocialSecurityNumber = userContext.Party?.SSN ?? userContext.UserParty.SSN;
userContext.SocialSecurityNumber =
userContext.Party?.SSN ?? userContext.Party?.Person?.SSN ?? userContext.UserParty.SSN;

return userContext;
}

private static ClaimWrapper GetClaim(IEnumerable<Claim> claims, string claimType)
{
var claim = claims.FirstOrDefault(x => x.Type.Equals(claimType, StringComparison.Ordinal))?.Value;
return new ClaimWrapper(claim);
}

private readonly record struct ClaimWrapper(string? Value)
{
public static implicit operator string?(ClaimWrapper claimWrapper)
{
return claimWrapper.Value;
}

public static implicit operator int(ClaimWrapper claimWrapper)
{
return string.IsNullOrEmpty(claimWrapper.Value)
? default
: Convert.ToInt32(claimWrapper.Value, CultureInfo.InvariantCulture);
}
}
}
5 changes: 4 additions & 1 deletion test/Altinn.App.Api.Tests/Data/Profile/User/1337.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
"PhoneNumber": "90001337",
"Email": "1337@altinnstudiotestusers.com",
"PartyId": 501337,
"Party": {},
"Party": {
"partyId": "501337",
"ssn": "01039012345"
},
"UserType": 1,
"ProfileSettingPreference": {
"Language": "nn",
Expand Down
17 changes: 10 additions & 7 deletions test/Altinn.App.Api.Tests/Helpers/UserHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Altinn.App.Core.Helpers;
using Altinn.App.Core.Internal.Profile;
using Altinn.App.Core.Internal.Registers;
using Altinn.Platform.Register.Models;
using FluentAssertions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
Expand Down Expand Up @@ -42,14 +43,14 @@ public static Fixture Create(ClaimsPrincipal userPrincipal, string? partyCookieV
public async ValueTask DisposeAsync() => await App.DisposeAsync();
}

[Fact]
public async Task GetUserContext_PerformsCorrectLogic()
[Theory]
[InlineData(1337, 501337, "01039012345")] // Has `Party` containing correct SSN
[InlineData(1001, 510001, null)] // Has no SSN, because of empty `Party`
[InlineData(1337, 510001, "01899699552")] // `Party` mismatch, forcing load via `IAltinnPartyClient`, resulting in SSN belonging to party 510001
public async Task GetUserContext_PerformsCorrectLogic(int userId, int partyId, string? ssn)
{
// Arrange
const int userId = 1337;
const int partyId = 501337;
const int authLevel = 3;

var userPrincipal = PrincipalUtil.GetUserPrincipal(userId, partyId, authLevel);
await using var fixture = Fixture.Create(userPrincipal);
var userHelper = new UserHelper(
Expand All @@ -60,7 +61,9 @@ public async Task GetUserContext_PerformsCorrectLogic()
var httpContextAccessor = fixture.App.Services.GetRequiredService<IHttpContextAccessor>();
var httpContext = httpContextAccessor.HttpContext;
var userProfile = await fixture.ProfileClientMock.GetUserProfile(userId);
var party = await fixture.AltinnPartyClientMock.GetParty(partyId);
var party = partyId.Equals(userProfile!.PartyId)
? userProfile!.Party
: await fixture.AltinnPartyClientMock.GetParty(partyId);

// Act
var result = await userHelper.GetUserContext(httpContext!);
Expand All @@ -71,7 +74,7 @@ public async Task GetUserContext_PerformsCorrectLogic()
.BeEquivalentTo(
new Altinn.App.Core.Models.UserContext
{
SocialSecurityNumber = "01039012345",
SocialSecurityNumber = ssn,
UserName = $"User{userId}",
UserId = userId,
PartyId = partyId,
Expand Down

0 comments on commit 5b449df

Please sign in to comment.