diff --git a/src/app.js b/src/app.js index 3c645b2..b7d432b 100644 --- a/src/app.js +++ b/src/app.js @@ -17,7 +17,6 @@ module.exports = (config) => { const articles = []; - const reload = (callback) => { fw.fetchArticles((err, list) => { if (err) { @@ -32,6 +31,8 @@ module.exports = (config) => { callback(true); }); }; + if (config['test']) + app.reload = reload; const render = (res, path, data, code = 200) => { res.render(path, data, (err, html) => { @@ -63,6 +64,13 @@ module.exports = (config) => { }); }); + app.get('*', (req, res, next) => { + if (config['home']['hidden'].includes(path.extname(req.path))) + showError(req.path, 404, res); + else + next(); + }); + app.get('*', express.static(config['data_dir'])); app.get('*', (req, res) => { diff --git a/src/config.default.json b/src/config.default.json index 7427b2c..ece0152 100644 --- a/src/config.default.json +++ b/src/config.default.json @@ -9,7 +9,8 @@ }, "home": { "index": "index.ejs", - "error": "error.ejs" + "error": "error.ejs", + "hidden": [".ejs"] }, "article": { "index": "index.md", diff --git a/test/app.test.js b/test/app.test.js index 62e4832..ae3b5de 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -14,8 +14,15 @@ const config = { 'view_engine': 'ejs', 'home': { 'index': testIndex, - 'error': testError - } + 'error': testError, + 'hidden': ['.ejs','.test'] + }, + 'article': { + 'index': 'index.md', + 'thumbnail_tag': 'thumbnail', + 'default_title': 'Untitled', + 'default_thumbnail': null + }, }; const app = require('../src/app')(config); @@ -46,15 +53,34 @@ describe('Test root path', () => { done(); }); }); - test('200 index page', (done) =>{ - fs.writeFileSync(path.join(dataDir,testIndex), 'hello there'); + test('200 no articles', (done) =>{ + fs.writeFileSync(path.join(dataDir,testIndex), 'articles <%= articles.length %>'); request(app).get('/').then((response) =>{ expect(response.statusCode).toBe(200); - expect(response.text).toBe('hello there'); + expect(response.text).toBe('articles 0'); done(); }); }); - //TODO test articles list + test('200 2 articles', (done) =>{ + utils.createEmptyDirs([ + path.join(dataDir, '2019', '05', '05'), + path.join(dataDir, '2018', '05', '05') + ]); + utils.createEmptyFiles([ + path.join(dataDir, '2019', '05', '05','index.md'), + path.join(dataDir, '2018', '05', '05','index.md') + ]); + fs.writeFileSync(path.join(dataDir,testIndex), 'articles <%= articles.length %>'); + app.reload((res) => { + expect(res).toBe(true); + request(app).get('/').then((response) =>{ + expect(response.statusCode).toBe(200); + expect(response.text).toBe('articles 2'); + done(); + }); + }); + + }); }); describe('Test static files', () => { @@ -72,6 +98,13 @@ describe('Test static files', () => { done(); }); }); + test('404 hidden file', (done) =>{ + fs.writeFileSync(path.join(dataDir,'somefile.test'), ''); + request(app).get('/somefile.test').then((response) =>{ + expect(response.statusCode).toBe(404); + done(); + }); + }); test('200 valid file', (done) =>{ fs.writeFileSync(`${dataDir}/somefile.txt`, 'filecontent'); request(app).get('/somefile.txt').then((response) =>{