public async Task BindModel_ThousandsSeparators_LeadToErrors(Type type)
        {
            // Arrange
            var bindingContext = GetBindingContext(type);

            bindingContext.ValueProvider = new SimpleValueProvider(new CultureInfo("en-GB"))
            {
                { "theModelName", "32,000" }
            };

            var binder = new SimpleTypeModelBinder(type, NullLoggerFactory.Instance);

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.False(bindingContext.Result.IsModelSet);

            var entry = Assert.Single(bindingContext.ModelState);

            Assert.Equal("theModelName", entry.Key);
            Assert.Equal("32,000", entry.Value.AttemptedValue);
            Assert.Equal(ModelValidationState.Invalid, entry.Value.ValidationState);

            var error = Assert.Single(entry.Value.Errors);

            Assert.Equal("The value '32,000' is not valid.", error.ErrorMessage);
            Assert.Null(error.Exception);
        }
        public async Task BindModel_ReturnsProvidedWhitespaceString_WhenNotConvertEmptyStringToNull(string value)
        {
            // Arrange
            var bindingContext = GetBindingContext(typeof(string));

            bindingContext.ValueProvider = new SimpleValueProvider
            {
                { "theModelName", value }
            };

            var metadataProvider = new TestModelMetadataProvider();

            metadataProvider
            .ForType(typeof(string))
            .DisplayDetails(d => d.ConvertEmptyStringToNull = false);
            bindingContext.ModelMetadata = metadataProvider.GetMetadataForType(typeof(string));

            var binder = new SimpleTypeModelBinder(typeof(string), NullLoggerFactory.Instance);

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.Same(value, bindingContext.Result.Model);
            Assert.True(bindingContext.ModelState.ContainsKey("theModelName"));
        }
        public AdjustToTimeZoneModelBinder(Type type, string timeZoneIdPropertyName)
        {
            if (type == null) throw new ArgumentNullException(nameof(type));

            //_attribute = attribute as IScrubberAttribute;
            _baseBinder = new SimpleTypeModelBinder(type);
            _timeZoneIdPropertyName = timeZoneIdPropertyName;
        }
Пример #4
0
        public async Task BindModel_EmptyValueProviderResult_ReturnsFailed()
        {
            // Arrange
            var bindingContext = GetBindingContext(typeof(int));
            var binder         = new SimpleTypeModelBinder(typeof(int));

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.Equal(ModelBindingResult.Failed(), bindingContext.Result);
            Assert.Empty(bindingContext.ModelState);
        }
        public async Task BindModel_ReturnsFailure_IfTypeCanBeConverted_AndConversionFails(Type destinationType)
        {
            // Arrange
            var bindingContext = GetBindingContext(destinationType);

            bindingContext.ValueProvider = new SimpleValueProvider
            {
                { "theModelName", "some-value" }
            };

            var binder = new SimpleTypeModelBinder(destinationType, NullLoggerFactory.Instance);

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.False(bindingContext.Result.IsModelSet);
        }
        public async Task BindModel_ReturnsFailure_IfTypeCanBeConverted_AndConversionFails(Type destinationType)
        {
            // Arrange
            var bindingContext = GetBindingContext(destinationType);
            bindingContext.ValueProvider = new SimpleValueProvider
            {
                { "theModelName", "some-value" }
            };

            var binder = new SimpleTypeModelBinder();

            // Act
            var result = await binder.BindModelResultAsync(bindingContext);

            // Assert
            Assert.NotEqual(default(ModelBindingResult), result);
            Assert.False(result.IsModelSet);
        }
        public async Task BindModel_ReturnsNull_IfTrimmedValueIsEmptyString(object value)
        {
            // Arrange
            var bindingContext = GetBindingContext(typeof(string));

            bindingContext.ValueProvider = new SimpleValueProvider
            {
                { "theModelName", value }
            };

            var binder = new SimpleTypeModelBinder(typeof(string), NullLoggerFactory.Instance);

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.Null(bindingContext.Result.Model);
            Assert.True(bindingContext.ModelState.ContainsKey("theModelName"));
        }
        public async Task BindModel_ReturnsFailure_IfTypeCanBeConverted_AndConversionFails(Type destinationType)
        {
            // Arrange
            var bindingContext = GetBindingContext(destinationType);

            bindingContext.ValueProvider = new SimpleValueProvider
            {
                { "theModelName", "some-value" }
            };

            var binder = new SimpleTypeModelBinder(destinationType);

            // Act
            var result = await binder.BindModelResultAsync(bindingContext);

            // Assert
            Assert.NotEqual(default(ModelBindingResult), result);
            Assert.False(result.IsModelSet);
        }
        public async Task BindModel_ValidValueProviderResult_ConvertEmptyStringsToNull()
        {
            // Arrange
            var bindingContext = GetBindingContext(typeof(string));

            bindingContext.ValueProvider = new SimpleValueProvider
            {
                { "theModelName", string.Empty }
            };

            var binder = new SimpleTypeModelBinder(typeof(string));

            // Act
            var result = await binder.BindModelResultAsync(bindingContext);

            // Assert
            Assert.Null(result.Model);
            Assert.True(bindingContext.ModelState.ContainsKey("theModelName"));
        }
        public async Task BindModel_EmptyValueProviderResult_ReturnsFailedAndLogsSuccessfully(ModelMetadata metadata)
        {
            // Arrange
            var bindingContext = GetBindingContext(typeof(int));

            bindingContext.ModelMetadata = metadata;

            var sink          = new TestSink();
            var loggerFactory = new TestLoggerFactory(sink, enabled: true);
            var binder        = new SimpleTypeModelBinder(typeof(int), loggerFactory);

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.Equal(ModelBindingResult.Failed(), bindingContext.Result);
            Assert.Empty(bindingContext.ModelState);
            Assert.Equal(2, sink.Writes.Count());
        }
