mirror of
https://github.com/bitwarden/server
synced 2025-12-11 13:53:40 +00:00
[PM-22739] Add ClaimsPrincipal Extension
feat: add ClaimsPrincipal Extension test: add tests
This commit is contained in:
@@ -0,0 +1,22 @@
|
|||||||
|
using System.Security.Claims;
|
||||||
|
using Bit.Core.Identity;
|
||||||
|
|
||||||
|
namespace Bit.Core.Auth.UserFeatures.SendAccess;
|
||||||
|
|
||||||
|
public static class SendAccessClaimsPrincipalExtensions
|
||||||
|
{
|
||||||
|
public static Guid GetSendId(this ClaimsPrincipal user)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(user);
|
||||||
|
|
||||||
|
var sendIdClaim = user.FindFirst(Claims.SendId)
|
||||||
|
?? throw new InvalidOperationException("Send ID claim not found.");
|
||||||
|
|
||||||
|
if (!Guid.TryParse(sendIdClaim.Value, out var sendGuid))
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Invalid Send ID claim value.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return sendGuid;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
using System.Security.Claims;
|
||||||
|
using Bit.Core.Auth.UserFeatures.SendAccess;
|
||||||
|
using Bit.Core.Identity;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Bit.Core.Test.Auth.UserFeatures.SendAccess;
|
||||||
|
|
||||||
|
public class SendAccessClaimsPrincipalExtensionsTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void GetSendId_ReturnsGuid_WhenClaimIsPresentAndValid()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var guid = Guid.NewGuid();
|
||||||
|
var claims = new[] { new Claim(Claims.SendId, guid.ToString()) };
|
||||||
|
var principal = new ClaimsPrincipal(new ClaimsIdentity(claims));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = principal.GetSendId();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(guid, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetSendId_ThrowsInvalidOperationException_WhenClaimIsMissing()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var principal = new ClaimsPrincipal(new ClaimsIdentity());
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
var ex = Assert.Throws<InvalidOperationException>(() => principal.GetSendId());
|
||||||
|
Assert.Equal("Send ID claim not found.", ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetSendId_ThrowsInvalidOperationException_WhenClaimValueIsInvalid()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var claims = new[] { new Claim(Claims.SendId, "not-a-guid") };
|
||||||
|
var principal = new ClaimsPrincipal(new ClaimsIdentity(claims));
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
var ex = Assert.Throws<InvalidOperationException>(() => principal.GetSendId());
|
||||||
|
Assert.Equal("Invalid Send ID claim value.", ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetSendId_ThrowsArgumentNullException_WhenPrincipalIsNull()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.Throws<ArgumentNullException>(() => SendAccessClaimsPrincipalExtensions.GetSendId(null));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user