public void OnAuthorization_ValidClaims_SuccessResult() { var obj = new ClaimRequirementFilter(FunctionCode.CONTENT, CommandCode.CREATE); var permissions = new List <string>() { "CONTENT_CREATE" }; var claims = new List <Claim>() { new Claim(SystemConstants.Claims.Permissions, JsonSerializer.Serialize(permissions)) }; var modelState = new ModelStateDictionary(); var httpContextMock = new DefaultHttpContext(); httpContextMock.Request.Query = new QueryCollection(new Dictionary <string, StringValues> { }); // if you are reading any properties from the query parameters var identity = new ClaimsIdentity(claims, "TestAuthType"); var claimsPrincipal = new ClaimsPrincipal(identity); httpContextMock.User = claimsPrincipal; var actionContext = new ActionContext( httpContextMock, Mock.Of <RouteData>(), Mock.Of <ActionDescriptor>(), modelState ); var actionExecutingContext = new AuthorizationFilterContext( actionContext, new List <IFilterMetadata>()) { Result = new OkResult() // It will return ok unless during code execution you change this when by condition }; obj.OnAuthorization(actionExecutingContext); Assert.IsType <OkResult>(actionExecutingContext.Result); }
public void Authorization_OnSuccess() { //Arrange var perm = new List <string>() { "Leader", "Teacher" }; var httpContextMock = new Mock <HttpContext>(); httpContextMock .Setup(a => a.Request.Headers["Authorization"]) .Returns("Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI2NDJiNDE4Yi05MWFjLTQ5NTQtODgxMC01ZDc1OWJhMjAxNTAiLCJuYW1lIjoidGVzdEB0ZXN0LmNvbSIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL25hbWUiOiJ0ZXN0QHRlc3QuY29tIiwiZXhwIjoxNjA2ODI5OTE4LCJpc3MiOiJodHRwOi8vbXlzaXRlLmNvbSIsImF1ZCI6Imh0dHA6Ly9teXNpdGUuY29tIn0.76s1wSHNCqyhqyXkDajD3YYcvyjxopa91VSPLwUr-AQ"); // var httpContextMock = new Mock<HttpContext>(); // Create the userValidatorMock var claims = new List <Claim> { new Claim(ClaimTypes.Name, "fdssdfsf") }; var identity = new ClaimsIdentity(claims, "Jwt"); IPrincipal userPrincpal = new ClaimsPrincipal(identity); var baseauth = new Mock <IBaseAuth>(); baseauth.Setup(bauth => bauth // For any parameter passed to IsValid .AuthenticateJwtToken(It.IsAny <string>()) ).Returns(Task.FromResult(userPrincpal)); baseauth.Setup(bauth => bauth // For any parameter passed to IsValid .AuthorizeUserClaim(userPrincpal, perm) ).Returns(true); var logger = new Mock <ILoggerManager>(); logger.Setup(lgr => lgr // For any parameter passed to IsValid .LogInfo(It.IsAny <string>()) ); var serviceProviderMocklog = new Mock <IServiceProvider>(); serviceProviderMocklog.Setup(provider => provider.GetService(typeof(ILoggerManager))) .Returns(logger.Object); var serviceProviderMockbAuth = new Mock <IServiceProvider>(); serviceProviderMockbAuth.Setup(provider => provider.GetService(typeof(IBaseAuth))) .Returns(baseauth.Object); httpContextMock.SetupGet(context => context.RequestServices) .Returns(serviceProviderMocklog.Object); httpContextMock.SetupGet(context => context.RequestServices) .Returns(serviceProviderMockbAuth.Object); ActionContext fakeActionContext = new ActionContext(httpContextMock.Object, new Microsoft.AspNetCore.Routing.RouteData(), new Microsoft.AspNetCore.Mvc.Abstractions.ActionDescriptor()); AuthorizationFilterContext fakeAuthFilterContext = new AuthorizationFilterContext(fakeActionContext, new List <IFilterMetadata> { }); //Act ClaimRequirementFilter claimReqFilter = new ClaimRequirementFilter(new string[] { "Leader", "Teacher" }); claimReqFilter.OnAuthorization(fakeAuthFilterContext); //Assert Assert.Null(fakeAuthFilterContext.Result); }
public void Constructor_CreateInstance_ShouldBe_NotNull() { var obj = new ClaimRequirementFilter(FunctionCode.CONTENT, CommandCode.CREATE); Assert.NotNull(obj); }