Пример #11
0
        public async Task BindModelAsync_ReturnsProvidedString(string value)
        {
            // Arrange
            var bindingContext = GetBindingContext(typeof(string));

            bindingContext.ValueProvider = new SimpleValueProvider
            {
                { "theModelName", value }
            };

            var binder = new SimpleTypeModelBinder(typeof(string));

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.Same(value, bindingContext.Result.Model);
            Assert.True(bindingContext.ModelState.ContainsKey("theModelName"));
        }
        public async Task BindModel_NullableDoubleValueProviderResult_ReturnsModel()
        {
            // Arrange
            var bindingContext = GetBindingContext(typeof(double?));

            bindingContext.ValueProvider = new SimpleValueProvider
            {
                { "theModelName", "12.5" }
            };

            var binder = new SimpleTypeModelBinder(typeof(double?), NullLoggerFactory.Instance);

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.True(bindingContext.Result.IsModelSet);
            Assert.Equal(12.5, bindingContext.Result.Model);
            Assert.True(bindingContext.ModelState.ContainsKey("theModelName"));
        }
Пример #13
0
        public async Task BindModel_ValidValueProviderResult_ReturnsModel()
        {
            // Arrange
            var bindingContext = GetBindingContext(typeof(int));

            bindingContext.ValueProvider = new SimpleValueProvider
            {
                { "theModelName", "42" }
            };

            var binder = new SimpleTypeModelBinder(typeof(int));

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.True(bindingContext.Result.IsModelSet);
            Assert.Equal(42, bindingContext.Result.Model);
            Assert.True(bindingContext.ModelState.ContainsKey("theModelName"));
        }
        public async Task BindModel_ValidValueProviderResultWithProvidedCulture_ReturnsModel()
        {
            // Arrange
            var bindingContext = GetBindingContext(typeof(decimal));

            bindingContext.ValueProvider = new SimpleValueProvider(new CultureInfo("fr-FR"))
            {
                { "theModelName", "12,5" }
            };

            var binder = new SimpleTypeModelBinder(typeof(decimal), NullLoggerFactory.Instance);

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.True(bindingContext.Result.IsModelSet);
            Assert.Equal(12.5M, bindingContext.Result.Model);
            Assert.True(bindingContext.ModelState.ContainsKey("theModelName"));
        }
        public async Task BindModel_NullableIntegerValueProviderResult_ReturnsModel()
        {
            // Arrange
            var bindingContext = GetBindingContext(typeof(int?));

            bindingContext.ValueProvider = new SimpleValueProvider
            {
                { "theModelName", "12" }
            };

            var binder = new SimpleTypeModelBinder(typeof(int?));

            // Act
            var result = await binder.BindModelResultAsync(bindingContext);

            // Assert
            Assert.True(result.IsModelSet);
            Assert.Equal(12, result.Model);
            Assert.True(bindingContext.ModelState.ContainsKey("theModelName"));
        }
        public async Task BindModel_BindsEnumModels_IfArrayElementIsStringValue()
        {
            // Arrange
            var bindingContext = GetBindingContext(typeof(IntEnum));

            bindingContext.ValueProvider = new SimpleValueProvider
            {
                { "theModelName", new object[] { "1" } }
            };

            var binder = new SimpleTypeModelBinder(typeof(IntEnum), NullLoggerFactory.Instance);

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.True(bindingContext.Result.IsModelSet);
            var boundModel = Assert.IsType <IntEnum>(bindingContext.Result.Model);

            Assert.Equal(IntEnum.Value1, boundModel);
        }
        public async Task BindModel_BindsEnumModels_IfArrayElementIsStringKey()
        {
            // Arrange
            var bindingContext = GetBindingContext(typeof(IntEnum));

            bindingContext.ValueProvider = new SimpleValueProvider
            {
                { "theModelName", new object[] { "Value1" } }
            };

            var binder = new SimpleTypeModelBinder(typeof(IntEnum));

            // Act
            var result = await binder.BindModelResultAsync(bindingContext);

            // Assert
            Assert.True(result.IsModelSet);
            var boundModel = Assert.IsType <IntEnum>(result.Model);

            Assert.Equal(IntEnum.Value1, boundModel);
        }
        public async Task BindModel_BindsFlagsEnumModels(string flagsEnumValue, int expected)
        {
            // Arrange
            var bindingContext = GetBindingContext(typeof(FlagsEnum));

            bindingContext.ValueProvider = new SimpleValueProvider
            {
                { "theModelName", flagsEnumValue }
            };

            var binder = new SimpleTypeModelBinder(typeof(FlagsEnum), NullLoggerFactory.Instance);

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.True(bindingContext.Result.IsModelSet);
            var boundModel = Assert.IsType <FlagsEnum>(bindingContext.Result.Model);

            Assert.Equal((FlagsEnum)expected, boundModel);
        }
