RSS feed in app

This commit is contained in:
Clément GOUIN
2019-06-20 16:13:50 +02:00
parent 1fa8007e0e
commit 9cb601528e
9 changed files with 147 additions and 173 deletions
+74 -19
View File
@@ -9,30 +9,26 @@ const testIndex = 'testindex.ejs';
const testError = 'testerror.ejs';
const testTemplate = 'testtemplate.ejs';
const config = {
'test': true,
'data_dir': dataDir,
'view_engine': 'ejs',
'home': {
'index': testIndex,
'error': testError,
'hidden': ['.ejs', '.test']
},
'article': {
'index': 'index.md',
'template': testTemplate,
'thumbnail_tag': 'thumbnail',
'default_title': 'Untitled',
'default_thumbnail': null
},
'showdown': {}
};
const config = require('../src/config')();
config['test'] = true;
config['data_dir'] = dataDir;
config['home']['index'] = testIndex;
config['home']['error'] = testError;
config['article']['template'] = testTemplate;
config['home']['hidden'].push('.test');
config['rss']['endpoint'] = '/rsstest';
config['rss']['length'] = 2;
const app = require('../src/app')(config);
beforeEach(() => {
beforeEach((done) => {
utils.deleteFolderSync(dataDir);
fs.mkdirSync(dataDir);
app.reload((res) => {
expect(res).toBe(true);
done();
});
});
afterAll(() => {
@@ -86,6 +82,65 @@ describe('Test root path', () => {
});
});
describe('Test RSS feed', () => {
test('404 rss deactivated', (done) => {
config['modules']['rss'] = false;
request(app).get('/rsstest').then((response) => {
expect(response.statusCode).toBe(404);
config['modules']['rss'] = true;
done();
});
});
test('200 empty rss', (done) => {
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) => {
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')
]);
app.reload((res) => {
expect(res).toBe(true);
request(app).get('/rsstest').then((response) => {
expect(response.statusCode).toBe(200);
expect(response.text.length).toBeGreaterThan(0);
expect(response.text.split('<item>').length).toBe(3);
done();
});
});
});
test('200 max rss items', (done) => {
utils.createEmptyDirs([
path.join(dataDir, '2019', '05', '05'),
path.join(dataDir, '2018', '05', '05'),
path.join(dataDir, '2017', '05', '05')
]);
utils.createEmptyFiles([
path.join(dataDir, '2019', '05', '05', 'index.md'),
path.join(dataDir, '2018', '05', '05', 'index.md'),
path.join(dataDir, '2017', '05', '05', 'index.md')
]);
app.reload((res) => {
expect(res).toBe(true);
request(app).get('/rsstest').then((response) => {
expect(response.statusCode).toBe(200);
expect(response.text.length).toBeGreaterThan(0);
expect(response.text.split('<item>').length).toBe(3);
done();
});
});
});
});
describe('Test articles rendering', () => {
test('404 article not found', (done) => {
request(app).get('/2019/05/06/untitled/').then((response) => {
+6 -2
View File
@@ -220,6 +220,8 @@ describe('Test article fetching', () => {
const file = path.join(dir, testIndex);
utils.createEmptyDirs([dir]);
utils.createEmptyFiles([file]);
const date = new Date(2019, 5, 5);
date.setUTCHours(0);
fw.fetchArticles((err, dict) => {
expect(err).toBeNull();
expect(dict).toBeDefined();
@@ -230,7 +232,7 @@ describe('Test article fetching', () => {
year: 2019,
month: 5,
day: 5,
date: new Date(2019, 5, 5),
date: date,
title: 'Untitled',
thumbnail: 'default.png',
escapedTitle: 'untitled',
@@ -248,6 +250,8 @@ describe('Test article fetching', () => {
![thumbnail](./thumbnail.jpg)
this is some text
`);
const date = new Date(2019, 5, 5);
date.setUTCHours(0);
fw.fetchArticles((err, dict) => {
expect(err).toBeNull();
expect(dict).toBeDefined();
@@ -258,7 +262,7 @@ describe('Test article fetching', () => {
year: 2019,
month: 5,
day: 5,
date: new Date(2019, 5, 5),
date: date,
title: 'Title with : info !',
thumbnail: path.join('2019', '05', '05', './thumbnail.jpg'),
escapedTitle: 'title_with___info',
-100
View File
@@ -1,100 +0,0 @@
/* jshint -W117 */
const config = {
'loopback_url': 'http://test.test/',
'rss': {
'title': 'test rss',
'description': 'description',
'endpoint': '/rss',
'length': 2
},
};
const rss = require('../src/rss')(config);
test('empty rss', () => {
const xml = rss.get({});
expect(xml).toBe('<?xml version="1.0" encoding="UTF-8"?>' +
'<rss version="2.0">' +
'<title>test rss</title>' +
'<description>description</description>' +
'<link>http://test.test/</link>' +
'<lastBuildDate>' + new Date().toString() + '</lastBuildDate>' +
'<lastPubDate>' + new Date().toString() + '</lastPubDate>' +
'<ttl>60</ttl>' +
'</rss>');
});
test('1 item', () => {
const data = {
'a': {
path: 'a',
realPath: 'b',
year: 2019,
month: 5,
day: 5,
date: new Date(2019, 5, 5),
title: 'Title with : info !',
thumbnail: '/2019/05/05/thumbnail.jpg/',
escapedTitle: 'title_with___info',
url: '/2019/05/05/title_with___info/',
}
};
const xml = rss.get(data);
expect(xml).toEqual('<?xml version="1.0" encoding="UTF-8"?>' +
'<rss version="2.0">' +
'<title>test rss</title>' +
'<description>description</description>' +
'<link>http://test.test/</link>' +
'<lastBuildDate>' + new Date().toString() + '</lastBuildDate>' +
'<lastPubDate>' + new Date().toString() + '</lastPubDate>' +
'<ttl>60</ttl>' +
'<item>' +
'<title>Title with : info !</title>' +
'<link>http://test.test/2019/05/05/title_with___info/</link>' +
'<pubDate>' + new Date(2019, 5, 5).toString() + '</pubDate>' +
'</item>' +
'</rss>');
});
test('3 items only 2 shown sorted', () => {
const data = {
'a': {
path: '2019/05/05',
date: new Date(2019, 5, 5),
title: 'a',
url: 'a',
},
'b': {
path: '2018/05/05',
date: new Date(2018, 5, 5),
title: 'b',
url: 'b',
},
'c': {
path: '2020/05/05',
date: new Date(2020, 5, 5),
title: 'c',
url: 'c',
}
};
const xml = rss.get(data);
expect(xml).toEqual('<?xml version="1.0" encoding="UTF-8"?>' +
'<rss version="2.0">' +
'<title>test rss</title>' +
'<description>description</description>' +
'<link>http://test.test/</link>' +
'<lastBuildDate>' + new Date().toString() + '</lastBuildDate>' +
'<lastPubDate>' + new Date().toString() + '</lastPubDate>' +
'<ttl>60</ttl>' +
'<item>' +
'<title>c</title>' +
'<link>http://test.test/c</link>' +
'<pubDate>' + new Date(2020, 5, 5).toString() + '</pubDate>' +
'</item>' +
'<item>' +
'<title>a</title>' +
'<link>http://test.test/a</link>' +
'<pubDate>' + new Date(2019, 5, 5).toString() + '</pubDate>' +
'</item>' +
'</rss>');
});