-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
|
||
|
@@ -220,4 +221,47 @@ public async Task BindingModelAsync_ShouldThrowInvalidOperationException_WhenEnu | |
|
||
await Assert.ThrowsAsync<ClaimParsingException>(() => claimModelBinder.BindModelAsync(bindingContext.Object)); | ||
} | ||
|
||
[Fact] | ||
public async Task BindingModelAsync_ShouldBeAbleToParseInts() | ||
{ | ||
int num = 42; | ||
Claim[] claims = [new Claim("someId", num.ToString(CultureInfo.InvariantCulture))]; | ||
Check warning on line 229 in test/DroidSolutions.Oss.AuthClaimBinderTest/ClaimModelBinderTests.cs GitHub Actions / coverage
Check warning on line 229 in test/DroidSolutions.Oss.AuthClaimBinderTest/ClaimModelBinderTests.cs GitHub Actions / build
Check warning on line 229 in test/DroidSolutions.Oss.AuthClaimBinderTest/ClaimModelBinderTests.cs GitHub Actions / build
|
||
ClaimsIdentity identity = new (claims, "Scheme"); | ||
ClaimsPrincipal principal = new(identity); | ||
|
||
Mock<HttpContext> httpContext = new(); | ||
httpContext.Setup(hc => hc.User).Returns(principal); | ||
|
||
Mock<DefaultModelBindingContext> bindingContext = new() { CallBase = true }; | ||
bindingContext.Setup(bc => bc.HttpContext).Returns(httpContext.Object); | ||
bindingContext.Setup(bc => bc.FieldName).Returns("someId"); | ||
bindingContext.Setup(bc => bc.ModelType).Returns(typeof(int)); | ||
|
||
ClaimModelBinder claimModelBinder = new(_logMock.Object, null); | ||
|
||
await claimModelBinder.BindModelAsync(bindingContext.Object); | ||
|
||
Assert.Equal(num, bindingContext.Object.Result.Model); | ||
} | ||
|
||
[Fact] | ||
public async Task BindingModelAsync_ShouldThrowInvalidOperationException_WhenIntIsNotParsable() | ||
{ | ||
Claim[] claims = [new Claim("someId", "42,5d")]; | ||
Check warning on line 251 in test/DroidSolutions.Oss.AuthClaimBinderTest/ClaimModelBinderTests.cs GitHub Actions / coverage
Check warning on line 251 in test/DroidSolutions.Oss.AuthClaimBinderTest/ClaimModelBinderTests.cs GitHub Actions / build
Check warning on line 251 in test/DroidSolutions.Oss.AuthClaimBinderTest/ClaimModelBinderTests.cs GitHub Actions / build
|
||
ClaimsIdentity identity = new(claims, "Scheme"); | ||
ClaimsPrincipal principal = new(identity); | ||
|
||
Mock<HttpContext> httpContext = new(); | ||
httpContext.Setup(hc => hc.User).Returns(principal); | ||
|
||
Mock<DefaultModelBindingContext> bindingContext = new() { CallBase = true }; | ||
bindingContext.Setup(bc => bc.HttpContext).Returns(httpContext.Object); | ||
bindingContext.Setup(bc => bc.FieldName).Returns("someId"); | ||
bindingContext.Setup(bc => bc.ModelType).Returns(typeof(int)); | ||
|
||
ClaimModelBinder claimModelBinder = new(_logMock.Object, null); | ||
|
||
await Assert.ThrowsAsync<ClaimParsingException>(() => claimModelBinder.BindModelAsync(bindingContext.Object)); | ||
} | ||
} |