There was an error in number validation check. If boundary value was an empty string, validation of a number always failed. This patch fixes the problem by not performing the check in these cases.

Basic unit tests for IPA.metadata_validator created.
--
Petr Vobornik
From 4bde0e0f7dd1b02ce6ae1b37a2f6a97efbb99726 Mon Sep 17 00:00:00 2001
From: Petr Vobornik <pvobo...@redhat.com>
Date: Tue, 11 Sep 2012 13:52:10 +0200
Subject: [PATCH] Fix integer validation when boundary value is empty string

There was an error in number validation check. If boundary value was an empty string, validation of a number always failed. This patch fixes the problem by not performing the check in these cases.
---
 install/ui/field.js              |   4 +-
 install/ui/ipa.js                |   4 ++
 install/ui/test/all_tests.html   |   1 +
 install/ui/test/index.html       |   1 +
 install/ui/test/jsl.conf         |   3 +-
 install/ui/test/utils_tests.html |  24 ++++++++
 install/ui/test/utils_tests.js   | 119 +++++++++++++++++++++++++++++++++++++++
 7 files changed, 153 insertions(+), 3 deletions(-)
 create mode 100644 install/ui/test/utils_tests.html
 create mode 100644 install/ui/test/utils_tests.js

diff --git a/install/ui/field.js b/install/ui/field.js
index 42da6f92cd102aa665b05b72783de9d04dcdcfb0..46113b8caac0026d6e8aefd9587552b2e88b9775 100644
--- a/install/ui/field.js
+++ b/install/ui/field.js
@@ -448,13 +448,13 @@ IPA.metadata_validator = function(spec) {
 
         if (number) {
 
-            if (metadata.minvalue !== undefined && Number(value) < Number(metadata.minvalue)) {
+            if (!IPA.not_defined(metadata.minvalue, true) && Number(value) < Number(metadata.minvalue)) {
                 message = IPA.messages.widget.validation.min_value;
                 message = message.replace('${value}', metadata.minvalue);
                 return that.false_result(message);
             }
 
-            if (metadata.maxvalue !== undefined && Number(value) > Number(metadata.maxvalue)) {
+            if (!IPA.not_defined(metadata.maxvalue, true) && Number(value) > Number(metadata.maxvalue)) {
                 message = IPA.messages.widget.validation.max_value;
                 message = message.replace('${value}', metadata.maxvalue);
                 return that.false_result(message);
diff --git a/install/ui/ipa.js b/install/ui/ipa.js
index 23c9933dfb97cb39a932f78d235fdaf844e42b7c..d704a0a562404d07f70d09e856f42fed224f9093 100644
--- a/install/ui/ipa.js
+++ b/install/ui/ipa.js
@@ -2021,6 +2021,10 @@ IPA.is_empty = function(value) {
     return empty;
 };
 
+IPA.not_defined = function(value, check_empty_str) {
+    return value === null || value === undefined || (check_empty_str && value === '');
+};
+
 IPA.array_diff = function(a, b) {
 
     if (a === b || (!a && !b)) return false;
diff --git a/install/ui/test/all_tests.html b/install/ui/test/all_tests.html
index 5a5eae54b5c6606564fd0e7b2df8ccc606dccef2..e9061b6ca8802b83c8f99c234ad7311606672695 100644
--- a/install/ui/test/all_tests.html
+++ b/install/ui/test/all_tests.html
@@ -32,6 +32,7 @@
     <script type="text/javascript" src="aci_tests.js"></script>
     <script type="text/javascript" src="widget_tests.js"></script>
     <script type="text/javascript" src="ip_tests.js"></script>
+    <script type="text/javascript" src="utils_tests.js"></script>
 </head>
 <body>
     <h1 id="qunit-header">Complete Test Suite</h1>
diff --git a/install/ui/test/index.html b/install/ui/test/index.html
index 1b623d40f794b1ffda90f6c3352840acbaeec26a..0a135188e518913aa3f3a191ab340f2f5e820abf 100644
--- a/install/ui/test/index.html
+++ b/install/ui/test/index.html
@@ -34,6 +34,7 @@
         <li><a href="aci_tests.html">Access Control Interface Test Suite</a>
         <li><a href="widget_tests.html">Widget Test Suite</a>
         <li><a href="ip_tests.html">IP Addresses Test Suite</a>
+        <li><a href="utils_tests.html">Utils Test Suite</a>
         </ul>
     </div>
 
diff --git a/install/ui/test/jsl.conf b/install/ui/test/jsl.conf
index 4654b714f91cb00696043bf3042fccd2b2e394bc..768a295f1ac7c956f588b99c60eecdb2bfb06c67 100644
--- a/install/ui/test/jsl.conf
+++ b/install/ui/test/jsl.conf
@@ -146,4 +146,5 @@
 +process ipa_tests.js
 +process ordered_map_tests.js
 +process widget_tests.js
-+process ip_tests.js
\ No newline at end of file
++process ip_tests.js
++process utils_tests.js
\ No newline at end of file
diff --git a/install/ui/test/utils_tests.html b/install/ui/test/utils_tests.html
new file mode 100644
index 0000000000000000000000000000000000000000..5b81cc35930766907f8db310f1e6e77edee95034
--- /dev/null
+++ b/install/ui/test/utils_tests.html
@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <title>IPA utils test suite</title>
+    <link rel="stylesheet" href="qunit.css" type="text/css" media="screen">
+    <script type="text/javascript" src="../jquery.js"></script>
+    <script type="text/javascript" src="../jquery.ba-bbq.js"></script>
+    <script type="text/javascript" src="../jquery-ui.js"></script>
+    <script type="text/javascript" src="../jquery.ordered-map.js"></script>
+    <script type="text/javascript" src="qunit.js"></script>
+    <script type="text/javascript" src="../ipa.js"></script>
+    <script type="text/javascript" src="../widget.js"></script>
+    <script type="text/javascript" src="../field.js"></script>
+    <script type="text/javascript" src="utils_tests.js"></script>
+</head>
+<body>
+    <h1 id="qunit-header">IPA utils test suite</h1>
+    <h2 id="qunit-banner"></h2>
+    <div id="qunit-testrunner-toolbar"></div>
+    <h2 id="qunit-userAgent"></h2>
+    <ol id="qunit-tests"></ol>
+    <div id="qunit-fixture"></div>
+</body>
+</html>
\ No newline at end of file
diff --git a/install/ui/test/utils_tests.js b/install/ui/test/utils_tests.js
new file mode 100644
index 0000000000000000000000000000000000000000..bc8469098f37f95686634589775483f919a81159
--- /dev/null
+++ b/install/ui/test/utils_tests.js
@@ -0,0 +1,119 @@
+/*  Authors:
+ *    Petr Vobornik <pvobo...@redhat.com>
+ *
+ * Copyright (C) 2012 Red Hat
+ * see file 'COPYING' for use and warranty information
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+var old;
+
+module('utils',{
+
+    setup: function() {
+        old = IPA.messages;
+        IPA.messages = {
+            widget: {
+                validation: {
+                    integer: "",
+                    decimal: "",
+                    min_value: "",
+                    max_value: "",
+                    pattern_errmsg: ""
+                }
+            }
+        };
+    },
+    teardown: function() {
+        IPA.messages = old;
+    }
+});
+
+test('Testing metadata validator', function() {
+
+    // using strings as values because it is an output of inputs
+
+    var validator = IPA.build({
+        factory: IPA.metadata_validator
+    });
+
+    var metadata = {
+        type: 'int',
+        maxvalue: 300,
+        minvalue: 30
+    };
+
+    var context = { metadata: metadata };
+
+    var value;
+
+    value = "50";
+    ok(validator.validate(value, context).valid, 'Checking lower maximun, alphabetically higher');
+
+    value = "200";
+    ok(validator.validate(value, context).valid, 'Checking higher minimum, alphabetically lower');
+
+    value = "29";
+    ok(!validator.validate(value, context).valid, 'Checking below minimum');
+
+    value = "301";
+    ok(!validator.validate(value, context).valid, 'Checking above maximum');
+
+    context.metadata.minvalue = 0;
+    value = "-1";
+    ok(!validator.validate(value, context).valid, 'Checking zero minimum - below');
+    value = "0";
+    ok(validator.validate(value, context).valid, 'Checking zero minimum - above');
+    value = "1";
+    ok(validator.validate(value, context).valid, 'Checking zero minimum - same');
+
+    context.metadata = {
+        type: 'int',
+        maxvalue: "",
+        minvalue: ""
+    };
+
+    ok(validator.validate(value, context).valid, 'Checking empty strings as boundaries');
+
+    context.metadata = {
+        type: 'int',
+        maxvalue: null,
+        minvalue: null
+    };
+    ok(validator.validate(value, context).valid, 'Checking null as boundaries');
+
+    context.metadata = {
+        type: 'int',
+        maxvalue: undefined,
+        minvalue: undefined
+    };
+    ok(validator.validate(value, context).valid, 'Checking undefined as boundaries');
+
+    context.metadata = {
+        type: 'Decimal',
+        maxvalue: "10.333",
+        minvalue: "-10.333"
+    };
+
+    value = "10.333";
+    ok(validator.validate(value, context).valid, 'Decimal: checking maximum');
+    value = "10.3331";
+    ok(!validator.validate(value, context).valid, 'Decimal: checking maximum - invalid');
+
+    value = "-10.333";
+    ok(validator.validate(value, context).valid, 'Decimal: checking minimum');
+    value = "-10.3331";
+    ok(!validator.validate(value, context).valid, 'Decimal: checking minimum - invalid');
+});
\ No newline at end of file
-- 
1.7.11.4

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to