public void GetFilters_WithNullController_ReturnsEmptyList() {
            // Arrange
            var context = new ControllerContext();
            var descriptor = new Mock<ActionDescriptor>().Object;
            var provider = new FilterAttributeFilterProvider();

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

            // Assert
            Assert.IsFalse(result.Any());
        }
        public void GetFilters_IncludesAttributesOnActionMethod() {
            // Arrange
            var context = new ControllerContext { Controller = new ControllerWithActionAttribute() };
            var controllerDescriptor = new ReflectedControllerDescriptor(context.Controller.GetType());
            var action = context.Controller.GetType().GetMethod("MyActionMethod");
            var actionDescriptor = new ReflectedActionDescriptor(action, "MyActionMethod", controllerDescriptor);
            var provider = new FilterAttributeFilterProvider();

            // Act
            Filter filter = provider.GetFilters(context, actionDescriptor).Single();

            // Assert
            MyFilterAttribute attrib = filter.Instance as MyFilterAttribute;
            Assert.IsNotNull(attrib);
            Assert.AreEqual(FilterScope.Action, filter.Scope);
            Assert.AreEqual(1234, filter.Order);
        }
        public void Integration_FilterAttributeFilterProvider_PassesThroughInstance()
        {
            // Arrange
            object expected = CreateProductUnderTest();
            FilterAttributeFilterProvider integrator = new FilterAttributeFilterProvider();
            ActionDescriptor actionDescriptor = CreateActionDescriptor(expected);
            ControllerContext controllerContext = CreateControllerContext();

            // Act
            IEnumerable<Filter> filters = integrator.GetFilters(controllerContext, actionDescriptor);

            // Assert
            Assert.NotNull(filters);
            Assert.Equal(1, filters.Count());
            Filter filter = filters.Single();
            Assert.NotNull(filter);
            Assert.Same(expected, filter.Instance);
        }
Exemple #4
0
        public void Integration_FilterAttributeFilterProvider_PassesThroughInstance()
        {
            // Arrange
            object expected = CreateProductUnderTest();
            FilterAttributeFilterProvider integrator = new FilterAttributeFilterProvider();
            ActionDescriptor  actionDescriptor       = CreateActionDescriptor(expected);
            ControllerContext controllerContext      = CreateControllerContext();

            // Act
            IEnumerable <Filter> filters = integrator.GetFilters(controllerContext, actionDescriptor);

            // Assert
            Assert.NotNull(filters);
            Assert.Equal(1, filters.Count());
            Filter filter = filters.Single();

            Assert.NotNull(filter);
            Assert.Same(expected, filter.Instance);
        }
Exemple #5
0
        public void Execute(IConfigsType dictData)
        {
            FilterAttributeFilterProvider item = FilterProviders.Providers.OfType <FilterAttributeFilterProvider>().FirstOrDefault();

            if (item != null)
            {
                FilterProviders.Providers.Remove(item);
            }
            FilterProviders.Providers.Add(new MvcFilterAttributeProvider());
            HandleErrorAttribute filter = GlobalFilters.Filters.OfType <HandleErrorAttribute>().FirstOrDefault();

            if (filter != null)
            {
                GlobalFilters.Filters.Remove(filter);
            }
            GlobalFilters.Filters.Add(new MvcHandleErrorAttribute());
            DependencyResolver.SetResolver(new MvcDependencyResolver());
            DynamicModuleUtility.RegisterModule(typeof(MvcApplactionHttpModule));
        }
        public void GetFilters_IncludesAttributesOnControllerType() {
            // Arrange
            var context = new ControllerContext { Controller = new ControllerWithTypeAttribute() };
            var controllerDescriptorMock = new Mock<ControllerDescriptor>();
            controllerDescriptorMock.Setup(cd => cd.GetCustomAttributes(typeof(FilterAttribute), true))
                                    .Returns(new FilterAttribute[] { new MyFilterAttribute { Order = 2112 } });
            var actionDescriptorMock = new Mock<ActionDescriptor>();
            actionDescriptorMock.Setup(ad => ad.ControllerDescriptor).Returns(controllerDescriptorMock.Object);
            var provider = new FilterAttributeFilterProvider();

            // Act
            Filter filter = provider.GetFilters(context, actionDescriptorMock.Object).Single();

            // Assert
            MyFilterAttribute attrib = filter.Instance as MyFilterAttribute;
            Assert.IsNotNull(attrib);
            Assert.AreEqual(FilterScope.Controller, filter.Scope);
            Assert.AreEqual(2112, filter.Order);
        }
Exemple #7
0
        public void GetFilters_IncludesAttributesOnActionMethod()
        {
            // Arrange
            var context = new ControllerContext {
                Controller = new ControllerWithActionAttribute()
            };
            var controllerDescriptor = new ReflectedControllerDescriptor(context.Controller.GetType());
            var action           = context.Controller.GetType().GetMethod("MyActionMethod");
            var actionDescriptor = new ReflectedActionDescriptor(action, "MyActionMethod", controllerDescriptor);
            var provider         = new FilterAttributeFilterProvider();

            // Act
            Filter filter = provider.GetFilters(context, actionDescriptor).Single();

            // Assert
            MyFilterAttribute attrib = filter.Instance as MyFilterAttribute;

            Assert.NotNull(attrib);
            Assert.Equal(FilterScope.Action, filter.Scope);
            Assert.Equal(1234, filter.Order);
        }
