Skip to content

Commit

Permalink
Fixed test.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamTV12345 committed Mar 26, 2024
1 parent eae462b commit 18e268a
Showing 1 changed file with 81 additions and 69 deletions.
150 changes: 81 additions & 69 deletions static/tests/backend/specs/exportHTML.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,33 @@
const common = require('ep_etherpad-lite/tests/backend/common');

let agent;
const apiKey = common.apiKey;
const apiVersion = 1;
const randomString = require('ep_etherpad-lite/static/js/pad_utils').randomString;
import {generateJWTToken, generateJWTTokenUser} from "ep_etherpad-lite/tests/backend/common";

// Creates a pad and returns the pad id. Calls the callback when finished.
const createPad = (padID, callback) => {
agent.get(`/api/${apiVersion}/createPad?apikey=${apiKey}&padID=${padID}`)
.end((err, res) => {
if (err || (res.body.code !== 0)) callback(new Error('Unable to create new Pad'));
const createPad = async (padID, callback) => {
agent.get(`/api/${apiVersion}/createPad?padID=${padID}`)
.set("Authorization", await generateJWTToken())
.end((err, res) => {
if (err || (res.body.code !== 0)) callback(new Error('Unable to create new Pad'));

callback(padID);
});
callback(padID);
});
};

const setHTML = (padID, html, callback) => {
agent.get(`/api/${apiVersion}/setHTML?apikey=${apiKey}&padID=${padID}&html=${html}`)
.end((err, res) => {
if (err || (res.body.code !== 0)) callback(new Error('Unable to set pad HTML'));
const setHTML = async (padID, html, callback) => {
agent.get(`/api/${apiVersion}/setHTML?padID=${padID}&html=${html}`)
.set("Authorization", await generateJWTToken())
.end((err, res) => {
if (err || (res.body.code !== 0)) callback(new Error('Unable to set pad HTML'));

callback(null, padID);
});
callback(null, padID);
});
};

const getHTMLEndPointFor =
(padID, callback) => `/api/${apiVersion}/getHTML?apikey=${apiKey}&padID=${padID}`;
(padID, callback) => `/api/${apiVersion}/getHTML?padID=${padID}`;

const buildHTML = (body) => `<html><body>${body}</body></html>`;

Expand All @@ -51,21 +53,23 @@ describe('export alignment to HTML', function () {
html = () => buildHTML('<center>Hello world</center>');
});

it('returns ok', function (done) {
it('returns ok', async function (done) {
agent.get(getHTMLEndPointFor(padID))
.expect('Content-Type', /json/)
.expect(200, done);
.set("Authorization", await generateJWTToken())
.expect('Content-Type', /json/)
.expect(200, done);
});

it('returns HTML with Subscript HTML tags', function (done) {
it('returns HTML with Subscript HTML tags', async function (done) {
agent.get(getHTMLEndPointFor(padID))
.expect((res) => {
const html = res.body.data.html;
const expectedHTML =
'<p style=\'text-align:center\'>Hello world</p><br><br></body></html>';
if (html.indexOf(expectedHTML) === -1) throw new Error('No center tag detected');
})
.end(done);
.set("Authorization", await generateJWTToken())
.expect((res) => {
const html = res.body.data.html;
const expectedHTML =
'<p style=\'text-align:center\'>Hello world</p><br><br></body></html>';
if (html.indexOf(expectedHTML) === -1) throw new Error('No center tag detected');
})
.end(done);
});
});

Expand All @@ -74,21 +78,23 @@ describe('export alignment to HTML', function () {
html = () => buildHTML('<justify>Hello world</justify>');
});

it('returns ok', function (done) {
it('returns ok', async function (done) {
agent.get(getHTMLEndPointFor(padID))
.expect('Content-Type', /json/)
.expect(200, done);
.set("Authorization", await generateJWTToken())
.expect('Content-Type', /json/)
.expect(200, done);
});

it('returns HTML with Subscript HTML tags', function (done) {
it('returns HTML with Subscript HTML tags', async function (done) {
agent.get(getHTMLEndPointFor(padID))
.expect((res) => {
const html = res.body.data.html;
const expectedHTML =
'<p style=\'text-align:justify\'>Hello world</p><br><br></body></html>';
if (html.indexOf(expectedHTML) === -1) throw new Error('No center tag detected');
})
.end(done);
.set("Authorization", await generateJWTToken())
.expect((res) => {
const html = res.body.data.html;
const expectedHTML =
'<p style=\'text-align:justify\'>Hello world</p><br><br></body></html>';
if (html.indexOf(expectedHTML) === -1) throw new Error('No center tag detected');
})
.end(done);
});
});

Expand All @@ -97,21 +103,23 @@ describe('export alignment to HTML', function () {
html = () => buildHTML('<right>Hello world</right>');
});

it('returns ok', function (done) {
it('returns ok', async function (done) {
agent.get(getHTMLEndPointFor(padID))
.expect('Content-Type', /json/)
.expect(200, done);
.set("Authorization", await generateJWTToken())
.expect('Content-Type', /json/)
.expect(200, done);
});

it('returns HTML with Subscript HTML tags', function (done) {
it('returns HTML with Subscript HTML tags', async function (done) {
agent.get(getHTMLEndPointFor(padID))
.expect((res) => {
const html = res.body.data.html;
const expectedHTML =
'<p style=\'text-align:right\'>Hello world</p><br><br></body></html>';
if (html.indexOf(expectedHTML) === -1) throw new Error('No right tag detected');
})
.end(done);
.set("Authorization", await generateJWTToken())
.expect((res) => {
const html = res.body.data.html;
const expectedHTML =
'<p style=\'text-align:right\'>Hello world</p><br><br></body></html>';
if (html.indexOf(expectedHTML) === -1) throw new Error('No right tag detected');
})
.end(done);
});
});

Expand All @@ -120,21 +128,23 @@ describe('export alignment to HTML', function () {
html = () => buildHTML('<left>Hello world</left>');
});

it('returns ok', function (done) {
it('returns ok', async function (done) {
agent.get(getHTMLEndPointFor(padID))
.expect('Content-Type', /json/)
.expect(200, done);
.set("Authorization", await generateJWTToken())
.expect('Content-Type', /json/)
.expect(200, done);
});

it('returns HTML with Subscript HTML tags', function (done) {
it('returns HTML with Subscript HTML tags', async function (done) {
agent.get(getHTMLEndPointFor(padID))
.expect((res) => {
const html = res.body.data.html;
const expectedHTML =
'<p style=\'text-align:left\'>Hello world</p><br><br></body></html>';
if (html.indexOf(expectedHTML) === -1) throw new Error('No left tag detected');
})
.end(done);
.set("Authorization", await generateJWTToken())
.expect((res) => {
const html = res.body.data.html;
const expectedHTML =
'<p style=\'text-align:left\'>Hello world</p><br><br></body></html>';
if (html.indexOf(expectedHTML) === -1) throw new Error('No left tag detected');
})
.end(done);
});
});

Expand All @@ -143,23 +153,25 @@ describe('export alignment to HTML', function () {
html = () => buildHTML('<h1><left>Hello world</left></h1>');
});

it('returns ok', function (done) {
it('returns ok', async function (done) {
agent.get(getHTMLEndPointFor(padID))
.expect('Content-Type', /json/)
.expect(200, done);
.set("Authorization", await generateJWTToken())
.expect('Content-Type', /json/)
.expect(200, done);
});

it('returns HTML with Subscript HTML tags', function (done) {
it('returns HTML with Subscript HTML tags', async function (done) {
try {
require.resolve('ep_headings2'); // eslint-disable-line n/no-missing-require
agent.get(getHTMLEndPointFor(padID))
.expect((res) => {
const html = res.body.data.html;
const expectedHTML =
'<h1 style=\'text-align:left\'>Hello world</h1><br><br></body></html>';
if (html.indexOf(expectedHTML) === -1) throw new Error('No left tag detected');
})
.end(done);
.set("Authorization", await generateJWTToken())
.expect((res) => {
const html = res.body.data.html;
const expectedHTML =
'<h1 style=\'text-align:left\'>Hello world</h1><br><br></body></html>';
if (html.indexOf(expectedHTML) === -1) throw new Error('No left tag detected');
})
.end(done);
} catch (e) {
if (e.message.indexOf('Cannot find module') === -1) {
throw new Error(e.message);
Expand Down

0 comments on commit 18e268a

Please sign in to comment.