public async Task UserCheckIdFilter_Success_UserIdHimself() { //Arrange------------------------------------------------------------------------------------------------------------------------------ var userId = "0d47394e-672f-4db7-898c-bfd8f32e2af7"; var httpContext = new DefaultHttpContext(); var rout = new RouteData(); rout.Values.Add("id", userId); var context = new ActionExecutingContext( new ActionContext { HttpContext = httpContext, RouteData = rout, ActionDescriptor = new ActionDescriptor() }, new List <IFilterMetadata>(), new Dictionary <string, object>(), new Mock <Controller>().Object); var claims = new[] { new Claim(ClaimTypes.NameIdentifier, userId), }; var identity = new ClaimsIdentity(claims); var claimsPrincipal = new ClaimsPrincipal(identity); _mockAccessor.Setup(x => x.HttpContext).Returns(httpContext); _mockAccessor.Setup(x => x.HttpContext.User.Identity.IsAuthenticated).Returns(true); _mockAccessor.Setup(x => x.HttpContext.User).Returns(claimsPrincipal); _mockoggerFactory.Setup(x => x.CreateLogger(It.IsAny <string>())).Returns(_mockLogger.Object); var filter = new UserCheckIdFilter(_mockoggerFactory.Object, _mockAccessor.Object); //Act---------------------------------------------------------------------------------------------------------------------------------- filter.OnActionExecuting(context); //Assert------------------------------------------------------------------------------------------------------------------------------- Assert.Null(context.Result); //Assert.IsType<UnauthorizedResult>(context.Result); }
public async Task UserCheckIdFilter_Fail_UserIdAnother() { //Arrange var userId = "5a3a2a02-7bbf-41f1-b401-25e7be899d24"; var UserIdAnother = "5a3a2a02-7bbf-41f1-b401-299d24asdadas"; var httpContext = new DefaultHttpContext(); var route = new RouteData(); route.Values.Add("id", UserIdAnother); var context = new ActionExecutingContext( new ActionContext { HttpContext = httpContext, RouteData = route, ActionDescriptor = new ActionDescriptor() }, new List <IFilterMetadata>(), new Dictionary <string, object>(), new Mock <Controller>().Object); var claims = new[] { new Claim(ClaimTypes.NameIdentifier, userId) }; var identity = new ClaimsIdentity(claims); var claimsPrincipal = new ClaimsPrincipal(identity); _moqHttpContextAccessor.Setup(o => o.HttpContext).Returns(httpContext); _moqHttpContextAccessor.Setup(o => o.HttpContext.User.Identity.IsAuthenticated).Returns(true); _moqHttpContextAccessor.Setup(o => o.HttpContext.User).Returns(claimsPrincipal); _moqLoggerFactory.Setup(o => o.CreateLogger(It.IsAny <string>())).Returns(_moqLogger.Object); var filter = new UserCheckIdFilter(_moqLoggerFactory.Object, _moqHttpContextAccessor.Object); //Act filter.OnActionExecuting(context); //Assert Assert.NotNull(context.Result); Assert.IsType <UnauthorizedResult>(context.Result); }