Exemple #8
0
        public void GetFilters_RetrievesNonCachedAttributesWhenConfiguredNotTo()
        {
            // Arrange
            var provider = new FilterAttributeFilterProvider(false);
            var context  = new ControllerContext {
                Controller = new DerivedController()
            };
            var controllerDescriptorMock = new Mock <ControllerDescriptor>();

            controllerDescriptorMock.Setup(cd => cd.GetFilterAttributes(false)).Returns(Enumerable.Empty <FilterAttribute>()).Verifiable();
            var actionDescriptorMock = new Mock <ActionDescriptor>();

            actionDescriptorMock.Setup(ad => ad.GetFilterAttributes(false)).Returns(Enumerable.Empty <FilterAttribute>()).Verifiable();
            actionDescriptorMock.Setup(ad => ad.ControllerDescriptor).Returns(controllerDescriptorMock.Object);

            // Act
            var result = provider.GetFilters(context, actionDescriptorMock.Object);

            // Assert
            controllerDescriptorMock.Verify();
            actionDescriptorMock.Verify();
        }
        public void GetFilters_RetrievesCachedAttributesByDefault()
        {
            // Arrange
            var provider = new FilterAttributeFilterProvider();
            var context  = new ControllerContext {
                Controller = new DerivedController()
            };
            var controllerDescriptorMock = new Mock <TestableControllerDescriptor>();

            controllerDescriptorMock.Setup(cd => cd.GetFilterAttributesPublic(true)).Returns(Enumerable.Empty <FilterAttribute>()).Verifiable();
            var actionDescriptorMock = new Mock <TestableActionDescriptor>();

            actionDescriptorMock.Setup(ad => ad.GetFilterAttributesPublic(true)).Returns(Enumerable.Empty <FilterAttribute>()).Verifiable();
            actionDescriptorMock.Setup(ad => ad.ControllerDescriptor).Returns(controllerDescriptorMock.Object);

            // Act
            var result = provider.GetFilters(context, actionDescriptorMock.Object);

            // Assert
            controllerDescriptorMock.Verify();
            actionDescriptorMock.Verify();
        }
Exemple #10
0
        public void GetFilters_IncludesTypeAttributesFromDerivedTypeWhenMethodIsOnBaseClass()
        { // DDB #208062
            // Arrange
            var context = new ControllerContext {
                Controller = new DerivedController()
            };
            var controllerDescriptor = new ReflectedControllerDescriptor(
                context.Controller.GetType()
                );
            var action           = context.Controller.GetType().GetMethod("MyActionMethod");
            var actionDescriptor = new ReflectedActionDescriptor(
                action,
                "MyActionMethod",
                controllerDescriptor
                );
            var provider = new FilterAttributeFilterProvider();

            // Act
            IEnumerable <Filter> filters = provider.GetFilters(context, actionDescriptor);

            // Assert
            Assert.NotNull(filters.Select(f => f.Instance).Cast <MyFilterAttribute>().Single());
        }
        public void GetFilters_IncludesTypeAttributesFromDerivedTypeWhenMethodIsOnBaseClass()
        { // DDB #208062
            // Arrange
            var context = new ControllerContext { Controller = new DerivedController() };
            var controllerDescriptor = new ReflectedControllerDescriptor(context.Controller.GetType());
            var action = context.Controller.GetType().GetMethod("MyActionMethod");
            var actionDescriptor = new ReflectedActionDescriptor(action, "MyActionMethod", controllerDescriptor);
            var provider = new FilterAttributeFilterProvider();

            // Act
            IEnumerable<Filter> filters = provider.GetFilters(context, actionDescriptor);

            // Assert
            Assert.NotNull(filters.Select(f => f.Instance).Cast<MyFilterAttribute>().Single());
        }
        public void GetFilters_RetrievesNonCachedAttributesWhenConfiguredNotTo()
        {
            // Arrange
            var provider = new FilterAttributeFilterProvider(false);
            var context = new ControllerContext { Controller = new DerivedController() };
            var controllerDescriptorMock = new Mock<ControllerDescriptor>();
            controllerDescriptorMock.Setup(cd => cd.GetFilterAttributes(false)).Returns(Enumerable.Empty<FilterAttribute>()).Verifiable();
            var actionDescriptorMock = new Mock<ActionDescriptor>();
            actionDescriptorMock.Setup(ad => ad.GetFilterAttributes(false)).Returns(Enumerable.Empty<FilterAttribute>()).Verifiable();
            actionDescriptorMock.Setup(ad => ad.ControllerDescriptor).Returns(controllerDescriptorMock.Object);

            // Act
            var result = provider.GetFilters(context, actionDescriptorMock.Object);

            // Assert
            controllerDescriptorMock.Verify();
            actionDescriptorMock.Verify();
        }
        public void GetFilters_RetrievesCachedAttributesByDefault() {
            // Arrange
            var provider = new FilterAttributeFilterProvider();
            var context = new ControllerContext { Controller = new DerivedController()};
            var controllerDescriptorMock = new Mock<TestableControllerDescriptor>();
            controllerDescriptorMock.Setup(cd => cd.GetFilterAttributesPublic(true)).Returns(Enumerable.Empty<FilterAttribute>()).Verifiable();
            var actionDescriptorMock = new Mock<TestableActionDescriptor>();
            actionDescriptorMock.Setup(ad => ad.GetFilterAttributesPublic(true)).Returns(Enumerable.Empty<FilterAttribute>()).Verifiable();
            actionDescriptorMock.Setup(ad => ad.ControllerDescriptor).Returns(controllerDescriptorMock.Object);

            // Act
            var result = provider.GetFilters(context, actionDescriptorMock.Object);

            // Assert
            controllerDescriptorMock.Verify();
            actionDescriptorMock.Verify();
        }