From dd37251da4d3b0783d69198dd4961585a452ecc9 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sat, 2 Oct 2021 19:17:47 -0400 Subject: [PATCH] tests: Promisify some backend tests --- .../backend/specs/api/characterEncoding.js | 77 +- src/tests/backend/specs/api/pad.js | 773 ++++++++---------- 2 files changed, 360 insertions(+), 490 deletions(-) diff --git a/src/tests/backend/specs/api/characterEncoding.js b/src/tests/backend/specs/api/characterEncoding.js index 300cf2d17..2e833d12c 100644 --- a/src/tests/backend/specs/api/characterEncoding.js +++ b/src/tests/backend/specs/api/characterEncoding.js @@ -8,6 +8,7 @@ const common = require('../../common'); const fs = require('fs'); +const fsp = fs.promises; let agent; const apiKey = common.apiKey; @@ -20,73 +21,59 @@ describe(__filename, function () { before(async function () { agent = await common.init(); }); describe('Connectivity For Character Encoding', function () { - it('can connect', function (done) { - agent.get('/api/') - .expect('Content-Type', /json/) - .expect(200, done); + it('can connect', async function () { + await agent.get('/api/') + .expect(200) + .expect('Content-Type', /json/); }); }); describe('API Versioning', function () { - it('finds the version tag', function (done) { - agent.get('/api/') - .expect((res) => { - apiVersion = res.body.currentVersion; - if (!res.body.currentVersion) throw new Error('No version set in API'); - return; - }) - .expect(200, done); + it('finds the version tag', async function () { + const res = await agent.get('/api/') + .expect(200); + apiVersion = res.body.currentVersion; + if (!res.body.currentVersion) throw new Error('No version set in API'); }); }); 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 // If your APIKey is password you deserve to fail all tests anyway - const permErrorURL = `/api/${apiVersion}/createPad?apikey=password&padID=test`; - agent.get(permErrorURL) - .expect(401, done); + await agent.get(`/api/${apiVersion}/createPad?apikey=password&padID=test`) + .expect(401); }); }); describe('createPad', function () { - it('creates a new Pad', function (done) { - agent.get(`${endPoint('createPad')}&padID=${testPadId}`) - .expect((res) => { - if (res.body.code !== 0) throw new Error('Unable to create new Pad'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('creates a new Pad', async function () { + const res = await agent.get(`${endPoint('createPad')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.code !== 0) throw new Error('Unable to create new Pad'); }); }); describe('setHTML', function () { - it('Sets the HTML of a Pad attempting to weird utf8 encoded content', function (done) { - fs.readFile('tests/backend/specs/api/emojis.html', 'utf8', (err, html) => { - agent.post(endPoint('setHTML')) - .send({ - padID: testPadId, - html, - }) - .expect((res) => { - if (res.body.code !== 0) throw new Error("Can't set HTML properly"); - }) - .expect('Content-Type', /json/) - .expect(200, done); - }); + it('Sets the HTML of a Pad attempting to weird utf8 encoded content', async function () { + const res = await agent.post(endPoint('setHTML')) + .send({ + padID: testPadId, + html: await fsp.readFile('tests/backend/specs/api/emojis.html', 'utf8'), + }) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.code !== 0) throw new Error("Can't set HTML properly"); }); }); describe('getHTML', function () { - it('get the HTML of Pad with emojis', function (done) { - agent.get(`${endPoint('getHTML')}&padID=${testPadId}`) - .expect((res) => { - if (res.body.data.html.indexOf('🇼') === -1) { - throw new Error('Unable to get the HTML'); - } - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('get the HTML of Pad with emojis', async function () { + const res = await agent.get(`${endPoint('getHTML')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.data.html.indexOf('🇼') === -1) throw new Error('Unable to get the HTML'); }); }); }); diff --git a/src/tests/backend/specs/api/pad.js b/src/tests/backend/specs/api/pad.js index e50f9a6ab..9e53961f9 100644 --- a/src/tests/backend/specs/api/pad.js +++ b/src/tests/backend/specs/api/pad.js @@ -8,7 +8,6 @@ */ const assert = require('assert').strict; -const async = require('async'); const common = require('../../common'); let agent; @@ -50,32 +49,28 @@ describe(__filename, function () { before(async function () { agent = await common.init(); }); describe('Connectivity', function () { - it('can connect', function (done) { - agent.get('/api/') - .expect('Content-Type', /json/) - .expect(200, done); + it('can connect', async function () { + await agent.get('/api/') + .expect(200) + .expect('Content-Type', /json/); }); }); describe('API Versioning', function () { - it('finds the version tag', function (done) { - agent.get('/api/') - .expect((res) => { - apiVersion = res.body.currentVersion; - if (!res.body.currentVersion) throw new Error('No version set in API'); - return; - }) - .expect(200, done); + it('finds the version tag', async function () { + const res = await agent.get('/api/') + .expect(200); + apiVersion = res.body.currentVersion; + if (!apiVersion) throw new Error('No version set in API'); }); }); 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 // If your APIKey is password you deserve to fail all tests anyway - const permErrorURL = `/api/${apiVersion}/createPad?apikey=password&padID=test`; - agent.get(permErrorURL) - .expect(401, done); + await agent.get(`/api/${apiVersion}/createPad?apikey=password&padID=test`) + .expect(401); }); }); @@ -122,313 +117,262 @@ describe(__filename, function () { */ describe('deletePad', function () { - it('deletes a Pad', function (done) { - agent.get(`${endPoint('deletePad')}&padID=${testPadId}`) - .expect('Content-Type', /json/) - .expect(200, done); // @TODO: we shouldn't expect 200 here since the pad may not exist + it('deletes a Pad', async function () { + await agent.get(`${endPoint('deletePad')}&padID=${testPadId}`) + .expect(200) // @TODO: we shouldn't expect 200 here since the pad may not exist + .expect('Content-Type', /json/); }); }); describe('createPad', function () { - it('creates a new Pad', function (done) { - agent.get(`${endPoint('createPad')}&padID=${testPadId}`) - .expect((res) => { - if (res.body.code !== 0) throw new Error('Unable to create new Pad'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('creates a new Pad', async function () { + const res = await agent.get(`${endPoint('createPad')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.code !== 0) throw new Error('Unable to create new Pad'); }); }); describe('getRevisionsCount', function () { - it('gets revision count of Pad', function (done) { - agent.get(`${endPoint('getRevisionsCount')}&padID=${testPadId}`) - .expect((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); + it('gets revision count of Pad', async function () { + const res = await agent.get(`${endPoint('getRevisionsCount')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + 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'); }); }); describe('getSavedRevisionsCount', function () { - it('gets saved revisions count of Pad', function (done) { - agent.get(`${endPoint('getSavedRevisionsCount')}&padID=${testPadId}`) - .expect((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); + it('gets saved revisions count of Pad', async function () { + const res = await agent.get(`${endPoint('getSavedRevisionsCount')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + 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'); }); }); describe('listSavedRevisions', function () { - it('gets saved revision list of Pad', function (done) { - agent.get(`${endPoint('listSavedRevisions')}&padID=${testPadId}`) - .expect((res) => { - if (res.body.code !== 0) throw new Error('Unable to get Saved Revisions List'); - assert.deepEqual(res.body.data.savedRevisions, []); - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('gets saved revision list of Pad', async function () { + const res = await agent.get(`${endPoint('listSavedRevisions')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.code !== 0) throw new Error('Unable to get Saved Revisions List'); + assert.deepEqual(res.body.data.savedRevisions, []); }); }); describe('getHTML', function () { - it('get the HTML of Pad', function (done) { - agent.get(`${endPoint('getHTML')}&padID=${testPadId}`) - .expect((res) => { - if (res.body.data.html.length <= 1) throw new Error('Unable to get the HTML'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('get the HTML of Pad', async function () { + const res = await agent.get(`${endPoint('getHTML')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.data.html.length <= 1) throw new Error('Unable to get the HTML'); }); }); describe('listAllPads', function () { - it('list all pads', function (done) { - agent.get(endPoint('listAllPads')) - .expect((res) => { - if (res.body.data.padIDs.includes(testPadId) !== true) { - throw new Error('Unable to find pad in pad list'); - } - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('list all pads', async function () { + const res = await agent.get(endPoint('listAllPads')) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.data.padIDs.includes(testPadId) !== true) { + throw new Error('Unable to find pad in pad list'); + } }); }); describe('deletePad', function () { - it('deletes a Pad', function (done) { - agent.get(`${endPoint('deletePad')}&padID=${testPadId}`) - .expect((res) => { - if (res.body.code !== 0) throw new Error('Pad Deletion failed'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('deletes a Pad', async function () { + const res = await agent.get(`${endPoint('deletePad')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.code !== 0) throw new Error('Pad Deletion failed'); }); }); describe('listAllPads', function () { - it('list all pads', function (done) { - agent.get(endPoint('listAllPads')) - .expect((res) => { - if (res.body.data.padIDs.includes(testPadId) !== false) { - throw new Error('Test pad should not be in pads list'); - } - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('list all pads', async function () { + const res = await agent.get(endPoint('listAllPads')) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.data.padIDs.includes(testPadId) !== false) { + throw new Error('Test pad should not be in pads list'); + } }); }); describe('getHTML', function () { - it('get the HTML of a Pad -- Should return a failure', function (done) { - agent.get(`${endPoint('getHTML')}&padID=${testPadId}`) - .expect((res) => { - if (res.body.code !== 1) throw new Error('Pad deletion failed'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('get the HTML of a Pad -- Should return a failure', async function () { + const res = await agent.get(`${endPoint('getHTML')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.code !== 1) throw new Error('Pad deletion failed'); }); }); describe('createPad', function () { - it('creates a new Pad with text', function (done) { - agent.get(`${endPoint('createPad')}&padID=${testPadId}&text=testText`) - .expect((res) => { - if (res.body.code !== 0) throw new Error('Pad Creation failed'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('creates a new Pad with text', async function () { + const res = await agent.get(`${endPoint('createPad')}&padID=${testPadId}&text=testText`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.code !== 0) throw new Error('Pad Creation failed'); }); }); describe('getText', function () { - it('gets the Pad text and expect it to be testText with \n which is a line break', function (done) { - agent.get(`${endPoint('getText')}&padID=${testPadId}`) - .expect((res) => { - if (res.body.data.text !== 'testText\n') throw new Error('Pad Creation with text'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('gets the Pad text and expect it to be testText with \n which is a line break', async function () { + const res = await agent.get(`${endPoint('getText')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.data.text !== 'testText\n') throw new Error('Pad Creation with text'); }); }); describe('setText', function () { - it('creates a new Pad with text', function (done) { - agent.post(endPoint('setText')) + it('creates a new Pad with text', async function () { + const res = await agent.post(endPoint('setText')) .send({ padID: testPadId, text: 'testTextTwo', }) - .expect((res) => { - if (res.body.code !== 0) throw new Error('Pad setting text failed'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + .expect(200) + .expect('Content-Type', /json/); + if (res.body.code !== 0) throw new Error('Pad setting text failed'); }); }); describe('getText', function () { - it('gets the Pad text', function (done) { - agent.get(`${endPoint('getText')}&padID=${testPadId}`) - .expect((res) => { - if (res.body.data.text !== 'testTextTwo\n') throw new Error('Setting Text'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('gets the Pad text', async function () { + const res = await agent.get(`${endPoint('getText')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.data.text !== 'testTextTwo\n') throw new Error('Setting Text'); }); }); describe('getRevisionsCount', function () { - it('gets Revision Count of a Pad', function (done) { - agent.get(`${endPoint('getRevisionsCount')}&padID=${testPadId}`) - .expect((res) => { - if (res.body.data.revisions !== 1) throw new Error('Unable to get text revision count'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('gets Revision Count of a Pad', async function () { + const res = await agent.get(`${endPoint('getRevisionsCount')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.data.revisions !== 1) throw new Error('Unable to get text revision count'); }); }); describe('saveRevision', function () { - it('saves Revision', function (done) { - agent.get(`${endPoint('saveRevision')}&padID=${testPadId}`) - .expect((res) => { - if (res.body.code !== 0) throw new Error('Unable to save Revision'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('saves Revision', async function () { + const res = await agent.get(`${endPoint('saveRevision')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.code !== 0) throw new Error('Unable to save Revision'); }); }); describe('getSavedRevisionsCount', function () { - it('gets saved revisions count of Pad', function (done) { - agent.get(`${endPoint('getSavedRevisionsCount')}&padID=${testPadId}`) - .expect((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); + it('gets saved revisions count of Pad', async function () { + const res = await agent.get(`${endPoint('getSavedRevisionsCount')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + 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'); + } }); }); describe('listSavedRevisions', function () { - it('gets saved revision list of Pad', function (done) { - agent.get(`${endPoint('listSavedRevisions')}&padID=${testPadId}`) - .expect((res) => { - if (res.body.code !== 0) throw new Error('Unable to get Saved Revisions List'); - assert.deepEqual(res.body.data.savedRevisions, [1]); - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('gets saved revision list of Pad', async function () { + const res = await agent.get(`${endPoint('listSavedRevisions')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.code !== 0) throw new Error('Unable to get Saved Revisions List'); + assert.deepEqual(res.body.data.savedRevisions, [1]); }); }); + describe('padUsersCount', function () { - it('gets User Count of a Pad', function (done) { - agent.get(`${endPoint('padUsersCount')}&padID=${testPadId}`) - .expect((res) => { - if (res.body.data.padUsersCount !== 0) throw new Error('Incorrect Pad User count'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('gets User Count of a Pad', async function () { + const res = await agent.get(`${endPoint('padUsersCount')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.data.padUsersCount !== 0) throw new Error('Incorrect Pad User count'); }); }); describe('getReadOnlyID', function () { - it('Gets the Read Only ID of a Pad', function (done) { - agent.get(`${endPoint('getReadOnlyID')}&padID=${testPadId}`) - .expect((res) => { - if (!res.body.data.readOnlyID) throw new Error('No Read Only ID for Pad'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('Gets the Read Only ID of a Pad', async function () { + const res = await agent.get(`${endPoint('getReadOnlyID')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + if (!res.body.data.readOnlyID) throw new Error('No Read Only ID for Pad'); }); }); describe('listAuthorsOfPad', function () { - it('Get Authors of the Pad', function (done) { - agent.get(`${endPoint('listAuthorsOfPad')}&padID=${testPadId}`) - .expect((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); + it('Get Authors of the Pad', async function () { + const res = await agent.get(`${endPoint('listAuthorsOfPad')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.data.authorIDs.length !== 0) { + throw new Error('# of Authors of pad is not 0'); + } }); }); describe('getLastEdited', function () { - it('Get When Pad was left Edited', function (done) { - agent.get(`${endPoint('getLastEdited')}&padID=${testPadId}`) - .expect((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); + it('Get When Pad was left Edited', async function () { + const res = await agent.get(`${endPoint('getLastEdited')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + if (!res.body.data.lastEdited) { + throw new Error('# of Authors of pad is not 0'); + } else { + lastEdited = res.body.data.lastEdited; + } }); }); describe('setText', function () { - it('creates a new Pad with text', function (done) { - agent.post(endPoint('setText')) + it('creates a new Pad with text', async function () { + const res = await agent.post(endPoint('setText')) .send({ padID: testPadId, text: 'testTextTwo', }) - .expect((res) => { - if (res.body.code !== 0) throw new Error('Pad setting text failed'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + .expect(200) + .expect('Content-Type', /json/); + if (res.body.code !== 0) throw new Error('Pad setting text failed'); }); }); describe('getLastEdited', function () { - it('Get When Pad was left Edited', function (done) { - agent.get(`${endPoint('getLastEdited')}&padID=${testPadId}`) - .expect((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); + it('Get When Pad was left Edited', async function () { + const res = await agent.get(`${endPoint('getLastEdited')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.data.lastEdited <= lastEdited) { + throw new Error('Editing A Pad is not updating when it was last edited'); + } }); }); describe('padUsers', function () { - it('gets User Count of a Pad', function (done) { - agent.get(`${endPoint('padUsers')}&padID=${testPadId}`) - .expect((res) => { - if (res.body.data.padUsers.length !== 0) throw new Error('Incorrect Pad Users'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('gets User Count of a Pad', async function () { + const res = await agent.get(`${endPoint('padUsers')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.data.padUsers.length !== 0) throw new Error('Incorrect Pad Users'); }); }); describe('deletePad', function () { - it('deletes a Pad', function (done) { - agent.get(`${endPoint('deletePad')}&padID=${testPadId}`) - .expect((res) => { - if (res.body.code !== 0) throw new Error('Pad Deletion failed'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('deletes a Pad', async function () { + const res = await agent.get(`${endPoint('deletePad')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.code !== 0) throw new Error('Pad Deletion failed'); }); }); @@ -436,198 +380,169 @@ describe(__filename, function () { const copiedPadId = makeid(); describe('createPad', function () { - it('creates a new Pad with text', function (done) { - agent.get(`${endPoint('createPad')}&padID=${testPadId}`) - .expect((res) => { - if (res.body.code !== 0) throw new Error('Pad Creation failed'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('creates a new Pad with text', async function () { + const res = await agent.get(`${endPoint('createPad')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.code !== 0) throw new Error('Pad Creation failed'); }); }); describe('setText', function () { - it('Sets text on a pad Id', function (done) { - agent.post(`${endPoint('setText')}&padID=${testPadId}`) + it('Sets text on a pad Id', async function () { + const res = await agent.post(`${endPoint('setText')}&padID=${testPadId}`) .field({text}) - .expect((res) => { - if (res.body.code !== 0) throw new Error('Pad Set Text failed'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + .expect(200) + .expect('Content-Type', /json/); + if (res.body.code !== 0) throw new Error('Pad Set Text failed'); }); }); describe('getText', function () { - it('Gets text on a pad Id', function (done) { - agent.get(`${endPoint('getText')}&padID=${testPadId}`) - .expect((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); + it('Gets text on a pad Id', async function () { + const res = await agent.get(`${endPoint('getText')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + 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'); }); }); describe('setText', function () { - it('Sets text on a pad Id including an explicit newline', function (done) { - agent.post(`${endPoint('setText')}&padID=${testPadId}`) + it('Sets text on a pad Id including an explicit newline', async function () { + const res = await agent.post(`${endPoint('setText')}&padID=${testPadId}`) .field({text: `${text}\n`}) - .expect((res) => { - if (res.body.code !== 0) throw new Error('Pad Set Text failed'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + .expect(200) + .expect('Content-Type', /json/); + if (res.body.code !== 0) throw new Error('Pad Set Text failed'); }); }); describe('getText', function () { - it("Gets text on a pad Id and doesn't have an excess newline", function (done) { - agent.get(`${endPoint('getText')}&padID=${testPadId}`) - .expect((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); + it("Gets text on a pad Id and doesn't have an excess newline", async function () { + const res = await agent.get(`${endPoint('getText')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + 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'); }); }); describe('getLastEdited', function () { - it('Gets when pad was last edited', function (done) { - agent.get(`${endPoint('getLastEdited')}&padID=${testPadId}`) - .expect((res) => { - if (res.body.lastEdited === 0) throw new Error('Get Last Edited Failed'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('Gets when pad was last edited', async function () { + const res = await agent.get(`${endPoint('getLastEdited')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.lastEdited === 0) throw new Error('Get Last Edited Failed'); }); }); describe('movePad', function () { - it('Move a Pad to a different Pad ID', function (done) { - agent.get(`${endPoint('movePad')}&sourceID=${testPadId}&destinationID=${newPadId}&force=true`) - .expect((res) => { - if (res.body.code !== 0) throw new Error('Moving Pad Failed'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('Move a Pad to a different Pad ID', async function () { + const res = await agent.get( + `${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'); }); }); describe('getText', function () { - it('Gets text on a pad Id', function (done) { - agent.get(`${endPoint('getText')}&padID=${newPadId}`) - .expect((res) => { - if (res.body.data.text !== `${text}\n`) throw new Error('Pad Get Text failed'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('Gets text on a pad Id', async function () { + const res = await agent.get(`${endPoint('getText')}&padID=${newPadId}`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.data.text !== `${text}\n`) throw new Error('Pad Get Text failed'); }); }); describe('movePad', function () { - it('Move a Pad to a different Pad ID', function (done) { - agent.get(`${endPoint('movePad')}&sourceID=${newPadId}&destinationID=${testPadId}` + - '&force=false') - .expect((res) => { - if (res.body.code !== 0) throw new Error('Moving Pad Failed'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('Move a Pad to a different Pad ID', async function () { + const res = await agent.get( + `${endPoint('movePad')}&sourceID=${newPadId}&destinationID=${testPadId}&force=false`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.code !== 0) throw new Error('Moving Pad Failed'); }); }); describe('getText', function () { - it('Gets text on a pad Id', function (done) { - agent.get(`${endPoint('getText')}&padID=${testPadId}`) - .expect((res) => { - if (res.body.data.text !== `${text}\n`) throw new Error('Pad Get Text failed'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('Gets text on a pad Id', async function () { + const res = await agent.get(`${endPoint('getText')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.data.text !== `${text}\n`) throw new Error('Pad Get Text failed'); }); }); describe('getLastEdited', function () { - it('Gets when pad was last edited', function (done) { - agent.get(`${endPoint('getLastEdited')}&padID=${testPadId}`) - .expect((res) => { - if (res.body.lastEdited === 0) throw new Error('Get Last Edited Failed'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('Gets when pad was last edited', async function () { + const res = await agent.get(`${endPoint('getLastEdited')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.lastEdited === 0) throw new Error('Get Last Edited Failed'); }); }); describe('appendText', function () { - it('Append text to a pad Id', function (done) { - agent.get(`${endPoint('appendText', '1.2.13')}&padID=${testPadId}&text=hello`) - .expect((res) => { - if (res.body.code !== 0) throw new Error('Pad Append Text failed'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('Append text to a pad Id', async function () { + const res = await agent.get( + `${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'); }); }); describe('getText', function () { - it('Gets text on a pad Id', function (done) { - agent.get(`${endPoint('getText')}&padID=${testPadId}`) - .expect((res) => { - if (res.body.code !== 0) throw new Error('Pad Get Text failed'); - if (res.body.data.text !== `${text}hello\n`) { - throw new Error('Pad Text not set properly'); - } - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('Gets text on a pad Id', async function () { + const res = await agent.get(`${endPoint('getText')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.code !== 0) throw new Error('Pad Get Text failed'); + if (res.body.data.text !== `${text}hello\n`) { + throw new Error('Pad Text not set properly'); + } }); }); - 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 = '
Hello HTML
'; - agent.post(endPoint('setHTML')) + const res = await agent.post(endPoint('setHTML')) .send({ padID: testPadId, html, }) - .expect((res) => { - if (res.body.code !== 0) { - throw new Error("Crappy HTML Can't be Imported[we weren't able to sanitize it']"); - } - }) - .expect('Content-Type', /json/) - .expect(200, done); + .expect(200) + .expect('Content-Type', /json/); + if (res.body.code !== 0) { + throw new Error("Crappy HTML Can't be Imported[we weren't able to sanitize it']"); + } }); }); describe('setHTML', function () { - it('Sets the HTML of a Pad with complex nested lists of different types', function (done) { - agent.post(endPoint('setHTML')) + it('Sets the HTML of a Pad with complex nested lists of different types', async function () { + const res = await agent.post(endPoint('setHTML')) .send({ padID: testPadId, html: ulHtml, }) - .expect((res) => { - if (res.body.code !== 0) throw new Error('List HTML cant be imported'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + .expect(200) + .expect('Content-Type', /json/); + if (res.body.code !== 0) throw new Error('List HTML cant be imported'); }); }); describe('getHTML', function () { - it('Gets back the HTML of a Pad with complex nested lists of different types', function (done) { - agent.get(`${endPoint('getHTML')}&padID=${testPadId}`) - .expect((res) => { - const receivedHtml = res.body.data.html.replace('
', '').toLowerCase(); - - if (receivedHtml !== expectedHtml) { - throw new Error(`HTML received from export is not the one we were expecting. + it('Gets back the HTML of a Pad with complex nested lists of different types', async function () { + const res = await agent.get(`${endPoint('getHTML')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + const receivedHtml = res.body.data.html.replace('
', '').toLowerCase(); + if (receivedHtml !== expectedHtml) { + throw new Error(`HTML received from export is not the one we were expecting. Received: ${receivedHtml} @@ -636,31 +551,27 @@ describe(__filename, function () { Which is a slightly modified version of the originally imported one: ${ulHtml}`); - } - }) - .expect('Content-Type', /json/) - .expect(200, done); + } }); }); describe('setHTML', function () { - it('Sets the HTML of a Pad with white space between list items', function (done) { - agent.get(`${endPoint('setHTML')}&padID=${testPadId}&html=${ulSpaceHtml}`) - .expect((res) => { - if (res.body.code !== 0) throw new Error('List HTML cant be imported'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('Sets the HTML of a Pad with white space between list items', async function () { + const res = await agent.get(`${endPoint('setHTML')}&padID=${testPadId}&html=${ulSpaceHtml}`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.code !== 0) throw new Error('List HTML cant be imported'); }); }); describe('getHTML', function () { - it('Gets back the HTML of a Pad with complex nested lists of different types', function (done) { - agent.get(`${endPoint('getHTML')}&padID=${testPadId}`) - .expect((res) => { - const receivedHtml = res.body.data.html.replace('
', '').toLowerCase(); - if (receivedHtml !== expectedSpaceHtml) { - throw new Error(`HTML received from export is not the one we were expecting. + it('Gets back the HTML of a Pad with complex nested lists of different types', async function () { + const res = await agent.get(`${endPoint('getHTML')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/); + const receivedHtml = res.body.data.html.replace('
', '').toLowerCase(); + if (receivedHtml !== expectedSpaceHtml) { + throw new Error(`HTML received from export is not the one we were expecting. Received: ${receivedHtml} @@ -669,39 +580,27 @@ describe(__filename, function () { Which is a slightly modified version of the originally imported one: ${ulSpaceHtml}`); - } - }) - .expect('Content-Type', /json/) - .expect(200, done); + } }); }); describe('createPad', function () { - it('errors if pad can be created', function (done) { - const badUrlChars = ['/', '%23', '%3F', '%26']; - async.map( - badUrlChars, - (badUrlChar, cb) => { - agent.get(`${endPoint('createPad')}&padID=${badUrlChar}`) - .expect((res) => { - if (res.body.code !== 1) throw new Error('Pad with bad characters was created'); - }) - .expect('Content-Type', /json/) - .end(cb); - }, - done); + it('errors if pad can be created', async function () { + await Promise.all(['/', '%23', '%3F', '%26'].map(async (badUrlChar) => { + const res = await agent.get(`${endPoint('createPad')}&padID=${badUrlChar}`) + .expect('Content-Type', /json/); + if (res.body.code !== 1) throw new Error('Pad with bad characters was created'); + })); }); }); describe('copyPad', function () { - it('copies the content of a existent pad', function (done) { - agent.get(`${endPoint('copyPad')}&sourceID=${testPadId}&destinationID=${copiedPadId}` + - '&force=true') - .expect((res) => { - if (res.body.code !== 0) throw new Error('Copy Pad Failed'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('copies the content of a existent pad', async function () { + const res = await agent.get( + `${endPoint('copyPad')}&sourceID=${testPadId}&destinationID=${copiedPadId}&force=true`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.code !== 0) throw new Error('Copy Pad Failed'); }); }); @@ -709,39 +608,32 @@ describe(__filename, function () { const sourcePadId = makeid(); let newPad; - before(function (done) { - createNewPadWithHtml(sourcePadId, ulHtml, done); + before(async function () { + await createNewPadWithHtml(sourcePadId, ulHtml); }); beforeEach(async function () { newPad = makeid(); }); - it('returns a successful response', function (done) { - agent.get(`${endPoint('copyPadWithoutHistory')}&sourceID=${sourcePadId}` + - `&destinationID=${newPad}&force=false`) - .expect((res) => { - if (res.body.code !== 0) throw new Error('Copy Pad Without History Failed'); - }) - .expect('Content-Type', /json/) - .expect(200, done); + it('returns a successful response', async function () { + const res = await agent.get(`${endPoint('copyPadWithoutHistory')}&sourceID=${sourcePadId}` + + `&destinationID=${newPad}&force=false`) + .expect(200) + .expect('Content-Type', /json/); + if (res.body.code !== 0) throw new Error('Copy Pad Without History Failed'); }); // 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) { - agent.get(`${endPoint('copyPadWithoutHistory')}&sourceID=${sourcePadId}` + - `&destinationID=${newPad}&force=false`) - .expect((res) => { - if (res.body.code !== 0) throw new Error('Copy Pad Without History Failed'); - }) - .end(() => { - agent.get(`${endPoint('getHTML')}&padID=${newPad}`) - .expect((res) => { - const receivedHtml = - res.body.data.html.replace('

', '').toLowerCase(); - - if (receivedHtml !== expectedHtml) { - throw new Error(`HTML received from export is not the one we were expecting. + it('creates a new pad with the same content as the source pad', async function () { + let res = await agent.get(`${endPoint('copyPadWithoutHistory')}&sourceID=${sourcePadId}` + + `&destinationID=${newPad}&force=false`); + if (res.body.code !== 0) throw new Error('Copy Pad Without History Failed'); + res = await agent.get(`${endPoint('getHTML')}&padID=${newPad}`) + .expect(200); + const receivedHtml = res.body.data.html.replace('

', '').toLowerCase(); + if (receivedHtml !== expectedHtml) { + throw new Error(`HTML received from export is not the one we were expecting. Received: ${receivedHtml} @@ -750,56 +642,50 @@ describe(__filename, function () { Which is a slightly modified version of the originally imported one: ${ulHtml}`); - } - }) - .expect(200, done); - }); + } }); context('when try copy a pad with a group that does not exist', function () { const padId = makeid(); const padWithNonExistentGroup = `notExistentGroup$${padId}`; - it('throws an error', function (done) { - agent.get(`${endPoint('copyPadWithoutHistory')}&sourceID=${sourcePadId}&` + - `destinationID=${padWithNonExistentGroup}&force=true`) - .expect((res) => { - // code 1, it means an error has happened - if (res.body.code !== 1) throw new Error('It should report an error'); - }) - .expect(200, done); + it('throws an error', async function () { + const res = await agent.get(`${endPoint('copyPadWithoutHistory')}` + + `&sourceID=${sourcePadId}` + + `&destinationID=${padWithNonExistentGroup}&force=true`) + .expect(200); + // code 1, it means an error has happened + if (res.body.code !== 1) throw new Error('It should report an error'); }); }); context('when try copy a pad and destination pad already exist', function () { const padIdExistent = makeid(); - before(function (done) { - createNewPadWithHtml(padIdExistent, ulHtml, done); + before(async function () { + await createNewPadWithHtml(padIdExistent, ulHtml); }); context('and force is false', function () { - it('throws an error', function (done) { - agent.get(`${endPoint('copyPadWithoutHistory')}&sourceID=${sourcePadId}` + - `&destinationID=${padIdExistent}&force=false`) - .expect((res) => { - // code 1, it means an error has happened - if (res.body.code !== 1) throw new Error('It should report an error'); - }) - .expect(200, done); + it('throws an error', async function () { + const res = await agent.get(`${endPoint('copyPadWithoutHistory')}` + + `&sourceID=${sourcePadId}` + + `&destinationID=${padIdExistent}&force=false`) + .expect(200); + // code 1, it means an error has happened + if (res.body.code !== 1) throw new Error('It should report an error'); }); }); context('and force is true', function () { - it('returns a successful response', function (done) { - agent.get(`${endPoint('copyPadWithoutHistory')}&sourceID=${sourcePadId}` + - `&destinationID=${padIdExistent}&force=true`) - .expect((res) => { - // code 1, it means an error has happened - if (res.body.code !== 0) { - throw new Error('Copy pad without history with force true failed'); - } - }) - .expect(200, done); + it('returns a successful response', async function () { + const res = await agent.get(`${endPoint('copyPadWithoutHistory')}` + + `&sourceID=${sourcePadId}` + + `&destinationID=${padIdExistent}&force=true`) + .expect(200); + // code 1, it means an error has happened + if (res.body.code !== 0) { + throw new Error('Copy pad without history with force true failed'); + } }); }); }); @@ -811,15 +697,12 @@ describe(__filename, function () { */ -const createNewPadWithHtml = (padId, html, cb) => { - agent.get(`${endPoint('createPad')}&padID=${padId}`) - .end(() => { - agent.post(endPoint('setHTML')) - .send({ - padID: padId, - html, - }) - .end(cb); +const createNewPadWithHtml = async (padId, html) => { + await agent.get(`${endPoint('createPad')}&padID=${padId}`); + await agent.post(endPoint('setHTML')) + .send({ + padID: padId, + html, }); };