Пример #19
0
        public async Task BindModel_CreatesError_WhenTypeConversionIsNull(Type destinationType)
        {
            // Arrange
            var bindingContext = GetBindingContext(destinationType);
            bindingContext.ValueProvider = new SimpleValueProvider
            {
                { "theModelName", string.Empty }
            };
            var binder = new SimpleTypeModelBinder();

            // Act
            var result = await binder.BindModelResultAsync(bindingContext);

            // Assert
            Assert.False(result.IsModelSet);
            Assert.Null(result.Model);

            var error = Assert.Single(bindingContext.ModelState["theModelName"].Errors);
            Assert.Equal("The value '' is invalid.", error.ErrorMessage, StringComparer.Ordinal);
            Assert.Null(error.Exception);
        }
        public async Task BindModel_CreatesError_WhenTypeConversionIsNull(Type destinationType)
        {
            // Arrange
            var bindingContext = GetBindingContext(destinationType);

            bindingContext.ValueProvider = new SimpleValueProvider
            {
                { "theModelName", string.Empty }
            };
            var binder = new SimpleTypeModelBinder(destinationType, NullLoggerFactory.Instance);

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.False(bindingContext.Result.IsModelSet);
            Assert.Null(bindingContext.Result.Model);

            var error = Assert.Single(bindingContext.ModelState["theModelName"].Errors);

            Assert.Equal("The value '' is invalid.", error.ErrorMessage, StringComparer.Ordinal);
            Assert.Null(error.Exception);
        }
        public async Task BindModel_CreatesErrorForFormatException_ValueProviderResultWithInvalidCulture()
        {
            // Arrange
            var bindingContext = GetBindingContext(typeof(decimal));

            bindingContext.ValueProvider = new SimpleValueProvider(new CultureInfo("en-GB"))
            {
                { "theModelName", "12,5" }
            };

            var binder = new SimpleTypeModelBinder(typeof(decimal), NullLoggerFactory.Instance);

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.False(bindingContext.Result.IsModelSet);
            Assert.Null(bindingContext.Result.Model);

            var error = Assert.Single(bindingContext.ModelState["theModelName"].Errors);

            Assert.Equal("The value '12,5' is not valid.", error.ErrorMessage, StringComparer.Ordinal);
            Assert.Null(error.Exception);
        }
