summaryrefslogtreecommitdiffstats
path: root/install/ui/test/ipa_tests.js
blob: 4fa0d1ea2cd868d88196e07b0160f4d7ec09f7fe (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*  Authors:
 *    Endi Sukma Dewata <edewata@redhat.com>
 *
 * Copyright (C) 2010 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/>.
 */

module('ipa');

test("Testing ipa_init().", function() {

    expect(1);

    IPA.ajax_options.async = false;

    IPA.init(
        "data",
        true,
        function(data, text_status, xhr) {
            ok(true, "ipa_init() succeeded.");
        },
        function(xhr, text_status, error_thrown) {
            ok(false, "ipa_init() failed: "+error_thrown);
        }
    );
});

test("Testing IPA.get_param_info().", function() {

    var param_info = IPA.get_param_info("user", "uid");
    ok(
        param_info,
        "IPA.get_param_info(\"user\", \"uid\") not null"
    );

    equals(
        param_info["label"], "User login",
        "IPA.get_param_info(\"user\", \"uid\")[\"label\"]"
    );

    equals(
        IPA.get_param_info("user", "wrong_attribute"), null,
        "IPA.get_param_info(\"user\", \"wrong_attribute\")"
    );

    equals(
        IPA.get_param_info("user", null), null,
        "IPA.get_param_info(\"user\", null)"
    );

    equals(
        IPA.get_param_info("wrong_entity", "uid"), null,
        "IPA.get_param_info(\"wrong_entity\", \"uid\")"
    );

    equals(
        IPA.get_param_info(null, "uid"), null,
        "IPA.get_param_info(null, \"uid\")"
    );
});

test("Testing IPA.get_member_attribute().", function() {

    equals(
        IPA.get_member_attribute("user", "group"), "memberof",
        "IPA.get_member_attribute(\"user\", \"group\")"
    );

    equals(
        IPA.get_member_attribute("user", "host"), null,
        "IPA.get_member_attribute(\"user\", \"host\")"
    );

    equals(
        IPA.get_member_attribute("user", null), null,
        "IPA.get_member_attribute(\"user\", null)"
    );

    equals(
        IPA.get_member_attribute(null, "group"), null,
        "IPA.get_member_attribute(null, \"group\")"
    );
});

test("Testing successful IPA.cmd().", function() {

    var method = 'method';
    var args = ['arg1', 'arg2', 'arg3'];
    var options = {
        opt1: 'val1',
        opt2: 'val2',
        opt3: 'val3'
    };
    var object = 'object';

    var success_handler_counter = 0;
    var error_handler_counter = 0;

    function success_handler(data, status, xhr) {
        success_handler_counter++;
    }

    function error_handler(xhr, text_status, error_thrown) {
        error_handler_counter++;
    }

    var orig = $.ajax;

    var xhr = {};
    var text_status = null;
    var error_thrown = {name:'ERROR', message:'An error has occured'};

    var ajax_counter = 0;

    $.ajax = function(request) {
        ajax_counter++;

        equals(
            request.url, "data/"+object+"_"+method+".json",
            "Checking request.url"
        );

        var data = JSON.parse(request.data);

        equals(
            data.method, object+'_'+method,
            "Checking method"
        );

        same(
            data.params, [args, options],
            "Checking parameters"
        );

        request.success(xhr, text_status, error_thrown);
    };

    IPA.cmd(method, args, options, success_handler, error_handler, object);

    equals(
        ajax_counter, 1,
        "Checking ajax invocation counter"
    );

    var dialog = IPA.error_dialog.parent('.ui-dialog');

    ok(
        !dialog.length,
        "The dialog box is not created."
    );

    ok(
        success_handler_counter == 1 && error_handler_counter == 0,
        "Only the success handler is called."
    );

    $.ajax = orig;
});

test("Testing unsuccessful IPA.cmd().", function() {

    var method = 'method';
    var args = ['arg1', 'arg2', 'arg3'];
    var options = {
        opt1: 'val1',
        opt2: 'val2',
        opt3: 'val3'
    };
    var object = 'object';

    var success_handler_counter = 0;
    var error_handler_counter = 0;

    function success_handler(data, status, xhr) {
        success_handler_counter++;
    }

    function error_handler(xhr, text_status, error_thrown) {
        error_handler_counter++;
    }

    var orig = $.ajax;

    var xhr = {};
    var text_status = null;
    var error_thrown = {name:'ERROR', message:'An error has occured'};

    var ajax_counter = 0;

    $.ajax = function(request) {
        ajax_counter++;

        equals(
            request.url, "data/"+object+"_"+method+".json",
            "Checking request.url"
        );

        var data = JSON.parse(request.data);

        equals(
            data.method, object+'_'+method,
            "Checking method"
        );

        same(
            data.params, [args, options],
            "Checking parameters"
        );

        request.error(xhr, text_status, error_thrown);
    };

    IPA.cmd(method, args, options, success_handler, error_handler, object);

    var dialog = IPA.error_dialog.parent('.ui-dialog');

    equals(
        ajax_counter, 1,
        "Checking ajax invocation counter"
    );

    ok(
        dialog.length == 1 && IPA.error_dialog.dialog('isOpen'),
        "The dialog box is created and open."
    );

    ok(
        success_handler_counter == 0 && error_handler_counter == 0,
        "Initially none of the handlers are called."
    );

    // search the retry button from the beginning
    var retry = $('button', dialog).first();
    retry.trigger('click');

    equals(
        ajax_counter, 2,
        "Checking ajax invocation counter"
    );

    ok(
        success_handler_counter == 0 && error_handler_counter == 0,
        "After 1st retry, none of the handlers are called."
    );

    // search the retry button from the beginning again because the dialog
    // has been recreated
    dialog = IPA.error_dialog.parent('.ui-dialog');
    retry = $('button', dialog).first();
    retry.trigger('click');

    equals(
        ajax_counter, 3,
        "Checking ajax invocation counter"
    );

    ok(
        success_handler_counter == 0 && error_handler_counter == 0,
        "After 2nd retry, none of the handlers are called."
    );

    // search the cancel button from the beginning because the dialog has
    // been recreated
    dialog = IPA.error_dialog.parent('.ui-dialog');
    var cancel = $('button', dialog).first().next();
    cancel.trigger('click');

    equals(
        ajax_counter, 3,
        "Checking ajax invocation counter"
    );

    ok(
        !IPA.error_dialog.dialog('isOpen'),
        "After cancel, the dialog box is closed."
    );

    ok(
        success_handler_counter == 0 && error_handler_counter == 1,
        "Only the error handler is called."
    );

    $.ajax = orig;
});