Article rendering tests
This commit is contained in:
+24
-14
@@ -29,12 +29,13 @@ module.exports = (config) => {
|
|||||||
* @param callback
|
* @param callback
|
||||||
*/
|
*/
|
||||||
const reload = (callback) => {
|
const reload = (callback) => {
|
||||||
fw.fetchArticles((err, list) => {
|
fw.fetchArticles((err, dict) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(false);
|
callback(false);
|
||||||
return console.error(cons.error, 'loading articles : ' + err);
|
return console.error(cons.error, 'loading articles : ' + err);
|
||||||
}
|
}
|
||||||
articles.splice(0, articles.length, ...list);
|
Object.keys(articles).forEach((key) => delete articles[key]);
|
||||||
|
Object.keys(dict).forEach((key) => articles[key] = dict[key]);
|
||||||
const nb = Object.keys(articles).length;
|
const nb = Object.keys(articles).length;
|
||||||
if (nb > 0)
|
if (nb > 0)
|
||||||
console.log(cons.ok, `loaded ${nb} article${nb > 1 ? 's' : ''}`);
|
console.log(cons.ok, `loaded ${nb} article${nb > 1 ? 's' : ''}`);
|
||||||
@@ -49,15 +50,15 @@ module.exports = (config) => {
|
|||||||
/**
|
/**
|
||||||
* Render the page with the view engine and catch errors
|
* Render the page with the view engine and catch errors
|
||||||
* @param res
|
* @param res
|
||||||
* @param path - path of the view
|
* @param vPath - path of the view
|
||||||
* @param data - data to pass to the view
|
* @param data - data to pass to the view
|
||||||
* @param code - code to send along the page
|
* @param code - code to send along the page
|
||||||
*/
|
*/
|
||||||
const render = (res, path, data, code = 200) => {
|
const render = (res, vPath, data, code = 200) => {
|
||||||
res.render(path, data, (err, html) => {
|
res.render(vPath, data, (err, html) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
res.sendStatus(500);
|
res.sendStatus(500);
|
||||||
console.log(cons.error, `failed to render ${path} : ${err}`);
|
console.log(cons.error, `failed to render ${vPath} : ${err}`);
|
||||||
} else
|
} else
|
||||||
res.status(code).send(html);
|
res.status(code).send(html);
|
||||||
});
|
});
|
||||||
@@ -92,21 +93,24 @@ module.exports = (config) => {
|
|||||||
|
|
||||||
// catch all article urls and render them
|
// catch all article urls and render them
|
||||||
app.get('*', (req, res, next) => {
|
app.get('*', (req, res, next) => {
|
||||||
if (req.path.test(/^\/\d{4}\/\d{2}\/\d{2}\/(\w*\/)?$/)) {
|
if (/^\/\d{4}\/\d{2}\/\d{2}\/(\w*\/)?$/.test(req.path)) {
|
||||||
const articlePath = req.path.substr(0, 11);
|
const articlePath = req.path.substr(1, 10);
|
||||||
const article = articles[articlePath];
|
const article = articles[articlePath];
|
||||||
if (!article)
|
if (!article)
|
||||||
showError(req.path, 404, res);
|
showError(req.path, 404, res);
|
||||||
else {
|
else {
|
||||||
renderer.render(article.realPath, (err, html) => {
|
renderer.render(path.join(article.realPath, config['article']['index']), (err, html) => {
|
||||||
if (err)
|
if (err) {
|
||||||
|
console.log(cons.error, `failed to render article ${req.path} : ${err}`);
|
||||||
return showError(req.path, 500, res);
|
return showError(req.path, 500, res);
|
||||||
|
}
|
||||||
article.content = html;
|
article.content = html;
|
||||||
const templatePath = path.join(config['data_dir'], config['home']['index']);
|
const templatePath = path.join(config['data_dir'], config['article']['template']);
|
||||||
fs.access(templatePath, fs.constants.R_OK, (err) => {
|
fs.access(templatePath, fs.constants.R_OK, (err) => {
|
||||||
if (err)
|
if (err) {
|
||||||
showError(req.path, 404, res);
|
console.log(cons.error, `no template found at ${templatePath}`);
|
||||||
else
|
showError(req.path, 500, res);
|
||||||
|
} else
|
||||||
render(res, templatePath, {article: article});
|
render(res, templatePath, {article: article});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -136,6 +140,12 @@ module.exports = (config) => {
|
|||||||
res.status(400).send('bad request');
|
res.status(400).send('bad request');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.use((err, req, res, next) => {
|
||||||
|
console.log(cons.error, `error when handling ${req.path} request : ${err}`);
|
||||||
|
console.error(err.stack);
|
||||||
|
next(err);
|
||||||
|
});
|
||||||
|
|
||||||
// must be use in a server.js to start the server
|
// must be use in a server.js to start the server
|
||||||
app.start = () => {
|
app.start = () => {
|
||||||
reload((res) => {
|
reload((res) => {
|
||||||
|
|||||||
+3
-3
@@ -75,7 +75,7 @@ module.exports = (config) => {
|
|||||||
.map((path) => path.match(/^\/(\d{4})\/(\d{2})\/(\d{2})\/$/))
|
.map((path) => path.match(/^\/(\d{4})\/(\d{2})\/(\d{2})\/$/))
|
||||||
.filter((matches) => matches && matches.length > 1);
|
.filter((matches) => matches && matches.length > 1);
|
||||||
if (paths.length === 0)
|
if (paths.length === 0)
|
||||||
cb(null, []);
|
cb(null, {});
|
||||||
const articles = {};
|
const articles = {};
|
||||||
let remaining = 0;
|
let remaining = 0;
|
||||||
paths.forEach((matches) => {
|
paths.forEach((matches) => {
|
||||||
@@ -94,8 +94,8 @@ module.exports = (config) => {
|
|||||||
article.title = info.title || config['article']['default_title'];
|
article.title = info.title || config['article']['default_title'];
|
||||||
article.thumbnail = info.thumbnail ? path.join(article.path, info.thumbnail) : config['article']['default_thumbnail'];
|
article.thumbnail = info.thumbnail ? path.join(article.path, info.thumbnail) : config['article']['default_thumbnail'];
|
||||||
article.escapedTitle = article.title.toLowerCase().replace(/[^\w]/gm, ' ').trim().replace(/ /gm, '_');
|
article.escapedTitle = article.title.toLowerCase().replace(/[^\w]/gm, ' ').trim().replace(/ /gm, '_');
|
||||||
article.url = path.join(article.path, article.escapedTitle)+'/';
|
article.url = '/' + path.join(article.path, article.escapedTitle) + '/';
|
||||||
articles[article.path]=article;
|
articles[article.path] = article;
|
||||||
remaining--;
|
remaining--;
|
||||||
if (remaining === 0)
|
if (remaining === 0)
|
||||||
cb(null, articles);
|
cb(null, articles);
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ const utils = require('./test_utils');
|
|||||||
const dataDir = 'test_data';
|
const dataDir = 'test_data';
|
||||||
const testIndex = 'testindex.ejs';
|
const testIndex = 'testindex.ejs';
|
||||||
const testError = 'testerror.ejs';
|
const testError = 'testerror.ejs';
|
||||||
|
const testTemplate = 'testtemplate.ejs';
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
'test': true,
|
'test': true,
|
||||||
@@ -19,10 +20,12 @@ const config = {
|
|||||||
},
|
},
|
||||||
'article': {
|
'article': {
|
||||||
'index': 'index.md',
|
'index': 'index.md',
|
||||||
|
'template' : testTemplate,
|
||||||
'thumbnail_tag': 'thumbnail',
|
'thumbnail_tag': 'thumbnail',
|
||||||
'default_title': 'Untitled',
|
'default_title': 'Untitled',
|
||||||
'default_thumbnail': null
|
'default_thumbnail': null
|
||||||
},
|
},
|
||||||
|
'showdown' : {}
|
||||||
};
|
};
|
||||||
|
|
||||||
const app = require('../src/app')(config);
|
const app = require('../src/app')(config);
|
||||||
@@ -83,6 +86,72 @@ describe('Test root path', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('Test articles rendering', () => {
|
||||||
|
test('404 article not found', (done) =>{
|
||||||
|
request(app).get('/2019/05/06/untitled/').then((response) =>{
|
||||||
|
expect(response.statusCode).toBe(404);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('500 no template', (done) =>{
|
||||||
|
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
|
||||||
|
fs.writeFileSync(path.join(dataDir, '2019', '05', '05','index.md'), '# Hello');
|
||||||
|
app.reload((res) => {
|
||||||
|
expect(res).toBe(true);
|
||||||
|
request(app).get('/2019/05/05/hello/').then((response) =>{
|
||||||
|
expect(response.statusCode).toBe(500);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('200 rendered article', (done) =>{
|
||||||
|
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
|
||||||
|
fs.writeFileSync(path.join(dataDir, '2019', '05', '05','index.md'), '# Hello');
|
||||||
|
fs.writeFileSync(path.join(dataDir, testTemplate), '<%- article.content %><%- `<a href="${article.url}">reload</a>` %>');
|
||||||
|
app.reload((res) => {
|
||||||
|
expect(res).toBe(true);
|
||||||
|
request(app).get('/2019/05/05/hello/').then((response) =>{
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
expect(response.text).toBe('<h1 id="hello">Hello</h1><a href="/2019/05/05/hello/">reload</a>');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('200 other url', (done) =>{
|
||||||
|
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
|
||||||
|
utils.createEmptyFiles([
|
||||||
|
path.join(dataDir, '2019', '05', '05','index.md'),
|
||||||
|
path.join(dataDir, testTemplate)
|
||||||
|
]);
|
||||||
|
app.reload((res) => {
|
||||||
|
expect(res).toBe(true);
|
||||||
|
request(app).get('/2019/05/05/anything/').then((response) =>{
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('200 other url 2', (done) =>{
|
||||||
|
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
|
||||||
|
utils.createEmptyFiles([
|
||||||
|
path.join(dataDir, '2019', '05', '05','index.md'),
|
||||||
|
path.join(dataDir, testTemplate)
|
||||||
|
]);
|
||||||
|
app.reload((res) => {
|
||||||
|
expect(res).toBe(true);
|
||||||
|
request(app).get('/2019/05/05/').then((response) =>{
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
describe('Test static files', () => {
|
describe('Test static files', () => {
|
||||||
test('404 invalid file no error page', (done) =>{
|
test('404 invalid file no error page', (done) =>{
|
||||||
request(app).get('/somefile.txt').then((response) =>{
|
request(app).get('/somefile.txt').then((response) =>{
|
||||||
|
|||||||
+16
-16
@@ -191,10 +191,10 @@ describe('Test article fetching', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
test('empty data dir', (done) => {
|
test('empty data dir', (done) => {
|
||||||
fw.fetchArticles((err, list) => {
|
fw.fetchArticles((err, dict) => {
|
||||||
expect(err).toBeNull();
|
expect(err).toBeNull();
|
||||||
expect(list).toBeDefined();
|
expect(dict).toBeDefined();
|
||||||
expect(list.length).toBe(0);
|
expect(Object.keys(dict).length).toBe(0);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -208,10 +208,10 @@ describe('Test article fetching', () => {
|
|||||||
path.join(dataDir, 'test', 'test', testIndex),
|
path.join(dataDir, 'test', 'test', testIndex),
|
||||||
path.join(dataDir, '2019', '05', testIndex)
|
path.join(dataDir, '2019', '05', testIndex)
|
||||||
]);
|
]);
|
||||||
fw.fetchArticles((err, list) => {
|
fw.fetchArticles((err, dict) => {
|
||||||
expect(err).toBeNull();
|
expect(err).toBeNull();
|
||||||
expect(list).toBeDefined();
|
expect(dict).toBeDefined();
|
||||||
expect(list.length).toBe(0);
|
expect(Object.keys(dict).length).toBe(0);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -220,11 +220,11 @@ describe('Test article fetching', () => {
|
|||||||
const file = path.join(dir, testIndex);
|
const file = path.join(dir, testIndex);
|
||||||
utils.createEmptyDirs([dir]);
|
utils.createEmptyDirs([dir]);
|
||||||
utils.createEmptyFiles([file]);
|
utils.createEmptyFiles([file]);
|
||||||
fw.fetchArticles((err, list) => {
|
fw.fetchArticles((err, dict) => {
|
||||||
expect(err).toBeNull();
|
expect(err).toBeNull();
|
||||||
expect(list).toBeDefined();
|
expect(dict).toBeDefined();
|
||||||
expect(list.length).toBe(1);
|
expect(Object.keys(dict).length).toBe(1);
|
||||||
expect(list[0]).toEqual({
|
expect(dict[path.join('2019', '05', '05')]).toEqual({
|
||||||
path: path.join('2019', '05', '05'),
|
path: path.join('2019', '05', '05'),
|
||||||
realPath: dir,
|
realPath: dir,
|
||||||
year: 2019,
|
year: 2019,
|
||||||
@@ -234,7 +234,7 @@ describe('Test article fetching', () => {
|
|||||||
title: 'Untitled',
|
title: 'Untitled',
|
||||||
thumbnail: 'default.png',
|
thumbnail: 'default.png',
|
||||||
escapedTitle: 'untitled',
|
escapedTitle: 'untitled',
|
||||||
url: path.join('2019', '05', '05', 'untitled')+'/',
|
url: '/'+path.join('2019', '05', '05', 'untitled')+'/',
|
||||||
});
|
});
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@@ -248,11 +248,11 @@ describe('Test article fetching', () => {
|
|||||||

|

|
||||||
this is some text
|
this is some text
|
||||||
`);
|
`);
|
||||||
fw.fetchArticles((err, list) => {
|
fw.fetchArticles((err, dict) => {
|
||||||
expect(err).toBeNull();
|
expect(err).toBeNull();
|
||||||
expect(list).toBeDefined();
|
expect(dict).toBeDefined();
|
||||||
expect(list.length).toBe(1);
|
expect(Object.keys(dict).length).toBe(1);
|
||||||
expect(list[0]).toEqual({
|
expect(dict[path.join('2019', '05', '05')]).toEqual({
|
||||||
path: path.join('2019', '05', '05'),
|
path: path.join('2019', '05', '05'),
|
||||||
realPath: dir,
|
realPath: dir,
|
||||||
year: 2019,
|
year: 2019,
|
||||||
@@ -262,7 +262,7 @@ describe('Test article fetching', () => {
|
|||||||
title: 'Title with : info !',
|
title: 'Title with : info !',
|
||||||
thumbnail: path.join('2019', '05', '05', './thumbnail.jpg'),
|
thumbnail: path.join('2019', '05', '05', './thumbnail.jpg'),
|
||||||
escapedTitle: 'title_with___info',
|
escapedTitle: 'title_with___info',
|
||||||
url: path.join('2019', '05', '05', 'title_with___info')+'/',
|
url: '/'+path.join('2019', '05', '05', 'title_with___info')+'/',
|
||||||
});
|
});
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user