Beispiel #1
0
        public void InitializeFilterFactory_WithExpectedPropertyHelpers_ForTempDataAttributeProperties()
        {
            // Arrange
            var expected        = typeof(TestController_OneTempDataProperty).GetProperty(nameof(TestController_OneTempDataProperty.Test2));
            var options         = Options.Create(new MvcViewOptions());
            var provider        = new TempDataApplicationModelProvider(options);
            var defaultProvider = new DefaultApplicationModelProvider(
                Options.Create(new MvcOptions()),
                TestModelMetadataProvider.CreateDefaultProvider());

            var context = new ApplicationModelProviderContext(new[] { typeof(TestController_OneTempDataProperty).GetTypeInfo() });

            defaultProvider.OnProvidersExecuting(context);

            // Act
            provider.OnProvidersExecuting(context);
            var controller = context.Result.Controllers.SingleOrDefault();
            var filter     = Assert.IsType <ControllerSaveTempDataPropertyFilterFactory>(Assert.Single(controller.Filters));

            // Assert
            Assert.NotNull(filter);
            var property = Assert.Single(filter.TempDataProperties);

            Assert.Same(expected, property.PropertyInfo);
            Assert.Equal("TempDataProperty-Test2", property.Key);
        }
        public void OnProvidersExecuting_SetsKeyPrefixToEmptyString_IfCompatSwitchIsSet()
        {
            // Arrange
            var expected = typeof(TestController_OneTempDataProperty).GetProperty(nameof(TestController_OneTempDataProperty.Test2));
            var options  = Options.Create(new MvcViewOptions {
                SuppressTempDataAttributePrefix = true
            });
            var provider        = new TempDataApplicationModelProvider(options);
            var defaultProvider = new DefaultApplicationModelProvider(Options.Create(new MvcOptions()));

            var context = new ApplicationModelProviderContext(new[] { typeof(TestController_OneTempDataProperty).GetTypeInfo() });

            defaultProvider.OnProvidersExecuting(context);

            // Act
            provider.OnProvidersExecuting(context);
            var controller = context.Result.Controllers.SingleOrDefault();
            var filter     = Assert.IsType <ControllerSaveTempDataPropertyFilterFactory>(Assert.Single(controller.Filters));

            // Assert
            Assert.NotNull(filter);
            var property = Assert.Single(filter.TempDataProperties);

            Assert.Same(expected, property.PropertyInfo);
            Assert.Equal("Test2", property.Key);
        }
        public void OnProvidersExecuting_ValidatesTempDataProperties()
        {
            // Arrange
            var type     = typeof(TestController_PrivateSet);
            var options  = Options.Create(new MvcViewOptions());
            var provider = new TempDataApplicationModelProvider(options);
            var expected = $"The '{type.FullName}.Test' property with TempDataAttribute is invalid. A property using TempDataAttribute must have a public getter and setter.";

            var context = GetContext(type);

            // Act & Assert
            var ex = Assert.Throws <InvalidOperationException>(() => provider.OnProvidersExecuting(context));

            Assert.Equal(expected, ex.Message);
        }
        public void ThrowsInvalidOperationException_NonPrimitiveType()
        {
            // Arrange
            var provider        = new TempDataApplicationModelProvider();
            var defaultProvider = new DefaultApplicationModelProvider(new TestOptionsManager <MvcOptions>());

            var context = new ApplicationModelProviderContext(new[] { typeof(TestController_NonPrimitiveType).GetTypeInfo() });

            defaultProvider.OnProvidersExecuting(context);

            // Act & Assert
            var exception = Assert.Throws <InvalidOperationException>(() =>
                                                                      provider.OnProvidersExecuting(context));

            Assert.Equal($"The '{typeof(TestController_NonPrimitiveType).FullName}.{nameof(TestController_NonPrimitiveType.Test)}' property with {nameof(TempDataAttribute)} is invalid. A property using {nameof(TempDataAttribute)} must be of primitive or string type.", exception.Message);
        }
Beispiel #5
0
        public void DoesNotInitializeFilterFactory_ThrowsInvalidOperationException_NonPrimitiveType()
        {
            // Arrange
            var provider        = new TempDataApplicationModelProvider();
            var defaultProvider = new DefaultApplicationModelProvider(Options.Create(new MvcOptions()));

            var context = new ApplicationModelProviderContext(new[] { typeof(TestController_OneValid_OneInvalidProperty).GetTypeInfo() });

            defaultProvider.OnProvidersExecuting(context);

            // Act
            var exception = Assert.Throws <InvalidOperationException>(() =>
                                                                      provider.OnProvidersExecuting(context));

            Assert.Equal($"The '{typeof(TestController_OneValid_OneInvalidProperty).FullName}.{nameof(TestController_OneValid_OneInvalidProperty.Test2)}' property with {nameof(TempDataAttribute)} is invalid. A property using {nameof(TempDataAttribute)} must be a primitive or string type.", exception.Message);
        }
        public void AddsTempDataPropertyFilter_ForTempDataAttributeProperties()
        {
            // Arrange
            var type     = typeof(TestController_NullableNonPrimitiveTempDataProperty);
            var options  = Options.Create(new MvcViewOptions());
            var provider = new TempDataApplicationModelProvider(options);

            var context = GetContext(type);

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            var controller = Assert.Single(context.Result.Controllers);

            Assert.IsType <ControllerSaveTempDataPropertyFilterFactory>(Assert.Single(controller.Filters));
        }
        public void OnProvidersExecuting_DoesNotAddFilter_IfTypeHasNoTempDataProperties()
        {
            // Arrange
            var type     = typeof(TestController_NoTempDataProperties);
            var options  = Options.Create(new MvcViewOptions());
            var provider = new TempDataApplicationModelProvider(options);

            var context = GetContext(type);

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            var controller = Assert.Single(context.Result.Controllers);

            Assert.Empty(controller.Filters);
        }
        public void AddsTempDataPropertyFilter_ForTempDataAttributeProperties(Type type)
        {
            // Arrange
            var provider        = new TempDataApplicationModelProvider();
            var defaultProvider = new DefaultApplicationModelProvider(new TestOptionsManager <MvcOptions>());

            var context = new ApplicationModelProviderContext(new[] { type.GetTypeInfo() });

            defaultProvider.OnProvidersExecuting(context);

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            var controller = Assert.Single(context.Result.Controllers);

            Assert.Single(controller.Filters, f => f is ControllerSaveTempDataPropertyFilterFactory);
        }
