diff --git a/src/tests/backend/specs/api/importexportGetPost.js b/src/tests/backend/specs/api/importexportGetPost.js index 54d085eda..daa8435b3 100644 --- a/src/tests/backend/specs/api/importexportGetPost.js +++ b/src/tests/backend/specs/api/importexportGetPost.js @@ -106,23 +106,52 @@ describe(__filename, function () { .expect((res) => assert.equal(res.body.data.text, padText.toString())); }); - for (const authn of [false, true]) { - it(`can export from read-only pad ID, authn ${authn}`, async function () { - settings.requireAuthentication = authn; - const get = (ep) => { - let req = agent.get(ep); - if (authn) req = req.auth('user', 'user-password'); - return req.expect(200); - }; - const ro = await get(`${endPoint('getReadOnlyID')}&padID=${testPadId}`) - .expect((res) => assert.ok(JSON.parse(res.text).data.readOnlyID)); - const readOnlyId = JSON.parse(ro.text).data.readOnlyID; - await get(`/p/${readOnlyId}/export/html`) - .expect((res) => assert(res.text.indexOf('This is the') !== -1)); - await get(`/p/${readOnlyId}/export/txt`) - .expect((res) => assert(res.text.indexOf('This is the') !== -1)); + describe('export from read-only pad ID', function () { + let readOnlyId; + + // This ought to be before(), but it must run after the top-level beforeEach() above. + beforeEach(async function () { + if (readOnlyId != null) return; + await agent.post(`/p/${testPadId}/import`) + .attach('file', padText, {filename: '/test.txt', contentType: 'text/plain'}) + .expect(200); + const res = await agent.get(`${endPoint('getReadOnlyID')}&padID=${testPadId}`) + .expect(200) + .expect('Content-Type', /json/) + .expect((res) => assert.equal(res.body.code, 0)); + readOnlyId = res.body.data.readOnlyID; }); - } + + for (const authn of [false, true]) { + describe(`requireAuthentication = ${authn}`, function () { + // This ought to be before(), but it must run after the top-level beforeEach() above. + beforeEach(async function () { + settings.requireAuthentication = authn; + }); + + for (const exportType of ['html', 'txt', 'etherpad']) { + describe(`export to ${exportType}`, function () { + let text; + + // This ought to be before(), but it must run after the top-level beforeEach() above. + beforeEach(async function () { + if (text != null) return; + let req = agent.get(`/p/${readOnlyId}/export/${exportType}`); + if (authn) req = req.auth('user', 'user-password'); + const res = await req + .expect(200) + .buffer(true).parse(superagent.parse.text); + text = res.text; + }); + + it('export OK', async function () { + assert.match(text, /This is the/); + }); + }); + } + }); + } + }); describe('Import/Export tests requiring AbiWord/LibreOffice', function () { before(async function () {