public void Constructor_WithNullAction_IgnoresArgument() { // Arrange & Act var attribute = new TestableRemoteAttribute(action: null, controller: "AController"); // Assert var keyValuePair = Assert.Single(attribute.RouteData); Assert.Equal(keyValuePair.Key, "controller"); }
public void Constructor_WithNullController_IgnoresArgument() { // Arrange & Act var attribute = new TestableRemoteAttribute("AnAction", controller: null); // Assert var keyValuePair = Assert.Single(attribute.RouteData); Assert.Equal(keyValuePair.Key, "action"); Assert.Null(attribute.RouteName); }
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."); }
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", "ParameterName"); // Act & Assert ExceptionHelper.ExpectInvalidOperationException( () => attribute.GetClientValidationRules(metadata, context), "No route matched!"); }
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; 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"]); }
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"]); }
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"]); }
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"]); }
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."); }
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"]); }
public void Constructor_WithRouteName_UpdatesProperty(string routeName) { // Arrange & Act var attribute = new TestableRemoteAttribute(routeName); // Assert Assert.Empty(attribute.RouteData); Assert.Equal(routeName, attribute.RouteName); }
public void Constructor_WithActionControllerAreaName_UpdatesAreaRouteData(string areaName) { // Arrange & Act var attribute = new TestableRemoteAttribute("AnAction", "AController", areaName: areaName); // Assert Assert.Equal(3, attribute.RouteData.Count); Assert.Contains("action", attribute.RouteData.Keys); Assert.Contains("controller", attribute.RouteData.Keys); var resultName = Assert.Single( attribute.RouteData, keyValuePair => string.Equals(keyValuePair.Key, "area", StringComparison.Ordinal)) .Value; Assert.Equal(areaName, resultName); Assert.Null(attribute.RouteName); }
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"]); }