Beispiel #9
0
        public void ThrowsInvalidOperationException_ForNullableNonPrimitiveType()
        {
            // Arrange
            var provider        = new TempDataApplicationModelProvider();
            var defaultProvider = new DefaultApplicationModelProvider(Options.Create(new MvcOptions()));
            var controllerType  = typeof(TestController_NullableNonPrimitiveTempDataProperty);
            var context         = new ApplicationModelProviderContext(new[] { controllerType.GetTypeInfo() });

            defaultProvider.OnProvidersExecuting(context);

            // Act & Assert
            var exception = Assert.Throws <InvalidOperationException>(() =>
                                                                      provider.OnProvidersExecuting(context));

            Assert.Equal($"The '{controllerType.FullName}.{nameof(TestController_NullableNonPrimitiveTempDataProperty.DateTime)}'"
                         + $" property with {nameof(TempDataAttribute)} is invalid. A property using {nameof(TempDataAttribute)} "
                         + $"must be a primitive or string type.", exception.Message);
        }
        public void ThrowsInvalidOperationException_NonPrimitiveType()
        {
            // Arrange
            var provider        = new TempDataApplicationModelProvider();
            var defaultProvider = new DefaultApplicationModelProvider(Options.Create(new MvcOptions()));

            var context = new ApplicationModelProviderContext(new[] { typeof(TestController_NonPrimitiveType).GetTypeInfo() });

            defaultProvider.OnProvidersExecuting(context);

            // Act & Assert
            var exception = Assert.Throws <InvalidOperationException>(() =>
                                                                      provider.OnProvidersExecuting(context));

            Assert.Equal(
                $"The '{typeof(TestController_NonPrimitiveType).FullName}.{nameof(TestController_NonPrimitiveType.Test)}'"
                + $" property with {nameof(TempDataAttribute)} is invalid. The '{typeof(TempDataSerializer).FullName}'"
                + $" cannot serialize an object of type '{typeof(Object).FullName}'.",
                exception.Message);
        }
        public void ThrowsInvalidOperationException_PrivateSetter()
        {
            // Arrange
            var provider        = new TempDataApplicationModelProvider();
            var defaultProvider = new DefaultApplicationModelProvider(Options.Create(new MvcOptions()));

            var context = new ApplicationModelProviderContext(new[] { typeof(TestController_PrivateSet).GetTypeInfo() });

            defaultProvider.OnProvidersExecuting(context);

            // Act & Assert
            var exception = Assert.Throws <InvalidOperationException>(() =>
                                                                      provider.OnProvidersExecuting(context));

            Assert.Equal(
                $"The '{typeof(TestController_PrivateSet).FullName}.{nameof(TestController_NonPrimitiveType.Test)}'"
                + $" property with {nameof(TempDataAttribute)} is invalid. A property using {nameof(TempDataAttribute)}"
                + " must have a public getter and setter.",
                exception.Message);
        }
Beispiel #12
0
        public void OnProvidersExecuting_DoesNotAddFilter_IfTypeHasNoTempDataProperties()
        {
            // Arrange
            var type            = typeof(TestController_NoTempDataProperties);
            var options         = Options.Create(new MvcViewOptions());
            var provider        = new TempDataApplicationModelProvider(options);
            var defaultProvider = new DefaultApplicationModelProvider(
                Options.Create(new MvcOptions()),
                TestModelMetadataProvider.CreateDefaultProvider());

            var context = new ApplicationModelProviderContext(new[] { type.GetTypeInfo() });

            defaultProvider.OnProvidersExecuting(context);

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            var controller = Assert.Single(context.Result.Controllers);

            Assert.Empty(controller.Filters);
        }
        public void InitializeFilterFactory_WithExpectedPropertyHelpers_ForTempDataAttributeProperties()
        {
            // Arrange
            var provider        = new TempDataApplicationModelProvider();
            var defaultProvider = new DefaultApplicationModelProvider(new TestOptionsManager <MvcOptions>());

            var context = new ApplicationModelProviderContext(new[] { typeof(TestController_OneTempDataProperty).GetTypeInfo() });

            defaultProvider.OnProvidersExecuting(context);

            // Act
            provider.OnProvidersExecuting(context);
            var controller = context.Result.Controllers.SingleOrDefault();
            var filter     = controller.Filters.OfType <ControllerSaveTempDataPropertyFilterFactory>();
            var saveTempDataPropertyFilterFactory = filter.SingleOrDefault();
            var expected = typeof(TestController_OneTempDataProperty).GetProperty(nameof(TestController_OneTempDataProperty.Test2));

            // Assert
            Assert.NotNull(saveTempDataPropertyFilterFactory);
            var tempDataPropertyHelper = Assert.Single(saveTempDataPropertyFilterFactory.TempDataProperties);

            Assert.Same(expected, tempDataPropertyHelper.PropertyInfo);
        }