-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fea0541
commit ad4193e
Showing
4 changed files
with
194 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,10 @@ | |
"PhoneNumber": "90001337", | ||
"Email": "[email protected]", | ||
"PartyId": 501337, | ||
"Party": {}, | ||
"Party": { | ||
"partyId": "501337", | ||
"ssn": "01039012345" | ||
}, | ||
"UserType": 1, | ||
"ProfileSettingPreference": { | ||
"Language": "nn", | ||
|
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,149 @@ | ||
using System.Security.Claims; | ||
using Altinn.App.Api.Tests.Mocks; | ||
using Altinn.App.Api.Tests.Utils; | ||
using Altinn.App.Core.Configuration; | ||
using Altinn.App.Core.Helpers; | ||
using Altinn.App.Core.Internal.Profile; | ||
using Altinn.App.Core.Internal.Registers; | ||
using FluentAssertions; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
using Moq; | ||
|
||
namespace Altinn.App.Api.Tests.Helpers; | ||
|
||
public class UserHelperTest | ||
{ | ||
private sealed record Fixture(WebApplication App) : IAsyncDisposable | ||
{ | ||
public readonly IOptions<GeneralSettings> GeneralSettings = Options.Create(new GeneralSettings()); | ||
public IProfileClient ProfileClientMock => App.Services.GetRequiredService<IProfileClient>(); | ||
public IAltinnPartyClient AltinnPartyClientMock => App.Services.GetRequiredService<IAltinnPartyClient>(); | ||
|
||
public static Fixture Create(ClaimsPrincipal userPrincipal, string? partyCookieValue = null) | ||
{ | ||
var app = TestUtils.AppBuilder.Build(overrideAltinnAppServices: services => | ||
{ | ||
var httpContextMock = new Mock<HttpContext>(); | ||
httpContextMock.Setup(x => x.Request.Cookies["AltinnPartyId"]).Returns(partyCookieValue); | ||
httpContextMock.Setup(httpContext => httpContext.User).Returns(userPrincipal); | ||
var httpContextAccessor = new Mock<IHttpContextAccessor>(); | ||
httpContextAccessor.Setup(x => x.HttpContext).Returns(httpContextMock.Object); | ||
|
||
services.AddSingleton(httpContextAccessor.Object); | ||
services.AddTransient<IProfileClient, ProfileClientMock>(); | ||
services.AddTransient<IAltinnPartyClient, AltinnPartyClientMock>(); | ||
}); | ||
return new Fixture(app); | ||
} | ||
|
||
public async ValueTask DisposeAsync() => await App.DisposeAsync(); | ||
} | ||
|
||
[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 authLevel = 3; | ||
var userPrincipal = PrincipalUtil.GetUserPrincipal(userId, partyId, authLevel); | ||
await using var fixture = Fixture.Create(userPrincipal); | ||
var userHelper = new UserHelper( | ||
profileClient: fixture.ProfileClientMock, | ||
altinnPartyClientService: fixture.AltinnPartyClientMock, | ||
settings: fixture.GeneralSettings | ||
); | ||
var httpContextAccessor = fixture.App.Services.GetRequiredService<IHttpContextAccessor>(); | ||
var httpContext = httpContextAccessor.HttpContext; | ||
var userProfile = await fixture.ProfileClientMock.GetUserProfile(userId); | ||
var party = partyId.Equals(userProfile!.PartyId) | ||
? userProfile!.Party | ||
: await fixture.AltinnPartyClientMock.GetParty(partyId); | ||
|
||
// Act | ||
var result = await userHelper.GetUserContext(httpContext!); | ||
|
||
// Assert | ||
result | ||
.Should() | ||
.BeEquivalentTo( | ||
new Altinn.App.Core.Models.UserContext | ||
{ | ||
SocialSecurityNumber = ssn, | ||
UserName = $"User{userId}", | ||
UserId = userId, | ||
PartyId = partyId, | ||
AuthenticationLevel = authLevel, | ||
User = userPrincipal, | ||
UserParty = userProfile!.Party, | ||
Party = party, | ||
} | ||
); | ||
} | ||
|
||
[Fact] | ||
public async Task GetUserContext_HandlesMissingClaims() | ||
{ | ||
// Arrange | ||
const int userId = 1001; | ||
const int authLevel = 3; | ||
var userPrincipal = PrincipalUtil.GetUserPrincipal(userId, default, authLevel); | ||
await using var fixture = Fixture.Create(userPrincipal); | ||
var userHelper = new UserHelper( | ||
profileClient: fixture.ProfileClientMock, | ||
altinnPartyClientService: fixture.AltinnPartyClientMock, | ||
settings: fixture.GeneralSettings | ||
); | ||
var httpContextAccessor = fixture.App.Services.GetRequiredService<IHttpContextAccessor>(); | ||
var httpContext = httpContextAccessor.HttpContext; | ||
var userProfile = await fixture.ProfileClientMock.GetUserProfile(userId); | ||
|
||
// Act | ||
var result = await userHelper.GetUserContext(httpContext!); | ||
|
||
// Assert | ||
result | ||
.Should() | ||
.BeEquivalentTo( | ||
new Altinn.App.Core.Models.UserContext | ||
{ | ||
SocialSecurityNumber = null, | ||
UserName = $"User{userId}", | ||
UserId = userId, | ||
PartyId = default, | ||
AuthenticationLevel = authLevel, | ||
User = userPrincipal, | ||
UserParty = userProfile!.Party, | ||
Party = null, | ||
} | ||
); | ||
} | ||
|
||
[Fact] | ||
public async Task GetUserContext_ThrowsOnMissingUserId() | ||
{ | ||
// Arrange | ||
var userPrincipal = PrincipalUtil.GetUserPrincipal(default, default); | ||
await using var fixture = Fixture.Create(userPrincipal); | ||
var userHelper = new UserHelper( | ||
profileClient: fixture.ProfileClientMock, | ||
altinnPartyClientService: fixture.AltinnPartyClientMock, | ||
settings: fixture.GeneralSettings | ||
); | ||
var httpContextAccessor = fixture.App.Services.GetRequiredService<IHttpContextAccessor>(); | ||
var httpContext = httpContextAccessor.HttpContext; | ||
|
||
// Act | ||
var act = async () => | ||
{ | ||
await userHelper.GetUserContext(httpContext!); | ||
}; | ||
|
||
// Assert | ||
await act.Should().ThrowAsync<Exception>().WithMessage("*not*ID*from*claims*"); | ||
} | ||
} |
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