Пример #1
0
        public void GetClientValidationRules_WithActionControllerArea_CallsUrlHelperWithExpectedValues()
        {
            // Arrange
            var attribute = new RemoteAttribute("Action", "Controller", "Test")
            {
                HttpMethod = "POST",
            };
            var url       = "/Test/Controller/Action";
            var urlHelper = new MockUrlHelper(url, routeName: null);
            var context   = GetValidationContext(urlHelper);

            // Act & Assert
            var rule = Assert.Single(attribute.GetClientValidationRules(context));

            Assert.Equal("remote", rule.ValidationType);
            Assert.Equal("'Length' is invalid.", rule.ErrorMessage);

            Assert.Equal(3, rule.ValidationParameters.Count);
            Assert.Equal("*.Length", rule.ValidationParameters["additionalfields"]);
            Assert.Equal("POST", rule.ValidationParameters["type"]);
            Assert.Equal(url, rule.ValidationParameters["url"]);

            var routeDictionary = Assert.IsType <RouteValueDictionary>(urlHelper.RouteValues);

            Assert.Equal(3, routeDictionary.Count);
            Assert.Equal("Action", routeDictionary["action"] as string);
            Assert.Equal("Controller", routeDictionary["controller"] as string);
            Assert.Equal("Test", routeDictionary["area"] as string);
        }
Пример #2
0
        public void GetClientValidationRules_WithActionController_NoController_Throws()
        {
            // Arrange
            var attribute = new RemoteAttribute("Action", "Controller");
            var context   = GetValidationContextWithNoController();

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

            Assert.Equal("No URL for remote validation could be found.", exception.Message);
        }
Пример #3
0
        public void GetClientValidationRules_WithBadRouteName_Throws()
        {
            // Arrange
            var attribute = new RemoteAttribute("nonexistentRoute");
            var context   = GetValidationContextWithArea(currentArea: null);

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

            Assert.Equal("No URL for remote validation could be found.", exception.Message);
        }
Пример #4
0
        public void FormatPropertyForClientValidation_WithInvalidPropertyName_Throws(string property)
        {
            // Arrange
            var expected = "Value cannot be null or empty." + Environment.NewLine + "Parameter name: property";

            // Act & Assert
            var exception = Assert.Throws <ArgumentException>(
                "property",
                () => RemoteAttribute.FormatPropertyForClientValidation(property));

            Assert.Equal(expected, exception.Message);
        }
Пример #5
0
        public void GetClientValidationRules_WithActionControllerAreaInArea_FindsControllerInDifferentArea()
        {
            // Arrange
            var attribute = new RemoteAttribute("Action", "Controller", "AnotherArea");
            var context   = GetValidationContextWithArea(currentArea: "Test");

            // Act & Assert
            var rule = Assert.Single(attribute.GetClientValidationRules(context));

            Assert.Equal("remote", rule.ValidationType);
            Assert.Equal("'Length' is invalid.", rule.ErrorMessage);

            Assert.Equal(2, rule.ValidationParameters.Count);
            Assert.Equal("*.Length", rule.ValidationParameters["additionalfields"]);
            Assert.Equal("/AnotherArea/Controller/Action", rule.ValidationParameters["url"]);
        }
Пример #6
0
        public void GetClientValidationRules_WithActionControllerArea_FindsControllerInRootArea(string areaName)
        {
            // Arrange
            var attribute = new RemoteAttribute("Action", "Controller", areaName);
            var context   = GetValidationContextWithArea(currentArea: null);

            // Act & Assert
            var rule = Assert.Single(attribute.GetClientValidationRules(context));

            Assert.Equal("remote", rule.ValidationType);
            Assert.Equal("'Length' is invalid.", rule.ErrorMessage);

            Assert.Equal(2, rule.ValidationParameters.Count);
            Assert.Equal("*.Length", rule.ValidationParameters["additionalfields"]);
            Assert.Equal("/UrlEncode[[Controller]]/UrlEncode[[Action]]", rule.ValidationParameters["url"]);
        }
Пример #7
0
        public void GetClientValidationRules_WithRoute_CallsUrlHelperWithExpectedValues()
        {
            // Arrange
            var routeName = "RouteName";
            var attribute = new RemoteAttribute(routeName);
            var url       = "/my/URL";
            var urlHelper = new MockUrlHelper(url, routeName);
            var context   = GetValidationContext(urlHelper);

            // Act & Assert
            var rule = Assert.Single(attribute.GetClientValidationRules(context));

            Assert.Equal("remote", rule.ValidationType);
            Assert.Equal("'Length' is invalid.", rule.ErrorMessage);

            Assert.Equal(2, rule.ValidationParameters.Count);
            Assert.Equal("*.Length", rule.ValidationParameters["additionalfields"]);
            Assert.Equal(url, rule.ValidationParameters["url"]);

            var routeDictionary = Assert.IsType <RouteValueDictionary>(urlHelper.RouteValues);

            Assert.Empty(routeDictionary);
        }
Пример #8
0
        public void GetClientValidationRules_WithActionControllerAreaInArea_FindsControllerInDifferentArea()
        {
            // Arrange
            var attribute = new RemoteAttribute("Action", "Controller", "AnotherArea");
            var context = GetValidationContextWithArea(currentArea: "Test");

            // Act & Assert
            var rule = Assert.Single(attribute.GetClientValidationRules(context));
            Assert.Equal("remote", rule.ValidationType);
            Assert.Equal("'Length' is invalid.", rule.ErrorMessage);

            Assert.Equal(2, rule.ValidationParameters.Count);
            Assert.Equal("*.Length", rule.ValidationParameters["additionalfields"]);
            Assert.Equal("/AnotherArea/Controller/Action", rule.ValidationParameters["url"]);
        }
Пример #9
0
        public void GetClientValidationRules_WithActionControllerArea_CallsUrlHelperWithExpectedValues()
        {
            // Arrange
            var attribute = new RemoteAttribute("Action", "Controller", "Test")
            {
                HttpMethod = "POST",
            };
            var url = "/Test/Controller/Action";
            var urlHelper = new MockUrlHelper(url, routeName: null);
            var context = GetValidationContext(urlHelper);

            // Act & Assert
            var rule = Assert.Single(attribute.GetClientValidationRules(context));
            Assert.Equal("remote", rule.ValidationType);
            Assert.Equal("'Length' is invalid.", rule.ErrorMessage);

            Assert.Equal(3, rule.ValidationParameters.Count);
            Assert.Equal("*.Length", rule.ValidationParameters["additionalfields"]);
            Assert.Equal("POST", rule.ValidationParameters["type"]);
            Assert.Equal(url, rule.ValidationParameters["url"]);

            var routeDictionary = Assert.IsType<RouteValueDictionary>(urlHelper.RouteValues);
            Assert.Equal(3, routeDictionary.Count);
            Assert.Equal("Action", routeDictionary["action"] as string);
            Assert.Equal("Controller", routeDictionary["controller"] as string);
            Assert.Equal("Test", routeDictionary["area"] as string);
        }
Пример #10
0
        public void GetClientValidationRules_WithRoute_CallsUrlHelperWithExpectedValues()
        {
            // Arrange
            var routeName = "RouteName";
            var attribute = new RemoteAttribute(routeName);
            var url = "/my/URL";
            var urlHelper = new MockUrlHelper(url, routeName);
            var context = GetValidationContext(urlHelper);

            // Act & Assert
            var rule = Assert.Single(attribute.GetClientValidationRules(context));
            Assert.Equal("remote", rule.ValidationType);
            Assert.Equal("'Length' is invalid.", rule.ErrorMessage);

            Assert.Equal(2, rule.ValidationParameters.Count);
            Assert.Equal("*.Length", rule.ValidationParameters["additionalfields"]);
            Assert.Equal(url, rule.ValidationParameters["url"]);

            var routeDictionary = Assert.IsType<RouteValueDictionary>(urlHelper.RouteValues);
            Assert.Empty(routeDictionary);
        }
Пример #11
0
        public void GetClientValidationRules_WithActionController_NoController_Throws()
        {
            // Arrange
            var attribute = new RemoteAttribute("Action", "Controller");
            var context = GetValidationContextWithNoController();

            // Act & Assert
            var exception = Assert.Throws<InvalidOperationException>(() => attribute.GetClientValidationRules(context));
            Assert.Equal("No URL for remote validation could be found.", exception.Message);
        }
Пример #12
0
        public void GetClientValidationRules_WithBadRouteName_Throws()
        {
            // Arrange
            var attribute = new RemoteAttribute("nonexistentRoute");
            var context = GetValidationContextWithArea(currentArea: null);

            // Act & Assert
            var exception = Assert.Throws<InvalidOperationException>(() => attribute.GetClientValidationRules(context));
            Assert.Equal("No URL for remote validation could be found.", exception.Message);
        }
Пример #13
0
        public void FormatAdditionalFieldsForClientValidation_WithInvalidPropertyName_Throws(string property)
        {
            // Arrange
            var attribute = new RemoteAttribute(routeName: "default");
            var expected = "Value cannot be null or empty." + Environment.NewLine + "Parameter name: property";

            // Act & Assert
            var exception = Assert.Throws<ArgumentException>(
                "property",
                () => attribute.FormatAdditionalFieldsForClientValidation(property));
            Assert.Equal(expected, exception.Message);
        }
Пример #14
0
        public void GetClientValidationRules_WithActionControllerArea_FindsControllerInRootArea(string areaName)
        {
            // Arrange
            var attribute = new RemoteAttribute("Action", "Controller", areaName);
            var context = GetValidationContextWithArea(currentArea: null);

            // Act & Assert
            var rule = Assert.Single(attribute.GetClientValidationRules(context));
            Assert.Equal("remote", rule.ValidationType);
            Assert.Equal("'Length' is invalid.", rule.ErrorMessage);

            Assert.Equal(2, rule.ValidationParameters.Count);
            Assert.Equal("*.Length", rule.ValidationParameters["additionalfields"]);
            Assert.Equal("/UrlEncode[[Controller]]/UrlEncode[[Action]]", rule.ValidationParameters["url"]);
        }