eslint update
This commit is contained in:
+67
-6
@@ -1,16 +1,19 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
'plugins': ['jest'],
|
plugins: [ 'jest' ],
|
||||||
'env': {
|
env: {
|
||||||
'commonjs': true,
|
'commonjs': true,
|
||||||
'es2021': true,
|
'es2021': true,
|
||||||
'node': true,
|
'node': true,
|
||||||
'jest/globals': true,
|
'jest/globals': true,
|
||||||
},
|
},
|
||||||
'extends': ['eslint:recommended', 'plugin:jest/recommended'],
|
extends: [
|
||||||
'parserOptions': {
|
'eslint:recommended',
|
||||||
'ecmaVersion': 12,
|
'plugin:jest/recommended',
|
||||||
|
],
|
||||||
|
parserOptions: {
|
||||||
|
ecmaVersion: 12,
|
||||||
},
|
},
|
||||||
'rules': {
|
rules: {
|
||||||
'indent': [
|
'indent': [
|
||||||
'error',
|
'error',
|
||||||
4,
|
4,
|
||||||
@@ -41,5 +44,63 @@ module.exports = {
|
|||||||
'error',
|
'error',
|
||||||
'always-multiline',
|
'always-multiline',
|
||||||
],
|
],
|
||||||
|
'complexity': 'error',
|
||||||
|
'consistent-return': 'error',
|
||||||
|
'dot-location': [
|
||||||
|
'error',
|
||||||
|
'property',
|
||||||
|
],
|
||||||
|
'eqeqeq': [
|
||||||
|
'error',
|
||||||
|
'always',
|
||||||
|
{ null: 'ignore' },
|
||||||
|
],
|
||||||
|
'no-empty-function': 'error',
|
||||||
|
'no-floating-decimal': 'error',
|
||||||
|
'no-multi-spaces': 'error',
|
||||||
|
'camelcase': [
|
||||||
|
'error',
|
||||||
|
{ properties: 'never' },
|
||||||
|
],
|
||||||
|
'comma-spacing': [
|
||||||
|
'error',
|
||||||
|
{ before: false, after: true },
|
||||||
|
],
|
||||||
|
'array-bracket-newline': [
|
||||||
|
'error',
|
||||||
|
{ multiline: true },
|
||||||
|
],
|
||||||
|
'array-element-newline': [
|
||||||
|
'error',
|
||||||
|
{ multiline: true, minItems: 2 },
|
||||||
|
],
|
||||||
|
'array-bracket-spacing': [
|
||||||
|
'error',
|
||||||
|
'always',
|
||||||
|
],
|
||||||
|
'object-curly-spacing': [
|
||||||
|
'error',
|
||||||
|
'always',
|
||||||
|
],
|
||||||
|
'comma-style': 'error',
|
||||||
|
'computed-property-spacing': 'error',
|
||||||
|
'eol-last': 'error',
|
||||||
|
'func-call-spacing': 'error',
|
||||||
|
'key-spacing': 'error',
|
||||||
|
'keyword-spacing': 'error',
|
||||||
|
'multiline-comment-style': 'error',
|
||||||
|
'newline-per-chained-call': 'error',
|
||||||
|
'no-lonely-if': 'error',
|
||||||
|
'no-multiple-empty-lines': 'error',
|
||||||
|
'no-trailing-spaces': 'error',
|
||||||
|
'no-unneeded-ternary': 'error',
|
||||||
|
'no-whitespace-before-property': 'error',
|
||||||
|
'operator-assignment': 'error',
|
||||||
|
'quote-props': [
|
||||||
|
'error',
|
||||||
|
'consistent-as-needed',
|
||||||
|
],
|
||||||
|
'space-before-blocks': 'error',
|
||||||
|
'space-infix-ops': 'error',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
+65
-57
@@ -28,26 +28,26 @@ const cons = {
|
|||||||
|
|
||||||
module.exports = (config) => {
|
module.exports = (config) => {
|
||||||
/**
|
/**
|
||||||
* Fetch articles from the data folder and send success as a response
|
* Fetch articles from the data folder and send success as a response
|
||||||
* @param success
|
* @param success
|
||||||
* @param error
|
* @param error
|
||||||
*/
|
*/
|
||||||
let reload;
|
let reload;
|
||||||
/**
|
/**
|
||||||
* Render the page with the view engine and catch errors
|
* Render the page with the view engine and catch errors
|
||||||
* @param req
|
* @param req
|
||||||
* @param res
|
* @param res
|
||||||
* @param vPath - 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
|
||||||
*/
|
*/
|
||||||
let render;
|
let render;
|
||||||
/**
|
/**
|
||||||
* Show an error with the correct page
|
* Show an error with the correct page
|
||||||
* @param req
|
* @param req
|
||||||
* @param res
|
* @param res
|
||||||
* @param code - error code
|
* @param code - error code
|
||||||
*/
|
*/
|
||||||
let showError;
|
let showError;
|
||||||
const fw = require('./file_walker')(config);
|
const fw = require('./file_walker')(config);
|
||||||
const renderer = require('./renderer')(config);
|
const renderer = require('./renderer')(config);
|
||||||
@@ -66,21 +66,22 @@ module.exports = (config) => {
|
|||||||
fw.fetchArticles((err, dict) => {
|
fw.fetchArticles((err, dict) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error(cons.error, 'error loading articles : ' + err);
|
console.error(cons.error, 'error loading articles : ' + err);
|
||||||
return error ? error() : null;
|
error();
|
||||||
}
|
|
||||||
Object.keys(articles).forEach((key) => delete articles[key]);
|
|
||||||
Object.keys(dict).forEach((key) => articles[key] = dict[key]);
|
|
||||||
const nb = Object.keys(articles).length;
|
|
||||||
const dnb = Object.values(articles).filter(a => a.draft).length;
|
|
||||||
if (nb > 0) {
|
|
||||||
console.log(cons.ok, `loaded ${nb} article${nb > 1 ? 's' : ''} (${dnb} drafted)`);
|
|
||||||
} else {
|
} else {
|
||||||
console.log(cons.warn, 'no articles loaded, check your configuration');
|
Object.keys(articles).forEach((key) => delete articles[key]);
|
||||||
|
Object.keys(dict).forEach((key) => articles[key] = dict[key]);
|
||||||
|
const nb = Object.keys(articles).length;
|
||||||
|
const dnb = Object.values(articles).filter(a => a.draft).length;
|
||||||
|
if (nb > 0) {
|
||||||
|
console.log(cons.ok, `loaded ${nb} article${nb > 1 ? 's' : ''} (${dnb} drafted)`);
|
||||||
|
} else {
|
||||||
|
console.log(cons.warn, 'no articles loaded, check your configuration');
|
||||||
|
}
|
||||||
|
|
||||||
|
lastRSS = '';
|
||||||
|
|
||||||
|
success();
|
||||||
}
|
}
|
||||||
|
|
||||||
lastRSS = '';
|
|
||||||
|
|
||||||
success();
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
if (config['test']) {
|
if (config['test']) {
|
||||||
@@ -115,7 +116,7 @@ module.exports = (config) => {
|
|||||||
if (err) {
|
if (err) {
|
||||||
res.sendStatus(code);
|
res.sendStatus(code);
|
||||||
} else {
|
} else {
|
||||||
render(req, res, errorPath, {error: code}, code);
|
render(req, res, errorPath, { error: code }, code);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -142,7 +143,7 @@ module.exports = (config) => {
|
|||||||
res.end = (chunk, encoding) => {
|
res.end = (chunk, encoding) => {
|
||||||
fs.appendFile(config['access_log'],
|
fs.appendFile(config['access_log'],
|
||||||
`${res.statusCode} ${req.method} ${req.url} ${new Date().toUTCString()} ${req.ips.join(' ') || req.ip}\n`,
|
`${res.statusCode} ${req.method} ${req.url} ${new Date().toUTCString()} ${req.ips.join(' ') || req.ip}\n`,
|
||||||
{encoding: 'UTF-8'}, () => {
|
{ encoding: 'UTF-8' }, () => {
|
||||||
res.end = end;
|
res.end = end;
|
||||||
res.end(chunk, encoding);
|
res.end(chunk, encoding);
|
||||||
});
|
});
|
||||||
@@ -162,7 +163,8 @@ module.exports = (config) => {
|
|||||||
render(req, res, homePath,
|
render(req, res, homePath,
|
||||||
{
|
{
|
||||||
articles: Object.values(articles)
|
articles: Object.values(articles)
|
||||||
.filter(d => !d.draft).sort((a, b) => ('' + b.path).localeCompare(a.path)),
|
.filter(d => !d.draft)
|
||||||
|
.sort((a, b) => ('' + b.path).localeCompare(a.path)),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -180,10 +182,10 @@ module.exports = (config) => {
|
|||||||
if (config['modules']['rss']) {
|
if (config['modules']['rss']) {
|
||||||
if (!lastRSS) {
|
if (!lastRSS) {
|
||||||
const feed = new Rss({
|
const feed = new Rss({
|
||||||
'title': config['rss']['title'],
|
title: config['rss']['title'],
|
||||||
'description': config['rss']['description'],
|
description: config['rss']['description'],
|
||||||
'feed_url': host + req.url,
|
feed_url: host + req.url,
|
||||||
'site_url': host,
|
site_url: host,
|
||||||
});
|
});
|
||||||
Object.values(articles)
|
Object.values(articles)
|
||||||
.slice(0, config['rss']['length'])
|
.slice(0, config['rss']['length'])
|
||||||
@@ -205,24 +207,29 @@ module.exports = (config) => {
|
|||||||
//webhook endpoint
|
//webhook endpoint
|
||||||
app.post(config['webhook']['endpoint'], (req, res) => {
|
app.post(config['webhook']['endpoint'], (req, res) => {
|
||||||
if (config['modules']['webhook']) {
|
if (config['modules']['webhook']) {
|
||||||
|
let valid = true;
|
||||||
if (config['webhook']['signature_header'] && config['webhook']['secret']) {
|
if (config['webhook']['signature_header'] && config['webhook']['secret']) {
|
||||||
const payload = JSON.stringify(req.body) || '';
|
const payload = JSON.stringify(req.body) || '';
|
||||||
const hmac = crypto.createHmac('sha1', config['webhook']['secret']);
|
const hmac = crypto.createHmac('sha1', config['webhook']['secret']);
|
||||||
const digest = 'sha1=' + hmac.update(payload).digest('hex');
|
const digest = 'sha1=' + hmac.update(payload).digest('hex');
|
||||||
const checksum = req.headers[config['webhook']['signature_header']];
|
const checksum = req.headers[config['webhook']['signature_header']];
|
||||||
if (!checksum || !digest || checksum !== digest) {
|
if (!checksum || !digest || checksum !== digest) {
|
||||||
return res.sendStatus(403);
|
res.sendStatus(403);
|
||||||
|
valid = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cp.exec(config['webhook']['pull_command'], {cwd: path.join(__dirname, '..', config['data_dir'])}, (err) => {
|
if (valid) {
|
||||||
if (err) {
|
cp.exec(config['webhook']['pull_command'], { cwd: path.join(__dirname, '..', config['data_dir']) }, (err) => {
|
||||||
console.log(cons.error, `command '${config['webhook']['pull_command']}' failed : ${err}`);
|
if (err) {
|
||||||
return res.sendStatus(500);
|
console.log(cons.error, `command '${config['webhook']['pull_command']}' failed : ${err}`);
|
||||||
}
|
res.sendStatus(500);
|
||||||
reload(() => {
|
} else {
|
||||||
res.sendStatus(200);
|
reload(() => {
|
||||||
|
res.sendStatus(200);
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
} else {
|
} else {
|
||||||
res.sendStatus(400);
|
res.sendStatus(400);
|
||||||
}
|
}
|
||||||
@@ -254,18 +261,19 @@ module.exports = (config) => {
|
|||||||
renderer.render(article.realPath, (err, html) => {
|
renderer.render(article.realPath, (err, html) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log(cons.error, `failed to render article ${req.path} : ${err}`);
|
console.log(cons.error, `failed to render article ${req.path} : ${err}`);
|
||||||
return showError(req, res, 500);
|
showError(req, res, 500);
|
||||||
|
} else {
|
||||||
|
article.content = html;
|
||||||
|
const templatePath = path.join(config['data_dir'], config['article']['template']);
|
||||||
|
fs.access(templatePath, fs.constants.R_OK, (err) => {
|
||||||
|
if (err) {
|
||||||
|
console.log(cons.error, `no template found at ${templatePath}`);
|
||||||
|
showError(req, res, 500);
|
||||||
|
} else {
|
||||||
|
render(req, res, templatePath, { article: article });
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
article.content = html;
|
|
||||||
const templatePath = path.join(config['data_dir'], config['article']['template']);
|
|
||||||
fs.access(templatePath, fs.constants.R_OK, (err) => {
|
|
||||||
if (err) {
|
|
||||||
console.log(cons.error, `no template found at ${templatePath}`);
|
|
||||||
showError(req, res, 500);
|
|
||||||
} else {
|
|
||||||
render(req, res, templatePath, {article: article});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -300,7 +308,7 @@ module.exports = (config) => {
|
|||||||
}
|
}
|
||||||
fs.appendFile(config['error_log'],
|
fs.appendFile(config['error_log'],
|
||||||
`500 ${req.method} ${req.url} ${new Date().toUTCString()} ${req.ips.join(' ') || req.ip}\n${err.stack}\n`,
|
`500 ${req.method} ${req.url} ${new Date().toUTCString()} ${req.ips.join(' ') || req.ip}\n${err.stack}\n`,
|
||||||
{encoding: 'UTF-8'}, () => {
|
{ encoding: 'UTF-8' }, () => {
|
||||||
next(err);
|
next(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+1
-1
@@ -25,7 +25,7 @@ const merge = (ref, src) => {
|
|||||||
|
|
||||||
module.exports = () => {
|
module.exports = () => {
|
||||||
try {
|
try {
|
||||||
let configData = fs.readFileSync('config.json', {encoding: 'UTF-8'});
|
let configData = fs.readFileSync('config.json', { encoding: 'UTF-8' });
|
||||||
let config = JSON.parse(configData);
|
let config = JSON.parse(configData);
|
||||||
return merge(refConfig, config);
|
return merge(refConfig, config);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
+82
-77
@@ -11,29 +11,31 @@ const joinUrl = (...paths) => path.join(...paths).replace(/\\/g, '/');
|
|||||||
const getFileTree = (dir, cb) => {
|
const getFileTree = (dir, cb) => {
|
||||||
let list = [];
|
let list = [];
|
||||||
let remaining = 0;
|
let remaining = 0;
|
||||||
fs.readdir(dir, {withFileTypes: true}, (err, items) => {
|
fs.readdir(dir, { withFileTypes: true }, (err, items) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return cb(err);
|
cb(err);
|
||||||
}
|
} else {
|
||||||
items.forEach((item) => {
|
items.forEach((item) => {
|
||||||
if (item.isDirectory()) {
|
if (item.isDirectory()) {
|
||||||
remaining++;
|
remaining++;
|
||||||
getFileTree(path.join(dir, item.name), (err, out) => {
|
getFileTree(path.join(dir, item.name), (err, out) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return cb(err);
|
cb(err);
|
||||||
}
|
} else {
|
||||||
list.push(...out);
|
list.push(...out);
|
||||||
remaining--;
|
remaining--;
|
||||||
if (remaining === 0) {
|
if (remaining === 0) {
|
||||||
cb(null, list);
|
cb(null, list);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
} else {
|
});
|
||||||
list.push(path.join(dir, item.name));
|
} else {
|
||||||
|
list.push(path.join(dir, item.name));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (remaining === 0) {
|
||||||
|
cb(null, list);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
if (remaining === 0) {
|
|
||||||
cb(null, list);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -45,21 +47,21 @@ const getFileTree = (dir, cb) => {
|
|||||||
* @param cb
|
* @param cb
|
||||||
*/
|
*/
|
||||||
const readIndexFile = (path, thumbnailTag, cb) => {
|
const readIndexFile = (path, thumbnailTag, cb) => {
|
||||||
fs.readFile(path, {encoding: 'UTF-8'}, (err, data) => {
|
fs.readFile(path, { encoding: 'UTF-8' }, (err, data) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return cb(err);
|
cb(err);
|
||||||
|
} else {
|
||||||
|
let info = {};
|
||||||
|
|
||||||
|
const regRes1 = data.match(/(^|[^#])#([^#\r\n]*)\r?\n?$/m);
|
||||||
|
info.title = regRes1 ? regRes1[2].trim() : undefined;
|
||||||
|
|
||||||
|
const thumbnailRegEx = new RegExp(`!\\[${thumbnailTag}]\\(([^)]*)\\)`, 'i');
|
||||||
|
const regRes2 = data.match(thumbnailRegEx);
|
||||||
|
info.thumbnail = regRes2 ? regRes2[1].trim() : undefined;
|
||||||
|
|
||||||
|
cb(null, info);
|
||||||
}
|
}
|
||||||
|
|
||||||
let info = {};
|
|
||||||
|
|
||||||
const regRes1 = data.match(/(^|[^#])#([^#\r\n]*)\r?\n?$/m);
|
|
||||||
info.title = regRes1 ? regRes1[2].trim() : undefined;
|
|
||||||
|
|
||||||
const thumbnailRegEx = new RegExp(`!\\[${thumbnailTag}]\\(([^)]*)\\)`, 'i');
|
|
||||||
const regRes2 = data.match(thumbnailRegEx);
|
|
||||||
info.thumbnail = regRes2 ? regRes2[1].trim() : undefined;
|
|
||||||
|
|
||||||
cb(null, info);
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -68,53 +70,56 @@ module.exports = (config) => {
|
|||||||
fileTree: config['test'] ? getFileTree : undefined,
|
fileTree: config['test'] ? getFileTree : undefined,
|
||||||
readIndexFile: config['test'] ? readIndexFile : undefined,
|
readIndexFile: config['test'] ? readIndexFile : undefined,
|
||||||
/**
|
/**
|
||||||
* find and read all articles inside the data directory
|
* find and read all articles inside the data directory
|
||||||
* @param cb
|
* @param cb
|
||||||
*/
|
*/
|
||||||
fetchArticles: (cb) => {
|
fetchArticles: (cb) => {
|
||||||
getFileTree(config['data_dir'], (err, fileList) => {
|
getFileTree(config['data_dir'], (err, fileList) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return cb(err);
|
cb(err);
|
||||||
}
|
} else {
|
||||||
const paths = fileList
|
const paths = fileList
|
||||||
.map((p) => p.substr(config['data_dir'].length + 1).split(path.sep))
|
.map((p) => p.substr(config['data_dir'].length + 1).split(path.sep))
|
||||||
.filter((p) => p.length === 4 && (p[3] === config['article']['index'] || p[3] === config['article']['draft']) &&
|
.filter((p) => p.length === 4 && (p[3] === config['article']['index'] || p[3] === config['article']['draft']) &&
|
||||||
/^\d{4}$/.test(p[0]) && /^\d{2}$/.test(p[1]) && /^\d{2}$/.test(p[2]));
|
/^\d{4}$/.test(p[0]) && /^\d{2}$/.test(p[1]) && /^\d{2}$/.test(p[2]));
|
||||||
if (paths.length === 0) {
|
if (paths.length === 0) {
|
||||||
cb(null, {});
|
cb(null, {});
|
||||||
}
|
}
|
||||||
const articles = {};
|
const articles = {};
|
||||||
let remaining = 0;
|
let remaining = 0;
|
||||||
paths.forEach((p) => {
|
paths.forEach((p) => {
|
||||||
const article = {
|
const article = {
|
||||||
path: joinUrl(p[0], p[1], p[2]),
|
path: joinUrl(p[0], p[1], p[2]),
|
||||||
draft: p[3] === config['article']['draft'],
|
draft: p[3] === config['article']['draft'],
|
||||||
realPath: path.join(config['data_dir'], p[0], p[1], p[2], p[3]),
|
realPath: path.join(config['data_dir'], p[0], p[1], p[2], p[3]),
|
||||||
year: parseInt(p[0]),
|
year: parseInt(p[0]),
|
||||||
month: parseInt(p[1]),
|
month: parseInt(p[1]),
|
||||||
day: parseInt(p[2]),
|
day: parseInt(p[2]),
|
||||||
};
|
};
|
||||||
article.date = new Date(article.year, article.month, article.day);
|
article.date = new Date(article.year, article.month, article.day);
|
||||||
article.date.setUTCHours(0);
|
article.date.setUTCHours(0);
|
||||||
remaining++;
|
remaining++;
|
||||||
readIndexFile(article.realPath, config['article']['thumbnail_tag'], (err, info) => {
|
readIndexFile(article.realPath, config['article']['thumbnail_tag'], (err, info) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return cb(err);
|
cb(err);
|
||||||
}
|
} else {
|
||||||
article.title = info.title || config['article']['default_title'];
|
article.title = info.title || config['article']['default_title'];
|
||||||
article.thumbnail = info.thumbnail ? joinUrl(article.path, info.thumbnail) : config['article']['default_thumbnail'];
|
article.thumbnail = info.thumbnail ? joinUrl(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, ' ')
|
||||||
article.url = '/' + joinUrl(article.path, article.escapedTitle) + '/';
|
.trim()
|
||||||
if (!articles[article.path] || !article.draft) {
|
.replace(/ /gm, '_');
|
||||||
articles[article.path] = article;
|
article.url = '/' + joinUrl(article.path, article.escapedTitle) + '/';
|
||||||
}
|
if (!articles[article.path] || !article.draft) {
|
||||||
remaining--;
|
articles[article.path] = article;
|
||||||
if (remaining === 0) {
|
}
|
||||||
cb(null, articles);
|
remaining--;
|
||||||
}
|
if (remaining === 0) {
|
||||||
|
cb(null, articles);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
+1
-1
@@ -25,7 +25,7 @@ if (!fs.existsSync('data')) {
|
|||||||
const dir = path.join('data', datetime.getFullYear().toString(), pad0(datetime.getMonth() + 1), pad0(datetime.getDate()));
|
const dir = path.join('data', datetime.getFullYear().toString(), pad0(datetime.getMonth() + 1), pad0(datetime.getDate()));
|
||||||
|
|
||||||
if (!fs.existsSync(dir)) {
|
if (!fs.existsSync(dir)) {
|
||||||
fs.mkdirSync(dir, {recursive: true});
|
fs.mkdirSync(dir, { recursive: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
copy(path.join('sample_data', 'article'), dir);
|
copy(path.join('sample_data', 'article'), dir);
|
||||||
|
|||||||
+119
-99
@@ -6,10 +6,10 @@ module.exports = (config) => {
|
|||||||
const converter = new showdown.Converter(config['showdown']);
|
const converter = new showdown.Converter(config['showdown']);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get parts outside of codes/scripts
|
* get parts outside of codes/scripts
|
||||||
* @param {string} data
|
* @param {string} data
|
||||||
* @returns {{index:number, end:number, text:string}[]} parts
|
* @returns {{index:number, end:number, text:string}[]} parts
|
||||||
*/
|
*/
|
||||||
const getParts = (data) => {
|
const getParts = (data) => {
|
||||||
let parts = [];
|
let parts = [];
|
||||||
let match;
|
let match;
|
||||||
@@ -67,17 +67,18 @@ module.exports = (config) => {
|
|||||||
|
|
||||||
const renderPrism = (data, cb) => {
|
const renderPrism = (data, cb) => {
|
||||||
if (!config['modules']['prism']) {
|
if (!config['modules']['prism']) {
|
||||||
return cb(data);
|
cb(data);
|
||||||
|
} else {
|
||||||
|
const codeRegex = /```([\w-]+)\r?\n((?:(?!```)[\s\S])*)\r?\n```/m;
|
||||||
|
let match;
|
||||||
|
while ((match = codeRegex.exec(data))) {
|
||||||
|
const lang = match[1].trim();
|
||||||
|
const code = match[2].trim();
|
||||||
|
const block = Prism.highlight(code, Prism.languages[lang] || Prism.languages.autoit, lang);
|
||||||
|
data = data.slice(0, match.index) + `<pre><code class="${lang} language-${lang}">` + block + '</code></pre>' + data.slice(match.index + match[0].length);
|
||||||
|
}
|
||||||
|
cb(data);
|
||||||
}
|
}
|
||||||
const codeRegex = /```([\w-]+)\r?\n((?:(?!```)[\s\S])*)\r?\n```/m;
|
|
||||||
let match;
|
|
||||||
while ((match = codeRegex.exec(data))) {
|
|
||||||
const lang = match[1].trim();
|
|
||||||
const code = match[2].trim();
|
|
||||||
const block = Prism.highlight(code, Prism.languages[lang] || Prism.languages.autoit, lang);
|
|
||||||
data = data.slice(0, match.index) + `<pre><code class="${lang} language-${lang}">` + block + '</code></pre>' + data.slice(match.index + match[0].length);
|
|
||||||
}
|
|
||||||
cb(data);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (config['modules']['plantuml']) {
|
if (config['modules']['plantuml']) {
|
||||||
@@ -87,22 +88,23 @@ module.exports = (config) => {
|
|||||||
const renderPlantUML = (data, cb) => {
|
const renderPlantUML = (data, cb) => {
|
||||||
/* global encode64 */
|
/* global encode64 */
|
||||||
if (!config['modules']['plantuml']) {
|
if (!config['modules']['plantuml']) {
|
||||||
return cb(data);
|
cb(data);
|
||||||
|
} else {
|
||||||
|
const parts = getParts(data);
|
||||||
|
const umlRegex = /@startuml\r?\n((?:(?!@enduml)[\s\S])*)\r?\n@enduml/m;
|
||||||
|
let match;
|
||||||
|
parts.forEach(part => {
|
||||||
|
while ((match = umlRegex.exec(part.text))) {
|
||||||
|
const code = match[1].trim();
|
||||||
|
const s = unescape(encodeURIComponent(code));
|
||||||
|
const compressed = global['zip_deflate'](s);
|
||||||
|
const url = `http://www.plantuml.com/plantuml/${config['plantuml']['output_format']}/${encode64(compressed)}`;
|
||||||
|
part.text = part.text.slice(0, match.index) + `<img alt="generated PlantUML diagram" src="${url}">` + part.text.slice(match.index + match[0].length);
|
||||||
|
}
|
||||||
|
data = data.slice(0, part.index) + part.text + data.slice(part.end);
|
||||||
|
});
|
||||||
|
cb(data);
|
||||||
}
|
}
|
||||||
const parts = getParts(data);
|
|
||||||
const umlRegex = /@startuml\r?\n((?:(?!@enduml)[\s\S])*)\r?\n@enduml/m;
|
|
||||||
let match;
|
|
||||||
parts.forEach(part => {
|
|
||||||
while ((match = umlRegex.exec(part.text))) {
|
|
||||||
const code = match[1].trim();
|
|
||||||
const s = unescape(encodeURIComponent(code));
|
|
||||||
const compressed = global['zip_deflate'](s);
|
|
||||||
const url = `http://www.plantuml.com/plantuml/${config['plantuml']['output_format']}/${encode64(compressed)}`;
|
|
||||||
part.text = part.text.slice(0, match.index) + `<img alt="generated PlantUML diagram" src="${url}">` + part.text.slice(match.index + match[0].length);
|
|
||||||
}
|
|
||||||
data = data.slice(0, part.index) + part.text + data.slice(part.end);
|
|
||||||
});
|
|
||||||
cb(data);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let mjAPI;
|
let mjAPI;
|
||||||
@@ -111,8 +113,18 @@ module.exports = (config) => {
|
|||||||
mjAPI.config({
|
mjAPI.config({
|
||||||
MathJax: {
|
MathJax: {
|
||||||
tex2jax: {
|
tex2jax: {
|
||||||
inlineMath: [['$', '$']],
|
inlineMath: [
|
||||||
displayMath: [['$$', '$$']],
|
[
|
||||||
|
'$',
|
||||||
|
'$',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
displayMath: [
|
||||||
|
[
|
||||||
|
'$$',
|
||||||
|
'$$',
|
||||||
|
],
|
||||||
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -120,40 +132,47 @@ module.exports = (config) => {
|
|||||||
|
|
||||||
const renderMathJax = (data, cb) => {
|
const renderMathJax = (data, cb) => {
|
||||||
if (!config['modules']['mathjax']) {
|
if (!config['modules']['mathjax']) {
|
||||||
return cb(data);
|
cb(data);
|
||||||
}
|
} else {
|
||||||
|
const parts = getParts(data);
|
||||||
|
|
||||||
const parts = getParts(data);
|
const doMJ = (match, format, i) => {
|
||||||
|
const eq = match[1].trim();
|
||||||
const doMJ = (match, format, i) => {
|
const output = config['mathjax']['output_format'];
|
||||||
const eq = match[1].trim();
|
const mjConf = {
|
||||||
const output = config['mathjax']['output_format'];
|
math: eq,
|
||||||
const mjConf = {
|
format: format,
|
||||||
math: eq,
|
speakText: config['mathjax']['speak_text'],
|
||||||
format: format,
|
};
|
||||||
speakText: config['mathjax']['speak_text'],
|
mjConf[output] = true;
|
||||||
};
|
mjAPI.typeset(mjConf, (res) => {
|
||||||
mjConf[output] = true;
|
data = data.slice(0, parts[i].index + match.index) + res[output] + data.slice(parts[i].index + match.index + match[0].length);
|
||||||
mjAPI.typeset(mjConf, (res) => {
|
renderMathJax(data, (data2) => {
|
||||||
data = data.slice(0, parts[i].index + match.index) + res[output] + data.slice(parts[i].index + match.index + match[0].length);
|
cb(data2);
|
||||||
renderMathJax(data, (data2) => {
|
});
|
||||||
cb(data2);
|
|
||||||
});
|
});
|
||||||
});
|
};
|
||||||
};
|
|
||||||
|
|
||||||
const eqRegex = /\$\$((?:(?!\$\$)[\s\S])*)\$\$/m;
|
const eqRegex = /\$\$((?:(?!\$\$)[\s\S])*)\$\$/m;
|
||||||
const inlineEqRegex = /\$([^$\n]*)\$/;
|
const inlineEqRegex = /\$([^$\n]*)\$/;
|
||||||
|
|
||||||
for (let i = 0; i < parts.length; i++) {
|
let found = false;
|
||||||
let match;
|
for (let i = 0; i < parts.length; i++) {
|
||||||
if ((match = eqRegex.exec(parts[i].text))) {
|
let match;
|
||||||
return doMJ(match, 'TeX', i);
|
if ((match = eqRegex.exec(parts[i].text))) {
|
||||||
} else if ((match = inlineEqRegex.exec(parts[i].text))) {
|
doMJ(match, 'TeX', i);
|
||||||
return doMJ(match, 'inline-TeX', i);
|
found = true;
|
||||||
|
break;
|
||||||
|
} else if ((match = inlineEqRegex.exec(parts[i].text))) {
|
||||||
|
doMJ(match, 'inline-TeX', i);
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found) {
|
||||||
|
cb(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cb(data);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let faDiagrams;
|
let faDiagrams;
|
||||||
@@ -165,36 +184,37 @@ module.exports = (config) => {
|
|||||||
|
|
||||||
const renderFaDiagrams = (data, cb) => {
|
const renderFaDiagrams = (data, cb) => {
|
||||||
if (!config['modules']['fa-diagrams']) {
|
if (!config['modules']['fa-diagrams']) {
|
||||||
return cb(data);
|
cb(data);
|
||||||
}
|
} else {
|
||||||
const parts = getParts(data);
|
const parts = getParts(data);
|
||||||
const diagramsRegex = /@startfad\r?\n((?:(?!@endfad)[\s\S])*)\r?\n@endfad/m;
|
const diagramsRegex = /@startfad\r?\n((?:(?!@endfad)[\s\S])*)\r?\n@endfad/m;
|
||||||
let match;
|
let match;
|
||||||
parts.forEach(part => {
|
parts.forEach(part => {
|
||||||
while ((match = diagramsRegex.exec(part.text))) {
|
while ((match = diagramsRegex.exec(part.text))) {
|
||||||
const code = match[1].trim();
|
const code = match[1].trim();
|
||||||
let output;
|
let output;
|
||||||
try {
|
try {
|
||||||
const diagData = toml.parse(code);
|
const diagData = toml.parse(code);
|
||||||
const findLineBreaks = (data) => {
|
const findLineBreaks = (data) => {
|
||||||
Object.keys(data).forEach(key => {
|
Object.keys(data).forEach(key => {
|
||||||
if (typeof data[key] === 'object') {
|
if (typeof data[key] === 'object') {
|
||||||
findLineBreaks(data[key]);
|
findLineBreaks(data[key]);
|
||||||
} else if (typeof data[key] === 'string') {
|
} else if (typeof data[key] === 'string') {
|
||||||
data[key] = data[key].replace(/\\n/gm, '\n');
|
data[key] = data[key].replace(/\\n/gm, '\n');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
findLineBreaks(diagData);
|
findLineBreaks(diagData);
|
||||||
output = faDiagrams.compute(diagData);
|
output = faDiagrams.compute(diagData);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
output = `<b style="color:red">${err.toString()}</b>`;
|
output = `<b style="color:red">${err.toString()}</b>`;
|
||||||
|
}
|
||||||
|
part.text = part.text.slice(0, match.index) + output + part.text.slice(match.index + match[0].length);
|
||||||
}
|
}
|
||||||
part.text = part.text.slice(0, match.index) + output + part.text.slice(match.index + match[0].length);
|
data = data.slice(0, part.index) + part.text + data.slice(part.end);
|
||||||
}
|
});
|
||||||
data = data.slice(0, part.index) + part.text + data.slice(part.end);
|
cb(data);
|
||||||
});
|
}
|
||||||
cb(data);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -205,22 +225,22 @@ module.exports = (config) => {
|
|||||||
renderMathJax: config['test'] ? renderMathJax : undefined,
|
renderMathJax: config['test'] ? renderMathJax : undefined,
|
||||||
renderFaDiagrams: config['test'] ? renderFaDiagrams : undefined,
|
renderFaDiagrams: config['test'] ? renderFaDiagrams : undefined,
|
||||||
render: (file, cb) => {
|
render: (file, cb) => {
|
||||||
fs.readFile(file, {encoding: 'UTF-8'}, (err, data) => {
|
fs.readFile(file, { encoding: 'UTF-8' }, (err, data) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return cb(err);
|
cb(err);
|
||||||
}
|
} else {
|
||||||
|
renderPlantUML(data, (data) => {
|
||||||
renderPlantUML(data, (data) => {
|
renderFaDiagrams(data, (data) => {
|
||||||
renderFaDiagrams(data, (data) => {
|
renderMathJax(data, (data) => {
|
||||||
renderMathJax(data, (data) => {
|
renderPrism(data, (data) => {
|
||||||
renderPrism(data, (data) => {
|
renderShowDown(data, (html) => {
|
||||||
renderShowDown(data, (html) => {
|
cb(null, html);
|
||||||
cb(null, html);
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,6 +5,6 @@ const fs = require('fs');
|
|||||||
* @param scriptPath
|
* @param scriptPath
|
||||||
*/
|
*/
|
||||||
module.exports = (scriptPath) => {
|
module.exports = (scriptPath) => {
|
||||||
eval.call(global, fs.readFileSync(scriptPath, {encoding: 'UTF-8'}));
|
eval.call(global, fs.readFileSync(scriptPath, { encoding: 'UTF-8' }));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+252
-207
@@ -49,115 +49,129 @@ describe('Test reload', () => {
|
|||||||
|
|
||||||
describe('Test request logging', () => {
|
describe('Test request logging', () => {
|
||||||
test('no log', (done) => {
|
test('no log', (done) => {
|
||||||
request(app).get('/rsstest').then(() => {
|
request(app).get('/rsstest')
|
||||||
expect(fs.existsSync(path.join(dataDir, 'access.log'))).toBe(false);
|
.then(() => {
|
||||||
done();
|
expect(fs.existsSync(path.join(dataDir, 'access.log'))).toBe(false);
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
test('get 200', (done) => {
|
test('get 200', (done) => {
|
||||||
config['access_log'] = path.join(dataDir, 'access.log');
|
config['access_log'] = path.join(dataDir, 'access.log');
|
||||||
request(app).get('/rsstest').then(() => {
|
request(app).get('/rsstest')
|
||||||
fs.readFile(path.join(dataDir, 'access.log'), {encoding: 'UTF-8'}, (err, data) => {
|
.then(() => {
|
||||||
expect(err).toBeNull();
|
fs.readFile(path.join(dataDir, 'access.log'), { encoding: 'UTF-8' }, (err, data) => {
|
||||||
expect(data).toBe('200 GET /rsstest ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\n');
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
test('post 400', (done) => {
|
|
||||||
config['access_log'] = path.join(dataDir, 'access.log');
|
|
||||||
request(app).post('/rsstest').then(() => {
|
|
||||||
fs.readFile(path.join(dataDir, 'access.log'), {encoding: 'UTF-8'}, (err, data) => {
|
|
||||||
expect(err).toBeNull();
|
|
||||||
expect(data).toBe('400 POST /rsstest ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\n');
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
test('2 requests', (done) => {
|
|
||||||
config['access_log'] = path.join(dataDir, 'access.log');
|
|
||||||
request(app).get('/rss').then(() => {
|
|
||||||
request(app).post('/rsstest').then(() => {
|
|
||||||
fs.readFile(path.join(dataDir, 'access.log'), {encoding: 'UTF-8'}, (err, data) => {
|
|
||||||
expect(err).toBeNull();
|
expect(err).toBeNull();
|
||||||
expect(data).toBe('404 GET /rss ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\n' +
|
expect(data).toBe('200 GET /rsstest ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\n');
|
||||||
'400 POST /rsstest ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\n');
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
test('post 400', (done) => {
|
||||||
|
config['access_log'] = path.join(dataDir, 'access.log');
|
||||||
|
request(app).post('/rsstest')
|
||||||
|
.then(() => {
|
||||||
|
fs.readFile(path.join(dataDir, 'access.log'), { encoding: 'UTF-8' }, (err, data) => {
|
||||||
|
expect(err).toBeNull();
|
||||||
|
expect(data).toBe('400 POST /rsstest ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\n');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
test('2 requests', (done) => {
|
||||||
|
config['access_log'] = path.join(dataDir, 'access.log');
|
||||||
|
request(app).get('/rss')
|
||||||
|
.then(() => {
|
||||||
|
request(app).post('/rsstest')
|
||||||
|
.then(() => {
|
||||||
|
fs.readFile(path.join(dataDir, 'access.log'), { encoding: 'UTF-8' }, (err, data) => {
|
||||||
|
expect(err).toBeNull();
|
||||||
|
expect(data).toBe('404 GET /rss ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\n' +
|
||||||
|
'400 POST /rsstest ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\n');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Test error logging', () => {
|
describe('Test error logging', () => {
|
||||||
test('no log', (done) => {
|
test('no log', (done) => {
|
||||||
config['home']['index'] = null;
|
config['home']['index'] = null;
|
||||||
request(app).get('/').then(() => {
|
request(app).get('/')
|
||||||
expect(fs.existsSync(path.join(dataDir, 'error.log'))).toBe(false);
|
.then(() => {
|
||||||
done();
|
expect(fs.existsSync(path.join(dataDir, 'error.log'))).toBe(false);
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
test('null error', (done) => {
|
test('null error', (done) => {
|
||||||
config['home']['index'] = null;
|
config['home']['index'] = null;
|
||||||
config['error_log'] = path.join(dataDir, 'error.log');
|
config['error_log'] = path.join(dataDir, 'error.log');
|
||||||
request(app).get('/').then(() => {
|
request(app).get('/')
|
||||||
fs.readFile(path.join(dataDir, 'error.log'), {encoding: 'UTF-8'}, (err, data) => {
|
.then(() => {
|
||||||
expect(err).toBeNull();
|
fs.readFile(path.join(dataDir, 'error.log'), { encoding: 'UTF-8' }, (err, data) => {
|
||||||
const start = data.split('\n').slice(0, 2).join('\n');
|
expect(err).toBeNull();
|
||||||
const expected = '500 GET / ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\nTypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string.';
|
const start = data.split('\n').slice(0, 2)
|
||||||
expect(start.indexOf(expected)).toBe(0);
|
.join('\n');
|
||||||
done();
|
const expected = '500 GET / ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\nTypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string.';
|
||||||
|
expect(start.indexOf(expected)).toBe(0);
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Test root path', () => {
|
describe('Test root path', () => {
|
||||||
test('404 no index no error', (done) => {
|
test('404 no index no error', (done) => {
|
||||||
request(app).get('/').then((response) => {
|
request(app).get('/')
|
||||||
expect(response.statusCode).toBe(404);
|
.then((response) => {
|
||||||
done();
|
expect(response.statusCode).toBe(404);
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
test('404 no index but error page', (done) => {
|
test('404 no index but error page', (done) => {
|
||||||
fs.writeFileSync(path.join(dataDir, testError), 'error <%= error %>');
|
fs.writeFileSync(path.join(dataDir, testError), 'error <%= error %>');
|
||||||
request(app).get('/').then((response) => {
|
request(app).get('/')
|
||||||
expect(response.statusCode).toBe(404);
|
.then((response) => {
|
||||||
expect(response.text).toBe('error 404');
|
expect(response.statusCode).toBe(404);
|
||||||
done();
|
expect(response.text).toBe('error 404');
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
test('500 render error', (done) => {
|
test('500 render error', (done) => {
|
||||||
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= null.length %>');
|
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= null.length %>');
|
||||||
request(app).get('/').then((response) => {
|
request(app).get('/')
|
||||||
expect(response.statusCode).toBe(500);
|
.then((response) => {
|
||||||
done();
|
expect(response.statusCode).toBe(500);
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
test('500 render error with page', (done) => {
|
test('500 render error with page', (done) => {
|
||||||
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= null.length %>');
|
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= null.length %>');
|
||||||
fs.writeFileSync(path.join(dataDir, testError), 'error <%= error %>');
|
fs.writeFileSync(path.join(dataDir, testError), 'error <%= error %>');
|
||||||
request(app).get('/').then((response) => {
|
request(app).get('/')
|
||||||
expect(response.statusCode).toBe(500);
|
.then((response) => {
|
||||||
expect(response.text).toBe('error 500');
|
expect(response.statusCode).toBe(500);
|
||||||
done();
|
expect(response.text).toBe('error 500');
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
test('500 render error with failing page', (done) => {
|
test('500 render error with failing page', (done) => {
|
||||||
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= null.length %>');
|
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= null.length %>');
|
||||||
fs.writeFileSync(path.join(dataDir, testError), 'error <%= null.error %>');
|
fs.writeFileSync(path.join(dataDir, testError), 'error <%= null.error %>');
|
||||||
request(app).get('/').then((response) => {
|
request(app).get('/')
|
||||||
expect(response.statusCode).toBe(500);
|
.then((response) => {
|
||||||
done();
|
expect(response.statusCode).toBe(500);
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
test('200 no articles', (done) => {
|
test('200 no articles', (done) => {
|
||||||
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= articles.length %>');
|
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= articles.length %>');
|
||||||
request(app).get('/').then((response) => {
|
request(app).get('/')
|
||||||
expect(response.statusCode).toBe(200);
|
.then((response) => {
|
||||||
expect(response.text).toBe('articles 0');
|
expect(response.statusCode).toBe(200);
|
||||||
done();
|
expect(response.text).toBe('articles 0');
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
test('200 2 articles 1 drafted', (done, fail) => {
|
test('200 2 articles 1 drafted', (done, fail) => {
|
||||||
utils.createEmptyDirs([
|
utils.createEmptyDirs([
|
||||||
@@ -173,11 +187,12 @@ describe('Test root path', () => {
|
|||||||
]);
|
]);
|
||||||
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= articles.length %>');
|
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= articles.length %>');
|
||||||
app.reload(() => {
|
app.reload(() => {
|
||||||
request(app).get('/').then((response) => {
|
request(app).get('/')
|
||||||
expect(response.statusCode).toBe(200);
|
.then((response) => {
|
||||||
expect(response.text).toBe('articles 2');
|
expect(response.statusCode).toBe(200);
|
||||||
done();
|
expect(response.text).toBe('articles 2');
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
}, fail);
|
}, fail);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -185,36 +200,42 @@ describe('Test root path', () => {
|
|||||||
describe('Test RSS feed', () => {
|
describe('Test RSS feed', () => {
|
||||||
test('404 rss deactivated', (done) => {
|
test('404 rss deactivated', (done) => {
|
||||||
config['modules']['rss'] = false;
|
config['modules']['rss'] = false;
|
||||||
request(app).get('/rsstest').then((response) => {
|
request(app).get('/rsstest')
|
||||||
expect(response.statusCode).toBe(404);
|
.then((response) => {
|
||||||
done();
|
expect(response.statusCode).toBe(404);
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
test('200 empty rss', (done) => {
|
test('200 empty rss', (done) => {
|
||||||
request(app).get('/rsstest').then((response) => {
|
request(app).get('/rsstest')
|
||||||
expect(response.statusCode).toBe(200);
|
.then((response) => {
|
||||||
expect(response.type).toBe('application/rss+xml');
|
|
||||||
expect(response.text.length).toBeGreaterThan(0);
|
|
||||||
expect(response.text.split('<item>').length).toBe(1);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
test('200 Mozilla fix', (done) => {
|
|
||||||
request(app).get('/rsstest').set('user-agent', 'Mozilla Firefox 64.0').then((response) => {
|
|
||||||
expect(response.statusCode).toBe(200);
|
|
||||||
expect(response.type).toBe('text/xml');
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
test('200 rss cache', (done) => {
|
|
||||||
request(app).get('/rsstest').then(() => {
|
|
||||||
request(app).get('/rsstest').then((response) => {
|
|
||||||
expect(response.statusCode).toBe(200);
|
expect(response.statusCode).toBe(200);
|
||||||
|
expect(response.type).toBe('application/rss+xml');
|
||||||
expect(response.text.length).toBeGreaterThan(0);
|
expect(response.text.length).toBeGreaterThan(0);
|
||||||
expect(response.text.split('<item>').length).toBe(1);
|
expect(response.text.split('<item>').length).toBe(1);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
test('200 Mozilla fix', (done) => {
|
||||||
|
request(app).get('/rsstest')
|
||||||
|
.set('user-agent', 'Mozilla Firefox 64.0')
|
||||||
|
.then((response) => {
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
expect(response.type).toBe('text/xml');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
test('200 rss cache', (done) => {
|
||||||
|
request(app).get('/rsstest')
|
||||||
|
.then(() => {
|
||||||
|
request(app).get('/rsstest')
|
||||||
|
.then((response) => {
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
expect(response.text.length).toBeGreaterThan(0);
|
||||||
|
expect(response.text.split('<item>').length).toBe(1);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
test('200 2 rss items', (done, fail) => {
|
test('200 2 rss items', (done, fail) => {
|
||||||
utils.createEmptyDirs([
|
utils.createEmptyDirs([
|
||||||
@@ -226,12 +247,13 @@ describe('Test RSS feed', () => {
|
|||||||
path.join(dataDir, '2018', '05', '05', 'index.md'),
|
path.join(dataDir, '2018', '05', '05', 'index.md'),
|
||||||
]);
|
]);
|
||||||
app.reload(() => {
|
app.reload(() => {
|
||||||
request(app).get('/rsstest').then((response) => {
|
request(app).get('/rsstest')
|
||||||
expect(response.statusCode).toBe(200);
|
.then((response) => {
|
||||||
expect(response.text.length).toBeGreaterThan(0);
|
expect(response.statusCode).toBe(200);
|
||||||
expect(response.text.split('<item>').length).toBe(3);
|
expect(response.text.length).toBeGreaterThan(0);
|
||||||
done();
|
expect(response.text.split('<item>').length).toBe(3);
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
}, fail);
|
}, fail);
|
||||||
});
|
});
|
||||||
test('200 max rss items', (done, fail) => {
|
test('200 max rss items', (done, fail) => {
|
||||||
@@ -246,12 +268,13 @@ describe('Test RSS feed', () => {
|
|||||||
path.join(dataDir, '2017', '05', '05', 'index.md'),
|
path.join(dataDir, '2017', '05', '05', 'index.md'),
|
||||||
]);
|
]);
|
||||||
app.reload(() => {
|
app.reload(() => {
|
||||||
request(app).get('/rsstest').then((response) => {
|
request(app).get('/rsstest')
|
||||||
expect(response.statusCode).toBe(200);
|
.then((response) => {
|
||||||
expect(response.text.length).toBeGreaterThan(0);
|
expect(response.statusCode).toBe(200);
|
||||||
expect(response.text.split('<item>').length).toBe(3);
|
expect(response.text.length).toBeGreaterThan(0);
|
||||||
done();
|
expect(response.text.split('<item>').length).toBe(3);
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
}, fail);
|
}, fail);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -259,45 +282,51 @@ describe('Test RSS feed', () => {
|
|||||||
describe('Test webhook', () => {
|
describe('Test webhook', () => {
|
||||||
test('400 webhook deactivated', (done) => {
|
test('400 webhook deactivated', (done) => {
|
||||||
config['modules']['webhook'] = false;
|
config['modules']['webhook'] = false;
|
||||||
request(app).post('/webhooktest').then((response) => {
|
request(app).post('/webhooktest')
|
||||||
expect(response.statusCode).toBe(400);
|
.then((response) => {
|
||||||
done();
|
expect(response.statusCode).toBe(400);
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
test('200 no secret', (done) => {
|
test('200 no secret', (done) => {
|
||||||
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05')]);
|
utils.createEmptyDirs([ path.join(dataDir, '2019', '05', '05') ]);
|
||||||
utils.createEmptyFiles([
|
utils.createEmptyFiles([
|
||||||
path.join(dataDir, '2019', '05', '05', 'index.md'),
|
path.join(dataDir, '2019', '05', '05', 'index.md'),
|
||||||
path.join(dataDir, testTemplate),
|
path.join(dataDir, testTemplate),
|
||||||
]);
|
]);
|
||||||
config['webhook']['pull_command'] = 'git --help';
|
config['webhook']['pull_command'] = 'git --help';
|
||||||
request(app).post('/webhooktest').then((response) => {
|
request(app).post('/webhooktest')
|
||||||
expect(response.statusCode).toBe(200);
|
.then((response) => {
|
||||||
request(app).get('/2019/05/05/').then((response) => {
|
|
||||||
expect(response.statusCode).toBe(200);
|
expect(response.statusCode).toBe(200);
|
||||||
done();
|
request(app).get('/2019/05/05/')
|
||||||
|
.then((response) => {
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
});
|
});
|
||||||
test('500 command failed', (done) => {
|
test('500 command failed', (done) => {
|
||||||
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05')]);
|
utils.createEmptyDirs([ path.join(dataDir, '2019', '05', '05') ]);
|
||||||
utils.createEmptyFiles([
|
utils.createEmptyFiles([
|
||||||
path.join(dataDir, '2019', '05', '05', 'index.md'),
|
path.join(dataDir, '2019', '05', '05', 'index.md'),
|
||||||
path.join(dataDir, testTemplate),
|
path.join(dataDir, testTemplate),
|
||||||
]);
|
]);
|
||||||
config['webhook']['pull_command'] = 'qzgfqgqz';
|
config['webhook']['pull_command'] = 'qzgfqgqz';
|
||||||
request(app).post('/webhooktest').then((response) => {
|
request(app).post('/webhooktest')
|
||||||
expect(response.statusCode).toBe(500);
|
.then((response) => {
|
||||||
done();
|
expect(response.statusCode).toBe(500);
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
test('403 wrong secret', (done) => {
|
test('403 wrong secret', (done) => {
|
||||||
config['webhook']['signature_header'] = 'testheader';
|
config['webhook']['signature_header'] = 'testheader';
|
||||||
config['webhook']['secret'] = 'testvalue';
|
config['webhook']['secret'] = 'testvalue';
|
||||||
request(app).post('/webhooktest').set('testheader', 'sha1=invalid').then((response) => {
|
request(app).post('/webhooktest')
|
||||||
expect(response.statusCode).toBe(403);
|
.set('testheader', 'sha1=invalid')
|
||||||
done();
|
.then((response) => {
|
||||||
});
|
expect(response.statusCode).toBe(403);
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
test('200 valid secret', (done) => {
|
test('200 valid secret', (done) => {
|
||||||
config['webhook']['signature_header'] = 'testheader';
|
config['webhook']['signature_header'] = 'testheader';
|
||||||
@@ -315,86 +344,93 @@ describe('Test webhook', () => {
|
|||||||
|
|
||||||
describe('Test articles rendering', () => {
|
describe('Test articles rendering', () => {
|
||||||
test('404 article not found', (done) => {
|
test('404 article not found', (done) => {
|
||||||
request(app).get('/2019/05/06/untitled/').then((response) => {
|
request(app).get('/2019/05/06/untitled/')
|
||||||
expect(response.statusCode).toBe(404);
|
.then((response) => {
|
||||||
done();
|
expect(response.statusCode).toBe(404);
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('500 fail to render', (done, fail) => {
|
test('500 fail to render', (done, fail) => {
|
||||||
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05')]);
|
utils.createEmptyDirs([ path.join(dataDir, '2019', '05', '05') ]);
|
||||||
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'index.md'), '# Hello');
|
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'index.md'), '# Hello');
|
||||||
fs.writeFileSync(path.join(dataDir, testTemplate), '<%- articl.content %><%- `<a href="${article.url}">reload</a>` %>');
|
fs.writeFileSync(path.join(dataDir, testTemplate), '<%- articl.content %><%- `<a href="${article.url}">reload</a>` %>');
|
||||||
app.reload(() => {
|
app.reload(() => {
|
||||||
request(app).get('/2019/05/05/hello/').then((response) => {
|
request(app).get('/2019/05/05/hello/')
|
||||||
expect(response.statusCode).toBe(500);
|
.then((response) => {
|
||||||
done();
|
expect(response.statusCode).toBe(500);
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
}, fail);
|
}, fail);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('500 no template', (done, fail) => {
|
test('500 no template', (done, fail) => {
|
||||||
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05')]);
|
utils.createEmptyDirs([ path.join(dataDir, '2019', '05', '05') ]);
|
||||||
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'index.md'), '# Hello');
|
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'index.md'), '# Hello');
|
||||||
app.reload(() => {
|
app.reload(() => {
|
||||||
request(app).get('/2019/05/05/hello/').then((response) => {
|
request(app).get('/2019/05/05/hello/')
|
||||||
expect(response.statusCode).toBe(500);
|
.then((response) => {
|
||||||
done();
|
expect(response.statusCode).toBe(500);
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
}, fail);
|
}, fail);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('200 rendered article', (done, fail) => {
|
test('200 rendered article', (done, fail) => {
|
||||||
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05')]);
|
utils.createEmptyDirs([ path.join(dataDir, '2019', '05', '05') ]);
|
||||||
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'index.md'), '# Hello');
|
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>` %>');
|
fs.writeFileSync(path.join(dataDir, testTemplate), '<%- article.content %><%- `<a href="${article.url}">reload</a>` %>');
|
||||||
app.reload(() => {
|
app.reload(() => {
|
||||||
request(app).get('/2019/05/05/hello/').then((response) => {
|
request(app).get('/2019/05/05/hello/')
|
||||||
expect(response.statusCode).toBe(200);
|
.then((response) => {
|
||||||
expect(response.text).toBe('<h1 id="hello">Hello</h1><a href="/2019/05/05/hello/">reload</a>');
|
expect(response.statusCode).toBe(200);
|
||||||
done();
|
expect(response.text).toBe('<h1 id="hello">Hello</h1><a href="/2019/05/05/hello/">reload</a>');
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
}, fail);
|
}, fail);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('200 rendered draft', (done, fail) => {
|
test('200 rendered draft', (done, fail) => {
|
||||||
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05')]);
|
utils.createEmptyDirs([ path.join(dataDir, '2019', '05', '05') ]);
|
||||||
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'draft.md'), '# Hello');
|
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'draft.md'), '# Hello');
|
||||||
fs.writeFileSync(path.join(dataDir, testTemplate), '<%- article.content %><%- `<a href="${article.url}">reload</a>` %>');
|
fs.writeFileSync(path.join(dataDir, testTemplate), '<%- article.content %><%- `<a href="${article.url}">reload</a>` %>');
|
||||||
app.reload(() => {
|
app.reload(() => {
|
||||||
request(app).get('/2019/05/05/hello/').then((response) => {
|
request(app).get('/2019/05/05/hello/')
|
||||||
expect(response.statusCode).toBe(200);
|
.then((response) => {
|
||||||
expect(response.text).toBe('<h1 id="hello">Hello</h1><a href="/2019/05/05/hello/">reload</a>');
|
expect(response.statusCode).toBe(200);
|
||||||
done();
|
expect(response.text).toBe('<h1 id="hello">Hello</h1><a href="/2019/05/05/hello/">reload</a>');
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
}, fail);
|
}, fail);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('200 other url', (done, fail) => {
|
test('200 other url', (done, fail) => {
|
||||||
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05')]);
|
utils.createEmptyDirs([ path.join(dataDir, '2019', '05', '05') ]);
|
||||||
utils.createEmptyFiles([
|
utils.createEmptyFiles([
|
||||||
path.join(dataDir, '2019', '05', '05', 'index.md'),
|
path.join(dataDir, '2019', '05', '05', 'index.md'),
|
||||||
path.join(dataDir, testTemplate),
|
path.join(dataDir, testTemplate),
|
||||||
]);
|
]);
|
||||||
app.reload(() => {
|
app.reload(() => {
|
||||||
request(app).get('/2019/05/05/anything/').then((response) => {
|
request(app).get('/2019/05/05/anything/')
|
||||||
expect(response.statusCode).toBe(200);
|
.then((response) => {
|
||||||
done();
|
expect(response.statusCode).toBe(200);
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
}, fail);
|
}, fail);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('200 other url 2', (done, fail) => {
|
test('200 other url 2', (done, fail) => {
|
||||||
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05')]);
|
utils.createEmptyDirs([ path.join(dataDir, '2019', '05', '05') ]);
|
||||||
utils.createEmptyFiles([
|
utils.createEmptyFiles([
|
||||||
path.join(dataDir, '2019', '05', '05', 'index.md'),
|
path.join(dataDir, '2019', '05', '05', 'index.md'),
|
||||||
path.join(dataDir, testTemplate),
|
path.join(dataDir, testTemplate),
|
||||||
]);
|
]);
|
||||||
app.reload(() => {
|
app.reload(() => {
|
||||||
request(app).get('/2019/05/05/').then((response) => {
|
request(app).get('/2019/05/05/')
|
||||||
expect(response.statusCode).toBe(200);
|
.then((response) => {
|
||||||
done();
|
expect(response.statusCode).toBe(200);
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
}, fail);
|
}, fail);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -402,72 +438,81 @@ describe('Test articles rendering', () => {
|
|||||||
|
|
||||||
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')
|
||||||
expect(response.statusCode).toBe(404);
|
.then((response) => {
|
||||||
done();
|
expect(response.statusCode).toBe(404);
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
test('404 invalid file but error page', (done) => {
|
test('404 invalid file but error page', (done) => {
|
||||||
fs.writeFileSync(path.join(dataDir, testError), 'error <%= error %>');
|
fs.writeFileSync(path.join(dataDir, testError), 'error <%= error %>');
|
||||||
request(app).get('/somefile.txt').then((response) => {
|
request(app).get('/somefile.txt')
|
||||||
expect(response.statusCode).toBe(404);
|
.then((response) => {
|
||||||
expect(response.text).toBe('error 404');
|
expect(response.statusCode).toBe(404);
|
||||||
done();
|
expect(response.text).toBe('error 404');
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
test('404 hidden file', (done) => {
|
test('404 hidden file', (done) => {
|
||||||
utils.createEmptyDirs([path.join(dataDir, 'tmp')]);
|
utils.createEmptyDirs([ path.join(dataDir, 'tmp') ]);
|
||||||
fs.writeFileSync(path.join(dataDir, 'tmp', 'somefile.ejs'), '');
|
fs.writeFileSync(path.join(dataDir, 'tmp', 'somefile.ejs'), '');
|
||||||
request(app).get('/tmp/somefile.ejs').then((response) => {
|
request(app).get('/tmp/somefile.ejs')
|
||||||
expect(response.statusCode).toBe(404);
|
.then((response) => {
|
||||||
done();
|
expect(response.statusCode).toBe(404);
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
test('404 hidden folder', (done) => {
|
test('404 hidden folder', (done) => {
|
||||||
utils.createEmptyDirs([path.join(dataDir, '.git')]);
|
utils.createEmptyDirs([ path.join(dataDir, '.git') ]);
|
||||||
fs.writeFileSync(path.join(dataDir, '.git', 'file.txt'), '');
|
fs.writeFileSync(path.join(dataDir, '.git', 'file.txt'), '');
|
||||||
request(app).get('/.git/file.txt').then((response) => {
|
request(app).get('/.git/file.txt')
|
||||||
expect(response.statusCode).toBe(404);
|
.then((response) => {
|
||||||
done();
|
expect(response.statusCode).toBe(404);
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
test('200 valid file', (done) => {
|
test('200 valid file', (done) => {
|
||||||
fs.writeFileSync(path.join(dataDir, 'somefile.css'), 'filecontent');
|
fs.writeFileSync(path.join(dataDir, 'somefile.css'), 'filecontent');
|
||||||
request(app).get('/somefile.css').then((response) => {
|
request(app).get('/somefile.css')
|
||||||
expect(response.statusCode).toBe(200);
|
.then((response) => {
|
||||||
expect(response.type).toBe('text/css');
|
expect(response.statusCode).toBe(200);
|
||||||
expect(response.text).toBe('filecontent');
|
expect(response.type).toBe('text/css');
|
||||||
done();
|
expect(response.text).toBe('filecontent');
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
test('200 valid resource of article', (done) => {
|
test('200 valid resource of article', (done) => {
|
||||||
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05')]);
|
utils.createEmptyDirs([ path.join(dataDir, '2019', '05', '05') ]);
|
||||||
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'somefile.txt'), 'filecontent');
|
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'somefile.txt'), 'filecontent');
|
||||||
request(app).get('/2019/05/05/title/somefile.txt').then((response) => {
|
request(app).get('/2019/05/05/title/somefile.txt')
|
||||||
expect(response.statusCode).toBe(200);
|
.then((response) => {
|
||||||
expect(response.text).toBe('filecontent');
|
expect(response.statusCode).toBe(200);
|
||||||
done();
|
expect(response.text).toBe('filecontent');
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Test other requests', () => {
|
describe('Test other requests', () => {
|
||||||
test('400 POST', (done) => {
|
test('400 POST', (done) => {
|
||||||
request(app).post('/').then((response) => {
|
request(app).post('/')
|
||||||
expect(response.statusCode).toBe(400);
|
.then((response) => {
|
||||||
done();
|
expect(response.statusCode).toBe(400);
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
test('400 PUT', (done) => {
|
test('400 PUT', (done) => {
|
||||||
request(app).put('/').then((response) => {
|
request(app).put('/')
|
||||||
expect(response.statusCode).toBe(400);
|
.then((response) => {
|
||||||
done();
|
expect(response.statusCode).toBe(400);
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
test('400 DELETE', (done) => {
|
test('400 DELETE', (done) => {
|
||||||
request(app).delete('/').then((response) => {
|
request(app).delete('/')
|
||||||
expect(response.statusCode).toBe(400);
|
.then((response) => {
|
||||||
done();
|
expect(response.statusCode).toBe(400);
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+10
-4
@@ -34,8 +34,8 @@ test('example config', () => {
|
|||||||
fs.unlinkSync(configFile);
|
fs.unlinkSync(configFile);
|
||||||
}
|
}
|
||||||
fs.copyFileSync(path.join('src', 'config.default.json'), configFile);
|
fs.copyFileSync(path.join('src', 'config.default.json'), configFile);
|
||||||
const data = fs.readFileSync(configFile, {encoding: 'UTF-8'});
|
const data = fs.readFileSync(configFile, { encoding: 'UTF-8' });
|
||||||
fs.writeFileSync(configFile, data.replace('3000', '3333'), {encoding: 'UTF-8'});
|
fs.writeFileSync(configFile, data.replace('3000', '3333'), { encoding: 'UTF-8' });
|
||||||
const config = require('../src/config')();
|
const config = require('../src/config')();
|
||||||
expect(config).toBeDefined();
|
expect(config).toBeDefined();
|
||||||
expect(config['node_port']).toBe(3333);
|
expect(config['node_port']).toBe(3333);
|
||||||
@@ -70,12 +70,18 @@ test('array parsing', () => {
|
|||||||
fs.writeFileSync(configFile, '{"home":{"hidden":["item1","item2"]}}');
|
fs.writeFileSync(configFile, '{"home":{"hidden":["item1","item2"]}}');
|
||||||
const config = require('../src/config')();
|
const config = require('../src/config')();
|
||||||
expect(config).toBeDefined();
|
expect(config).toBeDefined();
|
||||||
expect(config['home']['hidden']).toEqual(['item1', 'item2']);
|
expect(config['home']['hidden']).toEqual([
|
||||||
|
'item1',
|
||||||
|
'item2',
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('array fix', () => {
|
test('array fix', () => {
|
||||||
fs.writeFileSync(configFile, '{"home":{"hidden":{}}}');
|
fs.writeFileSync(configFile, '{"home":{"hidden":{}}}');
|
||||||
const config = require('../src/config')();
|
const config = require('../src/config')();
|
||||||
expect(config).toBeDefined();
|
expect(config).toBeDefined();
|
||||||
expect(config['home']['hidden']).toEqual(['*.ejs', '/.git*']);
|
expect(config['home']['hidden']).toEqual([
|
||||||
|
'*.ejs',
|
||||||
|
'/.git*',
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
+17
-14
@@ -8,14 +8,14 @@ const testIndex = 'testindex.md';
|
|||||||
const joinUrl = (...paths) => path.join(...paths).replace(/\\/g, '/');
|
const joinUrl = (...paths) => path.join(...paths).replace(/\\/g, '/');
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
'test': true,
|
test: true,
|
||||||
'data_dir': dataDir,
|
data_dir: dataDir,
|
||||||
'article': {
|
article: {
|
||||||
'index': testIndex,
|
index: testIndex,
|
||||||
'draft': 'draft.md',
|
draft: 'draft.md',
|
||||||
'default_title': 'Untitled',
|
default_title: 'Untitled',
|
||||||
'default_thumbnail': 'default.png',
|
default_thumbnail: 'default.png',
|
||||||
'thumbnail_tag': 'thumbnail',
|
thumbnail_tag: 'thumbnail',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -225,8 +225,8 @@ describe('Test article fetching', () => {
|
|||||||
test('empty index file', (done) => {
|
test('empty index file', (done) => {
|
||||||
const dir = path.join(dataDir, '2019', '05', '05');
|
const dir = path.join(dataDir, '2019', '05', '05');
|
||||||
const file = path.join(dir, testIndex);
|
const file = path.join(dir, testIndex);
|
||||||
utils.createEmptyDirs([dir]);
|
utils.createEmptyDirs([ dir ]);
|
||||||
utils.createEmptyFiles([file]);
|
utils.createEmptyFiles([ file ]);
|
||||||
const date = new Date(2019, 5, 5);
|
const date = new Date(2019, 5, 5);
|
||||||
date.setUTCHours(0);
|
date.setUTCHours(0);
|
||||||
fw.fetchArticles((err, dict) => {
|
fw.fetchArticles((err, dict) => {
|
||||||
@@ -252,7 +252,7 @@ describe('Test article fetching', () => {
|
|||||||
test('correct index file', (done) => {
|
test('correct index file', (done) => {
|
||||||
const dir = path.join(dataDir, '2019', '05', '05');
|
const dir = path.join(dataDir, '2019', '05', '05');
|
||||||
const file = path.join(dir, testIndex);
|
const file = path.join(dir, testIndex);
|
||||||
utils.createEmptyDirs([dir]);
|
utils.createEmptyDirs([ dir ]);
|
||||||
fs.writeFileSync(file, `
|
fs.writeFileSync(file, `
|
||||||
# Title with : info !
|
# Title with : info !
|
||||||

|

|
||||||
@@ -283,7 +283,7 @@ describe('Test article fetching', () => {
|
|||||||
test('correct draft file', (done) => {
|
test('correct draft file', (done) => {
|
||||||
const dir = path.join(dataDir, '2019', '05', '05');
|
const dir = path.join(dataDir, '2019', '05', '05');
|
||||||
const file = path.join(dir, 'draft.md');
|
const file = path.join(dir, 'draft.md');
|
||||||
utils.createEmptyDirs([dir]);
|
utils.createEmptyDirs([ dir ]);
|
||||||
fs.writeFileSync(file, `
|
fs.writeFileSync(file, `
|
||||||
# Title with : info !
|
# Title with : info !
|
||||||

|

|
||||||
@@ -315,8 +315,11 @@ describe('Test article fetching', () => {
|
|||||||
const dir = path.join(dataDir, '2019', '05', '05');
|
const dir = path.join(dataDir, '2019', '05', '05');
|
||||||
const file = path.join(dir, testIndex);
|
const file = path.join(dir, testIndex);
|
||||||
const file2 = path.join(dir, 'draft.md');
|
const file2 = path.join(dir, 'draft.md');
|
||||||
utils.createEmptyDirs([dir]);
|
utils.createEmptyDirs([ dir ]);
|
||||||
utils.createEmptyFiles([file, file2]);
|
utils.createEmptyFiles([
|
||||||
|
file,
|
||||||
|
file2,
|
||||||
|
]);
|
||||||
const date = new Date(2019, 5, 5);
|
const date = new Date(2019, 5, 5);
|
||||||
date.setUTCHours(0);
|
date.setUTCHours(0);
|
||||||
fw.fetchArticles((err, dict) => {
|
fw.fetchArticles((err, dict) => {
|
||||||
|
|||||||
+11
-13
@@ -6,23 +6,23 @@ const dataDir = 'test_data';
|
|||||||
const file = path.join(dataDir, 'test.md');
|
const file = path.join(dataDir, 'test.md');
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
'test': true,
|
test: true,
|
||||||
'modules': {
|
modules: {
|
||||||
'prism': true,
|
'prism': true,
|
||||||
'mathjax': true,
|
'mathjax': true,
|
||||||
'plantuml': true,
|
'plantuml': true,
|
||||||
'fa-diagrams': true,
|
'fa-diagrams': true,
|
||||||
},
|
},
|
||||||
'showdown': {
|
showdown: {
|
||||||
'simplifiedAutoLink': true,
|
simplifiedAutoLink: true,
|
||||||
'smartIndentationFix': true,
|
smartIndentationFix: true,
|
||||||
},
|
},
|
||||||
'mathjax': {
|
mathjax: {
|
||||||
'output_format': 'html',
|
output_format: 'html',
|
||||||
'speak_text': false,
|
speak_text: false,
|
||||||
},
|
},
|
||||||
'plantuml': {
|
plantuml: {
|
||||||
'output_format': 'svg',
|
output_format: 'svg',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -47,9 +47,7 @@ describe('get parts', () => {
|
|||||||
test('normal', () => {
|
test('normal', () => {
|
||||||
const data = 'Hello\nthere\ngeneral\nkenobi';
|
const data = 'Hello\nthere\ngeneral\nkenobi';
|
||||||
const parts = renderer.getParts(data);
|
const parts = renderer.getParts(data);
|
||||||
expect(parts.map(p => p.text)).toEqual([
|
expect(parts.map(p => p.text)).toEqual([ 'Hello\nthere\ngeneral\nkenobi' ]);
|
||||||
'Hello\nthere\ngeneral\nkenobi',
|
|
||||||
]);
|
|
||||||
});
|
});
|
||||||
test('lot of stuff', () => {
|
test('lot of stuff', () => {
|
||||||
const data = 'Hello\nthere\n```code```\ngeneral<script>script</script>\n<script>script2</script>\n```<script>script3</script>```kenobi';
|
const data = 'Hello\nthere\n```code```\ngeneral<script>script</script>\n<script>script2</script>\n```<script>script3</script>```kenobi';
|
||||||
|
|||||||
+2
-2
@@ -14,7 +14,7 @@ const deleteFolderSync = (dir) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
do {
|
do {
|
||||||
items = fs.readdirSync(dir, {withFileTypes: true});
|
items = fs.readdirSync(dir, { withFileTypes: true });
|
||||||
try {
|
try {
|
||||||
items.forEach(deleteItem);
|
items.forEach(deleteItem);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -26,6 +26,6 @@ const deleteFolderSync = (dir) => {
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
deleteFolderSync: deleteFolderSync,
|
deleteFolderSync: deleteFolderSync,
|
||||||
createEmptyDirs: (list) => list.forEach((path) => fs.mkdirSync(path, {recursive: true})),
|
createEmptyDirs: (list) => list.forEach((path) => fs.mkdirSync(path, { recursive: true })),
|
||||||
createEmptyFiles: (list) => list.forEach((file) => fs.writeFileSync(file, '')),
|
createEmptyFiles: (list) => list.forEach((file) => fs.writeFileSync(file, '')),
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user