From 9cb91ff2ee59cb488bd3a0b721cf69162dca67d3 Mon Sep 17 00:00:00 2001
From: SamTV12345 <40429738+samtv12345@users.noreply.github.com>
Date: Tue, 26 Mar 2024 19:34:46 +0100
Subject: [PATCH] Fixed api tests.
---
static/tests/backend/specs/exportHTML.js | 104 ++++++++++++-----------
1 file changed, 56 insertions(+), 48 deletions(-)
diff --git a/static/tests/backend/specs/exportHTML.js b/static/tests/backend/specs/exportHTML.js
index 4b33009..d522180 100644
--- a/static/tests/backend/specs/exportHTML.js
+++ b/static/tests/backend/specs/exportHTML.js
@@ -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) => `
${body}`;
@@ -49,19 +51,21 @@ describe('ep_headings2 - export headings to HTML', function () {
html = () => buildHTML('Hello world
');
});
- 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('Hello world
') === -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('Hello world
') === -1) throw new Error('No H1 tag detected');
+ })
+ .end(done);
});
});
@@ -70,20 +74,22 @@ describe('ep_headings2 - export headings to HTML', function () {
html = () => buildHTML('Hello world
Foo
');
});
- 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('Hello world
') === -1) throw new Error('No H1 tag detected');
- if (html.indexOf('Foo
') === -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('Hello world
') === -1) throw new Error('No H1 tag detected');
+ if (html.indexOf('Foo
') === -1) throw new Error('No H2 tag detected');
+ })
+ .end(done);
});
});
@@ -92,28 +98,30 @@ describe('ep_headings2 - export headings to HTML', function () {
html = () => buildHTML('Hello world
Foo
');
});
- 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('Hello world
') === -1) {
- throw new Error('No H1 tag detected');
- }
- if (html.indexOf('Foo
') === -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('Hello world
') === -1) {
+ throw new Error('No H1 tag detected');
+ }
+ if (html.indexOf('Foo
') === -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);