public void GetFiltersSortsFiltersByOrderFirstThenScope()
        {
            // Arrange
            var context = new ControllerContext();
            var descriptor = new Mock<ActionDescriptor>().Object;
            var actionFilter = new Filter(new Object(), FilterScope.Action, null);
            var controllerFilter = new Filter(new Object(), FilterScope.Controller, null);
            var globalFilter = new Filter(new Object(), FilterScope.Global, null);
            var earlyActionFilter = new Filter(new Object(), FilterScope.Action, -100);
            var lateGlobalFilter = new Filter(new Object(), FilterScope.Global, 100);
            var provider = new Mock<IFilterProvider>(MockBehavior.Strict);
            var collection = new FilterProviderCollection(new[] { provider.Object });
            provider.Setup(p => p.GetFilters(context, descriptor))
                .Returns(new[] { actionFilter, controllerFilter, globalFilter, earlyActionFilter, lateGlobalFilter });

            // Act
            Filter[] result = collection.GetFilters(context, descriptor).ToArray();

            // Assert
            Assert.Equal(5, result.Length);
            Assert.Same(earlyActionFilter, result[0]);
            Assert.Same(globalFilter, result[1]);
            Assert.Same(controllerFilter, result[2]);
            Assert.Same(actionFilter, result[3]);
            Assert.Same(lateGlobalFilter, result[4]);
        }
예제 #2
0
        public void GetFiltersIncludesLastFilterOnlyWithCustomFilterAllowMultipleFalse()
        { // DDB #222988
            // Arrange
            var context      = new ControllerContext();
            var descriptor   = new Mock <ActionDescriptor>().Object;
            var globalFilter = new Filter(
                new AllowMultipleCustomFilter(false),
                FilterScope.Global,
                null
                );
            var controllerFilter = new Filter(
                new AllowMultipleCustomFilter(false),
                FilterScope.Controller,
                null
                );
            var actionFilter = new Filter(
                new AllowMultipleCustomFilter(false),
                FilterScope.Action,
                null
                );
            var provider   = new Mock <IFilterProvider>(MockBehavior.Strict);
            var collection = new FilterProviderCollection(new[] { provider.Object });

            provider
            .Setup(p => p.GetFilters(context, descriptor))
            .Returns(new[] { controllerFilter, actionFilter, globalFilter });

            // Act
            IEnumerable <Filter> result = collection.GetFilters(context, descriptor);

            // Assert
            Assert.Same(actionFilter, result.Single());
        }
예제 #3
0
        public void GetFiltersSortsFiltersByOrderFirstThenScope()
        {
            // Arrange
            var context           = new ControllerContext();
            var descriptor        = new Mock <ActionDescriptor>().Object;
            var actionFilter      = new Filter(new Object(), FilterScope.Action, null);
            var controllerFilter  = new Filter(new Object(), FilterScope.Controller, null);
            var globalFilter      = new Filter(new Object(), FilterScope.Global, null);
            var earlyActionFilter = new Filter(new Object(), FilterScope.Action, -100);
            var lateGlobalFilter  = new Filter(new Object(), FilterScope.Global, 100);
            var provider          = new Mock <IFilterProvider>(MockBehavior.Strict);
            var collection        = new FilterProviderCollection(new[] { provider.Object });

            provider.Setup(p => p.GetFilters(context, descriptor))
            .Returns(new[] { actionFilter, controllerFilter, globalFilter, earlyActionFilter, lateGlobalFilter });

            // Act
            Filter[] result = collection.GetFilters(context, descriptor).ToArray();

            // Assert
            Assert.Equal(5, result.Length);
            Assert.Same(earlyActionFilter, result[0]);
            Assert.Same(globalFilter, result[1]);
            Assert.Same(controllerFilter, result[2]);
            Assert.Same(actionFilter, result[3]);
            Assert.Same(lateGlobalFilter, result[4]);
        }
        public void GuardClauses() {
            // Arrange
            var context = new ControllerContext();
            var descriptor = new Mock<ActionDescriptor>().Object;
            var collection = new FilterProviderCollection();

            // Act & Assert
            ExceptionHelper.ExpectArgumentNullException(
                () => collection.GetFilters(null, descriptor),
                "controllerContext"
            );
            ExceptionHelper.ExpectArgumentNullException(
                () => collection.GetFilters(context, null),
                "actionDescriptor"
            );
        }
        public IEnumerable <Filter> GetFilters(ControllerContext controllerContext,
                                               ActionDescriptor actionDescriptor)
        {
            var filters = _filterProviders.GetFilters(controllerContext, actionDescriptor);

            return(ExternalReview.IsInExternalReviewContext ? filters.Where(x => x.Instance.GetType() != _authorizeContent) : filters);
        }
예제 #6
0
        public void GetFiltersIncludesAllFiltersWithCustomFilterAllowMultipleTrue()
        { // DDB #222988
            // Arrange
            var context      = new ControllerContext();
            var descriptor   = new Mock <ActionDescriptor>().Object;
            var globalFilter = new Filter(
                new AllowMultipleCustomFilter(true),
                FilterScope.Global,
                null
                );
            var controllerFilter = new Filter(
                new AllowMultipleCustomFilter(true),
                FilterScope.Controller,
                null
                );
            var actionFilter = new Filter(
                new AllowMultipleCustomFilter(true),
                FilterScope.Action,
                null
                );
            var provider   = new Mock <IFilterProvider>(MockBehavior.Strict);
            var collection = new FilterProviderCollection(new[] { provider.Object });

            provider
            .Setup(p => p.GetFilters(context, descriptor))
            .Returns(new[] { controllerFilter, actionFilter, globalFilter });

            // Act
            List <Filter> result = collection.GetFilters(context, descriptor).ToList();

            // Assert
            Assert.Same(globalFilter, result[0]);
            Assert.Same(controllerFilter, result[1]);
            Assert.Same(actionFilter, result[2]);
        }
예제 #7
0
        public void GuardClauses()
        {
            // Arrange
            var context    = new ControllerContext();
            var descriptor = new Mock <ActionDescriptor>().Object;
            var collection = new FilterProviderCollection();

            // Act & Assert
            Assert.ThrowsArgumentNull(
                () => collection.GetFilters(null, descriptor),
                "controllerContext"
                );
            Assert.ThrowsArgumentNull(
                () => collection.GetFilters(context, null),
                "actionDescriptor"
                );
        }
            public IEnumerable <Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
            {
                var filters        = _filterProviders.GetFilters(controllerContext, actionDescriptor).ToList();
                var excludeFilters = new HashSet <Type>(filters.Select(filter => filter.Instance)
                                                        .OfType <ExcludeFilterAttribute>()
                                                        .SelectMany(filter => filter.FilterTypes));

                return(excludeFilters.Any() ? filters.Where(filter => !excludeFilters.Contains(filter.Instance.GetType())) : filters);
            }
    public IEnumerable <Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
    {
        var filters = _filterProviders.GetFilters(controllerContext, actionDescriptor).ToArray();
        var shouldOverrideCustomAuthorizeAttribute = filters.Any(filter => filter.Instance is OverrideCustomAuthorizeAttribute);

        if (shouldOverrideCustomAuthorizeAttribute)
        {
            // There is an OverrideCustomAuthorizeFilterAttribute present, remove all CustomAuthorizeAttributes from the list of filters
            return(filters.Where(filter => filter.Instance.GetType() != typeof(CustomAuthorizeAttribute)));
        }
        return(filters);
    }
        public IEnumerable <Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
        {
            Filter[] filters = _filterProviders.GetFilters(controllerContext, actionDescriptor).ToArray();

            List <ExcludeFiltersAttribute> excludeFilters = filters.Where(f => f.Instance is ExcludeFiltersAttribute)
                                                            .Select(f => f.Instance as ExcludeFiltersAttribute)
                                                            .ToList();

            List <Type> filtersToExclude = excludeFilters.SelectMany(excludeFilter => excludeFilter.Filters).ToList();

            List <Filter> filtersToApply = filters.Where(filter => !filtersToExclude.Contains(filter.Instance.GetType())).ToList();

            return(filtersToApply);
        }
        public IEnumerable <Filter> GetFilters(ControllerContext controllerContext,
                                               ActionDescriptor actionDescriptor)
        {
            var filters = _filterProviders.GetFilters(controllerContext, actionDescriptor);

            var projectId = _projectIdResolver.GetProjectId(controllerContext.HttpContext);

            if (projectId == null || projectId.Value == 0)
            {
                return(filters);
            }

            return(projectId > 0 ? filters.Where(x => x.Instance.GetType() != _authorizeContent) : filters);
        }
        public void GetFiltersUsesRegisteredProviders() {
            // Arrange
            var context = new ControllerContext();
            var descriptor = new Mock<ActionDescriptor>().Object;
            var filter = new Filter(new Object(), FilterScope.Action, null);
            var provider = new Mock<IFilterProvider>(MockBehavior.Strict);
            var collection = new FilterProviderCollection(new[] { provider.Object });
            provider.Setup(p => p.GetFilters(context, descriptor)).Returns(new[] { filter });

            // Act
            IEnumerable<Filter> result = collection.GetFilters(context, descriptor);

            // Assert
            Assert.AreSame(filter, result.Single());
        }
