feat: initial commit

This commit is contained in:
Thom Lamb
2026-06-23 11:18:54 -05:00
parent fc3ce9a074
commit 1bb980f070
790 changed files with 149255 additions and 218 deletions
@@ -0,0 +1,111 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Moq;
using NUnit.Framework;
using Strata.Analytics.Api.Authorization;
using Strata.Analytics.Biz.Workbooks.Services;
using Strata.Testing.Categories;
using System.Security.Claims;
using FluentAssertions;
using Microsoft.AspNetCore.Http.Features;
using Strata.Analytics.Models.Workbooks;
namespace Strata.Analytics.Test.Unit.Api.Authorization
{
[NUnit.Framework.Category(TestCategory.Unit)]
[Parallelizable(ParallelScope.Self)]
public class WorkbookMinimumRoleHandlerTest
{
[Test]
public async Task Handler_Fails_GivenWorkbookIdNotInRoute()
{
// arrange
var mockWorkbookService = new Mock<IWorkbookService>();
var mockHttpContextAccessor = new Mock<IHttpContextAccessor>();
var httpContext = new DefaultHttpContext();
mockHttpContextAccessor.Setup(_ => _.HttpContext).Returns(httpContext);
var authorizationHandlerContext = GetMockAuthorizationHandlerContext(WorkbookRole.Viewer);
var handler = new WorkbookMinimumRoleHandler(mockWorkbookService.Object, mockHttpContextAccessor.Object);
// act
await handler.HandleAsync(authorizationHandlerContext);
// assert
authorizationHandlerContext.HasSucceeded.Should().BeFalse();
}
[TestCase(WorkbookRole.Viewer, WorkbookRole.Viewer)]
[TestCase(WorkbookRole.Viewer, WorkbookRole.Editor)]
[TestCase(WorkbookRole.Editor, WorkbookRole.Editor)]
public async Task Handler_Succeeds_GivenUserHasMinimumAccess(WorkbookRole minimumWorkbookRole, WorkbookRole userWorkbookRole)
{
// arrange
var workbookService = GetMockWorkbookService(userWorkbookRole);
var httpContextAccessor = GetMockHttpContextAccessor();
var authorizationHandlerContext = GetMockAuthorizationHandlerContext(minimumWorkbookRole);
var handler = new WorkbookMinimumRoleHandler(workbookService, httpContextAccessor);
// act
await handler.HandleAsync(authorizationHandlerContext);
// assert
authorizationHandlerContext.HasSucceeded.Should().BeTrue();
}
[TestCase(WorkbookRole.Editor, WorkbookRole.Viewer)]
public async Task Handler_Fails_GivenUserDoesNotHaveMinimumAccess(WorkbookRole minimumWorkbookRole, WorkbookRole userWorkbookRole)
{
// arrange
var workbookService = GetMockWorkbookService(userWorkbookRole);
var httpContextAccessor = GetMockHttpContextAccessor();
var authorizationHandlerContext = GetMockAuthorizationHandlerContext(minimumWorkbookRole);
var handler = new WorkbookMinimumRoleHandler(workbookService, httpContextAccessor);
// act
await handler.HandleAsync(authorizationHandlerContext);
// assert
authorizationHandlerContext.HasSucceeded.Should().BeFalse();
}
private IWorkbookService GetMockWorkbookService(WorkbookRole userWorkbookRole)
{
var mockWorkbookService = new Mock<IWorkbookService>();
mockWorkbookService.Setup(service => service.GetCurrentUserRoleAsync(5, System.Threading.CancellationToken.None))
.ReturnsAsync(userWorkbookRole);
return mockWorkbookService.Object;
}
private IHttpContextAccessor GetMockHttpContextAccessor()
{
var mockHttpContextAccessor = new Mock<IHttpContextAccessor>();
var routeFeature = new RouteValuesFeature();
routeFeature.RouteValues.Add("workbookId", 5);
var features = new FeatureCollection();
features.Set<IRouteValuesFeature>(routeFeature);
var httpContext = new DefaultHttpContext(features);
mockHttpContextAccessor.Setup(_ => _.HttpContext).Returns(httpContext);
return mockHttpContextAccessor.Object;
}
private AuthorizationHandlerContext GetMockAuthorizationHandlerContext(WorkbookRole minimumWorkbookRole)
{
var requirement = new WorkbookMinimumRoleRequirement(minimumWorkbookRole);
var user = new ClaimsPrincipal(
new ClaimsIdentity(
new Claim[] { }, "Basic"));
return new AuthorizationHandlerContext(new[] { requirement }, user, null);
}
}
}