コード例 #1
0
        public void GetFilters_IncludesAttributesOnControllerType()
        {
            // Arrange
            var context = new ControllerContext {
                Controller = new ControllerWithTypeAttribute()
            };
            var controllerDescriptorMock = new Mock <ControllerDescriptor>();

            controllerDescriptorMock.Setup(cd => cd.GetFilterAttributes(It.IsAny <bool>()))
            .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.NotNull(attrib);
            Assert.Equal(FilterScope.Controller, filter.Scope);
            Assert.Equal(2112, filter.Order);
        }
コード例 #2
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).ToList();

            // Assert
            controllerDescriptorMock.Verify();
            actionDescriptorMock.Verify();
        }
コード例 #3
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);
        }
コード例 #4
0
        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());
        }
コード例 #5
0
        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.Empty(result);
        }
コード例 #6
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());
        }
コード例 #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.IsNotNull(attrib);
            Assert.AreEqual(FilterScope.Action, filter.Scope);
            Assert.AreEqual(1234, filter.Order);
        }
コード例 #8
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);
            Filter filter = Assert.Single(filters);

            Assert.NotNull(filter);
            Assert.Same(expected, filter.Instance);
        }
コード例 #9
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);
        }
コード例 #10
0
        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);
        }
コード例 #11
0
        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();
        }
コード例 #12
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());
        }
コード例 #13
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();
        }
コード例 #14
0
        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();
        }