示例#1
0
        public void UserAuthorizationAuthorized()
        {
            //Arrange
            var httpContext = new DefaultHttpContext {
                User = UserClaims()
            };
            var routeData = new RouteData();

            routeData.Values.Add("userId", "1");
            var actionContext = new ActionContext(httpContext, routeData, new ActionDescriptor());
            var authorizationFilterContext = new AuthorizationFilterContext(actionContext, new List <IFilterMetadata>());
            var userAuthorizationFilter    = new UserAuthorizationFilter();

            //Act
            userAuthorizationFilter.OnAuthorization(authorizationFilterContext);

            //Assert
            var result = authorizationFilterContext.Result;

            Assert.Null(result);
        }
示例#2
0
        public void UserAuthorizationUnauthorized()
        {
            //Arrange
            var httpContext = new DefaultHttpContext {
                User = UserClaims()
            };
            var routeData = new RouteData();

            routeData.Values.Add("userId", "2");
            var actionContext = new ActionContext(httpContext, routeData, new ActionDescriptor());
            var authorizationFilterContext = new AuthorizationFilterContext(actionContext, new List <IFilterMetadata>());
            var userAuthorizationFilter    = new UserAuthorizationFilter();

            //Act
            userAuthorizationFilter.OnAuthorization(authorizationFilterContext);

            //Assert
            Assert.NotNull(authorizationFilterContext.Result);
            var result = Assert.IsType <UnauthorizedResult>(authorizationFilterContext.Result);

            Assert.Equal(401, result.StatusCode);
        }