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)
if (res.body.code !== 0) throw new Error('Unable to create new Pad'); .expect('Content-Type', /json/);
}) 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: await fsp.readFile('tests/backend/specs/api/emojis.html', 'utf8'),
html, })
}) .expect(200)
.expect((res) => { .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);
}); });
}); });
}); });

File diff suppressed because it is too large Load diff