mirror of
https://github.com/codex-team/codex.docs.git
synced 2025-08-09 15:35:25 +02:00
Add mocha tests
This commit is contained in:
parent
3a1bb7b2a4
commit
23f06eae76
12 changed files with 2256 additions and 67 deletions
2
app.js
2
app.js
|
@ -15,7 +15,7 @@ app.set('view engine', 'twig');
|
|||
|
||||
app.use(logger('dev'));
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded({extended: true}))
|
||||
app.use(express.urlencoded({extended: true}));
|
||||
app.use(cookieParser());
|
||||
app.use(express.static(path.join(__dirname, 'public')));
|
||||
|
||||
|
|
20
bin/www
20
bin/www
|
@ -3,21 +3,21 @@
|
|||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
const app = require('../app')
|
||||
const debug = require('debug')('codex.editor.docs:server')
|
||||
const http = require('http')
|
||||
const app = require('../app');
|
||||
const debug = require('debug')('codex.editor.docs:server');
|
||||
const http = require('http');
|
||||
|
||||
/**
|
||||
* Get port from environment and store in Express.
|
||||
*/
|
||||
const port = normalizePort(process.env.PORT || '3000')
|
||||
const port = normalizePort(process.env.PORT || '3000');
|
||||
|
||||
app.set('port', port);
|
||||
|
||||
/**
|
||||
* Create HTTP server.
|
||||
*/
|
||||
const server = http.createServer(app)
|
||||
const server = http.createServer(app);
|
||||
|
||||
/**
|
||||
* Listen on provided port, on all network interfaces.
|
||||
|
@ -32,7 +32,7 @@ server.on('listening', onListening);
|
|||
*/
|
||||
|
||||
function normalizePort(val) {
|
||||
const port = parseInt(val, 10)
|
||||
const port = parseInt(val, 10);
|
||||
|
||||
if (isNaN(port)) {
|
||||
// named pipe
|
||||
|
@ -58,7 +58,7 @@ function onError(error) {
|
|||
|
||||
const bind = typeof port === 'string'
|
||||
? 'Pipe ' + port
|
||||
: 'Port ' + port
|
||||
: 'Port ' + port;
|
||||
|
||||
// handle specific listen errors with friendly messages
|
||||
switch (error.code) {
|
||||
|
@ -80,9 +80,11 @@ function onError(error) {
|
|||
*/
|
||||
|
||||
function onListening() {
|
||||
const addr = server.address()
|
||||
const addr = server.address();
|
||||
const bind = typeof addr === 'string'
|
||||
? 'pipe ' + addr
|
||||
: 'port ' + addr.port
|
||||
: 'port ' + addr.port;
|
||||
debug('Listening on ' + bind);
|
||||
}
|
||||
|
||||
module.exports = {server, app};
|
|
@ -5,29 +5,55 @@ class Pages {
|
|||
return ['title', 'body'];
|
||||
}
|
||||
|
||||
static async insert(data) {
|
||||
static async get (id) {
|
||||
const page = await model.get(id);
|
||||
|
||||
if (!page._id) {
|
||||
throw new Error('Page with given id does not exist');
|
||||
}
|
||||
|
||||
return page;
|
||||
}
|
||||
|
||||
static async getAll() {
|
||||
return await model.getAll();
|
||||
}
|
||||
|
||||
static async insert (data) {
|
||||
if (!Pages.validate(data)) {
|
||||
throw new Error('Invalid request format')
|
||||
}
|
||||
|
||||
const page = new model(null, data);
|
||||
const page = new model(data);
|
||||
|
||||
return page.save();
|
||||
}
|
||||
|
||||
static validate (data) {
|
||||
return Pages.REQUIRED_FIELDS.every(field => data.hasOwnProperty(field));
|
||||
return Pages.REQUIRED_FIELDS.every(field => typeof data[field] !== 'undefined');
|
||||
}
|
||||
|
||||
static async update(id, data) {
|
||||
const page = new model(id, data);
|
||||
static async update (id, data) {
|
||||
const page = await model.get(id);
|
||||
|
||||
if (!page._id) {
|
||||
throw new Error('Page with given id does not exist');
|
||||
}
|
||||
|
||||
page.data = data;
|
||||
|
||||
return page.save()
|
||||
}
|
||||
|
||||
static async remove (id) {
|
||||
const page = await model.get(id);
|
||||
|
||||
if (!page._id) {
|
||||
throw new Error('Page with given id does not exist');
|
||||
}
|
||||
|
||||
return page.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Pages;
|
|
@ -13,7 +13,7 @@ class Database {
|
|||
}
|
||||
|
||||
res(newDoc);
|
||||
}))
|
||||
}));
|
||||
}
|
||||
|
||||
async find (query, projection) {
|
||||
|
@ -23,7 +23,7 @@ class Database {
|
|||
}
|
||||
|
||||
res(docs);
|
||||
}
|
||||
};
|
||||
|
||||
return new Promise((res, rej) => {
|
||||
if (projection) {
|
||||
|
@ -31,7 +31,7 @@ class Database {
|
|||
} else {
|
||||
this.db.find(query, cbk(res, rej));
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
async findOne (query, projection) {
|
||||
|
@ -41,7 +41,7 @@ class Database {
|
|||
}
|
||||
|
||||
res(doc);
|
||||
}
|
||||
};
|
||||
|
||||
return new Promise((res, rej) => {
|
||||
if (projection) {
|
||||
|
@ -49,7 +49,7 @@ class Database {
|
|||
} else {
|
||||
this.db.findOne(query, cbk(res, rej));
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
async update (query, update, options = {}) {
|
||||
|
@ -59,7 +59,7 @@ class Database {
|
|||
}
|
||||
|
||||
res(result);
|
||||
}))
|
||||
}));
|
||||
}
|
||||
|
||||
async remove (query, options = {}) {
|
||||
|
@ -69,11 +69,12 @@ class Database {
|
|||
}
|
||||
|
||||
res(result);
|
||||
}))
|
||||
}));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
class: Database,
|
||||
pages: new Database(pages)
|
||||
};
|
|
@ -1,28 +1,31 @@
|
|||
const uuid4 = require('uuid4');
|
||||
const {pages} = require('../database');
|
||||
|
||||
class Page {
|
||||
|
||||
constructor (id, data = {}) {
|
||||
this.db = pages;
|
||||
static async get(_id) {
|
||||
const data = await pages.findOne({_id});
|
||||
|
||||
if (id) {
|
||||
this.db.findOne({_id: id}).then(page => {
|
||||
console.log(page)
|
||||
return new Page(data);
|
||||
}
|
||||
|
||||
if (!page) {
|
||||
this.data = data;
|
||||
return;
|
||||
}
|
||||
static async getAll(query = {}) {
|
||||
const docs = await pages.find(query);
|
||||
|
||||
return Promise.all(docs.map(doc => new Page(doc)));
|
||||
}
|
||||
|
||||
constructor (data = {}) {
|
||||
if (data === null) {
|
||||
data = {};
|
||||
}
|
||||
|
||||
this.db = pages;
|
||||
|
||||
if (data._id) {
|
||||
this._id = data._id;
|
||||
}
|
||||
|
||||
this._id = page._id;
|
||||
this.data = page;
|
||||
}).catch(ignored => {
|
||||
this.data = data;
|
||||
})
|
||||
} else {
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
|
||||
set data (pageData) {
|
||||
|
@ -43,25 +46,42 @@ class Page {
|
|||
}
|
||||
|
||||
set parent (parentPage) {
|
||||
this._parent = parentPage.id;
|
||||
this._parent = parentPage._id;
|
||||
}
|
||||
|
||||
get parent () {
|
||||
return this.db.find({_id: this._parent});
|
||||
return this.db.findOne({_id: this._parent})
|
||||
.then(data => new Page(data));
|
||||
}
|
||||
|
||||
get children () {
|
||||
return this.db.find({parent: this._id})
|
||||
.then(data => data.map(page => new Page(page)));
|
||||
}
|
||||
|
||||
async save () {
|
||||
if (!this._id) {
|
||||
const insertedRow = await this.db.insert(this.data);
|
||||
if (!this._id) {
|
||||
const insertedRow = await this.db.insert(this.data);
|
||||
|
||||
this._id = insertedRow._id;
|
||||
} else {
|
||||
await this.db.update({_id: this._id}, this.data);
|
||||
}
|
||||
this._id = insertedRow._id;
|
||||
} else {
|
||||
await this.db.update({_id: this._id}, this.data);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
async destroy () {
|
||||
await this.db.remove({_id: this._id});
|
||||
|
||||
delete this._id;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
toJSON () {
|
||||
return this.data;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Page;
|
|
@ -3,7 +3,8 @@
|
|||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"start": "node ./bin/www"
|
||||
"start": "nodemon ./bin/www",
|
||||
"test": "mocha --recursive ./test"
|
||||
},
|
||||
"dependencies": {
|
||||
"body-parser": "latest",
|
||||
|
@ -14,7 +15,13 @@
|
|||
"morgan": "~1.9.0",
|
||||
"multer": "^1.3.1",
|
||||
"nedb": "^1.8.0",
|
||||
"nodemon": "^1.18.3",
|
||||
"twig": "~0.10.3",
|
||||
"uuid4": "^1.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"chai": "^4.1.2",
|
||||
"chai-http": "^4.0.0",
|
||||
"mocha": "^5.2.0"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,14 +3,44 @@ const router = express.Router();
|
|||
const multer = require('multer')();
|
||||
const Pages = require('../controllers/pages');
|
||||
|
||||
/* GET users listing. */
|
||||
router.put('/page', multer.any(), async (req, res,) => {
|
||||
router.get('/page/:id', async (req, res) => {
|
||||
try {
|
||||
const page = await Pages.get(req.params.id);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
result: page.data
|
||||
})
|
||||
} catch (err) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: err.message
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/pages', async (req, res) => {
|
||||
try {
|
||||
const pages = await Pages.getAll();
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
result: pages
|
||||
});
|
||||
} catch (err) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: err.message
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
router.put('/page', multer.any(), async (req, res,) => {
|
||||
try {
|
||||
const page = await Pages.insert(req.body);
|
||||
res.json({
|
||||
success: true,
|
||||
result: page.data
|
||||
result: page
|
||||
});
|
||||
} catch (err) {
|
||||
res.status(400).json({
|
||||
|
@ -24,14 +54,12 @@ router.put('/page', multer.any(), async (req, res,) => {
|
|||
router.post('/page/:id', multer.any(), async (req, res) => {
|
||||
const {id} = req.params;
|
||||
|
||||
console.log(id);
|
||||
|
||||
try {
|
||||
const page = await Pages.update(id, req.body);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
result: page.data
|
||||
result: page
|
||||
});
|
||||
} catch (err) {
|
||||
res.status(400).json({
|
||||
|
@ -39,8 +67,22 @@ router.post('/page/:id', multer.any(), async (req, res) => {
|
|||
error: err.message
|
||||
});
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
router.put
|
||||
router.delete('/page/:id', async (req, res) => {
|
||||
try {
|
||||
const page = await Pages.remove(req.params.id);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
result: page
|
||||
})
|
||||
} catch (err) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: err.message
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
|
151
test/database.js
Normal file
151
test/database.js
Normal file
|
@ -0,0 +1,151 @@
|
|||
const fs = require('fs');
|
||||
const {expect} = require('chai');
|
||||
|
||||
const {class: Database} = require('../database');
|
||||
const Datastore = require('nedb');
|
||||
|
||||
describe('Database', () => {
|
||||
const pathToDB = './db/test.db';
|
||||
let nedbInstance;
|
||||
let db;
|
||||
|
||||
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});
|
||||
|
||||
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});
|
||||
|
||||
expect(insertedDoc).to.be.a('object');
|
||||
expect(insertedDoc.data).to.equal(data);
|
||||
|
||||
const foundDoc = await db.findOne({_id: insertedDoc._id});
|
||||
|
||||
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});
|
||||
|
||||
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});
|
||||
|
||||
expect(updatedDoc).not.be.null;
|
||||
expect(updatedDoc.data).not.equal(data);
|
||||
expect(updatedDoc.data).to.equal(updatedData);
|
||||
});
|
||||
|
||||
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});
|
||||
const insertedDoc2 = await db.insert({data: data2, flag: true, no: 2});
|
||||
|
||||
const foundDocs = await db.find({flag: true});
|
||||
|
||||
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});
|
||||
|
||||
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});
|
||||
|
||||
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.message).to.equal('Cannot read property \'_id\' of undefined')
|
||||
}
|
||||
|
||||
try {
|
||||
await db.find({size: {$invalidComparator: 1}});
|
||||
} catch (err) {
|
||||
expect(err.message).to.equal('Unknown comparison function $invalidComparator');
|
||||
}
|
||||
|
||||
try {
|
||||
await db.findOne({field: {$invalidComparator: 1}});
|
||||
} catch (err) {
|
||||
expect(err.message).to.equal('Unknown comparison function $invalidComparator');
|
||||
}
|
||||
|
||||
try {
|
||||
await db.update({field: {$undefinedComparator: 1}})
|
||||
} catch (err) {
|
||||
expect(err.message).to.equal('Unknown comparison function $undefinedComparator');
|
||||
}
|
||||
|
||||
try {
|
||||
await db.remove({field: {$undefinedComparator: 1}})
|
||||
} catch (err) {
|
||||
expect(err.message).to.equal('Unknown comparison function $undefinedComparator');
|
||||
}
|
||||
});
|
||||
|
||||
after(() => {
|
||||
if (fs.existsSync(pathToDB)) {
|
||||
fs.unlinkSync(pathToDB);
|
||||
}
|
||||
})
|
||||
});
|
21
test/express.js
Normal file
21
test/express.js
Normal file
|
@ -0,0 +1,21 @@
|
|||
const {app} = require('../bin/www');
|
||||
const chai = require('chai');
|
||||
const chaiHTTP = require('chai-http');
|
||||
const {expect} = chai;
|
||||
|
||||
chai.use(chaiHTTP);
|
||||
|
||||
|
||||
describe(`Express app`, () => {
|
||||
|
||||
it('App is available', (done) => {
|
||||
chai
|
||||
.request(app)
|
||||
.get('/')
|
||||
.end((err, res) => {
|
||||
expect(res).to.have.status(200);
|
||||
done();
|
||||
})
|
||||
});
|
||||
|
||||
});
|
166
test/models/page.js
Normal file
166
test/models/page.js
Normal file
|
@ -0,0 +1,166 @@
|
|||
const {expect} = require('chai');
|
||||
const Page = require('../../models/page');
|
||||
const {pages} = require('../../database');
|
||||
|
||||
describe('Page model', () => {
|
||||
|
||||
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.undefined;
|
||||
expect(data.body).to.be.undefined;
|
||||
expect(data.parent).to.be.undefined;
|
||||
|
||||
page = new Page(null);
|
||||
|
||||
data = page.data;
|
||||
|
||||
expect(data._id).to.be.undefined;
|
||||
expect(data.title).to.be.undefined;
|
||||
expect(data.body).to.be.undefined;
|
||||
expect(data.parent).to.be.undefined;
|
||||
|
||||
const initialData = {_id: 'page_id', title: 'Test page', body: 'Test page body'};
|
||||
|
||||
page = new Page(initialData);
|
||||
|
||||
const json = page.toJSON();
|
||||
data = page.data;
|
||||
|
||||
expect(data._id).to.equal(initialData._id);
|
||||
expect(data.title).to.equal(initialData.title);
|
||||
expect(data.body).to.equal(initialData.body);
|
||||
expect(data.parent).to.be.undefined;
|
||||
|
||||
expect(json._id).to.equal(initialData._id);
|
||||
expect(json.title).to.equal(initialData.title);
|
||||
expect(json.body).to.equal(initialData.body);
|
||||
expect(json.parent).to.be.undefined;
|
||||
|
||||
const update = {
|
||||
_id: 12345,
|
||||
title: 'Test page',
|
||||
body: 'Test page body'
|
||||
};
|
||||
|
||||
page.data = update;
|
||||
|
||||
data = page.data;
|
||||
|
||||
expect(data._id).to.equal(initialData._id);
|
||||
expect(data.title).to.equal(update.title);
|
||||
expect(data.body).to.equal(update.body);
|
||||
expect(data.parent).to.be.undefined;
|
||||
});
|
||||
|
||||
|
||||
it('Saving, updating and deleting model in the database', async () => {
|
||||
const initialData = {title: 'Test page', body: 'Test page body'};
|
||||
const page = new Page(initialData);
|
||||
|
||||
let savedPage = await page.save();
|
||||
|
||||
expect(savedPage._id).not.be.undefined;
|
||||
expect(savedPage.title).to.equal(initialData.title);
|
||||
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.body).to.equal(page.body);
|
||||
|
||||
const updateData = {title: 'Updated test page', body: 'Updated test page body'};
|
||||
|
||||
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.title);
|
||||
expect(updatedPage.body).to.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('Static get method', async () => {
|
||||
const initialData = {title: 'Test page', body: 'Test page body'};
|
||||
const page = new Page(initialData);
|
||||
|
||||
const savedPage = await page.save();
|
||||
|
||||
const foundPage = await Page.get(savedPage._id);
|
||||
|
||||
const {data} = foundPage;
|
||||
|
||||
expect(data._id).to.equal(savedPage._id);
|
||||
expect(data.title).to.equal(initialData.title);
|
||||
expect(data.body).to.equal(initialData.body);
|
||||
|
||||
await page.destroy();
|
||||
});
|
||||
|
||||
it('Static getAll method', async () => {
|
||||
const pages = [
|
||||
new Page({title: 'Page 1', body: 'Page 1 body'}),
|
||||
new Page({title: 'Page 2', body: 'Page 2 body'})
|
||||
];
|
||||
|
||||
const savedPages = await Promise.all(pages.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(pages[i].title);
|
||||
expect(page.body).to.equal(pages[i].body);
|
||||
});
|
||||
});
|
||||
|
||||
it('Parent pages', async () => {
|
||||
const parent = new Page({title: 'Parent page', body: 'Parent page body'});
|
||||
const {_id: parentId} = await parent.save();
|
||||
|
||||
const child = new Page({title: 'Child page', body: 'Child page body'});
|
||||
|
||||
child.parent = parent;
|
||||
|
||||
const {_id: childId} = await child.save();
|
||||
|
||||
const testedParent = await child.parent;
|
||||
|
||||
expect(testedParent._id).to.equal(parentId);
|
||||
expect(testedParent.title).to.equal(parent.title);
|
||||
expect(testedParent.body).to.equal(parent.body);
|
||||
|
||||
const children = await parent.children;
|
||||
|
||||
expect(children.length).to.equal(1);
|
||||
|
||||
const testedChild = children.pop();
|
||||
|
||||
expect(testedChild._id).to.equal(childId);
|
||||
expect(testedChild.title).to.equal(child.title);
|
||||
expect(testedChild.body).to.equal(child.body);
|
||||
expect(testedChild._parent).to.equal(child._parent);
|
||||
expect(testedChild._parent).to.equal(parent._id);
|
||||
|
||||
parent.destroy();
|
||||
child.destroy();
|
||||
});
|
||||
});
|
199
test/rest/pages.js
Normal file
199
test/rest/pages.js
Normal file
|
@ -0,0 +1,199 @@
|
|||
const {app} = require('../../bin/www');
|
||||
const model = require('../../models/page');
|
||||
|
||||
const chai = require('chai');
|
||||
const chaiHTTP = require('chai-http');
|
||||
const {expect} = chai;
|
||||
|
||||
chai.use(chaiHTTP);
|
||||
|
||||
describe('Pages REST: ', () => {
|
||||
let agent;
|
||||
|
||||
before(async () => {
|
||||
agent = chai.request.agent(app);
|
||||
});
|
||||
|
||||
it('Creating page', async () => {
|
||||
const title = 'Test page';
|
||||
const body = 'Test page body';
|
||||
|
||||
const res = await agent
|
||||
.put('/page')
|
||||
.send({title, body});
|
||||
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(title);
|
||||
expect(result.body).to.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(title);
|
||||
expect(createdPage.body).to.equal(body);
|
||||
|
||||
createdPage.destroy();
|
||||
});
|
||||
|
||||
it('Page data validation on create', async () => {
|
||||
const res = await agent
|
||||
.put('/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('Invalid request format');
|
||||
});
|
||||
|
||||
it('Finding page', async () => {
|
||||
const title = 'Test page';
|
||||
const body = 'Test page body';
|
||||
|
||||
const put = await agent
|
||||
.put('/page')
|
||||
.send({title, body});
|
||||
expect(put).to.have.status(200);
|
||||
expect(put).to.be.json;
|
||||
|
||||
const {result: {_id}} = put.body;
|
||||
|
||||
const get = await agent.get(`/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);
|
||||
|
||||
expect(foundPage._id).to.equal(_id);
|
||||
expect(foundPage.title).to.equal(title);
|
||||
expect(foundPage.body).to.equal(body);
|
||||
|
||||
foundPage.destroy();
|
||||
});
|
||||
|
||||
it('Finding page with not existing id', async () => {
|
||||
const res = await agent.get('/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 title = 'Test page';
|
||||
const body = 'Test page body';
|
||||
|
||||
let res = await agent
|
||||
.put('/page')
|
||||
.send({title, body});
|
||||
expect(res).to.have.status(200);
|
||||
expect(res).to.be.json;
|
||||
|
||||
const {result: {_id}} = res.body;
|
||||
|
||||
const updatedTitle = 'Updated test page';
|
||||
const updatedBody = 'Updated test page body';
|
||||
|
||||
res = await agent
|
||||
.post(`/page/${_id}`)
|
||||
.send({title: updatedTitle, body: updatedBody});
|
||||
|
||||
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(title);
|
||||
expect(result.title).to.equal(updatedTitle);
|
||||
expect(result.body).not.equal(body);
|
||||
expect(result.body).to.equal(updatedBody);
|
||||
|
||||
const updatedPage = await model.get(_id);
|
||||
|
||||
expect(updatedPage._id).to.equal(_id);
|
||||
expect(updatedPage.title).not.equal(title);
|
||||
expect(updatedPage.title).to.equal(updatedTitle);
|
||||
expect(updatedPage.body).not.equal(body);
|
||||
expect(updatedPage.body).to.equal(updatedBody);
|
||||
|
||||
updatedPage.destroy();
|
||||
});
|
||||
|
||||
it('Updating page with not existing id', async () => {
|
||||
const res = await agent
|
||||
. post('/page/not-existing-id')
|
||||
.send({title: 'Updated title', body: 'Updated body'});
|
||||
|
||||
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 title = 'Test page';
|
||||
const body = 'Test page body';
|
||||
|
||||
let res = await agent
|
||||
.put('/page')
|
||||
.send({title, body});
|
||||
expect(res).to.have.status(200);
|
||||
expect(res).to.be.json;
|
||||
|
||||
const {result: {_id}} = res.body;
|
||||
|
||||
res = await agent
|
||||
.delete(`/page/${_id}`);
|
||||
|
||||
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.undefined;
|
||||
expect(result.title).to.equal(title);
|
||||
expect(result.body).to.equal(body);
|
||||
|
||||
const deletedPage = await model.get(_id);
|
||||
|
||||
expect(deletedPage._id).to.be.undefined;
|
||||
});
|
||||
|
||||
it('Removing page with not existing id', async () => {
|
||||
const res = await agent
|
||||
.delete('/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');
|
||||
})
|
||||
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue