diff --git a/src/app.js b/src/app.js index f5f1ef2..2f34a07 100644 --- a/src/app.js +++ b/src/app.js @@ -2,9 +2,14 @@ const express = require('express'); const app = express(); const fs = require('fs'); const path = require('path'); + +//rss const Rss = require('rss'); + +///webhook const bodyParser = require('body-parser'); const crypto = require('crypto'); +const cp = require('child_process'); app.use(bodyParser.json()); /** @@ -142,8 +147,15 @@ module.exports = (config) => { return res.sendStatus(403); } } - res.sendStatus(200); - //TODO reload + cp.exec(config['webhook']['pull_command'], {cwd: path.join(__dirname, '..', config['data_dir'])}, (err) => { + if (err) { + console.log(cons.error, `command '${config['webhook']['pull_command']}' failed : ${err}`); + return res.sendStatus(500); + } + reload(() => { + res.sendStatus(200); + }); + }); } else { res.sendStatus(400); } diff --git a/src/config.default.json b/src/config.default.json index f138c22..a959260 100644 --- a/src/config.default.json +++ b/src/config.default.json @@ -31,7 +31,8 @@ "webhook": { "endpoint": "/webhook", "secret": "", - "signature_header": "" + "signature_header": "", + "pull_command": "git pull" }, "showdown": { "parseImgDimensions": true, diff --git a/test/app.test.js b/test/app.test.js index 1ca5f06..730d9e8 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -110,8 +110,8 @@ describe('Test RSS feed', () => { expect(response.text.length).toBeGreaterThan(0); expect(response.text.split('').length).toBe(3); done(); - }, fail); - }); + }); + }, fail); }); test('200 max rss items', (done, fail) => { utils.createEmptyDirs([ @@ -130,8 +130,8 @@ describe('Test RSS feed', () => { expect(response.text.length).toBeGreaterThan(0); expect(response.text.split('').length).toBe(3); done(); - }, fail); - }); + }); + }, fail); }); }); @@ -145,9 +145,29 @@ describe('Test webhook', () => { }); }); test('200 no secret', (done) => { + utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]); + utils.createEmptyFiles([ + path.join(dataDir, '2019', '05', '05', 'index.md'), + path.join(dataDir, testTemplate) + ]); + config['webhook']['pull_command'] = 'git --help'; request(app).post('/webhooktest').then((response) => { expect(response.statusCode).toBe(200); - //TODO test reload + request(app).get('/2019/05/05/').then((response) => { + expect(response.statusCode).toBe(200); + done(); + }); + }); + }); + test('500 command failed', (done) => { + utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]); + utils.createEmptyFiles([ + path.join(dataDir, '2019', '05', '05', 'index.md'), + path.join(dataDir, testTemplate) + ]); + config['webhook']['pull_command'] = 'qzgfqgqz'; + request(app).post('/webhooktest').then((response) => { + expect(response.statusCode).toBe(500); done(); }); }); @@ -170,12 +190,12 @@ describe('Test webhook', () => { test('200 valid secret', (done) => { config['webhook']['signature_header'] = 'testheader'; config['webhook']['secret'] = 'testvalue'; + config['webhook']['pull_command'] = 'git --help'; request(app).post('/webhooktest') .send({}) .set('testheader', 'sha1=d924d5bd4b36faf9d572844ac9c12a09ce3e7134') .then((response) => { expect(response.statusCode).toBe(200); - //TODO test reload done(); }); }); @@ -196,8 +216,8 @@ describe('Test articles rendering', () => { request(app).get('/2019/05/05/hello/').then((response) => { expect(response.statusCode).toBe(500); done(); - }, fail); - }); + }); + }, fail); }); test('200 rendered article', (done, fail) => { @@ -209,8 +229,8 @@ describe('Test articles rendering', () => { expect(response.statusCode).toBe(200); expect(response.text).toBe('

Hello

reload'); done(); - }, fail); - }); + }); + }, fail); }); test('200 other url', (done, fail) => { @@ -223,8 +243,8 @@ describe('Test articles rendering', () => { request(app).get('/2019/05/05/anything/').then((response) => { expect(response.statusCode).toBe(200); done(); - }, fail); - }); + }); + }, fail); }); test('200 other url 2', (done, fail) => { @@ -237,8 +257,8 @@ describe('Test articles rendering', () => { request(app).get('/2019/05/05/').then((response) => { expect(response.statusCode).toBe(200); done(); - }, fail); - }); + }); + }, fail); }); });