public void ActionControllerArea_InArea_RemoteFindsControllerInNamedArea()
        {
            // Arrange
            ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForProperty(modelAccessor: null,
                                                                                           containerType: typeof(string), propertyName: "Length");
            TestableRemoteAttribute attribute = new TestableRemoteAttribute("Action", "Controller", "AnotherArea");

            attribute.HttpMethod = "POST";

            var context = new AreaRegistrationContext("Test", attribute.RouteTable);

            context.MapRoute(name: null, url: "Test/{controller}/{action}");
            context = new AreaRegistrationContext("AnotherArea", attribute.RouteTable);
            context.MapRoute(name: null, url: "AnotherArea/{controller}/{action}");

            attribute.RouteTable.Add(new Route("{controller}/{action}", new MvcRouteHandler()));

            // Act
            ModelClientValidationRule rule =
                attribute.GetClientValidationRules(metadata, GetMockControllerContextWithArea(url: null, areaName: "Test"))
                .Single();

            // Assert
            Assert.Equal("remote", rule.ValidationType);
            Assert.Equal("/AnotherArea/Controller/Action", rule.ValidationParameters["url"]);
        }
예제 #2
0
        public void ActionControllerReturnsCorrectClientData()
        {
            // Arrange
            string url = null;
            Mock <ControllerContext> context = new Mock <ControllerContext>();

            context.Expect(c => c.HttpContext.Request.ApplicationPath)
            .Returns("/");
            context.Expect(c => c.HttpContext.Response.ApplyAppPathModifier(It.IsAny <string>()))
            .Callback <string>(vpath => url = vpath)
            .Returns(() => url);
            ModelMetadata           metadata  = ModelMetadataProviders.Current.GetMetadataForProperty(null, typeof(string), "Length");
            TestableRemoteAttribute attribute = new TestableRemoteAttribute("Action", "Controller", "ParameterName");

            attribute.RouteTable.Add(new Route("{controller}/{action}", new MvcRouteHandler()));

            // Act
            ModelClientValidationRule rule = attribute.GetClientValidationRules(metadata, context.Object).Single();

            // Assert
            Assert.AreEqual("remote", rule.ValidationType);
            Assert.AreEqual("The field Length is invalid.", rule.ErrorMessage);
            Assert.AreEqual(2, rule.ValidationParameters.Count);
            Assert.AreEqual(url, rule.ValidationParameters["url"]);
            Assert.AreEqual("ParameterName", rule.ValidationParameters["parameterName"]);
        }
예제 #3
0
        public void ActionControllerReturnsCorrectClientDataWithNamedParameters()
        {
            // Arrange
            string url = null;

            ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForProperty(
                null,
                typeof(string),
                "Length"
                );
            TestableRemoteAttribute attribute = new TestableRemoteAttribute("Action", "Controller");

            attribute.HttpMethod       = "POST";
            attribute.AdditionalFields = "Password,ConfirmPassword";

            attribute.RouteTable.Add(new Route("{controller}/{action}", new MvcRouteHandler()));

            // Act
            ModelClientValidationRule rule = attribute
                                             .GetClientValidationRules(metadata, GetMockControllerContext(url))
                                             .Single();

            // Assert
            Assert.Equal("remote", rule.ValidationType);
            Assert.Equal("'Length' is invalid.", rule.ErrorMessage);
            Assert.Equal(3, rule.ValidationParameters.Count);
            Assert.Equal("/Controller/Action", rule.ValidationParameters["url"]);
            Assert.Equal(
                "*.Length,*.Password,*.ConfirmPassword",
                rule.ValidationParameters["additionalfields"]
                );
            Assert.Equal("POST", rule.ValidationParameters["type"]);
        }
예제 #4
0
        public void ActionControllerReturnsCorrectClientDataWithoutNamedParameters()
        {
            // Arrange
            string url = null;

            ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForProperty(
                null,
                typeof(string),
                "Length"
                );
            TestableRemoteAttribute attribute = new TestableRemoteAttribute("Action", "Controller");

            attribute.RouteTable.Add(new Route("{controller}/{action}", new MvcRouteHandler()));

            // Act
            ModelClientValidationRule rule = attribute
                                             .GetClientValidationRules(metadata, GetMockControllerContext(url))
                                             .Single();

            // Assert
            Assert.Equal("remote", rule.ValidationType);
            Assert.Equal("'Length' is invalid.", rule.ErrorMessage);
            Assert.Equal(2, rule.ValidationParameters.Count);
            Assert.Equal("/Controller/Action", rule.ValidationParameters["url"]);
            Assert.Equal("*.Length", rule.ValidationParameters["additionalfields"]);
            Assert.Throws <KeyNotFoundException>(
                () => rule.ValidationParameters["type"],
                "The given key was not present in the dictionary."
                );
        }
