1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-22 12:15:36 +01:00
bitwarden-server/test/Core.Test/Utilities/AuthorizationServiceExtensionTests.cs
2024-09-23 08:45:14 +10:00

39 lines
1.5 KiB
C#

using System.Security.Claims;
using Bit.Core.Exceptions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization.Infrastructure;
using NSubstitute;
using Xunit;
using AuthorizationServiceExtensions = Bit.Core.Utilities.AuthorizationServiceExtensions;
namespace Bit.Core.Test.Utilities;
public class AuthorizationServiceExtensionTests
{
[Fact]
async Task AuthorizeOrThrowAsync_ThrowsNotFoundException_IfResourceIsNull()
{
var authorizationService = Substitute.For<IAuthorizationService>();
await Assert.ThrowsAsync<NotFoundException>(() =>
AuthorizationServiceExtensions.AuthorizeOrThrowAsync(authorizationService, new ClaimsPrincipal(),
null, new OperationAuthorizationRequirement()));
}
[Fact]
async Task AuthorizeOrThrowAsync_ThrowsNotFoundException_IfAuthorizationFails()
{
var authorizationService = Substitute.For<IAuthorizationService>();
var claimsPrincipal = new ClaimsPrincipal();
var requirement = new OperationAuthorizationRequirement();
var resource = new object();
authorizationService
.AuthorizeAsync(claimsPrincipal, resource, Arg.Is<IEnumerable<IAuthorizationRequirement>>(r =>
r.First() == requirement))
.Returns(AuthorizationResult.Failed());
await Assert.ThrowsAsync<NotFoundException>(() =>
AuthorizationServiceExtensions.AuthorizeOrThrowAsync(authorizationService, claimsPrincipal, resource, requirement));
}
}