tests: Promisify some backend tests

This commit is contained in:
Richard Hansen 2021-10-02 19:17:47 -04:00
parent 39a971e3b9
commit dd37251da4
2 changed files with 360 additions and 490 deletions

View file

@ -8,6 +8,7 @@
const common = require('../../common'); const common = require('../../common');
const fs = require('fs'); const fs = require('fs');
const fsp = fs.promises;
let agent; let agent;
const apiKey = common.apiKey; const apiKey = common.apiKey;
@ -20,73 +21,59 @@ describe(__filename, function () {
before(async function () { agent = await common.init(); }); before(async function () { agent = await common.init(); });
describe('Connectivity For Character Encoding', function () { describe('Connectivity For Character Encoding', function () {
it('can connect', function (done) { it('can connect', async function () {
agent.get('/api/') await agent.get('/api/')
.expect('Content-Type', /json/) .expect(200)
.expect(200, done); .expect('Content-Type', /json/);
}); });
}); });
describe('API Versioning', function () { describe('API Versioning', function () {
it('finds the version tag', function (done) { it('finds the version tag', async function () {
agent.get('/api/') const res = await agent.get('/api/')
.expect((res) => { .expect(200);
apiVersion = res.body.currentVersion; apiVersion = res.body.currentVersion;
if (!res.body.currentVersion) throw new Error('No version set in API'); if (!res.body.currentVersion) throw new Error('No version set in API');
return;
})
.expect(200, done);
}); });
}); });
describe('Permission', function () { describe('Permission', function () {
it('errors with invalid APIKey', function (done) { it('errors with invalid APIKey', async function () {
// This is broken because Etherpad doesn't handle HTTP codes properly see #2343 // 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 // If your APIKey is password you deserve to fail all tests anyway
const permErrorURL = `/api/${apiVersion}/createPad?apikey=password&padID=test`; await agent.get(`/api/${apiVersion}/createPad?apikey=password&padID=test`)
agent.get(permErrorURL) .expect(401);
.expect(401, done);
}); });
}); });
describe('createPad', function () { describe('createPad', function () {
it('creates a new Pad', function (done) { it('creates a new Pad', async function () {
agent.get(`${endPoint('createPad')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('createPad')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) throw new Error('Unable to create new Pad'); if (res.body.code !== 0) throw new Error('Unable to create new Pad');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('setHTML', function () { describe('setHTML', function () {
it('Sets the HTML of a Pad attempting to weird utf8 encoded content', function (done) { it('Sets the HTML of a Pad attempting to weird utf8 encoded content', async function () {
fs.readFile('tests/backend/specs/api/emojis.html', 'utf8', (err, html) => { const res = await agent.post(endPoint('setHTML'))
agent.post(endPoint('setHTML'))
.send({ .send({
padID: testPadId, padID: testPadId,
html, html: await fsp.readFile('tests/backend/specs/api/emojis.html', 'utf8'),
}) })
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) throw new Error("Can't set HTML properly"); if (res.body.code !== 0) throw new Error("Can't set HTML properly");
})
.expect('Content-Type', /json/)
.expect(200, done);
});
}); });
}); });
describe('getHTML', function () { describe('getHTML', function () {
it('get the HTML of Pad with emojis', function (done) { it('get the HTML of Pad with emojis', async function () {
agent.get(`${endPoint('getHTML')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('getHTML')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
if (res.body.data.html.indexOf('&#127484') === -1) { .expect('Content-Type', /json/);
throw new Error('Unable to get the HTML'); if (res.body.data.html.indexOf('&#127484') === -1) throw new Error('Unable to get the HTML');
}
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
}); });

View file

@ -8,7 +8,6 @@
*/ */
const assert = require('assert').strict; const assert = require('assert').strict;
const async = require('async');
const common = require('../../common'); const common = require('../../common');
let agent; let agent;
@ -50,32 +49,28 @@ describe(__filename, function () {
before(async function () { agent = await common.init(); }); before(async function () { agent = await common.init(); });
describe('Connectivity', function () { describe('Connectivity', function () {
it('can connect', function (done) { it('can connect', async function () {
agent.get('/api/') await agent.get('/api/')
.expect('Content-Type', /json/) .expect(200)
.expect(200, done); .expect('Content-Type', /json/);
}); });
}); });
describe('API Versioning', function () { describe('API Versioning', function () {
it('finds the version tag', function (done) { it('finds the version tag', async function () {
agent.get('/api/') const res = await agent.get('/api/')
.expect((res) => { .expect(200);
apiVersion = res.body.currentVersion; apiVersion = res.body.currentVersion;
if (!res.body.currentVersion) throw new Error('No version set in API'); if (!apiVersion) throw new Error('No version set in API');
return;
})
.expect(200, done);
}); });
}); });
describe('Permission', function () { describe('Permission', function () {
it('errors with invalid APIKey', function (done) { it('errors with invalid APIKey', async function () {
// This is broken because Etherpad doesn't handle HTTP codes properly see #2343 // 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 // If your APIKey is password you deserve to fail all tests anyway
const permErrorURL = `/api/${apiVersion}/createPad?apikey=password&padID=test`; await agent.get(`/api/${apiVersion}/createPad?apikey=password&padID=test`)
agent.get(permErrorURL) .expect(401);
.expect(401, done);
}); });
}); });
@ -122,313 +117,262 @@ describe(__filename, function () {
*/ */
describe('deletePad', function () { describe('deletePad', function () {
it('deletes a Pad', function (done) { it('deletes a Pad', async function () {
agent.get(`${endPoint('deletePad')}&padID=${testPadId}`) await agent.get(`${endPoint('deletePad')}&padID=${testPadId}`)
.expect('Content-Type', /json/) .expect(200) // @TODO: we shouldn't expect 200 here since the pad may not exist
.expect(200, done); // @TODO: we shouldn't expect 200 here since the pad may not exist .expect('Content-Type', /json/);
}); });
}); });
describe('createPad', function () { describe('createPad', function () {
it('creates a new Pad', function (done) { it('creates a new Pad', async function () {
agent.get(`${endPoint('createPad')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('createPad')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) throw new Error('Unable to create new Pad'); if (res.body.code !== 0) throw new Error('Unable to create new Pad');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('getRevisionsCount', function () { describe('getRevisionsCount', function () {
it('gets revision count of Pad', function (done) { it('gets revision count of Pad', async function () {
agent.get(`${endPoint('getRevisionsCount')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('getRevisionsCount')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) throw new Error('Unable to get Revision Count'); 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'); if (res.body.data.revisions !== 0) throw new Error('Incorrect Revision Count');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('getSavedRevisionsCount', function () { describe('getSavedRevisionsCount', function () {
it('gets saved revisions count of Pad', function (done) { it('gets saved revisions count of Pad', async function () {
agent.get(`${endPoint('getSavedRevisionsCount')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('getSavedRevisionsCount')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) throw new Error('Unable to get Saved Revisions Count'); if (res.body.code !== 0) throw new Error('Unable to get Saved Revisions Count');
if (res.body.data.savedRevisions !== 0) { if (res.body.data.savedRevisions !== 0) throw new Error('Incorrect Saved Revisions Count');
throw new Error('Incorrect Saved Revisions Count');
}
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('listSavedRevisions', function () { describe('listSavedRevisions', function () {
it('gets saved revision list of Pad', function (done) { it('gets saved revision list of Pad', async function () {
agent.get(`${endPoint('listSavedRevisions')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('listSavedRevisions')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) throw new Error('Unable to get Saved Revisions List'); if (res.body.code !== 0) throw new Error('Unable to get Saved Revisions List');
assert.deepEqual(res.body.data.savedRevisions, []); assert.deepEqual(res.body.data.savedRevisions, []);
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('getHTML', function () { describe('getHTML', function () {
it('get the HTML of Pad', function (done) { it('get the HTML of Pad', async function () {
agent.get(`${endPoint('getHTML')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('getHTML')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.data.html.length <= 1) throw new Error('Unable to get the HTML'); if (res.body.data.html.length <= 1) throw new Error('Unable to get the HTML');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('listAllPads', function () { describe('listAllPads', function () {
it('list all pads', function (done) { it('list all pads', async function () {
agent.get(endPoint('listAllPads')) const res = await agent.get(endPoint('listAllPads'))
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.data.padIDs.includes(testPadId) !== true) { if (res.body.data.padIDs.includes(testPadId) !== true) {
throw new Error('Unable to find pad in pad list'); throw new Error('Unable to find pad in pad list');
} }
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('deletePad', function () { describe('deletePad', function () {
it('deletes a Pad', function (done) { it('deletes a Pad', async function () {
agent.get(`${endPoint('deletePad')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('deletePad')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) throw new Error('Pad Deletion failed'); if (res.body.code !== 0) throw new Error('Pad Deletion failed');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('listAllPads', function () { describe('listAllPads', function () {
it('list all pads', function (done) { it('list all pads', async function () {
agent.get(endPoint('listAllPads')) const res = await agent.get(endPoint('listAllPads'))
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.data.padIDs.includes(testPadId) !== false) { if (res.body.data.padIDs.includes(testPadId) !== false) {
throw new Error('Test pad should not be in pads list'); throw new Error('Test pad should not be in pads list');
} }
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('getHTML', function () { describe('getHTML', function () {
it('get the HTML of a Pad -- Should return a failure', function (done) { it('get the HTML of a Pad -- Should return a failure', async function () {
agent.get(`${endPoint('getHTML')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('getHTML')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 1) throw new Error('Pad deletion failed'); if (res.body.code !== 1) throw new Error('Pad deletion failed');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('createPad', function () { describe('createPad', function () {
it('creates a new Pad with text', function (done) { it('creates a new Pad with text', async function () {
agent.get(`${endPoint('createPad')}&padID=${testPadId}&text=testText`) const res = await agent.get(`${endPoint('createPad')}&padID=${testPadId}&text=testText`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) throw new Error('Pad Creation failed'); if (res.body.code !== 0) throw new Error('Pad Creation failed');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('getText', function () { describe('getText', function () {
it('gets the Pad text and expect it to be testText with \n which is a line break', function (done) { it('gets the Pad text and expect it to be testText with \n which is a line break', async function () {
agent.get(`${endPoint('getText')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('getText')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.data.text !== 'testText\n') throw new Error('Pad Creation with text'); if (res.body.data.text !== 'testText\n') throw new Error('Pad Creation with text');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('setText', function () { describe('setText', function () {
it('creates a new Pad with text', function (done) { it('creates a new Pad with text', async function () {
agent.post(endPoint('setText')) const res = await agent.post(endPoint('setText'))
.send({ .send({
padID: testPadId, padID: testPadId,
text: 'testTextTwo', text: 'testTextTwo',
}) })
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) throw new Error('Pad setting text failed'); if (res.body.code !== 0) throw new Error('Pad setting text failed');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('getText', function () { describe('getText', function () {
it('gets the Pad text', function (done) { it('gets the Pad text', async function () {
agent.get(`${endPoint('getText')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('getText')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.data.text !== 'testTextTwo\n') throw new Error('Setting Text'); if (res.body.data.text !== 'testTextTwo\n') throw new Error('Setting Text');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('getRevisionsCount', function () { describe('getRevisionsCount', function () {
it('gets Revision Count of a Pad', function (done) { it('gets Revision Count of a Pad', async function () {
agent.get(`${endPoint('getRevisionsCount')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('getRevisionsCount')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.data.revisions !== 1) throw new Error('Unable to get text revision count'); 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 () { describe('saveRevision', function () {
it('saves Revision', function (done) { it('saves Revision', async function () {
agent.get(`${endPoint('saveRevision')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('saveRevision')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) throw new Error('Unable to save Revision'); if (res.body.code !== 0) throw new Error('Unable to save Revision');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('getSavedRevisionsCount', function () { describe('getSavedRevisionsCount', function () {
it('gets saved revisions count of Pad', function (done) { it('gets saved revisions count of Pad', async function () {
agent.get(`${endPoint('getSavedRevisionsCount')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('getSavedRevisionsCount')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) throw new Error('Unable to get Saved Revisions Count'); if (res.body.code !== 0) throw new Error('Unable to get Saved Revisions Count');
if (res.body.data.savedRevisions !== 1) { if (res.body.data.savedRevisions !== 1) {
throw new Error('Incorrect Saved Revisions Count'); throw new Error('Incorrect Saved Revisions Count');
} }
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('listSavedRevisions', function () { describe('listSavedRevisions', function () {
it('gets saved revision list of Pad', function (done) { it('gets saved revision list of Pad', async function () {
agent.get(`${endPoint('listSavedRevisions')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('listSavedRevisions')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) throw new Error('Unable to get Saved Revisions List'); if (res.body.code !== 0) throw new Error('Unable to get Saved Revisions List');
assert.deepEqual(res.body.data.savedRevisions, [1]); assert.deepEqual(res.body.data.savedRevisions, [1]);
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('padUsersCount', function () { describe('padUsersCount', function () {
it('gets User Count of a Pad', function (done) { it('gets User Count of a Pad', async function () {
agent.get(`${endPoint('padUsersCount')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('padUsersCount')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.data.padUsersCount !== 0) throw new Error('Incorrect Pad User count'); if (res.body.data.padUsersCount !== 0) throw new Error('Incorrect Pad User count');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('getReadOnlyID', function () { describe('getReadOnlyID', function () {
it('Gets the Read Only ID of a Pad', function (done) { it('Gets the Read Only ID of a Pad', async function () {
agent.get(`${endPoint('getReadOnlyID')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('getReadOnlyID')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (!res.body.data.readOnlyID) throw new Error('No Read Only ID for Pad'); if (!res.body.data.readOnlyID) throw new Error('No Read Only ID for Pad');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('listAuthorsOfPad', function () { describe('listAuthorsOfPad', function () {
it('Get Authors of the Pad', function (done) { it('Get Authors of the Pad', async function () {
agent.get(`${endPoint('listAuthorsOfPad')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('listAuthorsOfPad')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.data.authorIDs.length !== 0) { if (res.body.data.authorIDs.length !== 0) {
throw new Error('# of Authors of pad is not 0'); throw new Error('# of Authors of pad is not 0');
} }
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('getLastEdited', function () { describe('getLastEdited', function () {
it('Get When Pad was left Edited', function (done) { it('Get When Pad was left Edited', async function () {
agent.get(`${endPoint('getLastEdited')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('getLastEdited')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (!res.body.data.lastEdited) { if (!res.body.data.lastEdited) {
throw new Error('# of Authors of pad is not 0'); throw new Error('# of Authors of pad is not 0');
} else { } else {
lastEdited = res.body.data.lastEdited; lastEdited = res.body.data.lastEdited;
} }
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('setText', function () { describe('setText', function () {
it('creates a new Pad with text', function (done) { it('creates a new Pad with text', async function () {
agent.post(endPoint('setText')) const res = await agent.post(endPoint('setText'))
.send({ .send({
padID: testPadId, padID: testPadId,
text: 'testTextTwo', text: 'testTextTwo',
}) })
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) throw new Error('Pad setting text failed'); if (res.body.code !== 0) throw new Error('Pad setting text failed');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('getLastEdited', function () { describe('getLastEdited', function () {
it('Get When Pad was left Edited', function (done) { it('Get When Pad was left Edited', async function () {
agent.get(`${endPoint('getLastEdited')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('getLastEdited')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.data.lastEdited <= lastEdited) { if (res.body.data.lastEdited <= lastEdited) {
throw new Error('Editing A Pad is not updating when it was last edited'); throw new Error('Editing A Pad is not updating when it was last edited');
} }
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('padUsers', function () { describe('padUsers', function () {
it('gets User Count of a Pad', function (done) { it('gets User Count of a Pad', async function () {
agent.get(`${endPoint('padUsers')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('padUsers')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.data.padUsers.length !== 0) throw new Error('Incorrect Pad Users'); if (res.body.data.padUsers.length !== 0) throw new Error('Incorrect Pad Users');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('deletePad', function () { describe('deletePad', function () {
it('deletes a Pad', function (done) { it('deletes a Pad', async function () {
agent.get(`${endPoint('deletePad')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('deletePad')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) throw new Error('Pad Deletion failed'); if (res.body.code !== 0) throw new Error('Pad Deletion failed');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
@ -436,196 +380,167 @@ describe(__filename, function () {
const copiedPadId = makeid(); const copiedPadId = makeid();
describe('createPad', function () { describe('createPad', function () {
it('creates a new Pad with text', function (done) { it('creates a new Pad with text', async function () {
agent.get(`${endPoint('createPad')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('createPad')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) throw new Error('Pad Creation failed'); if (res.body.code !== 0) throw new Error('Pad Creation failed');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('setText', function () { describe('setText', function () {
it('Sets text on a pad Id', function (done) { it('Sets text on a pad Id', async function () {
agent.post(`${endPoint('setText')}&padID=${testPadId}`) const res = await agent.post(`${endPoint('setText')}&padID=${testPadId}`)
.field({text}) .field({text})
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) throw new Error('Pad Set Text failed'); if (res.body.code !== 0) throw new Error('Pad Set Text failed');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('getText', function () { describe('getText', function () {
it('Gets text on a pad Id', function (done) { it('Gets text on a pad Id', async function () {
agent.get(`${endPoint('getText')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('getText')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) throw new Error('Pad Get Text failed'); 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'); if (res.body.data.text !== `${text}\n`) throw new Error('Pad Text not set properly');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('setText', function () { describe('setText', function () {
it('Sets text on a pad Id including an explicit newline', function (done) { it('Sets text on a pad Id including an explicit newline', async function () {
agent.post(`${endPoint('setText')}&padID=${testPadId}`) const res = await agent.post(`${endPoint('setText')}&padID=${testPadId}`)
.field({text: `${text}\n`}) .field({text: `${text}\n`})
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) throw new Error('Pad Set Text failed'); if (res.body.code !== 0) throw new Error('Pad Set Text failed');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('getText', function () { describe('getText', function () {
it("Gets text on a pad Id and doesn't have an excess newline", function (done) { it("Gets text on a pad Id and doesn't have an excess newline", async function () {
agent.get(`${endPoint('getText')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('getText')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) throw new Error('Pad Get Text failed'); 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'); 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 () { describe('getLastEdited', function () {
it('Gets when pad was last edited', function (done) { it('Gets when pad was last edited', async function () {
agent.get(`${endPoint('getLastEdited')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('getLastEdited')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.lastEdited === 0) throw new Error('Get Last Edited Failed'); if (res.body.lastEdited === 0) throw new Error('Get Last Edited Failed');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('movePad', function () { describe('movePad', function () {
it('Move a Pad to a different Pad ID', function (done) { it('Move a Pad to a different Pad ID', async function () {
agent.get(`${endPoint('movePad')}&sourceID=${testPadId}&destinationID=${newPadId}&force=true`) const res = await agent.get(
.expect((res) => { `${endPoint('movePad')}&sourceID=${testPadId}&destinationID=${newPadId}&force=true`)
.expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) throw new Error('Moving Pad Failed'); if (res.body.code !== 0) throw new Error('Moving Pad Failed');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('getText', function () { describe('getText', function () {
it('Gets text on a pad Id', function (done) { it('Gets text on a pad Id', async function () {
agent.get(`${endPoint('getText')}&padID=${newPadId}`) const res = await agent.get(`${endPoint('getText')}&padID=${newPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.data.text !== `${text}\n`) throw new Error('Pad Get Text failed'); if (res.body.data.text !== `${text}\n`) throw new Error('Pad Get Text failed');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('movePad', function () { describe('movePad', function () {
it('Move a Pad to a different Pad ID', function (done) { it('Move a Pad to a different Pad ID', async function () {
agent.get(`${endPoint('movePad')}&sourceID=${newPadId}&destinationID=${testPadId}` + const res = await agent.get(
'&force=false') `${endPoint('movePad')}&sourceID=${newPadId}&destinationID=${testPadId}&force=false`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) throw new Error('Moving Pad Failed'); if (res.body.code !== 0) throw new Error('Moving Pad Failed');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('getText', function () { describe('getText', function () {
it('Gets text on a pad Id', function (done) { it('Gets text on a pad Id', async function () {
agent.get(`${endPoint('getText')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('getText')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.data.text !== `${text}\n`) throw new Error('Pad Get Text failed'); if (res.body.data.text !== `${text}\n`) throw new Error('Pad Get Text failed');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('getLastEdited', function () { describe('getLastEdited', function () {
it('Gets when pad was last edited', function (done) { it('Gets when pad was last edited', async function () {
agent.get(`${endPoint('getLastEdited')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('getLastEdited')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.lastEdited === 0) throw new Error('Get Last Edited Failed'); if (res.body.lastEdited === 0) throw new Error('Get Last Edited Failed');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('appendText', function () { describe('appendText', function () {
it('Append text to a pad Id', function (done) { it('Append text to a pad Id', async function () {
agent.get(`${endPoint('appendText', '1.2.13')}&padID=${testPadId}&text=hello`) const res = await agent.get(
.expect((res) => { `${endPoint('appendText', '1.2.13')}&padID=${testPadId}&text=hello`)
.expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) throw new Error('Pad Append Text failed'); if (res.body.code !== 0) throw new Error('Pad Append Text failed');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('getText', function () { describe('getText', function () {
it('Gets text on a pad Id', function (done) { it('Gets text on a pad Id', async function () {
agent.get(`${endPoint('getText')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('getText')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) throw new Error('Pad Get Text failed'); if (res.body.code !== 0) throw new Error('Pad Get Text failed');
if (res.body.data.text !== `${text}hello\n`) { if (res.body.data.text !== `${text}hello\n`) {
throw new Error('Pad Text not set properly'); throw new Error('Pad Text not set properly');
} }
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('setHTML', function () { describe('setHTML', function () {
it('Sets the HTML of a Pad attempting to pass ugly HTML', function (done) { it('Sets the HTML of a Pad attempting to pass ugly HTML', async function () {
const html = '<div><b>Hello HTML</title></head></div>'; const html = '<div><b>Hello HTML</title></head></div>';
agent.post(endPoint('setHTML')) const res = await agent.post(endPoint('setHTML'))
.send({ .send({
padID: testPadId, padID: testPadId,
html, html,
}) })
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) { if (res.body.code !== 0) {
throw new Error("Crappy HTML Can't be Imported[we weren't able to sanitize it']"); throw new Error("Crappy HTML Can't be Imported[we weren't able to sanitize it']");
} }
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('setHTML', function () { describe('setHTML', function () {
it('Sets the HTML of a Pad with complex nested lists of different types', function (done) { it('Sets the HTML of a Pad with complex nested lists of different types', async function () {
agent.post(endPoint('setHTML')) const res = await agent.post(endPoint('setHTML'))
.send({ .send({
padID: testPadId, padID: testPadId,
html: ulHtml, html: ulHtml,
}) })
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) throw new Error('List HTML cant be imported'); if (res.body.code !== 0) throw new Error('List HTML cant be imported');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('getHTML', function () { describe('getHTML', function () {
it('Gets back the HTML of a Pad with complex nested lists of different types', function (done) { it('Gets back the HTML of a Pad with complex nested lists of different types', async function () {
agent.get(`${endPoint('getHTML')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('getHTML')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
const receivedHtml = res.body.data.html.replace('<br></body>', '</body>').toLowerCase(); const receivedHtml = res.body.data.html.replace('<br></body>', '</body>').toLowerCase();
if (receivedHtml !== expectedHtml) { if (receivedHtml !== expectedHtml) {
throw new Error(`HTML received from export is not the one we were expecting. throw new Error(`HTML received from export is not the one we were expecting.
Received: Received:
@ -637,27 +552,23 @@ describe(__filename, function () {
Which is a slightly modified version of the originally imported one: Which is a slightly modified version of the originally imported one:
${ulHtml}`); ${ulHtml}`);
} }
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('setHTML', function () { describe('setHTML', function () {
it('Sets the HTML of a Pad with white space between list items', function (done) { it('Sets the HTML of a Pad with white space between list items', async function () {
agent.get(`${endPoint('setHTML')}&padID=${testPadId}&html=${ulSpaceHtml}`) const res = await agent.get(`${endPoint('setHTML')}&padID=${testPadId}&html=${ulSpaceHtml}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) throw new Error('List HTML cant be imported'); if (res.body.code !== 0) throw new Error('List HTML cant be imported');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('getHTML', function () { describe('getHTML', function () {
it('Gets back the HTML of a Pad with complex nested lists of different types', function (done) { it('Gets back the HTML of a Pad with complex nested lists of different types', async function () {
agent.get(`${endPoint('getHTML')}&padID=${testPadId}`) const res = await agent.get(`${endPoint('getHTML')}&padID=${testPadId}`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
const receivedHtml = res.body.data.html.replace('<br></body>', '</body>').toLowerCase(); const receivedHtml = res.body.data.html.replace('<br></body>', '</body>').toLowerCase();
if (receivedHtml !== expectedSpaceHtml) { if (receivedHtml !== expectedSpaceHtml) {
throw new Error(`HTML received from export is not the one we were expecting. throw new Error(`HTML received from export is not the one we were expecting.
@ -670,38 +581,26 @@ describe(__filename, function () {
Which is a slightly modified version of the originally imported one: Which is a slightly modified version of the originally imported one:
${ulSpaceHtml}`); ${ulSpaceHtml}`);
} }
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
describe('createPad', function () { describe('createPad', function () {
it('errors if pad can be created', function (done) { it('errors if pad can be created', async function () {
const badUrlChars = ['/', '%23', '%3F', '%26']; await Promise.all(['/', '%23', '%3F', '%26'].map(async (badUrlChar) => {
async.map( const res = await agent.get(`${endPoint('createPad')}&padID=${badUrlChar}`)
badUrlChars, .expect('Content-Type', /json/);
(badUrlChar, cb) => {
agent.get(`${endPoint('createPad')}&padID=${badUrlChar}`)
.expect((res) => {
if (res.body.code !== 1) throw new Error('Pad with bad characters was created'); if (res.body.code !== 1) throw new Error('Pad with bad characters was created');
}) }));
.expect('Content-Type', /json/)
.end(cb);
},
done);
}); });
}); });
describe('copyPad', function () { describe('copyPad', function () {
it('copies the content of a existent pad', function (done) { it('copies the content of a existent pad', async function () {
agent.get(`${endPoint('copyPad')}&sourceID=${testPadId}&destinationID=${copiedPadId}` + const res = await agent.get(
'&force=true') `${endPoint('copyPad')}&sourceID=${testPadId}&destinationID=${copiedPadId}&force=true`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) throw new Error('Copy Pad Failed'); if (res.body.code !== 0) throw new Error('Copy Pad Failed');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
}); });
@ -709,37 +608,30 @@ describe(__filename, function () {
const sourcePadId = makeid(); const sourcePadId = makeid();
let newPad; let newPad;
before(function (done) { before(async function () {
createNewPadWithHtml(sourcePadId, ulHtml, done); await createNewPadWithHtml(sourcePadId, ulHtml);
}); });
beforeEach(async function () { beforeEach(async function () {
newPad = makeid(); newPad = makeid();
}); });
it('returns a successful response', function (done) { it('returns a successful response', async function () {
agent.get(`${endPoint('copyPadWithoutHistory')}&sourceID=${sourcePadId}` + const res = await agent.get(`${endPoint('copyPadWithoutHistory')}&sourceID=${sourcePadId}` +
`&destinationID=${newPad}&force=false`) `&destinationID=${newPad}&force=false`)
.expect((res) => { .expect(200)
.expect('Content-Type', /json/);
if (res.body.code !== 0) throw new Error('Copy Pad Without History Failed'); if (res.body.code !== 0) throw new Error('Copy Pad Without History Failed');
})
.expect('Content-Type', /json/)
.expect(200, done);
}); });
// this test validates if the source pad's text and attributes are kept // this test validates if the source pad's text and attributes are kept
it('creates a new pad with the same content as the source pad', function (done) { it('creates a new pad with the same content as the source pad', async function () {
agent.get(`${endPoint('copyPadWithoutHistory')}&sourceID=${sourcePadId}` + let res = await agent.get(`${endPoint('copyPadWithoutHistory')}&sourceID=${sourcePadId}` +
`&destinationID=${newPad}&force=false`) `&destinationID=${newPad}&force=false`);
.expect((res) => {
if (res.body.code !== 0) throw new Error('Copy Pad Without History Failed'); if (res.body.code !== 0) throw new Error('Copy Pad Without History Failed');
}) res = await agent.get(`${endPoint('getHTML')}&padID=${newPad}`)
.end(() => { .expect(200);
agent.get(`${endPoint('getHTML')}&padID=${newPad}`) const receivedHtml = res.body.data.html.replace('<br><br></body>', '</body>').toLowerCase();
.expect((res) => {
const receivedHtml =
res.body.data.html.replace('<br><br></body>', '</body>').toLowerCase();
if (receivedHtml !== expectedHtml) { if (receivedHtml !== expectedHtml) {
throw new Error(`HTML received from export is not the one we were expecting. throw new Error(`HTML received from export is not the one we were expecting.
Received: Received:
@ -751,55 +643,49 @@ describe(__filename, function () {
Which is a slightly modified version of the originally imported one: Which is a slightly modified version of the originally imported one:
${ulHtml}`); ${ulHtml}`);
} }
})
.expect(200, done);
});
}); });
context('when try copy a pad with a group that does not exist', function () { context('when try copy a pad with a group that does not exist', function () {
const padId = makeid(); const padId = makeid();
const padWithNonExistentGroup = `notExistentGroup$${padId}`; const padWithNonExistentGroup = `notExistentGroup$${padId}`;
it('throws an error', function (done) { it('throws an error', async function () {
agent.get(`${endPoint('copyPadWithoutHistory')}&sourceID=${sourcePadId}&` + const res = await agent.get(`${endPoint('copyPadWithoutHistory')}` +
`destinationID=${padWithNonExistentGroup}&force=true`) `&sourceID=${sourcePadId}` +
.expect((res) => { `&destinationID=${padWithNonExistentGroup}&force=true`)
.expect(200);
// code 1, it means an error has happened // code 1, it means an error has happened
if (res.body.code !== 1) throw new Error('It should report an error'); if (res.body.code !== 1) throw new Error('It should report an error');
})
.expect(200, done);
}); });
}); });
context('when try copy a pad and destination pad already exist', function () { context('when try copy a pad and destination pad already exist', function () {
const padIdExistent = makeid(); const padIdExistent = makeid();
before(function (done) { before(async function () {
createNewPadWithHtml(padIdExistent, ulHtml, done); await createNewPadWithHtml(padIdExistent, ulHtml);
}); });
context('and force is false', function () { context('and force is false', function () {
it('throws an error', function (done) { it('throws an error', async function () {
agent.get(`${endPoint('copyPadWithoutHistory')}&sourceID=${sourcePadId}` + const res = await agent.get(`${endPoint('copyPadWithoutHistory')}` +
`&sourceID=${sourcePadId}` +
`&destinationID=${padIdExistent}&force=false`) `&destinationID=${padIdExistent}&force=false`)
.expect((res) => { .expect(200);
// code 1, it means an error has happened // code 1, it means an error has happened
if (res.body.code !== 1) throw new Error('It should report an error'); if (res.body.code !== 1) throw new Error('It should report an error');
})
.expect(200, done);
}); });
}); });
context('and force is true', function () { context('and force is true', function () {
it('returns a successful response', function (done) { it('returns a successful response', async function () {
agent.get(`${endPoint('copyPadWithoutHistory')}&sourceID=${sourcePadId}` + const res = await agent.get(`${endPoint('copyPadWithoutHistory')}` +
`&sourceID=${sourcePadId}` +
`&destinationID=${padIdExistent}&force=true`) `&destinationID=${padIdExistent}&force=true`)
.expect((res) => { .expect(200);
// code 1, it means an error has happened // code 1, it means an error has happened
if (res.body.code !== 0) { if (res.body.code !== 0) {
throw new Error('Copy pad without history with force true failed'); throw new Error('Copy pad without history with force true failed');
} }
})
.expect(200, done);
}); });
}); });
}); });
@ -811,15 +697,12 @@ describe(__filename, function () {
*/ */
const createNewPadWithHtml = (padId, html, cb) => { const createNewPadWithHtml = async (padId, html) => {
agent.get(`${endPoint('createPad')}&padID=${padId}`) await agent.get(`${endPoint('createPad')}&padID=${padId}`);
.end(() => { await agent.post(endPoint('setHTML'))
agent.post(endPoint('setHTML'))
.send({ .send({
padID: padId, padID: padId,
html, html,
})
.end(cb);
}); });
}; };