예제 #5
0
        public void NoRouteWithActionControllerThrows() {
            // Arrange
            ControllerContext context = new ControllerContext();
            ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForProperty(null, typeof(string), "Length");
            TestableRemoteAttribute attribute = new TestableRemoteAttribute("Action", "Controller");

            // Act & Assert
            ExceptionHelper.ExpectInvalidOperationException(
                () => new List<ModelClientValidationRule>(attribute.GetClientValidationRules(metadata, context)),
                "No url for remote validation could be found.");
        }
예제 #6
0
        public void BadRouteNameThrows() {
            // Arrange
            ControllerContext context = new ControllerContext();
            ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(object));
            TestableRemoteAttribute attribute = new TestableRemoteAttribute("RouteName");

            // Act & Assert
            ExceptionHelper.ExpectArgumentException(
                () => new List<ModelClientValidationRule>(attribute.GetClientValidationRules(metadata, context)),
                "A route named 'RouteName' could not be found in the route collection.\r\nParameter name: name");
        }
        public void NoRouteWithActionControllerThrows()
        {
            // Arrange
            ControllerContext       context   = new ControllerContext();
            ModelMetadata           metadata  = ModelMetadataProviders.Current.GetMetadataForProperty(null, typeof(string), "Length");
            TestableRemoteAttribute attribute = new TestableRemoteAttribute("Action", "Controller");

            // Act & Assert
            Assert.Throws <InvalidOperationException>(
                () => new List <ModelClientValidationRule>(attribute.GetClientValidationRules(metadata, context)),
                "No url for remote validation could be found.");
        }
        public void BadRouteNameThrows()
        {
            // Arrange
            ControllerContext       context   = new ControllerContext();
            ModelMetadata           metadata  = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(object));
            TestableRemoteAttribute attribute = new TestableRemoteAttribute("RouteName");

            // Act & Assert
            Assert.Throws <ArgumentException>(
                () => new List <ModelClientValidationRule>(attribute.GetClientValidationRules(metadata, context)),
                "A route named 'RouteName' could not be found in the route collection.\r\nParameter name: name");
        }
예제 #9
0
        public void NoRouteWithActionControllerThrows()
        {
            // Arrange
            ControllerContext       context   = new ControllerContext();
            ModelMetadata           metadata  = ModelMetadataProviders.Current.GetMetadataForProperty(null, typeof(string), "Length");
            TestableRemoteAttribute attribute = new TestableRemoteAttribute("Action", "Controller", "ParameterName");

            // Act & Assert
            ExceptionHelper.ExpectInvalidOperationException(
                () => attribute.GetClientValidationRules(metadata, context),
                "No route matched!");
        }
예제 #10
0
        public void GoodRouteNameReturnsCorrectClientData() {
            // Arrange
            string url = null;
            ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForProperty(null, typeof(string), "Length");
            TestableRemoteAttribute attribute = new TestableRemoteAttribute("RouteName");
            attribute.RouteTable.Add("RouteName", new Route("my/url", new MvcRouteHandler()));

            // Act
            ModelClientValidationRule rule = attribute.GetClientValidationRules(metadata, GetMockControllerContext(url)).Single();

            // Assert
            Assert.AreEqual("remote", rule.ValidationType);
            Assert.AreEqual("'Length' is invalid.", rule.ErrorMessage);
            Assert.AreEqual(2, rule.ValidationParameters.Count);
            Assert.AreEqual("/my/url", rule.ValidationParameters["url"]);
        }
        public void GoodRouteNameReturnsCorrectClientData()
        {
            // Arrange
            string                  url       = null;
            ModelMetadata           metadata  = ModelMetadataProviders.Current.GetMetadataForProperty(null, typeof(string), "Length");
            TestableRemoteAttribute attribute = new TestableRemoteAttribute("RouteName");

            attribute.RouteTable.Add("RouteName", new Route("my/url", new MvcRouteHandler()));

            // Act
            ModelClientValidationRule rule = attribute.GetClientValidationRules(metadata, GetMockControllerContext(url)).Single();

            // Assert
            Assert.Equal("remote", rule.ValidationType);
            Assert.Equal("'Length' is invalid.", rule.ErrorMessage);
            Assert.Equal(2, rule.ValidationParameters.Count);
            Assert.Equal("/my/url", rule.ValidationParameters["url"]);
        }
