Skip to content

Commit

Permalink
Fixed api tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamTV12345 committed Mar 26, 2024
1 parent 4f45c68 commit 9cb91ff
Showing 1 changed file with 56 additions and 48 deletions.
104 changes: 56 additions & 48 deletions static/tests/backend/specs/exportHTML.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,32 @@

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

let agent;
const apiKey = common.apiKey;
const apiVersion = 1;

// 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'));
callback(padID);
});
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);
});
};

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'));
callback(null, padID);
});
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);
});
};

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 @@ -49,19 +51,21 @@ describe('ep_headings2 - export headings to HTML', function () {
html = () => buildHTML('<h1>Hello world</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 Headings HTML tags', function (done) {
it('returns HTML with Headings HTML tags', async function (done) {
agent.get(getHTMLEndPointFor(padID))
.expect((res) => {
const html = res.body.data.html;
if (html.indexOf('<h1>Hello world</h1>') === -1) throw new Error('No H1 tag detected');
})
.end(done);
.set("Authorization", await generateJWTToken())
.expect((res) => {
const html = res.body.data.html;
if (html.indexOf('<h1>Hello world</h1>') === -1) throw new Error('No H1 tag detected');
})
.end(done);
});
});

Expand All @@ -70,20 +74,22 @@ describe('ep_headings2 - export headings to HTML', function () {
html = () => buildHTML('<h1>Hello world</h1><br/><h2>Foo</h2>');
});

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 Multiple Headings HTML tags', function (done) {
it('returns HTML with Multiple Headings HTML tags', async function (done) {
agent.get(getHTMLEndPointFor(padID))
.expect((res) => {
const html = res.body.data.html;
if (html.indexOf('<h1>Hello world</h1>') === -1) throw new Error('No H1 tag detected');
if (html.indexOf('<h2>Foo</h2>') === -1) throw new Error('No H2 tag detected');
})
.end(done);
.set("Authorization", await generateJWTToken())
.expect((res) => {
const html = res.body.data.html;
if (html.indexOf('<h1>Hello world</h1>') === -1) throw new Error('No H1 tag detected');
if (html.indexOf('<h2>Foo</h2>') === -1) throw new Error('No H2 tag detected');
})
.end(done);
});
});

Expand All @@ -92,28 +98,30 @@ describe('ep_headings2 - export headings to HTML', function () {
html = () => buildHTML('<h1><left>Hello world</left></h1><br/><h2><center>Foo</center></h2>');
});

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 Multiple Headings HTML tags', function (done) {
it('returns HTML with Multiple Headings HTML tags', async function (done) {
try {
// eslint-disable-next-line n/no-extraneous-require, n/no-missing-require
require.resolve('ep_align');
agent.get(getHTMLEndPointFor(padID))
.expect((res) => {
const html = res.body.data.html;
console.warn('HTML', html);
if (html.indexOf('<h1 style=\'text-align:left\'>Hello world</h1>') === -1) {
throw new Error('No H1 tag detected');
}
if (html.indexOf('<h2 style=\'text-align:center\'>Foo</h2>') === -1) {
throw new Error('No H2 tag detected');
}
})
.end(done);
.set("Authorization", await generateJWTToken())
.expect((res) => {
const html = res.body.data.html;
console.warn('HTML', html);
if (html.indexOf('<h1 style=\'text-align:left\'>Hello world</h1>') === -1) {
throw new Error('No H1 tag detected');
}
if (html.indexOf('<h2 style=\'text-align:center\'>Foo</h2>') === -1) {
throw new Error('No H2 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 9cb91ff

Please sign in to comment.