Пример #22
0
        public async Task BindModel_Error_FormatExceptionsTurnedIntoStringsInModelState()
        {
            // Arrange
            var message        = "The value 'not an integer' is not valid for Int32.";
            var bindingContext = GetBindingContext(typeof(int));

            bindingContext.ValueProvider = new SimpleValueProvider
            {
                { "theModelName", "not an integer" }
            };

            var binder = new SimpleTypeModelBinder();

            // Act
            var result = await binder.BindModelResultAsync(bindingContext);

            // Assert
            Assert.NotEqual(default(ModelBindingResult), result);
            Assert.Null(result.Model);
            Assert.False(bindingContext.ModelState.IsValid);
            var error = Assert.Single(bindingContext.ModelState["theModelName"].Errors);

            Assert.Equal(message, error.ErrorMessage);
        }
        public async Task BindModel_ValidValueProviderResult_ReturnsModelAndLogsSuccessfully(ModelMetadata metadata)
        {
            // Arrange
            var bindingContext = GetBindingContext(typeof(int));

            bindingContext.ModelMetadata = metadata;
            bindingContext.ValueProvider = new SimpleValueProvider
            {
                { "theModelName", "42" }
            };

            var sink          = new TestSink();
            var loggerFactory = new TestLoggerFactory(sink, enabled: true);
            var binder        = new SimpleTypeModelBinder(typeof(int), loggerFactory);

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.True(bindingContext.Result.IsModelSet);
            Assert.Equal(42, bindingContext.Result.Model);
            Assert.True(bindingContext.ModelState.ContainsKey("theModelName"));
            Assert.Equal(2, sink.Writes.Count());
        }
        public async Task BindModel_Error_FormatExceptionsTurnedIntoStringsInModelState()
        {
            // Arrange
            var message        = "The value 'not an integer' is not valid.";
            var bindingContext = GetBindingContext(typeof(int));

            bindingContext.ValueProvider = new SimpleValueProvider
            {
                { "theModelName", "not an integer" }
            };

            var binder = new SimpleTypeModelBinder(typeof(int), NullLoggerFactory.Instance);

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.False(bindingContext.Result.IsModelSet);
            Assert.Null(bindingContext.Result.Model);
            Assert.False(bindingContext.ModelState.IsValid);
            var error = Assert.Single(bindingContext.ModelState["theModelName"].Errors);

            Assert.Equal(message, error.ErrorMessage);
        }
Пример #25
0
        public async Task BindModel_ValidValueProviderResult_ReturnsModel()
        {
            // Arrange
            var bindingContext = GetBindingContext(typeof(int));
            bindingContext.ValueProvider = new SimpleValueProvider
            {
                { "theModelName", "42" }
            };

            var binder = new SimpleTypeModelBinder();

            // Act
            var result = await binder.BindModelResultAsync(bindingContext);

            // Assert
            Assert.True(result.IsModelSet);
            Assert.Equal(42, result.Model);
            Assert.True(bindingContext.ModelState.ContainsKey("theModelName"));
        }
Пример #26
0
        public async Task BindModel_Error_FormatExceptionsTurnedIntoStringsInModelState()
        {
            // Arrange
            var message = "The value 'not an integer' is not valid for Int32.";
            var bindingContext = GetBindingContext(typeof(int));
            bindingContext.ValueProvider = new SimpleValueProvider
            {
                { "theModelName", "not an integer" }
            };

            var binder = new SimpleTypeModelBinder();

            // Act
            var result = await binder.BindModelResultAsync(bindingContext);

            // Assert
            Assert.NotEqual(default(ModelBindingResult), result);
            Assert.Null(result.Model);
            Assert.False(bindingContext.ModelState.IsValid);
            var error = Assert.Single(bindingContext.ModelState["theModelName"].Errors);
            Assert.Equal(message, error.ErrorMessage);
        }
 public AbpDateTimeModelBinder(Type type)
 {
     _type = type;
     _simpleTypeModelBinder = new SimpleTypeModelBinder(type);
 }
Пример #28
0
        public async Task BindModel_ValidValueProviderResult_ConvertEmptyStringsToNull()
        {
            // Arrange
            var bindingContext = GetBindingContext(typeof(string));
            bindingContext.ValueProvider = new SimpleValueProvider
            {
                { "theModelName", string.Empty }
            };

            var binder = new SimpleTypeModelBinder();

            // Act
            var result = await binder.BindModelResultAsync(bindingContext);

            // Assert
            Assert.Null(result.Model);
            Assert.True(bindingContext.ModelState.ContainsKey("theModelName"));
        }
Пример #29
0
        public async Task BindModel_EmptyValueProviderResult_ReturnsFailed()
        {
            // Arrange
            var bindingContext = GetBindingContext(typeof(int));
            var binder = new SimpleTypeModelBinder();

            // Act
            var result = await binder.BindModelResultAsync(bindingContext);

            // Assert
            Assert.Equal(ModelBindingResult.Failed("theModelName"), result);
            Assert.Empty(bindingContext.ModelState);
        }
Пример #30
0
        public async Task BindModel_BindsFlagsEnumModels(string flagsEnumValue, int expected)
        {
            // Arrange
            var bindingContext = GetBindingContext(typeof(FlagsEnum));
            bindingContext.ValueProvider = new SimpleValueProvider
            {
                { "theModelName", flagsEnumValue }
            };

            var binder = new SimpleTypeModelBinder();

            // Act
            var result = await binder.BindModelResultAsync(bindingContext);

            // Assert
            Assert.True(result.IsModelSet);
            var boundModel = Assert.IsType<FlagsEnum>(result.Model);
            Assert.Equal((FlagsEnum)expected, boundModel);
        }