예제 #12
0
        public void GoodRouteNameReturnsCorrectClientData() {
            // Arrange
            string url = null;
            Mock<ControllerContext> context = new Mock<ControllerContext>();
            context.Expect(c => c.HttpContext.Request.ApplicationPath)
                   .Returns("/");
            context.Expect(c => c.HttpContext.Response.ApplyAppPathModifier(It.IsAny<string>()))
                   .Callback<string>(vpath => url = vpath)
                   .Returns(() => url);
            ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForProperty(null, typeof(string), "Length");
            TestableRemoteAttribute attribute = new TestableRemoteAttribute("RouteName", "ParameterName");
            attribute.RouteTable.Add("RouteName", new Route("my/url", new MvcRouteHandler()));

            // Act
            ModelClientValidationRule rule = attribute.GetClientValidationRules(metadata, context.Object).Single();

            // Assert
            Assert.AreEqual("remote", rule.ValidationType);
            Assert.AreEqual("The field Length is invalid.", rule.ErrorMessage);
            Assert.AreEqual(2, rule.ValidationParameters.Count);
            Assert.AreEqual(url, rule.ValidationParameters["url"]);
            Assert.AreEqual("ParameterName", rule.ValidationParameters["parameterName"]);
        }
예제 #13
0
        public void ActionControllerReturnsCorrectClientDataWithoutNamedParameters() {
            // Arrange
            string url = null;

            ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForProperty(null, typeof(string), "Length");
            TestableRemoteAttribute attribute = new TestableRemoteAttribute("Action", "Controller");
            attribute.RouteTable.Add(new Route("{controller}/{action}", new MvcRouteHandler()));

            // Act
            ModelClientValidationRule rule = attribute.GetClientValidationRules(metadata, GetMockControllerContext(url)).Single();

            // Assert
            Assert.AreEqual("remote", rule.ValidationType);
            Assert.AreEqual("'Length' is invalid.", rule.ErrorMessage);
            Assert.AreEqual(2, rule.ValidationParameters.Count);
            Assert.AreEqual("/Controller/Action", rule.ValidationParameters["url"]);
            Assert.AreEqual("*.Length", rule.ValidationParameters["additionalfields"]);
            ExceptionHelper.ExpectException<KeyNotFoundException>(
                () => rule.ValidationParameters["type"],
                "The given key was not present in the dictionary.");
        }
예제 #14
0
        public void ActionControllerReturnsCorrectClientDataWithNamedParameters() {
            // Arrange
            string url = null;

            ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForProperty(null, typeof(string), "Length");
            TestableRemoteAttribute attribute = new TestableRemoteAttribute("Action", "Controller");
            attribute.HttpMethod = "POST";
            attribute.AdditionalFields = "Password,ConfirmPassword";

            attribute.RouteTable.Add(new Route("{controller}/{action}", new MvcRouteHandler()));

            // Act
            ModelClientValidationRule rule = attribute.GetClientValidationRules(metadata, GetMockControllerContext(url)).Single();

            // Assert
            Assert.AreEqual("remote", rule.ValidationType);
            Assert.AreEqual("'Length' is invalid.", rule.ErrorMessage);
            Assert.AreEqual(3, rule.ValidationParameters.Count);
            Assert.AreEqual("/Controller/Action", rule.ValidationParameters["url"]);
            Assert.AreEqual("*.Length,*.Password,*.ConfirmPassword", rule.ValidationParameters["additionalfields"]);
            Assert.AreEqual("POST", rule.ValidationParameters["type"]);
        }
예제 #15
0
        public void NoRouteWithActionControllerThrows() {
            // Arrange
            ControllerContext context = new ControllerContext();
            ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForProperty(null, typeof(string), "Length");
            TestableRemoteAttribute attribute = new TestableRemoteAttribute("Action", "Controller", "ParameterName");

            // Act & Assert
            ExceptionHelper.ExpectInvalidOperationException(
                () => attribute.GetClientValidationRules(metadata, context),
                "No route matched!");
        }
예제 #16
0
        public void ActionControllerAreaReference_WithUseRootInArea_RemoteFindsControllerInRoot()
        {
            // Arrange
            ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForProperty(modelAccessor: null,
                containerType: typeof(string), propertyName: "Length");
            TestableRemoteAttribute attribute = new TestableRemoteAttribute("Action", "Controller", AreaReference.UseRoot);
            attribute.HttpMethod = "POST";

            var context = new AreaRegistrationContext("Test", attribute.RouteTable);
            context.MapRoute(name: null, url: "Test/{controller}/{action}");

            attribute.RouteTable.Add(new Route("{controller}/{action}", new MvcRouteHandler()));

            // Act
            ModelClientValidationRule rule =
                attribute.GetClientValidationRules(metadata, GetMockControllerContextWithArea(url: null, areaName: "Test"))
                .Single();

            // Assert
            Assert.Equal("remote", rule.ValidationType);
            Assert.Equal("/Controller/Action", rule.ValidationParameters["url"]);
        }