mirror of
https://github.com/codex-team/codex.docs.git
synced 2025-07-22 22:59:42 +02:00
Beautiful urls (#18)
* Added uri property to Page model * Added aliases collection * Added routing form aliases * Fixed redirect after page creation * Added abiltity to support few pages with same title * Added ability to change uri manually * Changed hash function * Changed uri parsing * Removed pages controller promise * Modified page's tests * Added tests for alias model * Added tests for aliases * Escaping special characters * Added missed files * Fixed bugs related to translation * Fixed parent page link * Added server validation for uri * Changed css properties order * Made uri property of page be optional * Prevented alias creation from empty uri * Moved alias types to model
This commit is contained in:
parent
ed69336481
commit
d872e78339
28 changed files with 807 additions and 45 deletions
|
@ -1,6 +1,7 @@
|
|||
const {app} = require('../../bin/www');
|
||||
const model = require('../../src/models/page');
|
||||
const PageOrder = require('../../src/models/pageOrder');
|
||||
const translateString = require('../../src/utils/translation');
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
@ -13,6 +14,16 @@ chai.use(chaiHTTP);
|
|||
|
||||
describe('Pages REST: ', () => {
|
||||
let agent;
|
||||
const transformToUri = (string) => {
|
||||
return translateString(string
|
||||
.replace(/ /g, ' ')
|
||||
.replace(/[^a-zA-Z0-9А-Яа-яЁё ]/g, ' ')
|
||||
.replace(/ +/g, ' ')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.split(' ')
|
||||
.join('-'));
|
||||
};
|
||||
|
||||
before(async () => {
|
||||
agent = chai.request.agent(app);
|
||||
|
@ -35,9 +46,9 @@ describe('Pages REST: ', () => {
|
|||
text: 'Page header'
|
||||
}
|
||||
}
|
||||
],
|
||||
]
|
||||
};
|
||||
const parent = 0;
|
||||
const parent = 0;
|
||||
const res = await agent
|
||||
.put('/api/page')
|
||||
.send({body, parent});
|
||||
|
@ -50,6 +61,7 @@ describe('Pages REST: ', () => {
|
|||
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);
|
||||
|
@ -57,13 +69,14 @@ describe('Pages REST: ', () => {
|
|||
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()
|
||||
await pageOrder.destroy();
|
||||
});
|
||||
|
||||
it('Page data validation on create', async () => {
|
||||
|
@ -115,6 +128,7 @@ describe('Pages REST: ', () => {
|
|||
|
||||
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();
|
||||
|
@ -164,10 +178,11 @@ describe('Pages REST: ', () => {
|
|||
}
|
||||
]
|
||||
};
|
||||
const updatedUri = 'updated-uri';
|
||||
|
||||
res = await agent
|
||||
.post(`/api/page/${_id}`)
|
||||
.send({body: updatedBody});
|
||||
.send({body: updatedBody, uri: updatedUri});
|
||||
|
||||
expect(res).to.have.status(200);
|
||||
expect(res).to.be.json;
|
||||
|
@ -178,6 +193,8 @@ describe('Pages REST: ', () => {
|
|||
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);
|
||||
|
||||
|
@ -187,6 +204,8 @@ describe('Pages REST: ', () => {
|
|||
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);
|
||||
|
||||
|
@ -194,6 +213,65 @@ describe('Pages REST: ', () => {
|
|||
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')
|
||||
|
@ -223,7 +301,7 @@ describe('Pages REST: ', () => {
|
|||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'Page header'
|
||||
text: 'Page header to be deleted'
|
||||
}
|
||||
}
|
||||
]
|
||||
|
@ -249,6 +327,7 @@ describe('Pages REST: ', () => {
|
|||
expect(success).to.be.true;
|
||||
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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue