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(); var mockHttpContextAccessor = new Mock(); 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(); mockWorkbookService.Setup(service => service.GetCurrentUserRoleAsync(5, System.Threading.CancellationToken.None)) .ReturnsAsync(userWorkbookRole); return mockWorkbookService.Object; } private IHttpContextAccessor GetMockHttpContextAccessor() { var mockHttpContextAccessor = new Mock(); var routeFeature = new RouteValuesFeature(); routeFeature.RouteValues.Add("workbookId", 5); var features = new FeatureCollection(); features.Set(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); } } }