/
stanza-dataforms.js
455 lines (401 loc) · 13.5 KB
/
stanza-dataforms.js
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import jxt from 'jxt';
import { JID } from 'xmpp-jid';
import * as fs from 'fs';
import test from 'tape';
const JXT = jxt.createRegistry();
JXT.use(require('../lib/protocol').default);
const DataForm = JXT.getDefinition('x', 'jabber:x:data');
const formXML = fs.readFileSync(__dirname + '/samples/dataform-1.xml');
const submittedXML = fs.readFileSync(__dirname + '/samples/dataform-2.xml');
const itemsXML = fs.readFileSync(__dirname + '/samples/dataform-3.xml');
const validationXML = fs.readFileSync(__dirname + '/samples/dataform-4.xml');
const mediaXML = fs.readFileSync(__dirname + '/samples/dataform-5.xml');
const layoutXML = fs.readFileSync(__dirname + '/samples/dataform-6.xml');
const nestedLayoutXML = fs.readFileSync(__dirname + '/samples/dataform-7.xml');
test('Convert Form XML to Data Form object', function(t) {
const form = JXT.parse(formXML, DataForm).toJSON();
t.equal(form.type, 'form');
t.equal(form.title, 'Bot Configuration');
t.same(form.instructions, ['Fill out this form to configure your new bot!']);
t.equal(form.fields.length, 12);
t.same(form.fields[0], {
type: 'hidden',
name: 'FORM_TYPE',
value: ['jabber:bot']
});
t.same(form.fields[1], {
type: 'fixed',
value: ['Section 1: Bot Info']
});
t.same(form.fields[2], {
type: 'text-single',
name: 'botname',
label: 'The name of your bot'
});
t.same(form.fields[3], {
type: 'text-multi',
name: 'description',
label: 'Helpful description of your bot'
});
t.same(form.fields[4], {
type: 'boolean',
name: 'public',
label: 'Public bot?',
required: true
});
t.same(form.fields[5], {
type: 'text-private',
name: 'password',
label: 'Password for special access'
});
t.same(form.fields[6], {
type: 'fixed',
value: ['Section 2: Features']
});
t.same(form.fields[7], {
type: 'list-multi',
name: 'features',
label: 'What features will the bot support?',
options: [
{ label: 'Contests', value: 'contests' },
{ label: 'News', value: 'news' },
{ label: 'Polls', value: 'polls' },
{ label: 'Reminders', value: 'reminders' },
{ label: 'Search', value: 'search' }
],
value: ['news', 'search']
});
t.same(form.fields[8], {
type: 'fixed',
value: ['Section 3: Subscriber List']
});
t.same(form.fields[9], {
type: 'list-single',
name: 'maxsubs',
label: 'Maximum number of subscribers',
options: [
{ label: '10', value: '10' },
{ label: '20', value: '20' },
{ label: '30', value: '30' },
{ label: '50', value: '50' },
{ label: '100', value: '100' },
{ label: 'None', value: 'none' }
],
value: '20'
});
t.same(form.fields[10], {
type: 'fixed',
value: ['Section 4: Invitations']
});
t.same(form.fields[11], {
type: 'jid-multi',
name: 'invitelist',
label: 'People to invite',
desc: 'Tell all your friends about your new bot!'
});
t.end();
});
test('Convert Form Submission Results XML to Data Form object', function(t) {
let form = JXT.parse(submittedXML, DataForm).toJSON();
// Clients are required to send the type attribute back when
// submitting forms, so we have to apply the type values back
// if we want values cast properly.
const fieldTypes = {
FORM_TYPE: 'hidden',
botname: 'text-single',
description: 'text-multi',
public: 'boolean',
password: 'text-private',
features: 'list-multi',
maxsubs: 'list-single',
invitelist: 'jid-multi'
};
for (let i = 0; i < form.fields.length; i++) {
form.fields[i].type = fieldTypes[form.fields[i].name];
}
form = new DataForm(form).toJSON();
t.equal(form.type, 'submit');
t.equal(form.fields.length, 8);
t.same(form.fields[0], {
type: 'hidden',
name: 'FORM_TYPE',
value: ['jabber:bot']
});
t.same(form.fields[1], {
type: 'text-single',
name: 'botname',
value: 'The Jabber Google Bot'
});
t.same(form.fields[2], {
type: 'text-multi',
name: 'description',
value:
'This bot enables you to send requests to\n' +
'Google and receive the search results right\n' +
"in your Jabber client. It's really cool!\n" +
'It even supports Google News!'
});
t.same(form.fields[3], {
type: 'boolean',
name: 'public'
});
t.ok(!form.fields[3].value);
t.same(form.fields[4], {
type: 'text-private',
name: 'password',
value: 'v3r0na'
});
t.same(form.fields[5], {
type: 'list-multi',
name: 'features',
value: ['news', 'search']
});
t.same(form.fields[6], {
type: 'list-single',
name: 'maxsubs',
value: '50'
});
t.same(form.fields[7], {
type: 'jid-multi',
name: 'invitelist',
value: [new JID('juliet@capulet.com'), new JID('benvolio@montague.net')]
});
t.end();
});
test('Convert Form Item Results XML to Data Form object', function(t) {
const form = JXT.parse(itemsXML, DataForm).toJSON();
t.equal(form.type, 'result');
t.equal(form.title, 'Joogle Search: verona');
t.equal(form.reportedFields.length, 3);
t.equal(form.items.length, 5);
t.same(form.reportedFields, [
{ name: 'name', type: 'text-single' },
{ name: 'url', type: 'text-single' },
{ name: 'recommended', type: 'boolean' }
]);
t.same(form.items[0].fields, [
{
name: 'name',
value: 'Comune di Verona - Benvenuti nel sito ufficiale',
type: 'text-single'
},
{
name: 'url',
value: 'http://www.comune.verona.it/',
type: 'text-single'
},
{
name: 'recommended',
value: true,
type: 'boolean'
}
]);
t.end();
});
test('Convert Form with Validation XML to Data Form object', function(t) {
const form = JXT.parse(validationXML, DataForm).toJSON();
t.equal(form.fields.length, 5);
t.same(form.fields[0], {
type: 'text-single',
name: 'evt.date',
label: 'Event Date/Time',
value: '2003-10-06T11:22:00-07:00',
validation: {
dataType: 'xs:dateTime',
basic: true
}
});
t.same(form.fields[1], {
type: 'list-single',
name: 'evt.category',
label: 'Event Category',
options: [{ value: 'holiday' }, { value: 'reminder' }, { value: 'appointment' }],
validation: {
dataType: 'xs:string',
open: true
}
});
t.same(form.fields[2], {
type: 'text-single',
name: 'evt.rsvp',
label: 'RSVP Date/Time',
value: '2003-10-06T11:22:00-07:00',
validation: {
dataType: 'xs:dateTime',
range: {
min: '2003-10-05T00:00:00-07:00',
max: '2003-10-24T23:59:59-07:00'
}
}
});
t.same(form.fields[3], {
type: 'text-single',
name: 'ssn',
label: 'Social Security Number',
desc: "This field should be your SSN, including '-' (e.g. 123-12-1234)",
validation: {
dataType: 'xs:string',
regex: '([0-9]{3})-([0-9]{2})-([0-9]{4})'
}
});
t.same(form.fields[4], {
type: 'list-multi',
name: 'evt.notify-methods',
label: 'Notify me by',
options: [
{ value: 'e-mail' },
{ value: 'jabber/xmpp' },
{ value: 'work phone' },
{ value: 'home phone' },
{ value: 'cell phone' }
],
validation: {
dataType: 'xs:string',
basic: true,
select: {
min: 1,
max: 3
}
}
});
t.end();
});
test('Convert Form with Media Field XML to Data Form object', function(t) {
const form = JXT.parse(mediaXML, DataForm).toJSON();
t.equal(form.fields.length, 1);
t.same(form.fields[0], {
type: 'text-single',
name: 'ocr',
media: {
height: 80,
width: 290,
uris: [
{
type: 'image/jpeg',
uri: 'http://www.victim.example/challenges/ocr.jpeg?F3A6292C'
},
{
type: 'image/jpeg',
uri: 'cid:sha1+f24030b8d91d233bac14777be5ab531ca3b9f102@bob.xmpp.org'
}
]
}
});
t.end();
});
test('Form layout', function(t) {
const form = JXT.parse(layoutXML, DataForm).toJSON();
t.same(form.layout, [
{
label: 'Personal Information',
contents: [
{ text: 'This is page one of three.' },
{
text:
'Note: In accordance with the XSF privacy policy, your personal information will never be shared outside the organization in any way for any purpose; however, your name and JID may be published in the XSF membership directory.'
},
{ field: 'name.first' },
{ field: 'name.last' },
{ field: 'email' },
{ field: 'jid' },
{ field: 'background' }
]
},
{
label: 'Community Activity',
contents: [
{ text: 'This is page two of three.' },
{
text:
"We use this page to gather information about any XEPs you've worked on, as well as your mailing list activity."
},
{ text: "You do post to the mailing lists, don't you?" },
{ field: 'activity.mailing-lists' },
{ field: 'activity.xeps' }
]
},
{
label: 'Plans and Reasonings',
contents: [
{ text: 'This is page three of three.' },
{ text: "You're almost done!" },
{
text:
'This is where you describe your future plans and why you think you deserve to be a member of the XMPP Standards Foundation.'
},
{ field: 'future' },
{ field: 'reasoning' }
]
}
]);
t.end();
});
test('Form layout with nested sections', function(t) {
const form = JXT.parse(nestedLayoutXML, DataForm).toJSON();
t.same(form.layout, [
{
contents: [
{
section: {
label: 'Personal Information',
contents: [
{
text:
'Note: In accordance with the XSF privacy policy, your personal information will never be shared outside the organization in any way for any purpose; however, your name and JID may be published in the XSF membership directory.'
},
{
section: {
label: 'Name',
contents: [
{ text: 'Who are you?' },
{ field: 'name.first' },
{ field: 'name.last' }
]
}
},
{
section: {
label: 'Contact Information',
contents: [
{ text: 'How can we contact you?' },
{ field: 'email' },
{ field: 'jid' }
]
}
},
{ field: 'background' }
]
}
},
{
section: {
label: 'Community Activity',
contents: [
{
text:
"We use this page to gather information about any XEPs you've worked on, as well as your mailing list activity."
},
{ text: "You do post to the mailing lists, don't you?" },
{ field: 'activity.mailing-lists' },
{ field: 'activity.xeps' }
]
}
},
{
section: {
label: 'Plans and Reasonings',
contents: [
{
text:
'This is where you describe your future plans and why you think you deserve to be a member of the XMPP Standards Foundation.'
},
{ field: 'future' },
{ field: 'reasoning' }
]
}
}
]
}
]);
t.end();
});