mirror of
https://github.com/codex-team/codex.docs.git
synced 2025-08-05 05:25:27 +02:00
Typescript rewrite (#147)
* Updated highlight.js * Update .codexdocsrc.sample remove undefined page for a fresh new install * backend rewritten in TS * test -> TS, .added dockerignore, bug fixed * Removed compiled js files, eslint codex/ts added * fixed jsdocs warning, leaving editor confirmation * use path.resolve for DB paths * db drives updated + fixed User model * redundant cleared + style fixed * explicit type fixing * fixing testing code * added body block type * compiled JS files -> dist, fixed compiling errors * fixed compiling error, re-organized ts source code * updated Dockerfile * fixed link to parent page * up nodejs version * fix package name * fix deps Co-authored-by: nvc8996 <nvc.8996@gmail.com> Co-authored-by: Taly <vitalik7tv@yandex.ru>
This commit is contained in:
parent
059cfb96f9
commit
34514761f5
99 changed files with 3817 additions and 2249 deletions
196
src/test/database.ts
Normal file
196
src/test/database.ts
Normal file
|
@ -0,0 +1,196 @@
|
|||
import fs from 'fs';
|
||||
import config from 'config';
|
||||
import { expect } from 'chai';
|
||||
import Datastore from 'nedb';
|
||||
|
||||
import { Database } from '../backend/utils/database';
|
||||
|
||||
interface Document {
|
||||
data?: any;
|
||||
_id?: string;
|
||||
update?: boolean;
|
||||
no?: any;
|
||||
}
|
||||
|
||||
describe('Database', () => {
|
||||
const pathToDB = `./${config.get('database')}/test.db`;
|
||||
let nedbInstance;
|
||||
let db: Database<any>;
|
||||
|
||||
before(() => {
|
||||
if (fs.existsSync(pathToDB)) {
|
||||
fs.unlinkSync(pathToDB);
|
||||
}
|
||||
});
|
||||
|
||||
it('Creating db instance', async () => {
|
||||
nedbInstance = new Datastore({ filename: pathToDB, autoload: true });
|
||||
db = new Database(nedbInstance);
|
||||
});
|
||||
|
||||
it('Inserting document', async () => {
|
||||
const data = 'Text data';
|
||||
|
||||
const insertedDoc = await db.insert({ data }) as Document;
|
||||
|
||||
expect(insertedDoc).to.be.a('object');
|
||||
expect(insertedDoc.data).to.equal(data);
|
||||
});
|
||||
|
||||
it('Finding document', async () => {
|
||||
const data = 'Text data';
|
||||
|
||||
const insertedDoc = await db.insert({ data }) as Document;
|
||||
|
||||
expect(insertedDoc).to.be.a('object');
|
||||
expect(insertedDoc.data).to.equal(data);
|
||||
|
||||
const foundDoc = await db.findOne({ _id: insertedDoc._id }) as Document;
|
||||
|
||||
expect(foundDoc).not.be.null;
|
||||
expect(foundDoc._id).to.equal(insertedDoc._id);
|
||||
expect(foundDoc.data).to.equal(data);
|
||||
|
||||
const projectedDoc = await db.findOne({ _id: insertedDoc._id }, { data: 1, _id: 0 });
|
||||
|
||||
expect(Object.keys(projectedDoc).length).to.equal(1);
|
||||
expect(Object.keys(projectedDoc).pop()).to.equal('data');
|
||||
});
|
||||
|
||||
it('Updating document', async () => {
|
||||
const data = 'Text data';
|
||||
|
||||
const insertedDoc = await db.insert({ data }) as Document;
|
||||
|
||||
expect(insertedDoc).to.be.a('object');
|
||||
expect(insertedDoc.data).to.equal(data);
|
||||
|
||||
const updatedData = 'Updated text data';
|
||||
|
||||
await db.update({ _id: insertedDoc._id }, { data: updatedData });
|
||||
|
||||
const updatedDoc = await db.findOne({ _id: insertedDoc._id }) as Document;
|
||||
|
||||
expect(updatedDoc).not.be.null;
|
||||
expect(updatedDoc.data).not.equal(data);
|
||||
expect(updatedDoc.data).to.equal(updatedData);
|
||||
});
|
||||
|
||||
it('Updating documents with options', async () => {
|
||||
const data = {
|
||||
update: true,
|
||||
data: 'Text data',
|
||||
};
|
||||
|
||||
await db.insert(data);
|
||||
await db.insert(data);
|
||||
|
||||
let numberOfUpdatedDocs = await db.update({ update: true }, { $set: { data: 'First update' } }, { multi: true });
|
||||
|
||||
expect(numberOfUpdatedDocs).to.equal(2);
|
||||
|
||||
const affectedDocs = await db.update(
|
||||
{ update: true },
|
||||
{ $set: { data: 'Second update' } },
|
||||
{
|
||||
multi: true,
|
||||
returnUpdatedDocs: true,
|
||||
}
|
||||
) as Array<Document>;
|
||||
|
||||
expect(affectedDocs).to.be.a('array');
|
||||
affectedDocs.forEach((doc: Document) => {
|
||||
expect(doc.data).to.equal('Second update');
|
||||
});
|
||||
|
||||
const upsertedDoc = await db.update({ update: true, data: 'First update' }, { $set: { data: 'Third update' } }, { upsert: true }) as Document;
|
||||
|
||||
expect(upsertedDoc.update).to.be.true;
|
||||
expect(upsertedDoc.data).to.equal('Third update');
|
||||
|
||||
numberOfUpdatedDocs = await db.update({ data: 'Third update' }, { $set: { data: 'Fourth update' } }, { upsert: true });
|
||||
|
||||
expect(numberOfUpdatedDocs).to.equal(1);
|
||||
});
|
||||
|
||||
it('Finding documents', async () => {
|
||||
const data1 = 'Text data 1';
|
||||
const data2 = 'Text data 2';
|
||||
|
||||
const insertedDoc1 = await db.insert({ data: data1, flag: true, no: 1 }) as Document;
|
||||
const insertedDoc2 = await db.insert({ data: data2, flag: true, no: 2 }) as Document;
|
||||
|
||||
const foundDocs = await db.find({ flag: true }) as Array<Document>;
|
||||
|
||||
expect(foundDocs).to.be.a('array');
|
||||
expect(foundDocs.length).to.equal(2);
|
||||
|
||||
foundDocs.sort(({ no: a }, { no: b }) => a - b);
|
||||
|
||||
expect(foundDocs[0]._id).to.equal(insertedDoc1._id);
|
||||
expect(foundDocs[0].data).to.equal(insertedDoc1.data);
|
||||
expect(foundDocs[1]._id).to.equal(insertedDoc2._id);
|
||||
expect(foundDocs[1].data).to.equal(insertedDoc2.data);
|
||||
|
||||
const projectedDocs = await db.find({ flag: true }, { no: 1, _id: 0 }) as Array<Document>;
|
||||
|
||||
expect(projectedDocs.length).to.equal(2);
|
||||
projectedDocs.forEach(data => {
|
||||
expect(Object.keys(data).length).to.equal(1);
|
||||
expect(Object.keys(data).pop()).to.equal('no');
|
||||
});
|
||||
});
|
||||
|
||||
it('Removing document', async () => {
|
||||
const data = 'Text data';
|
||||
|
||||
const insertedDoc = await db.insert({ data }) as Document;
|
||||
|
||||
expect(insertedDoc).to.be.a('object');
|
||||
expect(insertedDoc.data).to.equal(data);
|
||||
|
||||
await db.remove({ _id: insertedDoc._id });
|
||||
|
||||
const deletedDoc = await db.findOne({ _id: insertedDoc._id });
|
||||
|
||||
expect(deletedDoc).to.be.null;
|
||||
});
|
||||
|
||||
it('Test invalid database queries', async () => {
|
||||
try {
|
||||
await db.insert({});
|
||||
} catch (err) {
|
||||
expect((err as Error).message).to.equal('Cannot read property \'_id\' of undefined');
|
||||
}
|
||||
|
||||
try {
|
||||
await db.find({ size: { $invalidComparator: 1 } });
|
||||
} catch (err) {
|
||||
expect((err as Error).message).to.equal('Unknown comparison function $invalidComparator');
|
||||
}
|
||||
|
||||
try {
|
||||
await db.findOne({ field: { $invalidComparator: 1 } });
|
||||
} catch (err) {
|
||||
expect((err as Error).message).to.equal('Unknown comparison function $invalidComparator');
|
||||
}
|
||||
|
||||
try {
|
||||
await db.update({ field: { $undefinedComparator: 1 } }, {});
|
||||
} catch (err) {
|
||||
expect((err as Error).message).to.equal('Unknown comparison function $undefinedComparator');
|
||||
}
|
||||
|
||||
try {
|
||||
await db.remove({ field: { $undefinedComparator: 1 } });
|
||||
} catch (err) {
|
||||
expect((err as Error).message).to.equal('Unknown comparison function $undefinedComparator');
|
||||
}
|
||||
});
|
||||
|
||||
after(() => {
|
||||
if (fs.existsSync(pathToDB)) {
|
||||
fs.unlinkSync(pathToDB);
|
||||
}
|
||||
});
|
||||
});
|
19
src/test/express.ts
Normal file
19
src/test/express.ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
import chaiHTTP from 'chai-http';
|
||||
import chai, { expect } from 'chai';
|
||||
|
||||
import server from '../bin/server';
|
||||
|
||||
const app = server.app;
|
||||
|
||||
chai.use(chaiHTTP);
|
||||
|
||||
describe('Express app', () => {
|
||||
it('App is available', async () => {
|
||||
const agent = chai.request.agent(app);
|
||||
|
||||
const result = await agent
|
||||
.get('/');
|
||||
|
||||
expect(result).to.have.status(200);
|
||||
});
|
||||
});
|
141
src/test/models/alias.ts
Normal file
141
src/test/models/alias.ts
Normal file
|
@ -0,0 +1,141 @@
|
|||
import { expect } from 'chai';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import config from 'config';
|
||||
import Alias from '../../backend/models/alias';
|
||||
import { binaryMD5 } from '../../backend/utils/crypto';
|
||||
import database from '../../backend/utils/database';
|
||||
|
||||
const aliases = database['aliases'];
|
||||
|
||||
describe('Alias model', () => {
|
||||
after(() => {
|
||||
const pathToDB = path.resolve(__dirname, '../../../', config.get('database'), './aliases.db');
|
||||
|
||||
if (fs.existsSync(pathToDB)) {
|
||||
fs.unlinkSync(pathToDB);
|
||||
}
|
||||
});
|
||||
|
||||
it('Working with empty model', async () => {
|
||||
let alias = new Alias();
|
||||
|
||||
expect(alias.data).to.be.a('object');
|
||||
|
||||
let {data} = alias;
|
||||
|
||||
expect(data._id).to.be.undefined;
|
||||
expect(data.hash).to.be.undefined;
|
||||
expect(data.type).to.be.undefined;
|
||||
expect(data.deprecated).to.be.false;
|
||||
expect(data.id).to.be.undefined;
|
||||
|
||||
alias = new Alias();
|
||||
|
||||
data = alias.data;
|
||||
|
||||
expect(data._id).to.be.undefined;
|
||||
expect(data.hash).to.be.undefined;
|
||||
expect(data.type).to.be.undefined;
|
||||
expect(data.deprecated).to.be.false;
|
||||
expect(data.id).to.be.undefined;
|
||||
|
||||
const initialData = {
|
||||
_id: 'alias_id',
|
||||
type: Alias.types.PAGE,
|
||||
id: 'page_id'
|
||||
};
|
||||
const aliasName = 'alias name';
|
||||
|
||||
alias = new Alias(initialData, aliasName);
|
||||
data = alias.data;
|
||||
|
||||
expect(data._id).to.equal(initialData._id);
|
||||
expect(data.hash).to.equal(binaryMD5(aliasName));
|
||||
expect(data.type).to.equal(initialData.type);
|
||||
expect(data.deprecated).to.equal(false);
|
||||
|
||||
const update = {
|
||||
type: Alias.types.PAGE,
|
||||
id: 'page_id',
|
||||
hash: binaryMD5('another test hash'),
|
||||
deprecated: true
|
||||
};
|
||||
|
||||
alias.data = update;
|
||||
|
||||
data = alias.data;
|
||||
|
||||
expect(data._id).to.equal(initialData._id);
|
||||
expect(data.type).to.equal(update.type);
|
||||
expect(data.hash).to.equal(update.hash);
|
||||
expect(data.deprecated).to.equal(update.deprecated);
|
||||
});
|
||||
|
||||
it('Static get method', async () => {
|
||||
const initialData = {
|
||||
type: Alias.types.PAGE,
|
||||
id: 'page_id'
|
||||
};
|
||||
const aliasName = 'alias name';
|
||||
|
||||
const alias = new Alias(initialData, aliasName);
|
||||
|
||||
const savedAlias = await alias.save();
|
||||
|
||||
const foundAlias = await Alias.get(aliasName);
|
||||
|
||||
const {data} = foundAlias;
|
||||
|
||||
expect(data._id).to.equal(savedAlias._id);
|
||||
expect(data.hash).to.equal(binaryMD5(aliasName));
|
||||
expect(data.type).to.equal(initialData.type);
|
||||
expect(data.deprecated).to.equal(false);
|
||||
});
|
||||
|
||||
it('Saving, updating and deleting model in the database', async () => {
|
||||
const initialData = {
|
||||
type: Alias.types.PAGE,
|
||||
id: 'page_id'
|
||||
};
|
||||
const aliasName = 'alias name';
|
||||
|
||||
const alias = new Alias(initialData, aliasName);
|
||||
|
||||
const savedAlias = await alias.save();
|
||||
|
||||
expect(savedAlias._id).not.be.undefined;
|
||||
expect(savedAlias.hash).to.equal(binaryMD5(aliasName));
|
||||
expect(savedAlias.type).to.equal(initialData.type);
|
||||
expect(savedAlias.id).to.equal(initialData.id);
|
||||
expect(savedAlias.deprecated).to.equal(false);
|
||||
|
||||
const insertedAlias = await aliases.findOne({_id: savedAlias._id}) as Alias;
|
||||
|
||||
expect(insertedAlias._id).to.equal(savedAlias._id);
|
||||
expect(insertedAlias.hash).to.equal(savedAlias.hash);
|
||||
expect(insertedAlias.type).to.equal(savedAlias.type);
|
||||
expect(insertedAlias.id).to.equal(savedAlias.id);
|
||||
expect(insertedAlias.deprecated).to.equal(savedAlias.deprecated);
|
||||
|
||||
const updateData = {
|
||||
type: Alias.types.PAGE,
|
||||
id: 'page_id',
|
||||
hash: binaryMD5('another test hash'),
|
||||
deprecated: true
|
||||
};
|
||||
|
||||
alias.data = updateData;
|
||||
await alias.save();
|
||||
|
||||
expect(alias._id).to.equal(insertedAlias._id);
|
||||
|
||||
const updatedAlias = await aliases.findOne({_id: alias._id}) as Alias;
|
||||
|
||||
expect(updatedAlias._id).to.equal(savedAlias._id);
|
||||
expect(updatedAlias.hash).to.equal(updateData.hash);
|
||||
expect(updatedAlias.type).to.equal(updateData.type);
|
||||
expect(updatedAlias.id).to.equal(updateData.id);
|
||||
expect(updatedAlias.deprecated).to.equal(updateData.deprecated);
|
||||
});
|
||||
});
|
237
src/test/models/file.ts
Normal file
237
src/test/models/file.ts
Normal file
|
@ -0,0 +1,237 @@
|
|||
import { expect } from 'chai';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import config from 'config';
|
||||
import File from '../../backend/models/file';
|
||||
import database from '../../backend/utils/database';
|
||||
|
||||
const files = database['files'];
|
||||
|
||||
describe('File model', () => {
|
||||
|
||||
after(() => {
|
||||
const pathToDB = path.resolve(__dirname, '../../../', config.get('database'), './files.db');
|
||||
|
||||
if (fs.existsSync(pathToDB)) {
|
||||
fs.unlinkSync(pathToDB);
|
||||
}
|
||||
});
|
||||
|
||||
it('Working with empty model', async () => {
|
||||
let file = new File();
|
||||
|
||||
expect(file.data).to.be.a('object');
|
||||
|
||||
let { data } = file;
|
||||
|
||||
expect(data._id).to.be.undefined;
|
||||
expect(data.name).to.be.undefined;
|
||||
expect(data.filename).to.be.undefined;
|
||||
expect(data.path).to.be.undefined;
|
||||
expect(data.size).to.be.undefined;
|
||||
expect(data.mimetype).to.be.undefined;
|
||||
|
||||
file = new File();
|
||||
|
||||
data = file.data;
|
||||
|
||||
expect(data._id).to.be.undefined;
|
||||
expect(data.name).to.be.undefined;
|
||||
expect(data.filename).to.be.undefined;
|
||||
expect(data.path).to.be.undefined;
|
||||
expect(data.size).to.be.undefined;
|
||||
expect(data.mimetype).to.be.undefined;
|
||||
|
||||
const initialData = {
|
||||
_id: 'file_id',
|
||||
name: 'filename',
|
||||
filename: 'randomname',
|
||||
path: '/uploads/randomname',
|
||||
size: 1024,
|
||||
mimetype: 'image/png'
|
||||
};
|
||||
|
||||
file = new File(initialData);
|
||||
|
||||
data = file.data;
|
||||
|
||||
expect(data._id).to.equal(initialData._id);
|
||||
expect(data.name).to.equal(initialData.name);
|
||||
expect(data.filename).to.equal(initialData.filename);
|
||||
expect(data.path).to.equal(initialData.path);
|
||||
expect(data.size).to.equal(initialData.size);
|
||||
expect(data.mimetype).to.equal(initialData.mimetype);
|
||||
|
||||
const update = {
|
||||
_id: '12345',
|
||||
name: 'updated filename',
|
||||
filename: 'updated randomname',
|
||||
path: '/uploads/updated randomname',
|
||||
size: 2048,
|
||||
mimetype: 'image/jpeg'
|
||||
};
|
||||
|
||||
file.data = update;
|
||||
|
||||
data = file.data;
|
||||
|
||||
expect(data._id).to.equal(initialData._id);
|
||||
expect(data.name).to.equal(update.name);
|
||||
expect(data.filename).to.equal(update.filename);
|
||||
expect(data.path).to.equal(update.path);
|
||||
expect(data.size).to.equal(update.size);
|
||||
expect(data.mimetype).to.equal(update.mimetype);
|
||||
});
|
||||
|
||||
it('Saving, updating and deleting model in the database', async () => {
|
||||
const initialData = {
|
||||
name: 'filename',
|
||||
filename: 'randomname',
|
||||
path: '/uploads/randomname',
|
||||
size: 1024,
|
||||
mimetype: 'image/png'
|
||||
};
|
||||
|
||||
const file = new File(initialData);
|
||||
|
||||
const savedFile = await file.save();
|
||||
|
||||
expect(savedFile._id).not.be.undefined;
|
||||
expect(savedFile.name).to.equal(initialData.name);
|
||||
expect(savedFile.filename).to.equal(initialData.filename);
|
||||
expect(savedFile.path).to.equal(initialData.path);
|
||||
expect(savedFile.size).to.equal(initialData.size);
|
||||
expect(savedFile.mimetype).to.equal(initialData.mimetype);
|
||||
|
||||
const insertedFile = await files.findOne({ _id: file._id });
|
||||
|
||||
expect(insertedFile._id).to.equal(file._id);
|
||||
expect(insertedFile.name).to.equal(file.name);
|
||||
expect(insertedFile.filename).to.equal(file.filename);
|
||||
expect(insertedFile.path).to.equal(file.path);
|
||||
expect(insertedFile.size).to.equal(file.size);
|
||||
expect(insertedFile.mimetype).to.equal(file.mimetype);
|
||||
|
||||
const updateData = {
|
||||
_id: '12345',
|
||||
name: 'updated filename',
|
||||
filename: 'updated randomname',
|
||||
path: '/uploads/updated randomname',
|
||||
size: 2048,
|
||||
mimetype: 'image/jpeg'
|
||||
};
|
||||
|
||||
file.data = updateData;
|
||||
await file.save();
|
||||
|
||||
expect(file._id).to.equal(insertedFile._id);
|
||||
|
||||
const updatedFile = await files.findOne({ _id: file._id });
|
||||
|
||||
expect(updatedFile._id).to.equal(savedFile._id);
|
||||
expect(updatedFile.name).to.equal(updateData.name);
|
||||
expect(updatedFile.filename).to.equal(updateData.filename);
|
||||
expect(updatedFile.path).to.equal(updateData.path);
|
||||
expect(updatedFile.size).to.equal(updateData.size);
|
||||
expect(updatedFile.mimetype).to.equal(updateData.mimetype);
|
||||
|
||||
await file.destroy();
|
||||
|
||||
expect(file._id).to.be.undefined;
|
||||
|
||||
const removedFile = await files.findOne({ _id: updatedFile._id });
|
||||
|
||||
expect(removedFile).to.be.null;
|
||||
});
|
||||
|
||||
it('Static get method', async () => {
|
||||
const initialData = {
|
||||
name: 'filename',
|
||||
filename: 'randomname',
|
||||
path: '/uploads/randomname',
|
||||
size: 1024,
|
||||
mimetype: 'image/png'
|
||||
};
|
||||
|
||||
const file = new File(initialData);
|
||||
|
||||
const savedFile = await file.save();
|
||||
|
||||
if (savedFile._id !== undefined){
|
||||
const foundFile = await File.get(savedFile._id);
|
||||
|
||||
const { data } = foundFile;
|
||||
|
||||
expect(data._id).to.equal(savedFile._id);
|
||||
expect(data.name).to.equal(savedFile.name);
|
||||
expect(data.filename).to.equal(savedFile.filename);
|
||||
expect(data.path).to.equal(savedFile.path);
|
||||
expect(data.size).to.equal(savedFile.size);
|
||||
expect(data.mimetype).to.equal(savedFile.mimetype);
|
||||
}
|
||||
|
||||
await file.destroy();
|
||||
});
|
||||
|
||||
it('Static getByFilename method', async () => {
|
||||
const initialData = {
|
||||
name: 'filename',
|
||||
filename: 'randomname',
|
||||
path: '/uploads/randomname',
|
||||
size: 1024,
|
||||
mimetype: 'image/png'
|
||||
};
|
||||
|
||||
const file = new File(initialData);
|
||||
|
||||
const savedFile = await file.save();
|
||||
|
||||
if (savedFile.filename !== undefined){
|
||||
const foundFile = await File.getByFilename(savedFile.filename);
|
||||
|
||||
const { data } = foundFile;
|
||||
|
||||
expect(data._id).to.equal(savedFile._id);
|
||||
expect(data.name).to.equal(savedFile.name);
|
||||
expect(data.filename).to.equal(savedFile.filename);
|
||||
expect(data.path).to.equal(savedFile.path);
|
||||
expect(data.size).to.equal(savedFile.size);
|
||||
expect(data.mimetype).to.equal(savedFile.mimetype);
|
||||
}
|
||||
|
||||
await file.destroy();
|
||||
});
|
||||
|
||||
it('Static getAll method', async () => {
|
||||
const filesToSave = [
|
||||
new File({
|
||||
name: 'filename1',
|
||||
filename: 'randomname1',
|
||||
path: '/uploads/randomname1',
|
||||
size: 1024,
|
||||
mimetype: 'image/png'
|
||||
}),
|
||||
new File({
|
||||
name: 'filename2',
|
||||
filename: 'randomname2',
|
||||
path: '/uploads/randomname2',
|
||||
size: 2048,
|
||||
mimetype: 'image/jpeg'
|
||||
}),
|
||||
];
|
||||
|
||||
const savedFiles = await Promise.all(filesToSave.map(file => file.save()));
|
||||
|
||||
const foundFiles = await File.getAll({ _id: { $in: savedFiles.map(file => file._id) } });
|
||||
|
||||
expect(foundFiles.length).to.equal(2);
|
||||
|
||||
foundFiles.forEach((file, i) => {
|
||||
expect(file.name).to.equal(filesToSave[i].name);
|
||||
expect(file.filename).to.equal(filesToSave[i].filename);
|
||||
expect(file.path).to.equal(filesToSave[i].path);
|
||||
expect(file.size).to.equal(filesToSave[i].size);
|
||||
expect(file.mimetype).to.equal(filesToSave[i].mimetype);
|
||||
});
|
||||
});
|
||||
});
|
394
src/test/models/page.ts
Normal file
394
src/test/models/page.ts
Normal file
|
@ -0,0 +1,394 @@
|
|||
import { expect } from 'chai';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import config from 'config';
|
||||
import Page from '../../backend/models/page';
|
||||
import translateString from '../../backend/utils/translation';
|
||||
import database from '../../backend/utils/database';
|
||||
|
||||
const pages = database['pages'];
|
||||
|
||||
describe('Page model', () => {
|
||||
const transformToUri = (text: string): string => {
|
||||
return translateString(text
|
||||
.replace(/ /g, ' ')
|
||||
.replace(/[^a-zA-Z0-9А-Яа-яЁё ]/g, ' ')
|
||||
.replace(/ +/g, ' ')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.split(' ')
|
||||
.join('-'));
|
||||
};
|
||||
|
||||
after(() => {
|
||||
const pathToDB = path.resolve(__dirname, '../../../', config.get('database'), './pages.db');
|
||||
|
||||
if (fs.existsSync(pathToDB)) {
|
||||
fs.unlinkSync(pathToDB);
|
||||
}
|
||||
});
|
||||
|
||||
it('Working with empty model', async () => {
|
||||
let page = new Page();
|
||||
|
||||
expect(page.data).to.be.a('object');
|
||||
|
||||
let {data} = page;
|
||||
|
||||
expect(data._id).to.be.undefined;
|
||||
expect(data.title).to.be.empty;
|
||||
expect(data.uri).to.be.empty;
|
||||
expect(data.body).to.be.undefined;
|
||||
expect(data.parent).to.be.equal('0');
|
||||
|
||||
page = new Page();
|
||||
|
||||
data = page.data;
|
||||
|
||||
expect(data._id).to.be.undefined;
|
||||
expect(data.title).to.be.empty;
|
||||
expect(data.uri).to.be.empty;
|
||||
expect(data.body).to.be.undefined;
|
||||
expect(data.parent).to.be.equal('0');
|
||||
|
||||
const initialData = {
|
||||
_id: 'page_id',
|
||||
body: {
|
||||
blocks: [
|
||||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'Page header'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
page = new Page(initialData);
|
||||
|
||||
const json = page.toJSON();
|
||||
|
||||
data = page.data;
|
||||
|
||||
expect(data._id).to.equal(initialData._id);
|
||||
expect(data.title).to.equal(initialData.body.blocks[0].data.text);
|
||||
expect(data.uri).to.be.empty;
|
||||
expect(data.body).to.deep.equal(initialData.body);
|
||||
expect(data.parent).to.be.equal('0');
|
||||
|
||||
expect(json._id).to.equal(initialData._id);
|
||||
expect(json.title).to.equal(initialData.body.blocks[0].data.text);
|
||||
expect(json.title).to.equal(initialData.body.blocks[0].data.text);
|
||||
expect(json.body).to.deep.equal(initialData.body);
|
||||
expect(json.parent).to.be.equal('0');
|
||||
|
||||
const update = {
|
||||
_id: '12345',
|
||||
body: {
|
||||
blocks: [
|
||||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'Updated page header'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
page.data = update;
|
||||
|
||||
data = page.data;
|
||||
|
||||
expect(data._id).to.equal(initialData._id);
|
||||
expect(data.title).to.equal(update.body.blocks[0].data.text);
|
||||
expect(data.uri).to.be.empty;
|
||||
expect(data.body).to.equal(update.body);
|
||||
expect(data.parent).to.be.equal('0');
|
||||
});
|
||||
|
||||
it('Saving, updating and deleting model in the database', async () => {
|
||||
const initialData = {
|
||||
body: {
|
||||
blocks: [
|
||||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'New page header'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
const page = new Page(initialData);
|
||||
|
||||
const savedPage = await page.save();
|
||||
|
||||
expect(savedPage._id).not.be.undefined;
|
||||
expect(savedPage.title).to.equal(initialData.body.blocks[0].data.text);
|
||||
expect(savedPage.uri).to.equal(transformToUri(initialData.body.blocks[0].data.text));
|
||||
expect(savedPage.body).to.equal(initialData.body);
|
||||
expect(page._id).not.be.undefined;
|
||||
|
||||
const insertedPage = await pages.findOne({_id: page._id});
|
||||
|
||||
expect(insertedPage._id).to.equal(page._id);
|
||||
expect(insertedPage.title).to.equal(page.title);
|
||||
expect(insertedPage.uri).to.equal(page.uri);
|
||||
expect(insertedPage.body).to.deep.equal(page.body);
|
||||
|
||||
const updateData = {
|
||||
body: {
|
||||
blocks: [
|
||||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'Updated page header'
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
uri: 'updated-uri'
|
||||
};
|
||||
|
||||
page.data = updateData;
|
||||
await page.save();
|
||||
|
||||
expect(page._id).to.equal(insertedPage._id);
|
||||
|
||||
const updatedPage = await pages.findOne({_id: page._id});
|
||||
|
||||
expect(updatedPage._id).to.equal(savedPage._id);
|
||||
expect(updatedPage.title).to.equal(updateData.body.blocks[0].data.text);
|
||||
expect(updatedPage.uri).to.equal(updateData.uri);
|
||||
expect(updatedPage.body).to.deep.equal(updateData.body);
|
||||
|
||||
await page.destroy();
|
||||
|
||||
expect(page._id).to.be.undefined;
|
||||
|
||||
const removedPage = await pages.findOne({_id: updatedPage._id});
|
||||
|
||||
expect(removedPage).to.be.null;
|
||||
});
|
||||
|
||||
it('Handle multiple page creation with the same uri', async () => {
|
||||
const initialData = {
|
||||
body: {
|
||||
blocks: [
|
||||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'New page header'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
const firstPage = new Page(initialData);
|
||||
let firstSavedPage = await firstPage.save();
|
||||
const secondPage = new Page(initialData);
|
||||
const secondSavedPage = await secondPage.save();
|
||||
|
||||
expect(secondSavedPage.uri).to.equal(transformToUri(initialData.body.blocks[0].data.text) + '-1');
|
||||
|
||||
const newUri = 'new-uri';
|
||||
|
||||
firstPage.data = {...firstPage.data, uri: newUri};
|
||||
firstSavedPage = await firstPage.save();
|
||||
|
||||
expect(firstSavedPage.uri).to.equal(newUri);
|
||||
|
||||
const thirdPage = new Page(initialData);
|
||||
const thirdSavedPage = await thirdPage.save();
|
||||
|
||||
expect(thirdSavedPage.uri).to.equal(transformToUri(initialData.body.blocks[0].data.text));
|
||||
});
|
||||
|
||||
it('Static get method', async () => {
|
||||
const initialData = {
|
||||
body: {
|
||||
blocks: [
|
||||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'Test Page header'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
const page = new Page(initialData);
|
||||
|
||||
const savedPage = await page.save();
|
||||
|
||||
if (savedPage._id == undefined) {
|
||||
await page.destroy();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const foundPage = await Page.get(savedPage._id);
|
||||
|
||||
const {data} = foundPage;
|
||||
|
||||
expect(data._id).to.equal(savedPage._id);
|
||||
expect(data.title).to.equal(initialData.body.blocks[0].data.text);
|
||||
expect(data.uri).to.equal(transformToUri(initialData.body.blocks[0].data.text));
|
||||
expect(data.body).to.deep.equal(initialData.body);
|
||||
|
||||
await page.destroy();
|
||||
});
|
||||
|
||||
it('Static getAll method', async () => {
|
||||
const pagesToSave = [
|
||||
new Page({
|
||||
body: {
|
||||
blocks: [
|
||||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'Page 1 header'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}),
|
||||
new Page({
|
||||
body: {
|
||||
blocks: [
|
||||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'Page 2 header'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
})
|
||||
];
|
||||
|
||||
const savedPages = await Promise.all(pagesToSave.map(page => page.save()));
|
||||
|
||||
const foundPages = await Page.getAll({_id: {$in: savedPages.map(page => page._id)}});
|
||||
|
||||
expect(foundPages.length).to.equal(2);
|
||||
foundPages.forEach((page, i) => {
|
||||
expect(page.title).to.equal(pagesToSave[i].body.blocks[0].data.text);
|
||||
expect(page.uri).to.equal(transformToUri(pagesToSave[i].body.blocks[0].data.text));
|
||||
expect(page.body).to.deep.equal(pagesToSave[i].body);
|
||||
});
|
||||
});
|
||||
|
||||
it('Parent pages', async () => {
|
||||
const parent = new Page(
|
||||
{
|
||||
body: {
|
||||
blocks: [
|
||||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'Parent page header'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
);
|
||||
const {_id: parentId} = await parent.save();
|
||||
|
||||
const child = new Page(
|
||||
{
|
||||
body: {
|
||||
blocks: [
|
||||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'Child page header'
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
parent: parentId,
|
||||
}
|
||||
);
|
||||
|
||||
const {_id: childId} = await child.save();
|
||||
|
||||
const testedParent = await child.getParent();
|
||||
|
||||
expect(testedParent).to.be.not.null;
|
||||
if (testedParent) {
|
||||
expect(testedParent._id).to.equal(parentId);
|
||||
expect(testedParent.title).to.equal(parent.body.blocks[0].data.text);
|
||||
expect(testedParent.uri).to.equal(transformToUri(parent.body.blocks[0].data.text));
|
||||
expect(testedParent.body).to.deep.equal(parent.body);
|
||||
}
|
||||
|
||||
const children = await parent.children;
|
||||
|
||||
expect(children.length).to.equal(1);
|
||||
|
||||
const temp: Page|undefined = children.pop();
|
||||
const testedChild: Page = !temp ? new Page({}) : temp;
|
||||
|
||||
expect(testedChild._id).to.equal(childId);
|
||||
expect(testedChild.title).to.equal(child.body.blocks[0].data.text);
|
||||
expect(testedChild.uri).to.equal(transformToUri(child.body.blocks[0].data.text));
|
||||
expect(testedChild.body).to.deep.equal(child.body);
|
||||
expect(testedChild._parent).to.equal(child._parent);
|
||||
expect(testedChild._parent).to.equal(parent._id);
|
||||
|
||||
parent.destroy();
|
||||
child.destroy();
|
||||
});
|
||||
|
||||
it('Extracting title from page body', async () => {
|
||||
const pageData = {
|
||||
body: {
|
||||
blocks: [
|
||||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'Page header'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
const page = new Page(pageData);
|
||||
|
||||
expect(page.title).to.equal(pageData.body.blocks[0].data.text);
|
||||
});
|
||||
|
||||
it('test deletion', async () => {
|
||||
const pageIndexes = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
|
||||
const orders = {
|
||||
'0' : ['1', '2', '3'],
|
||||
'1' : ['4', '5'],
|
||||
'5' : ['6', '7', '8'],
|
||||
'3' : ['9'],
|
||||
} as { [key: string]: string[] };
|
||||
|
||||
function deleteRecursively(startFrom: string): void {
|
||||
const order: string[] = orders[startFrom];
|
||||
|
||||
if (!order) {
|
||||
const found = pageIndexes.indexOf(startFrom);
|
||||
|
||||
pageIndexes.splice(found, 1);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
order.forEach(id => {
|
||||
deleteRecursively(id);
|
||||
});
|
||||
|
||||
const found = pageIndexes.indexOf(startFrom);
|
||||
pageIndexes.splice(found, 1);
|
||||
}
|
||||
});
|
||||
});
|
149
src/test/models/pageOrder.ts
Normal file
149
src/test/models/pageOrder.ts
Normal file
|
@ -0,0 +1,149 @@
|
|||
import { expect } from 'chai';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import config from 'config';
|
||||
import PageOrder from '../../backend/models/pageOrder';
|
||||
import database from '../../backend/utils/database';
|
||||
|
||||
const pagesOrder = database['pagesOrder'];
|
||||
|
||||
describe('PageOrder model', () => {
|
||||
after(() => {
|
||||
const pathToDB = path.resolve(__dirname, '../../../', config.get('database'), './pagesOrder.db');
|
||||
|
||||
if (fs.existsSync(pathToDB)) {
|
||||
fs.unlinkSync(pathToDB);
|
||||
}
|
||||
});
|
||||
|
||||
it('Empty Model', async () => {
|
||||
const pageOrder = new PageOrder();
|
||||
|
||||
expect(pageOrder.data).to.be.a('object');
|
||||
|
||||
let {data} = pageOrder;
|
||||
|
||||
expect(data._id).to.be.undefined;
|
||||
expect(data.page).to.be.to.equal('0');
|
||||
expect(data.order).to.be.an('array').that.is.empty;
|
||||
|
||||
let page = new PageOrder();
|
||||
|
||||
data = page.data;
|
||||
|
||||
expect(data._id).to.be.undefined;
|
||||
expect(data.page).to.be.to.equal('0');
|
||||
expect(data.order).to.be.an('array').that.is.empty;
|
||||
|
||||
const testData = {
|
||||
_id: 'order_id',
|
||||
page: 'page_id',
|
||||
order: []
|
||||
};
|
||||
|
||||
page = new PageOrder(testData);
|
||||
data = page.data;
|
||||
|
||||
expect(data._id).to.equal(testData._id);
|
||||
expect(data.page).to.equal(testData.page);
|
||||
expect(data.order).to.be.an('array').that.is.empty;
|
||||
});
|
||||
|
||||
it('Testing Model methods', async () => {
|
||||
const testData = {
|
||||
page: 'page_id',
|
||||
order: ['1', '2']
|
||||
};
|
||||
const pageOrder = new PageOrder(testData);
|
||||
const {data} = await pageOrder.save();
|
||||
|
||||
expect(data._id).not.be.undefined;
|
||||
expect(data.page).to.equal(testData.page);
|
||||
expect(data.order).to.deep.equals(testData.order);
|
||||
|
||||
const insertedPageOrder = await pagesOrder.findOne({_id: data._id}) as PageOrder;
|
||||
expect(insertedPageOrder._id).to.equal(data._id);
|
||||
expect(insertedPageOrder.page).to.equal(data.page);
|
||||
expect(insertedPageOrder.order).to.deep.equal(data.order);
|
||||
|
||||
const updateData = {
|
||||
page: 'page_id_2',
|
||||
order: ['3']
|
||||
};
|
||||
|
||||
pageOrder.data = updateData;
|
||||
await pageOrder.save();
|
||||
|
||||
expect(pageOrder.data._id).to.equal(insertedPageOrder._id);
|
||||
|
||||
const updatedData = await pagesOrder.findOne({_id: insertedPageOrder._id}) as PageOrder;
|
||||
|
||||
expect(updatedData.page).to.equal(updateData.page);
|
||||
expect(updatedData.order).to.deep.equal(updateData.order);
|
||||
|
||||
await pageOrder.destroy();
|
||||
|
||||
expect(pageOrder.data._id).to.be.undefined;
|
||||
|
||||
const removedPage = await pagesOrder.findOne({_id: updatedData._id});
|
||||
|
||||
expect(removedPage).to.be.null;
|
||||
});
|
||||
|
||||
it('Testing push and remove order methods', async () => {
|
||||
const testData = {
|
||||
page: 'page_id',
|
||||
order: ['1', '2']
|
||||
};
|
||||
const pageOrder = new PageOrder(testData);
|
||||
await pageOrder.save();
|
||||
pageOrder.push('3');
|
||||
expect(pageOrder.data.order).to.be.an('array').that.is.not.empty;
|
||||
if (pageOrder.data.order !== undefined) {
|
||||
pageOrder.data.order.forEach((el) => {
|
||||
expect(el).to.be.an('string');
|
||||
});
|
||||
}
|
||||
|
||||
expect(pageOrder.data.order).to.deep.equals(['1', '2', '3']);
|
||||
|
||||
pageOrder.remove('2');
|
||||
expect(pageOrder.data.order).to.deep.equals(['1', '3']);
|
||||
|
||||
expect(() => {
|
||||
pageOrder.push(3);
|
||||
}).to.throw('given id is not string');
|
||||
|
||||
pageOrder.push('4');
|
||||
pageOrder.push('5');
|
||||
pageOrder.push('2');
|
||||
|
||||
pageOrder.putAbove('2', '3');
|
||||
expect(pageOrder.data.order).to.deep.equals(['1', '2', '3', '4', '5']);
|
||||
|
||||
pageOrder.putAbove('2', '10');
|
||||
expect(pageOrder.data.order).to.deep.equals(['1', '2', '3', '4', '5']);
|
||||
await pageOrder.destroy();
|
||||
});
|
||||
|
||||
it('Testing static methods', async () => {
|
||||
const testData = {
|
||||
page: 'page_id',
|
||||
order: ['1', '2']
|
||||
};
|
||||
const pageOrder = new PageOrder(testData);
|
||||
const insertedData = await pageOrder.save();
|
||||
|
||||
if (insertedData.data.page !== undefined) {
|
||||
const insertedPageOrder = await PageOrder.get(insertedData.data.page);
|
||||
expect(insertedPageOrder).to.instanceOf(PageOrder);
|
||||
expect(insertedPageOrder.data._id).to.be.equal(insertedData.data._id);
|
||||
}
|
||||
|
||||
const emptyInstance = await PageOrder.get('');
|
||||
expect(emptyInstance.data.page).to.be.equal('0');
|
||||
expect(emptyInstance.data.order).to.be.an('array').that.is.empty;
|
||||
|
||||
await pageOrder.destroy();
|
||||
});
|
||||
});
|
272
src/test/rcparser.ts
Normal file
272
src/test/rcparser.ts
Normal file
|
@ -0,0 +1,272 @@
|
|||
import { expect } from 'chai';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import config from 'config';
|
||||
import sinon = require('sinon');
|
||||
|
||||
import rcParser from '../backend/utils/rcparser';
|
||||
|
||||
const rcPath = path.resolve(process.cwd(), config.get('rcFile'));
|
||||
|
||||
describe('RC file parser test', () => {
|
||||
afterEach(() => {
|
||||
if (fs.existsSync(rcPath)) {
|
||||
fs.unlinkSync(rcPath);
|
||||
}
|
||||
});
|
||||
|
||||
it('Default config', async () => {
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(parsedConfig).to.be.deep.equal(rcParser.DEFAULTS);
|
||||
});
|
||||
|
||||
it('Invalid JSON formatted config', () => {
|
||||
const invalidJson = '{title: "Codex Docs"}';
|
||||
const spy = sinon.spy(console, 'log');
|
||||
|
||||
fs.writeFileSync(rcPath, invalidJson, 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(spy.calledOnce).to.be.true;
|
||||
expect(spy.calledWith('CodeX Docs rc file should be in JSON format.')).to.be.true;
|
||||
|
||||
expect(parsedConfig).to.be.deep.equal(rcParser.DEFAULTS);
|
||||
spy.restore();
|
||||
});
|
||||
|
||||
it('Normal config', () => {
|
||||
const normalConfig = {
|
||||
title: 'Documentation',
|
||||
menu: [
|
||||
{ title: 'Option 1', uri: '/option1' },
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
],
|
||||
};
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(parsedConfig).to.be.deep.equal(normalConfig);
|
||||
});
|
||||
|
||||
it('Missed title', () => {
|
||||
const normalConfig = {
|
||||
menu: [
|
||||
{ title: 'Option 1', uri: '/option1' },
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
],
|
||||
};
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(parsedConfig.menu).to.be.deep.equal(normalConfig.menu);
|
||||
expect(parsedConfig.title).to.be.equal(rcParser.DEFAULTS.title);
|
||||
});
|
||||
|
||||
it('Missed menu', () => {
|
||||
const normalConfig = {
|
||||
title: 'Documentation',
|
||||
};
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(parsedConfig.title).to.be.equal(normalConfig.title);
|
||||
expect(parsedConfig.menu).to.be.deep.equal(rcParser.DEFAULTS.menu);
|
||||
});
|
||||
|
||||
it('Menu is not an array', () => {
|
||||
const normalConfig = {
|
||||
title: 'Documentation',
|
||||
menu: {
|
||||
0: { title: 'Option 1', uri: '/option1' },
|
||||
1: { title: 'Option 2', uri: '/option2' },
|
||||
2: { title: 'Option 3', uri: '/option3' },
|
||||
},
|
||||
};
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
const spy = sinon.spy(console, 'log');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(spy.calledOnce).to.be.true;
|
||||
expect(spy.calledWith('Menu section in the rc file must be an array.')).to.be.true;
|
||||
|
||||
expect(parsedConfig.title).to.be.equal(normalConfig.title);
|
||||
expect(parsedConfig.menu).to.be.deep.equal(rcParser.DEFAULTS.menu);
|
||||
spy.restore();
|
||||
});
|
||||
|
||||
it('Menu option is a string', () => {
|
||||
const normalConfig = {
|
||||
title: 'Documentation',
|
||||
menu: [
|
||||
'Option 1',
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
],
|
||||
};
|
||||
|
||||
const expectedMenu = [
|
||||
{ title: 'Option 1', uri: '/option-1' },
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
];
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(parsedConfig.title).to.be.equal(normalConfig.title);
|
||||
expect(parsedConfig.menu).to.be.deep.equal(expectedMenu);
|
||||
});
|
||||
|
||||
it('Menu option is not a string or an object', () => {
|
||||
const normalConfig = {
|
||||
title: 'Documentation',
|
||||
menu: [
|
||||
[ { title: 'Option 1', uri: '/option1' } ],
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
],
|
||||
};
|
||||
|
||||
const expectedMenu = [
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
];
|
||||
const spy = sinon.spy(console, 'log');
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(spy.calledOnce).to.be.true;
|
||||
expect(spy.calledWith('Menu option #1 in rc file must be a string or an object')).to.be.true;
|
||||
|
||||
expect(parsedConfig.title).to.be.equal(normalConfig.title);
|
||||
expect(parsedConfig.menu).to.be.deep.equal(expectedMenu);
|
||||
spy.restore();
|
||||
});
|
||||
|
||||
it('Menu option title is undefined', () => {
|
||||
const normalConfig = {
|
||||
title: 'Documentation',
|
||||
menu: [
|
||||
{ uri: '/option1' },
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
],
|
||||
};
|
||||
|
||||
const expectedMenu = [
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
];
|
||||
const spy = sinon.spy(console, 'log');
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(spy.calledOnce).to.be.true;
|
||||
expect(spy.calledWith('Menu option #1 title must be a string.')).to.be.true;
|
||||
|
||||
expect(parsedConfig.title).to.be.equal(normalConfig.title);
|
||||
expect(parsedConfig.menu).to.be.deep.equal(expectedMenu);
|
||||
spy.restore();
|
||||
});
|
||||
|
||||
it('Menu option title is not a string', () => {
|
||||
const normalConfig = {
|
||||
title: 'Documentation',
|
||||
menu: [
|
||||
{ title: [], uri: '/option1' },
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
],
|
||||
};
|
||||
|
||||
const expectedMenu = [
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
];
|
||||
const spy = sinon.spy(console, 'log');
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(spy.calledOnce).to.be.true;
|
||||
expect(spy.calledWith('Menu option #1 title must be a string.')).to.be.true;
|
||||
|
||||
expect(parsedConfig.title).to.be.equal(normalConfig.title);
|
||||
expect(parsedConfig.menu).to.be.deep.equal(expectedMenu);
|
||||
spy.restore();
|
||||
});
|
||||
|
||||
it('Menu option uri is undefined', () => {
|
||||
const normalConfig = {
|
||||
title: 'Documentation',
|
||||
menu: [
|
||||
{ title: 'Option 1' },
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
],
|
||||
};
|
||||
|
||||
const expectedMenu = [
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
];
|
||||
const spy = sinon.spy(console, 'log');
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(spy.calledOnce).to.be.true;
|
||||
expect(spy.calledWith('Menu option #1 uri must be a string.')).to.be.true;
|
||||
|
||||
expect(parsedConfig.title).to.be.equal(normalConfig.title);
|
||||
expect(parsedConfig.menu).to.be.deep.equal(expectedMenu);
|
||||
spy.restore();
|
||||
});
|
||||
|
||||
it('Menu option title is not a string', () => {
|
||||
const normalConfig = {
|
||||
title: 'Documentation',
|
||||
menu: [
|
||||
{ title: 'Option 1', uri: [] },
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
],
|
||||
};
|
||||
|
||||
const expectedMenu = [
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
];
|
||||
const spy = sinon.spy(console, 'log');
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(spy.calledOnce).to.be.true;
|
||||
expect(spy.calledWith('Menu option #1 uri must be a string.')).to.be.true;
|
||||
|
||||
expect(parsedConfig.title).to.be.equal(normalConfig.title);
|
||||
expect(parsedConfig.menu).to.be.deep.equal(expectedMenu);
|
||||
spy.restore();
|
||||
});
|
||||
});
|
60
src/test/rest/aliases.ts
Normal file
60
src/test/rest/aliases.ts
Normal file
|
@ -0,0 +1,60 @@
|
|||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import config from 'config';
|
||||
import chai from 'chai';
|
||||
import chaiHTTP from 'chai-http';
|
||||
import server from '../../bin/server';
|
||||
|
||||
const {expect} = chai;
|
||||
const app = server.app;
|
||||
|
||||
chai.use(chaiHTTP);
|
||||
|
||||
describe('Aliases REST: ', () => {
|
||||
let agent: ChaiHttp.Agent;
|
||||
|
||||
before(async () => {
|
||||
agent = chai.request.agent(app);
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
const pathToDB = path.resolve(__dirname, '../../', config.get('database'), './pages.db');
|
||||
|
||||
if (fs.existsSync(pathToDB)) {
|
||||
fs.unlinkSync(pathToDB);
|
||||
}
|
||||
|
||||
const pathToAliasDB = path.resolve(__dirname, '../../', config.get('database'), './aliases.db');
|
||||
|
||||
if (fs.existsSync(pathToAliasDB)) {
|
||||
fs.unlinkSync(pathToAliasDB);
|
||||
}
|
||||
});
|
||||
|
||||
it('Finding page with alias', async () => {
|
||||
const body = {
|
||||
time: 1548375408533,
|
||||
blocks: [
|
||||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'Test header'
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
const put = await agent
|
||||
.put('/api/page')
|
||||
.send({body});
|
||||
|
||||
expect(put).to.have.status(200);
|
||||
expect(put).to.be.json;
|
||||
|
||||
const {result: {uri}} = put.body;
|
||||
|
||||
const get = await agent.get('/' + uri);
|
||||
|
||||
expect(get).to.have.status(200);
|
||||
});
|
||||
});
|
526
src/test/rest/pages.ts
Normal file
526
src/test/rest/pages.ts
Normal file
|
@ -0,0 +1,526 @@
|
|||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import config from 'config';
|
||||
import chai from 'chai';
|
||||
import chaiHTTP from 'chai-http';
|
||||
import server from '../../bin/server';
|
||||
import model from '../../backend/models/page';
|
||||
import Page from '../../backend/models/page';
|
||||
import PageOrder from '../../backend/models/pageOrder';
|
||||
import translateString from '../../backend/utils/translation';
|
||||
|
||||
const {expect} = chai;
|
||||
const app = server.app;
|
||||
|
||||
chai.use(chaiHTTP);
|
||||
|
||||
describe('Pages REST: ', () => {
|
||||
let agent: ChaiHttp.Agent;
|
||||
const transformToUri = (text: string):string => {
|
||||
return translateString(text
|
||||
.replace(/ /g, ' ')
|
||||
.replace(/[^a-zA-Z0-9А-Яа-яЁё ]/g, ' ')
|
||||
.replace(/ +/g, ' ')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.split(' ')
|
||||
.join('-'));
|
||||
};
|
||||
|
||||
before(async () => {
|
||||
agent = chai.request.agent(app);
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
const pathToPagesDB = path.resolve(__dirname, '../../../', config.get('database'), './pages.db');
|
||||
const pathToPagesOrderDB = path.resolve(__dirname, '../../../', config.get('database'), './pagesOrder.db');
|
||||
const pathToAliasesDB = path.resolve(__dirname, '../../../', config.get('database'), './aliases.db');
|
||||
|
||||
if (fs.existsSync(pathToPagesDB)) {
|
||||
fs.unlinkSync(pathToPagesDB);
|
||||
}
|
||||
|
||||
if (fs.existsSync(pathToPagesOrderDB)) {
|
||||
fs.unlinkSync(pathToPagesOrderDB);
|
||||
}
|
||||
|
||||
if (fs.existsSync(pathToAliasesDB)) {
|
||||
fs.unlinkSync(pathToAliasesDB);
|
||||
}
|
||||
});
|
||||
|
||||
it('Creating page', async () => {
|
||||
const body = {
|
||||
blocks: [
|
||||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'Page header'
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
const parent = 0;
|
||||
const res = await agent
|
||||
.put('/api/page')
|
||||
.send({body, parent});
|
||||
|
||||
expect(res).to.have.status(200);
|
||||
expect(res).to.be.json;
|
||||
|
||||
const {success, result} = res.body;
|
||||
|
||||
expect(success).to.be.true;
|
||||
expect(result._id).to.be.a('string');
|
||||
expect(result.title).to.equal(body.blocks[0].data.text);
|
||||
expect(result.uri).to.equal(transformToUri(body.blocks[0].data.text));
|
||||
expect(result.body).to.deep.equal(body);
|
||||
|
||||
const createdPage = await model.get(result._id);
|
||||
|
||||
expect(createdPage).not.be.null;
|
||||
expect(createdPage._id).to.equal(result._id);
|
||||
expect(createdPage.title).to.equal(body.blocks[0].data.text);
|
||||
expect(createdPage.uri).to.equal(transformToUri(body.blocks[0].data.text));
|
||||
expect(createdPage.body).to.deep.equal(body);
|
||||
|
||||
const pageOrder = await PageOrder.get('' + (createdPage.data.parent || 0));
|
||||
expect(pageOrder.order).to.be.an('array');
|
||||
|
||||
await createdPage.destroy();
|
||||
await pageOrder.destroy();
|
||||
});
|
||||
|
||||
it('Page data validation on create', async () => {
|
||||
const res = await agent
|
||||
.put('/api/page')
|
||||
.send({ someField: 'Some text' });
|
||||
|
||||
expect(res).to.have.status(400);
|
||||
expect(res).to.be.json;
|
||||
|
||||
const { success, error } = res.body;
|
||||
|
||||
expect(success).to.be.false;
|
||||
expect(error).to.equal('validationError');
|
||||
});
|
||||
|
||||
it('Finding page', async () => {
|
||||
const body = {
|
||||
blocks: [
|
||||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'Page header'
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
const put = await agent
|
||||
.put('/api/page')
|
||||
.send({body});
|
||||
|
||||
expect(put).to.have.status(200);
|
||||
expect(put).to.be.json;
|
||||
|
||||
const {result: {_id}} = put.body;
|
||||
|
||||
const get = await agent.get(`/api/page/${_id}`);
|
||||
|
||||
expect(get).to.have.status(200);
|
||||
expect(get).to.be.json;
|
||||
|
||||
const {success} = get.body;
|
||||
|
||||
expect(success).to.be.true;
|
||||
|
||||
const foundPage = await model.get(_id);
|
||||
const pageOrder = await PageOrder.get('' + foundPage._parent);
|
||||
|
||||
expect(foundPage._id).to.equal(_id);
|
||||
expect(foundPage.title).to.equal(body.blocks[0].data.text);
|
||||
expect(foundPage.uri).to.equal(transformToUri(body.blocks[0].data.text));
|
||||
expect(foundPage.body).to.deep.equal(body);
|
||||
|
||||
await pageOrder.destroy();
|
||||
await foundPage.destroy();
|
||||
});
|
||||
|
||||
it('Finding page with not existing id', async () => {
|
||||
const res = await agent.get('/api/page/not-existing-id');
|
||||
|
||||
expect(res).to.have.status(400);
|
||||
expect(res).to.be.json;
|
||||
|
||||
const {success, error} = res.body;
|
||||
|
||||
expect(success).to.be.false;
|
||||
expect(error).to.equal('Page with given id does not exist');
|
||||
});
|
||||
|
||||
it('Updating page', async () => {
|
||||
const body = {
|
||||
blocks: [
|
||||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'Page header'
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
let res = await agent
|
||||
.put('/api/page')
|
||||
.send({body});
|
||||
|
||||
expect(res).to.have.status(200);
|
||||
expect(res).to.be.json;
|
||||
|
||||
const {result: {_id}} = res.body;
|
||||
|
||||
const updatedBody = {
|
||||
blocks: [
|
||||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'Updated page header'
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
const updatedUri = 'updated-uri';
|
||||
|
||||
res = await agent
|
||||
.post(`/api/page/${_id}`)
|
||||
.send({body: updatedBody, uri: updatedUri});
|
||||
|
||||
expect(res).to.have.status(200);
|
||||
expect(res).to.be.json;
|
||||
|
||||
const {success, result} = res.body;
|
||||
|
||||
expect(success).to.be.true;
|
||||
expect(result._id).to.equal(_id);
|
||||
expect(result.title).not.equal(body.blocks[0].data.text);
|
||||
expect(result.title).to.equal(updatedBody.blocks[0].data.text);
|
||||
expect(result.uri).not.equal(transformToUri(body.blocks[0].data.text));
|
||||
expect(result.uri).to.equal(updatedUri);
|
||||
expect(result.body).not.equal(body);
|
||||
expect(result.body).to.deep.equal(updatedBody);
|
||||
|
||||
const updatedPage = await model.get(_id);
|
||||
const pageOrder = await PageOrder.get('' + updatedPage._parent);
|
||||
|
||||
expect(updatedPage._id).to.equal(_id);
|
||||
expect(updatedPage.title).not.equal(body.blocks[0].data.text);
|
||||
expect(updatedPage.title).to.equal(updatedBody.blocks[0].data.text);
|
||||
expect(updatedPage.uri).not.equal(transformToUri(body.blocks[0].data.text));
|
||||
expect(updatedPage.uri).to.equal(updatedUri);
|
||||
expect(updatedPage.body).not.equal(body);
|
||||
expect(updatedPage.body).to.deep.equal(updatedBody);
|
||||
|
||||
await pageOrder.destroy();
|
||||
await updatedPage.destroy();
|
||||
});
|
||||
|
||||
it('Handle multiple page creation with the same uri', async () => {
|
||||
const body = {
|
||||
blocks: [
|
||||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'Page header'
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
let res = await agent
|
||||
.put('/api/page')
|
||||
.send({body});
|
||||
|
||||
expect(res).to.have.status(200);
|
||||
expect(res).to.be.json;
|
||||
|
||||
const {result: {_id}} = res.body;
|
||||
|
||||
res = await agent
|
||||
.put('/api/page')
|
||||
.send({body: body});
|
||||
|
||||
expect(res).to.have.status(200);
|
||||
expect(res).to.be.json;
|
||||
|
||||
const {success: secondPageSuccess, result: secondPageResult} = res.body;
|
||||
|
||||
expect(secondPageSuccess).to.be.true;
|
||||
expect(secondPageResult.title).to.equal(body.blocks[0].data.text);
|
||||
expect(secondPageResult.uri).to.equal(transformToUri(body.blocks[0].data.text) + '-1');
|
||||
expect(secondPageResult.body).to.deep.equal(body);
|
||||
|
||||
const newFirstPageUri = 'New-uri';
|
||||
|
||||
res = await agent
|
||||
.post(`/api/page/${_id}`)
|
||||
.send({body: body, uri: newFirstPageUri});
|
||||
|
||||
expect(res).to.have.status(200);
|
||||
expect(res).to.be.json;
|
||||
|
||||
res = await agent
|
||||
.put('/api/page')
|
||||
.send({body: body});
|
||||
|
||||
expect(res).to.have.status(200);
|
||||
expect(res).to.be.json;
|
||||
|
||||
const {success: thirdPageSuccess, result: thirdPageResult} = res.body;
|
||||
|
||||
expect(thirdPageSuccess).to.be.true;
|
||||
expect(thirdPageResult.title).to.equal(body.blocks[0].data.text);
|
||||
expect(thirdPageResult.uri).to.equal(transformToUri(body.blocks[0].data.text));
|
||||
expect(thirdPageResult.body).to.deep.equal(body);
|
||||
});
|
||||
|
||||
it('Updating page with not existing id', async () => {
|
||||
const res = await agent
|
||||
.post('/api/page/not-existing-id')
|
||||
.send({body: {
|
||||
blocks: [
|
||||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'Page header'
|
||||
}
|
||||
}
|
||||
]
|
||||
}});
|
||||
|
||||
expect(res).to.have.status(400);
|
||||
expect(res).to.be.json;
|
||||
|
||||
const {success, error} = res.body;
|
||||
|
||||
expect(success).to.be.false;
|
||||
expect(error).to.equal('Page with given id does not exist');
|
||||
});
|
||||
|
||||
it('Removing page', async () => {
|
||||
const body = {
|
||||
blocks: [
|
||||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'Page header to be deleted'
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
let res = await agent
|
||||
.put('/api/page')
|
||||
.send({body});
|
||||
|
||||
expect(res).to.have.status(200);
|
||||
expect(res).to.be.json;
|
||||
|
||||
const {result: {_id}} = res.body;
|
||||
|
||||
res = await agent
|
||||
.delete(`/api/page/${_id}`);
|
||||
|
||||
expect(res).to.have.status(200);
|
||||
expect(res).to.be.json;
|
||||
|
||||
const {success, result} = res.body;
|
||||
|
||||
expect(success).to.be.true;
|
||||
|
||||
if (result) {
|
||||
expect(result._id).to.be.undefined;
|
||||
expect(result.title).to.equal(body.blocks[0].data.text);
|
||||
expect(result.uri).to.equal(transformToUri(body.blocks[0].data.text));
|
||||
expect(result.body).to.deep.equal(body);
|
||||
const deletedPage = await model.get(_id);
|
||||
|
||||
expect(deletedPage._id).to.be.undefined;
|
||||
} else {
|
||||
expect(result).to.be.null;
|
||||
}
|
||||
});
|
||||
|
||||
it('Removing page with not existing id', async () => {
|
||||
const res = await agent
|
||||
.delete('/api/page/not-existing-id');
|
||||
|
||||
expect(res).to.have.status(400);
|
||||
expect(res).to.be.json;
|
||||
|
||||
const {success, error} = res.body;
|
||||
|
||||
expect(success).to.be.false;
|
||||
expect(error).to.equal('Page with given id does not exist');
|
||||
});
|
||||
|
||||
async function createPageTree():Promise<string[]> {
|
||||
/**
|
||||
* Creating page tree
|
||||
*
|
||||
* 0
|
||||
* / \
|
||||
* 1 2
|
||||
* / \ \
|
||||
* 3 5 6
|
||||
* / / \
|
||||
* 4 7 8
|
||||
*/
|
||||
const body = {
|
||||
blocks: [
|
||||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'Page header'
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
let parent, res, result;
|
||||
|
||||
/** Page 1 */
|
||||
parent = 0;
|
||||
res = await agent
|
||||
.put('/api/page')
|
||||
.send({body, parent});
|
||||
|
||||
result = res.body.result;
|
||||
const page1 = result;
|
||||
|
||||
/** Page 2 */
|
||||
parent = 0;
|
||||
res = await agent
|
||||
.put('/api/page')
|
||||
.send({body, parent});
|
||||
|
||||
result = res.body.result;
|
||||
const page2 = result;
|
||||
|
||||
/** Page 3 */
|
||||
parent = page1._id;
|
||||
res = await agent
|
||||
.put('/api/page')
|
||||
.send({body, parent});
|
||||
|
||||
result = res.body.result;
|
||||
const page3 = result;
|
||||
|
||||
/** Page 4 */
|
||||
parent = page3._id;
|
||||
res = await agent
|
||||
.put('/api/page')
|
||||
.send({body, parent});
|
||||
|
||||
result = res.body.result;
|
||||
const page4 = result;
|
||||
|
||||
/** Page 5 */
|
||||
parent = page1._id;
|
||||
res = await agent
|
||||
.put('/api/page')
|
||||
.send({body, parent});
|
||||
|
||||
result = res.body.result;
|
||||
const page5 = result;
|
||||
|
||||
/** Page 6 */
|
||||
parent = page2._id;
|
||||
res = await agent
|
||||
.put('/api/page')
|
||||
.send({body, parent});
|
||||
|
||||
result = res.body.result;
|
||||
const page6 = result;
|
||||
|
||||
/** Page 7 */
|
||||
parent = page6._id;
|
||||
res = await agent
|
||||
.put('/api/page')
|
||||
.send({body, parent});
|
||||
|
||||
result = res.body.result;
|
||||
const page7 = result;
|
||||
|
||||
/** Page 8 */
|
||||
parent = page6._id;
|
||||
res = await agent
|
||||
.put('/api/page')
|
||||
.send({body, parent});
|
||||
|
||||
result = res.body.result;
|
||||
const page8 = result;
|
||||
|
||||
return [
|
||||
0,
|
||||
page1._id,
|
||||
page2._id,
|
||||
page3._id,
|
||||
page4._id,
|
||||
page5._id,
|
||||
page6._id,
|
||||
page7._id,
|
||||
page8._id
|
||||
];
|
||||
}
|
||||
|
||||
it('Removing a page and its children', async () => {
|
||||
const pages = await createPageTree();
|
||||
|
||||
/**
|
||||
* Deleting from tree page1
|
||||
* Also pages 3, 5 and 4 must be deleted
|
||||
*/
|
||||
await agent
|
||||
.delete(`/api/page/${pages[1]}`);
|
||||
|
||||
const page3 = await Page.get(pages[3]);
|
||||
expect(page3.data._id).to.be.undefined;
|
||||
|
||||
const page4 = await Page.get(pages[4]);
|
||||
expect(page4.data._id).to.be.undefined;
|
||||
|
||||
const page5 = await Page.get(pages[5]);
|
||||
expect(page5.data._id).to.be.undefined;
|
||||
|
||||
/** Okay, pages above is deleted */
|
||||
const page2 = await Page.get(pages[2]);
|
||||
expect(page2.data._id).not.to.be.undefined;
|
||||
|
||||
/** First check pages 6, 7 and 8 before deleting */
|
||||
let page6 = await Page.get(pages[6]);
|
||||
expect(page6.data._id).not.to.be.undefined;
|
||||
|
||||
let page7 = await Page.get(pages[7]);
|
||||
expect(page7.data._id).not.to.be.undefined;
|
||||
|
||||
let page8 = await Page.get(pages[8]);
|
||||
expect(page8.data._id).not.to.be.undefined;
|
||||
|
||||
/**
|
||||
* Delete page6
|
||||
* also pages 7 and 8 must be deleted
|
||||
*/
|
||||
await agent
|
||||
.delete(`/api/page/${pages[6]}`);
|
||||
|
||||
page6 = await Page.get(pages[6]);
|
||||
expect(page6.data._id).to.be.undefined;
|
||||
|
||||
page7 = await Page.get(pages[7]);
|
||||
expect(page7.data._id).to.be.undefined;
|
||||
|
||||
page8 = await Page.get(pages[8]);
|
||||
expect(page8.data._id).to.be.undefined;
|
||||
});
|
||||
});
|
3
src/test/rest/test_file.json
Normal file
3
src/test/rest/test_file.json
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"Hello": "world"
|
||||
}
|
BIN
src/test/rest/test_image.png
Normal file
BIN
src/test/rest/test_image.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 13 KiB |
273
src/test/rest/transport.ts
Normal file
273
src/test/rest/transport.ts
Normal file
|
@ -0,0 +1,273 @@
|
|||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import fileType from 'file-type';
|
||||
import chai from 'chai';
|
||||
import chaiHTTP from 'chai-http';
|
||||
import rimraf from 'rimraf';
|
||||
import config from 'config';
|
||||
import server from '../../bin/server';
|
||||
import model from '../../backend/models/file';
|
||||
|
||||
const {expect} = chai;
|
||||
const app = server.app;
|
||||
|
||||
chai.use(chaiHTTP);
|
||||
|
||||
describe('Transport routes: ', () => {
|
||||
let agent: ChaiHttp.Agent;
|
||||
|
||||
before(async () => {
|
||||
agent = chai.request.agent(app);
|
||||
|
||||
if (!fs.existsSync('./' + config.get('uploads'))) {
|
||||
fs.mkdirSync('./' + config.get('uploads'));
|
||||
}
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
const pathToDB = path.resolve(__dirname, '../../../', config.get('database'), './files.db');
|
||||
|
||||
if (fs.existsSync(pathToDB)) {
|
||||
fs.unlinkSync(pathToDB);
|
||||
}
|
||||
|
||||
if (fs.existsSync('./' + config.get('uploads'))) {
|
||||
rimraf.sync('./' + config.get('uploads'));
|
||||
}
|
||||
});
|
||||
|
||||
it('Uploading an image', async () => {
|
||||
const name = 'test_image.png';
|
||||
const image = fs.readFileSync(path.resolve(`./src/test/rest/${name}`));
|
||||
const res = await agent
|
||||
.post('/api/transport/image')
|
||||
.attach('image', image, name);
|
||||
|
||||
expect(res).to.have.status(200);
|
||||
expect(res).to.be.json;
|
||||
|
||||
const { body } = res;
|
||||
|
||||
const file = await model.get(body._id);
|
||||
|
||||
expect(body.success).to.equal(1);
|
||||
expect(file._id).to.equal(body._id);
|
||||
expect(file.name).to.equal(name);
|
||||
expect(file.filename).to.equal(body.filename);
|
||||
expect(file.path).to.equal(body.path);
|
||||
|
||||
const type = await fileType.fromBuffer(image);
|
||||
expect(type).to.be.not.undefined;
|
||||
if (type !== undefined) {
|
||||
expect(file.mimetype).to.equal(type.mime);
|
||||
expect(file.size).to.equal(image.byteLength);
|
||||
|
||||
expect(file.path).to.be.not.undefined;
|
||||
if (file.path !== undefined) {
|
||||
const getRes = await agent
|
||||
.get(file.path);
|
||||
|
||||
expect(getRes).to.have.status(200);
|
||||
expect(getRes).to.have.header('content-type', type.mime);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it('Uploading an image with map option', async () => {
|
||||
const name = 'test_image.png';
|
||||
const image = fs.readFileSync(path.resolve(`./src/test/rest/${name}`));
|
||||
const res = await agent
|
||||
.post('/api/transport/image')
|
||||
.attach('image', image, name)
|
||||
.field('map', JSON.stringify({_id: '_id', path: 'file:url', size: 'file:size', name: 'file:name'}));
|
||||
|
||||
expect(res).to.have.status(200);
|
||||
expect(res).to.be.json;
|
||||
|
||||
const { body } = res;
|
||||
|
||||
const file = await model.get(body._id);
|
||||
|
||||
expect(body.success).to.equal(1);
|
||||
expect(file.name).to.equal(body.file.name);
|
||||
expect(file.path).to.equal(body.file.url);
|
||||
expect(file.size).to.equal(body.file.size);
|
||||
});
|
||||
|
||||
it('Uploading a file', async () => {
|
||||
const name = 'test_file.json';
|
||||
const json = fs.readFileSync(path.resolve(`./src/test/rest/${name}`));
|
||||
const res = await agent
|
||||
.post('/api/transport/file')
|
||||
.attach('file', json, name);
|
||||
|
||||
expect(res).to.have.status(200);
|
||||
expect(res).to.be.json;
|
||||
|
||||
const { body } = res;
|
||||
|
||||
const file = await model.get(body._id);
|
||||
|
||||
expect(body.success).to.equal(1);
|
||||
expect(file._id).to.equal(body._id);
|
||||
expect(file.name).to.equal(name);
|
||||
expect(file.filename).to.equal(body.filename);
|
||||
expect(file.path).to.equal(body.path);
|
||||
expect(file.size).to.equal(json.byteLength);
|
||||
|
||||
expect(file.path).to.be.not.undefined;
|
||||
if (file.path !== undefined){
|
||||
const getRes = await agent
|
||||
.get(file.path);
|
||||
|
||||
expect(getRes).to.have.status(200);
|
||||
expect(getRes).to.have.header('content-type', new RegExp(`^${file.mimetype}`));
|
||||
}
|
||||
});
|
||||
|
||||
it('Uploading a file with map option', async () => {
|
||||
const name = 'test_file.json';
|
||||
const json = fs.readFileSync(path.resolve(`./src/test/rest/${name}`));
|
||||
const res = await agent
|
||||
.post('/api/transport/file')
|
||||
.attach('file', json, name)
|
||||
.field('map', JSON.stringify({_id: '_id', path: 'file:url', size: 'file:size', name: 'file:name'}));
|
||||
|
||||
expect(res).to.have.status(200);
|
||||
expect(res).to.be.json;
|
||||
|
||||
const { body } = res;
|
||||
|
||||
const file = await model.get(body._id);
|
||||
|
||||
expect(body.success).to.equal(1);
|
||||
expect(file.name).to.equal(body.file.name);
|
||||
expect(file.path).to.equal(body.file.url);
|
||||
expect(file.size).to.equal(body.file.size);
|
||||
});
|
||||
|
||||
it('Send file URL to fetch', async () => {
|
||||
const url = 'https://codex.so/public/app/img/codex-logo.svg';
|
||||
const res = await agent
|
||||
.post('/api/transport/fetch')
|
||||
.field('url', url);
|
||||
|
||||
expect(res).to.have.status(200);
|
||||
expect(res).to.be.json;
|
||||
|
||||
const { body } = res;
|
||||
|
||||
const file = await model.get(body._id);
|
||||
|
||||
expect(body.success).to.equal(1);
|
||||
expect(file._id).to.equal(body._id);
|
||||
expect(file.name).to.equal(body.name);
|
||||
expect(file.filename).to.equal(body.filename);
|
||||
expect(file.path).to.equal(body.path);
|
||||
expect(file.size).to.equal(body.size);
|
||||
|
||||
expect(file.path).to.be.not.undefined;
|
||||
if (file.path !== undefined){
|
||||
const getRes = await agent
|
||||
.get(file.path);
|
||||
|
||||
expect(getRes).to.have.status(200);
|
||||
expect(getRes).to.have.header('content-type', file.mimetype);
|
||||
}
|
||||
});
|
||||
|
||||
it('Send an file URL to fetch with map option', async () => {
|
||||
const url = 'https://codex.so/public/app/img/codex-logo.svg';
|
||||
const res = await agent
|
||||
.post('/api/transport/fetch')
|
||||
.field('url', url)
|
||||
.field('map', JSON.stringify({_id: '_id', path: 'file:url', size: 'file:size', name: 'file:name'}));
|
||||
|
||||
expect(res).to.have.status(200);
|
||||
expect(res).to.be.json;
|
||||
|
||||
const { body } = res;
|
||||
|
||||
const file = await model.get(body._id);
|
||||
|
||||
expect(body.success).to.equal(1);
|
||||
expect(file.name).to.equal(body.file.name);
|
||||
expect(file.path).to.equal(body.file.url);
|
||||
expect(file.size).to.equal(body.file.size);
|
||||
});
|
||||
|
||||
it('Negative tests for file uploading', async () => {
|
||||
let res = await agent
|
||||
.post('/api/transport/file')
|
||||
.send();
|
||||
|
||||
let {body} = res;
|
||||
|
||||
expect(res).to.have.status(400);
|
||||
expect(body.success).to.equal(0);
|
||||
|
||||
const name = 'test_file.json';
|
||||
const json = fs.readFileSync(path.resolve(`./src/test/rest/${name}`));
|
||||
res = await agent
|
||||
.post('/api/transport/file')
|
||||
.attach('file', json, name)
|
||||
.field('map', '{unvalid_json)');
|
||||
|
||||
body = res.body;
|
||||
|
||||
expect(res).to.have.status(500);
|
||||
expect(body.success).to.equal(0);
|
||||
});
|
||||
|
||||
it('Negative tests for image uploading', async () => {
|
||||
let res = await agent
|
||||
.post('/api/transport/image')
|
||||
.send();
|
||||
|
||||
let {body} = res;
|
||||
|
||||
expect(res).to.have.status(400);
|
||||
expect(body.success).to.equal(0);
|
||||
|
||||
let name = 'test_file.json';
|
||||
const json = fs.readFileSync(path.resolve(`./src/test/rest/${name}`));
|
||||
res = await agent
|
||||
.post('/api/transport/image')
|
||||
.attach('image', json, name);
|
||||
|
||||
expect(res).to.have.status(400);
|
||||
|
||||
name = 'test_image.png';
|
||||
const image = fs.readFileSync(path.resolve(`./src/test/rest/${name}`));
|
||||
res = await agent
|
||||
.post('/api/transport/image')
|
||||
.attach('image', image, name)
|
||||
.field('map', '{unvalid_json)');
|
||||
|
||||
body = res.body;
|
||||
|
||||
expect(res).to.have.status(500);
|
||||
expect(body.success).to.equal(0);
|
||||
});
|
||||
|
||||
it('Negative tests for file fetching', async () => {
|
||||
let res = await agent
|
||||
.post('/api/transport/fetch')
|
||||
.send();
|
||||
|
||||
let {body} = res;
|
||||
|
||||
expect(res).to.have.status(400);
|
||||
expect(body.success).to.equal(0);
|
||||
|
||||
const url = 'https://invalidurl';
|
||||
res = await agent
|
||||
.post('/api/transport/fetch')
|
||||
.field('url', url);
|
||||
|
||||
body = res.body;
|
||||
|
||||
expect(res).to.have.status(500);
|
||||
expect(body.success).to.equal(0);
|
||||
}).timeout(50000);
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue