summaryrefslogtreecommitdiffstats
path: root/install/ui/test/utils_tests.js
blob: 2bef5845970fb9717a74be18e153a0c8234c95b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*  Authors:
 *    Petr Vobornik <pvoborni@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/>.
 */

define(['freeipa/ipa', 'freeipa/jquery', 'freeipa/field', 'freeipa/widget'],
       function(IPA, $) {  return function() {

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');
});

test('Testing IPA.defined', function() {

    // positive
    same(IPA.defined({}), true, 'Object');
    same(IPA.defined(0), true, 'Zero number');
    same(IPA.defined(1), true, 'Some number');
    same(IPA.defined(false), true, 'false');
    same(IPA.defined(true), true, 'true');
    same(IPA.defined(function(){}), true, 'function');
    same(IPA.defined(''), true, 'Empty string - not checking');

    // negative
    same(IPA.defined('', true), false, 'Empty string - checking');
    same(IPA.defined(undefined), false, 'undefined');
    same(IPA.defined(null), false, 'null');
});

};});