2021-01-24 00:56:11 -05:00
|
|
|
'use strict';
|
|
|
|
|
2020-03-20 21:02:19 +00:00
|
|
|
/*
|
2021-02-04 18:43:27 -05:00
|
|
|
* This file is copied & modified from <basedir>/src/tests/backend/specs/api/pad.js
|
2020-03-20 21:02:19 +00:00
|
|
|
*
|
|
|
|
* TODO: maybe unify those two files and merge in a single one.
|
|
|
|
*/
|
|
|
|
|
2021-10-03 20:23:30 -04:00
|
|
|
const assert = require('assert').strict;
|
2020-10-08 01:37:17 -04:00
|
|
|
const common = require('../../common');
|
2020-03-20 21:02:19 +00:00
|
|
|
const fs = require('fs');
|
2021-10-02 19:17:47 -04:00
|
|
|
const fsp = fs.promises;
|
2020-03-20 21:02:19 +00:00
|
|
|
|
2021-02-16 18:35:50 -05:00
|
|
|
let agent;
|
2020-10-08 01:37:17 -04:00
|
|
|
const apiKey = common.apiKey;
|
2020-11-23 13:21:51 -05:00
|
|
|
let apiVersion = 1;
|
|
|
|
const testPadId = makeid();
|
2020-03-20 21:02:19 +00:00
|
|
|
|
2021-02-16 18:25:15 -05:00
|
|
|
const endPoint = (point, version) => `/api/${version || apiVersion}/${point}?apikey=${apiKey}`;
|
|
|
|
|
2020-11-23 13:21:51 -05:00
|
|
|
describe(__filename, function () {
|
2021-02-16 18:35:50 -05:00
|
|
|
before(async function () { agent = await common.init(); });
|
|
|
|
|
2021-10-03 20:06:30 -04:00
|
|
|
describe('Sanity checks', function () {
|
2021-10-02 19:17:47 -04:00
|
|
|
it('can connect', async function () {
|
|
|
|
await agent.get('/api/')
|
|
|
|
.expect(200)
|
|
|
|
.expect('Content-Type', /json/);
|
2020-10-09 18:19:46 -04:00
|
|
|
});
|
2020-03-20 21:02:19 +00:00
|
|
|
|
2021-10-02 19:17:47 -04:00
|
|
|
it('finds the version tag', async function () {
|
|
|
|
const res = await agent.get('/api/')
|
|
|
|
.expect(200);
|
|
|
|
apiVersion = res.body.currentVersion;
|
2021-10-03 20:23:30 -04:00
|
|
|
assert(apiVersion);
|
2020-10-09 18:19:46 -04:00
|
|
|
});
|
2020-03-20 21:02:19 +00:00
|
|
|
|
2021-10-02 19:17:47 -04:00
|
|
|
it('errors with invalid APIKey', async function () {
|
2020-10-09 18:19:46 -04:00
|
|
|
// 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
|
2021-10-02 19:17:47 -04:00
|
|
|
await agent.get(`/api/${apiVersion}/createPad?apikey=password&padID=test`)
|
|
|
|
.expect(401);
|
2020-10-09 18:19:46 -04:00
|
|
|
});
|
2020-11-23 13:21:51 -05:00
|
|
|
});
|
2020-03-20 21:02:19 +00:00
|
|
|
|
2021-10-03 20:06:30 -04:00
|
|
|
describe('Tests', function () {
|
2021-10-02 19:17:47 -04:00
|
|
|
it('creates a new Pad', async function () {
|
|
|
|
const res = await agent.get(`${endPoint('createPad')}&padID=${testPadId}`)
|
|
|
|
.expect(200)
|
|
|
|
.expect('Content-Type', /json/);
|
2021-10-03 20:23:30 -04:00
|
|
|
assert.equal(res.body.code, 0);
|
2020-10-09 18:19:46 -04:00
|
|
|
});
|
2020-03-20 21:02:19 +00:00
|
|
|
|
2021-10-02 19:17:47 -04:00
|
|
|
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/);
|
2021-10-03 20:23:30 -04:00
|
|
|
assert.equal(res.body.code, 0);
|
2020-03-20 21:02:19 +00:00
|
|
|
});
|
|
|
|
|
2021-10-02 19:17:47 -04:00
|
|
|
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/);
|
2021-10-03 20:23:30 -04:00
|
|
|
assert.match(res.body.data.html, /🇼/);
|
2020-10-09 18:19:46 -04:00
|
|
|
});
|
2020-11-23 13:21:51 -05:00
|
|
|
});
|
2020-10-09 18:19:46 -04:00
|
|
|
});
|
2020-03-20 21:02:19 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
End of test
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2020-11-21 13:37:57 -05:00
|
|
|
function makeid() {
|
2020-11-23 13:21:51 -05:00
|
|
|
let text = '';
|
|
|
|
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
2020-03-20 21:02:19 +00:00
|
|
|
|
2020-11-23 13:21:51 -05:00
|
|
|
for (let i = 0; i < 10; i++) {
|
2020-03-20 21:02:19 +00:00
|
|
|
text += possible.charAt(Math.floor(Math.random() * possible.length));
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|