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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
|
var assert = require('assert')
supertest = require(__dirname+'/../../../../src/node_modules/supertest'),
fs = require('fs'),
api = supertest('http://localhost:9001');
path = require('path');
var filePath = path.join(__dirname, '../../../../APIKEY.txt');
var apiKey = fs.readFileSync(filePath, {encoding: 'utf-8'});
apiKey = apiKey.replace(/\n$/, "");
var apiVersion = 1;
var testPadId = makeid();
var lastEdited = "";
var text = generateLongText();
var ULhtml = '<!DOCTYPE html><html><body><ul class="bullet"><li>one</li><li>2</li></ul><br><ul><ul class="bullet"><li>UL2</li></ul></ul></body></html>';
describe('Connectivity', function(){
it('errors if can not connect', function(done) {
api.get('/api/')
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('API Versioning', function(){
it('errors if can not connect', function(done) {
api.get('/api/')
.expect(function(res){
apiVersion = res.body.currentVersion;
if (!res.body.currentVersion) throw new Error("No version set in API");
return;
})
.expect(200, done)
});
})
describe('Permission', function(){
it('errors if can connect without correct APIKey', function(done) {
// This is broken because Etherpad doesn't handle HTTP codes properly see #2343
// If your APIKey is password you deserve to fail all tests anyway
var permErrorURL = '/api/'+apiVersion+'/createPad?apikey=password&padID=test';
api.get(permErrorURL)
.expect(401, done)
});
})
/* Pad Tests Order of execution
-> deletePad -- This gives us a guaranteed clear environment
-> createPad
-> getRevisions -- Should be 0
-> getSavedRevisionsCount(padID) -- Should be 0
-> listSavedRevisions(padID) -- Should be an empty array
-> getHTML -- Should be the default pad text in HTML format
-> deletePad -- Should just delete a pad
-> getHTML -- Should return an error
-> createPad(withText)
-> getText -- Should have the text specified above as the pad text
-> setText
-> getText -- Should be the text set before
-> getRevisions -- Should be 0 still?
-> saveRevision
-> getSavedRevisionsCount(padID) -- Should be 0 still?
-> listSavedRevisions(padID) -- Should be an empty array still ?
-> padUsersCount -- Should be 0
-> getReadOnlyId -- Should be a value
-> listAuthorsOfPad(padID) -- should be empty array?
-> getLastEdited(padID) -- Should be when pad was made
-> setText(padId)
-> getLastEdited(padID) -- Should be when setText was performed
-> padUsers(padID) -- Should be when setText was performed
-> setText(padId, "hello world")
-> getLastEdited(padID) -- Should be when pad was made
-> getText(padId) -- Should be "hello world"
-> movePad(padID, newPadId) -- Should provide consistant pad data
-> getText(newPadId) -- Should be "hello world"
-> movePad(newPadID, originalPadId) -- Should provide consistant pad data
-> getText(originalPadId) -- Should be "hello world"
-> getLastEdited(padID) -- Should not be 0
-> setHTML(padID) -- Should fail on invalid HTML
-> setHTML(padID) *3 -- Should fail on invalid HTML
-> getHTML(padID) -- Should return HTML close to posted HTML
*/
describe('deletePad', function(){
it('deletes a Pad', function(done) {
api.get(endPoint('deletePad')+"&padID="+testPadId)
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('createPad', function(){
it('creates a new Pad', function(done) {
api.get(endPoint('createPad')+"&padID="+testPadId)
.expect(function(res){
if(res.body.code !== 0) throw new Error("Unable to create new Pad");
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('getRevisionsCount', function(){
it('gets revision count of Pad', function(done) {
api.get(endPoint('getRevisionsCount')+"&padID="+testPadId)
.expect(function(res){
if(res.body.code !== 0) throw new Error("Unable to get Revision Count");
if(res.body.data.revisions !== 0) throw new Error("Incorrect Revision Count");
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('getSavedRevisionsCount', function(){
it('gets saved revisions count of Pad', function(done) {
api.get(endPoint('getSavedRevisionsCount')+"&padID="+testPadId)
.expect(function(res){
if(res.body.code !== 0) throw new Error("Unable to get Saved Revisions Count");
if(res.body.data.savedRevisions !== 0) throw new Error("Incorrect Saved Revisions Count");
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('listSavedRevisions', function(){
it('gets saved revision list of Pad', function(done) {
api.get(endPoint('listSavedRevisions')+"&padID="+testPadId)
.expect(function(res){
if(res.body.code !== 0) throw new Error("Unable to get Saved Revisions List");
if(!res.body.data.savedRevisions.equals([])) throw new Error("Incorrect Saved Revisions List");
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('getHTML', function(){
it('get the HTML of Pad', function(done) {
api.get(endPoint('getHTML')+"&padID="+testPadId)
.expect(function(res){
if(res.body.data.html.length <= 1) throw new Error("Unable to get the HTML");
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('deletePad', function(){
it('deletes a Pad', function(done) {
api.get(endPoint('deletePad')+"&padID="+testPadId)
.expect(function(res){
if(res.body.code !== 0) throw new Error("Pad Deletion failed")
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('getHTML', function(){
it('get the HTML of a Pad -- Should return a failure', function(done) {
api.get(endPoint('getHTML')+"&padID="+testPadId)
.expect(function(res){
if(res.body.code !== 1) throw new Error("Pad deletion failed")
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('createPad', function(){
it('creates a new Pad with text', function(done) {
api.get(endPoint('createPad')+"&padID="+testPadId+"&text=testText")
.expect(function(res){
if(res.body.code !== 0) throw new Error("Pad Creation failed")
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('getText', function(){
it('gets the Pad text and expect it to be testText with \n which is a line break', function(done) {
api.get(endPoint('getText')+"&padID="+testPadId)
.expect(function(res){
if(res.body.data.text !== "testText\n") throw new Error("Pad Creation with text")
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('setText', function(){
it('creates a new Pad with text', function(done) {
api.get(endPoint('setText')+"&padID="+testPadId+"&text=testTextTwo")
.expect(function(res){
if(res.body.code !== 0) throw new Error("Pad setting text failed");
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('getText', function(){
it('gets the Pad text', function(done) {
api.get(endPoint('getText')+"&padID="+testPadId)
.expect(function(res){
if(res.body.data.text !== "testTextTwo\n") throw new Error("Setting Text")
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('getRevisionsCount', function(){
it('gets Revision Count of a Pad', function(done) {
api.get(endPoint('getRevisionsCount')+"&padID="+testPadId)
.expect(function(res){
if(res.body.data.revisions !== 1) throw new Error("Unable to get text revision count")
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('saveRevision', function(){
it('saves Revision', function(done) {
api.get(endPoint('saveRevision')+"&padID="+testPadId)
.expect(function(res){
if(res.body.code !== 0) throw new Error("Unable to save Revision");
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('getSavedRevisionsCount', function(){
it('gets saved revisions count of Pad', function(done) {
api.get(endPoint('getSavedRevisionsCount')+"&padID="+testPadId)
.expect(function(res){
if(res.body.code !== 0) throw new Error("Unable to get Saved Revisions Count");
if(res.body.data.savedRevisions !== 1) throw new Error("Incorrect Saved Revisions Count");
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('listSavedRevisions', function(){
it('gets saved revision list of Pad', function(done) {
api.get(endPoint('listSavedRevisions')+"&padID="+testPadId)
.expect(function(res){
if(res.body.code !== 0) throw new Error("Unable to get Saved Revisions List");
if(!res.body.data.savedRevisions.equals([1])) throw new Error("Incorrect Saved Revisions List");
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('padUsersCount', function(){
it('gets User Count of a Pad', function(done) {
api.get(endPoint('padUsersCount')+"&padID="+testPadId)
.expect(function(res){
if(res.body.data.padUsersCount !== 0) throw new Error("Incorrect Pad User count")
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('getReadOnlyID', function(){
it('Gets the Read Only ID of a Pad', function(done) {
api.get(endPoint('getReadOnlyID')+"&padID="+testPadId)
.expect(function(res){
if(!res.body.data.readOnlyID) throw new Error("No Read Only ID for Pad")
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('listAuthorsOfPad', function(){
it('Get Authors of the Pad', function(done) {
api.get(endPoint('listAuthorsOfPad')+"&padID="+testPadId)
.expect(function(res){
if(res.body.data.authorIDs.length !== 0) throw new Error("# of Authors of pad is not 0")
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('getLastEdited', function(){
it('Get When Pad was left Edited', function(done) {
api.get(endPoint('getLastEdited')+"&padID="+testPadId)
.expect(function(res){
if(!res.body.data.lastEdited){
throw new Error("# of Authors of pad is not 0")
}else{
lastEdited = res.body.data.lastEdited;
}
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('setText', function(){
it('creates a new Pad with text', function(done) {
api.get(endPoint('setText')+"&padID="+testPadId+"&text=testTextTwo")
.expect(function(res){
if(res.body.code !== 0) throw new Error("Pad setting text failed");
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('getLastEdited', function(){
it('Get When Pad was left Edited', function(done) {
api.get(endPoint('getLastEdited')+"&padID="+testPadId)
.expect(function(res){
if(res.body.data.lastEdited <= lastEdited){
throw new Error("Editing A Pad is not updating when it was last edited")
}
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('padUsers', function(){
it('gets User Count of a Pad', function(done) {
api.get(endPoint('padUsers')+"&padID="+testPadId)
.expect(function(res){
if(res.body.data.padUsers.length !== 0) throw new Error("Incorrect Pad Users")
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('deletePad', function(){
it('deletes a Pad', function(done) {
api.get(endPoint('deletePad')+"&padID="+testPadId)
.expect(function(res){
if(res.body.code !== 0) throw new Error("Pad Deletion failed")
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
var originalPadId = testPadId;
var newPadId = makeid();
describe('createPad', function(){
it('creates a new Pad with text', function(done) {
api.get(endPoint('createPad')+"&padID="+testPadId)
.expect(function(res){
if(res.body.code !== 0) throw new Error("Pad Creation failed")
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('setText', function(){
it('Sets text on a pad Id', function(done) {
api.get(endPoint('setText')+"&padID="+testPadId+"&text="+text)
.expect(function(res){
if(res.body.code !== 0) throw new Error("Pad Set Text failed")
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('getText', function(){
it('Gets text on a pad Id', function(done) {
api.get(endPoint('getText')+"&padID="+testPadId)
.expect(function(res){
if(res.body.code !== 0) throw new Error("Pad Get Text failed")
if(res.body.data.text !== text+"\n") throw new Error("Pad Text not set properly");
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('getLastEdited', function(){
it('Gets when pad was last edited', function(done) {
api.get(endPoint('getLastEdited')+"&padID="+testPadId)
.expect(function(res){
if(res.body.lastEdited === 0) throw new Error("Get Last Edited Failed")
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('movePad', function(){
it('Move a Pad to a different Pad ID', function(done) {
api.get(endPoint('movePad')+"&sourceID="+testPadId+"&destinationID="+newPadId+"&force=true")
.expect(function(res){
if(res.body.code !== 0) throw new Error("Moving Pad Failed")
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('getText', function(){
it('Gets text on a pad Id', function(done) {
api.get(endPoint('getText')+"&padID="+newPadId)
.expect(function(res){
if(res.body.data.text !== text+"\n") throw new Error("Pad Get Text failed")
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('movePad', function(){
it('Move a Pad to a different Pad ID', function(done) {
api.get(endPoint('movePad')+"&sourceID="+newPadId+"&destinationID="+testPadId+"&force=false")
.expect(function(res){
if(res.body.code !== 0) throw new Error("Moving Pad Failed")
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('getText', function(){
it('Gets text on a pad Id', function(done) {
api.get(endPoint('getText')+"&padID="+testPadId)
.expect(function(res){
if(res.body.data.text !== text+"\n") throw new Error("Pad Get Text failed")
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('getLastEdited', function(){
it('Gets when pad was last edited', function(done) {
api.get(endPoint('getLastEdited')+"&padID="+testPadId)
.expect(function(res){
if(res.body.lastEdited === 0) throw new Error("Get Last Edited Failed")
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('setHTML', function(){
it('Sets the HTML of a Pad attempting to pass ugly HTML', function(done) {
var html = "<div><b>Hello HTML</title></head></div>";
api.get(endPoint('setHTML')+"&padID="+testPadId+"&html="+html)
.expect(function(res){
console.log(res.body.code);
if(res.body.code !== 1) throw new Error("Allowing crappy HTML to be imported")
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('setHTML', function(){
it('Sets the HTML of a Pad with a bunch of weird unordered lists inserted', function(done) {
api.get(endPoint('setHTML')+"&padID="+testPadId+"&html="+ULhtml)
.expect(function(res){
if(res.body.code !== 0) throw new Error("List HTML cant be imported")
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('getHTML', function(){
it('Gets the HTML of a Pad with a bunch of weird unordered lists inserted', function(done) {
api.get(endPoint('getHTML')+"&padID="+testPadId)
.expect(function(res){
var ehtml = res.body.data.html.replace("<br></body>", "</body>").toLowerCase();
var uhtml = ULhtml.toLowerCase();
if(ehtml !== uhtml) throw new Error("Imported HTML does not match served HTML")
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
/*
-> movePadForce Test
*/
var endPoint = function(point){
return '/api/'+apiVersion+'/'+point+'?apikey='+apiKey;
}
function makeid()
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 5; i++ ){
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
function generateLongText(){
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 80000; i++ ){
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
// Need this to compare arrays (listSavedRevisions test)
Array.prototype.equals = function (array) {
// if the other array is a falsy value, return
if (!array)
return false;
// compare lengths - can save a lot of time
if (this.length != array.length)
return false;
for (var i = 0, l=this.length; i < l; i++) {
// Check if we have nested arrays
if (this[i] instanceof Array && array[i] instanceof Array) {
// recurse into the nested arrays
if (!this[i].equals(array[i]))
return false;
} else if (this[i] != array[i]) {
// Warning - two different object instances will never be equal: {x:20} != {x:20}
return false;
}
}
return true;
}
|