예제 #13
0
        public void GetFiltersUsesRegisteredProviders()
        {
            // Arrange
            var context    = new ControllerContext();
            var descriptor = new Mock <ActionDescriptor>().Object;
            var filter     = new Filter(new Object(), FilterScope.Action, null);
            var provider   = new Mock <IFilterProvider>(MockBehavior.Strict);
            var collection = new FilterProviderCollection(new[] { provider.Object });

            provider.Setup(p => p.GetFilters(context, descriptor)).Returns(new[] { filter });

            // Act
            IEnumerable <Filter> result = collection.GetFilters(context, descriptor);

            // Assert
            Assert.Same(filter, result.Single());
        }
        public void GetFiltersDelegatesToResolver()
        {
            // Arrange
            var context = new ControllerContext();
            var descriptor = new Mock<ActionDescriptor>().Object;
            var filter = new Filter(new Object(), FilterScope.Action, null);
            var provider = new Mock<IFilterProvider>(MockBehavior.Strict);
            var resolver = new Resolver<IEnumerable<IFilterProvider>> { Current = new[] { provider.Object } };
            var collection = new FilterProviderCollection(resolver);

            provider.Setup(p => p.GetFilters(context, descriptor)).Returns(new[] { filter });

            // Act
            IEnumerable<Filter> result = collection.GetFilters(context, descriptor);

            // Assert
            Assert.Same(filter, result.Single());
        }
        public void GetFiltersDelegatesToResolver()
        {
            // Arrange
            var context    = new ControllerContext();
            var descriptor = new Mock <ActionDescriptor>().Object;
            var filter     = new Filter(new Object(), FilterScope.Action, null);
            var provider   = new Mock <IFilterProvider>(MockBehavior.Strict);
            var resolver   = new Resolver <IEnumerable <IFilterProvider> > {
                Current = new[] { provider.Object }
            };
            var collection = new FilterProviderCollection(resolver);

            provider.Setup(p => p.GetFilters(context, descriptor)).Returns(new[] { filter });

            // Act
            IEnumerable <Filter> result = collection.GetFilters(context, descriptor);

            // Assert
            Assert.AreSame(filter, result.Single());
        }
예제 #16
0
        public void GetFiltersDelegatesToResolver()
        {
            // Arrange
            var context    = new ControllerContext();
            var descriptor = new Mock <ActionDescriptor>().Object;
            var filter     = new Filter(new Object(), FilterScope.Action, null);
            var provider   = new Mock <IFilterProvider>(MockBehavior.Strict);

            provider.Setup(p => p.GetFilters(context, descriptor)).Returns(new[] { filter });

            Mock <IDependencyResolver> resolver = new Mock <IDependencyResolver>();

            resolver.Setup(r => r.GetServices(typeof(IFilterProvider))).Returns(new[] { provider.Object });


            var collection = new FilterProviderCollection(new IFilterProvider[0], resolver.Object);

            // Act
            IEnumerable <Filter> result = collection.GetFilters(context, descriptor);

            // Assert
            Assert.Same(filter, result.Single());
        }
        public void GetFiltersIncludesAllFiltersWithCustomFilterAllowMultipleTrue()
        { // DDB #222988
            // Arrange
            var context = new ControllerContext();
            var descriptor = new Mock<ActionDescriptor>().Object;
            var globalFilter = new Filter(new AllowMultipleCustomFilter(true), FilterScope.Global, null);
            var controllerFilter = new Filter(new AllowMultipleCustomFilter(true), FilterScope.Controller, null);
            var actionFilter = new Filter(new AllowMultipleCustomFilter(true), FilterScope.Action, null);
            var provider = new Mock<IFilterProvider>(MockBehavior.Strict);
            var collection = new FilterProviderCollection(new[] { provider.Object });
            provider.Setup(p => p.GetFilters(context, descriptor))
                .Returns(new[] { controllerFilter, actionFilter, globalFilter });

            // Act
            List<Filter> result = collection.GetFilters(context, descriptor).ToList();

            // Assert
            Assert.Same(globalFilter, result[0]);
            Assert.Same(controllerFilter, result[1]);
            Assert.Same(actionFilter, result[2]);
        }
        public void GetFiltersIncludesLastFilterOnlyWithCustomFilterAllowMultipleFalse()
        { // DDB #222988
            // Arrange
            var context = new ControllerContext();
            var descriptor = new Mock<ActionDescriptor>().Object;
            var globalFilter = new Filter(new AllowMultipleCustomFilter(false), FilterScope.Global, null);
            var controllerFilter = new Filter(new AllowMultipleCustomFilter(false), FilterScope.Controller, null);
            var actionFilter = new Filter(new AllowMultipleCustomFilter(false), FilterScope.Action, null);
            var provider = new Mock<IFilterProvider>(MockBehavior.Strict);
            var collection = new FilterProviderCollection(new[] { provider.Object });
            provider.Setup(p => p.GetFilters(context, descriptor))
                .Returns(new[] { controllerFilter, actionFilter, globalFilter });

            // Act
            IEnumerable<Filter> result = collection.GetFilters(context, descriptor);

            // Assert
            Assert.Same(actionFilter, result.Single());
        }