public void CanCreateModel_ReturnsFalse_IfNotIsTopLevelObjectAndModelIsMarkedWithBinderMetadata() { // Get the property metadata so that it is not a top level object. var modelMetadata = GetMetadataForType(typeof(Document)) .Properties .First(metadata => metadata.PropertyName == nameof(Document.SubDocument)); var bindingContext = new MutableObjectBinderContext { ModelBindingContext = new ModelBindingContext { ModelMetadata = modelMetadata, OperationBindingContext = new OperationBindingContext { ValidatorProvider = Mock.Of<IModelValidatorProvider>(), }, BindingSource = modelMetadata.BindingSource, BinderModelName = modelMetadata.BinderModelName, ModelState = new ModelStateDictionary(), }, }; var mutableBinder = new MutableObjectModelBinder(); // Act var canCreate = mutableBinder.CanCreateModel(bindingContext); // Assert Assert.False(canCreate); }
public void CanUpdateProperty_ReadOnlyValueType_ReturnsFalse() { // Arrange var propertyMetadata = GetMetadataForCanUpdateProperty("ReadOnlyInt"); // Act var canUpdate = MutableObjectModelBinder.CanUpdatePropertyInternal(propertyMetadata); // Assert Assert.False(canUpdate); }
public void CanUpdateProperty_ReadOnlyReferenceTypeNotBlacklisted_ReturnsTrue() { // Arrange var propertyMetadata = GetMetadataForCanUpdateProperty("ReadOnlyObject"); // Act var canUpdate = MutableObjectModelBinder.CanUpdatePropertyInternal(propertyMetadata); // Assert Assert.True(canUpdate); }
public void CanUpdateProperty_HasPublicSetter_ReturnsTrue() { // Arrange var propertyMetadata = GetMetadataForCanUpdateProperty("ReadWriteString"); // Act var canUpdate = MutableObjectModelBinder.CanUpdatePropertyInternal(propertyMetadata); // Assert Assert.True(canUpdate); }
public void GetRequiredPropertiesCollection_MixedAttributes() { // Arrange var bindingContext = new ModelBindingContext { ModelMetadata = GetMetadataForObject(new ModelWithMixedBindingBehaviors()), ValidatorProvider = Mock.Of <IModelValidatorProvider>() }; // Act var validationInfo = MutableObjectModelBinder.GetPropertyValidationInfo(bindingContext); // Assert Assert.Equal(new[] { "Required" }, validationInfo.RequiredProperties); Assert.Equal(new[] { "Never" }, validationInfo.SkipProperties); }
public void NullCheckFailedHandler_ModelStateValid_AddsErrorString() { // Arrange var modelState = new ModelStateDictionary(); var modelMetadata = GetMetadataForType(typeof(Person)); var validationNode = new ModelValidationNode(modelMetadata, "foo"); var validationContext = new ModelValidationContext(new DataAnnotationsModelMetadataProvider(), Mock.Of <IModelValidatorProvider>(), modelState, modelMetadata, null); var e = new ModelValidatedEventArgs(validationContext, parentNode: null); // Act var handler = MutableObjectModelBinder.CreateNullCheckFailedHandler(modelMetadata, incomingValue: null); handler(validationNode, e); // Assert Assert.True(modelState.ContainsKey("foo")); Assert.Equal("A value is required.", modelState["foo"].Errors[0].ErrorMessage); }
public void NullCheckFailedHandler_ModelStateAlreadyInvalid_DoesNothing() { // Arrange var modelState = new ModelStateDictionary(); modelState.AddModelError("foo.bar", "Some existing error."); var modelMetadata = GetMetadataForType(typeof(Person)); var validationNode = new ModelValidationNode(modelMetadata, "foo"); var validationContext = new ModelValidationContext(new DataAnnotationsModelMetadataProvider(), Mock.Of <IModelValidatorProvider>(), modelState, modelMetadata, null); var e = new ModelValidatedEventArgs(validationContext, parentNode: null); // Act var handler = MutableObjectModelBinder.CreateNullCheckFailedHandler(modelMetadata, incomingValue: null); handler(validationNode, e); // Assert Assert.False(modelState.ContainsKey("foo")); }
public async Task CanCreateModel_ReturnsTrue_IfNotIsTopLevelObject_BasedOnValueAvailability( bool valueAvailable) { // Arrange var mockValueProvider = new Mock<IValueProvider>(MockBehavior.Strict); mockValueProvider .Setup(provider => provider.ContainsPrefixAsync("SimpleContainer.Simple.Name")) .Returns(Task.FromResult(valueAvailable)); var typeMetadata = GetMetadataForType(typeof(SimpleContainer)); var modelMetadata = typeMetadata.Properties[nameof(SimpleContainer.Simple)]; var bindingContext = new MutableObjectBinderContext { ModelBindingContext = new ModelBindingContext { ModelMetadata = modelMetadata, ModelName = "SimpleContainer.Simple", OperationBindingContext = new OperationBindingContext { ValidatorProvider = Mock.Of<IModelValidatorProvider>(), ValueProvider = mockValueProvider.Object, MetadataProvider = TestModelMetadataProvider.CreateDefaultProvider(), }, ValueProvider = mockValueProvider.Object, }, PropertyMetadata = modelMetadata.Properties, }; var mutableBinder = new MutableObjectModelBinder(); // Act var result = await mutableBinder.CanCreateModel(bindingContext); // Assert // Result matches whether first Simple property can bind. Assert.Equal(valueAvailable, result); }
public async Task CanCreateModel_ReturnsTrue_IfIsTopLevelObjectAndModelIsMarkedWithBinderMetadata() { var bindingContext = new MutableObjectBinderContext { ModelBindingContext = new ModelBindingContext { // Here the metadata represents a top level object. IsTopLevelObject = true, ModelMetadata = GetMetadataForType(typeof(Document)), OperationBindingContext = new OperationBindingContext { ValidatorProvider = Mock.Of<IModelValidatorProvider>(), } } }; var mutableBinder = new MutableObjectModelBinder(); // Act var canCreate = await mutableBinder.CanCreateModel(bindingContext); // Assert Assert.True(canCreate); }