2021-01-30 14:02:59 +01:00
|
|
|
// external lib
|
|
|
|
const Parser = require("rss-parser");
|
|
|
|
|
|
|
|
// tested class
|
2021-02-07 13:01:48 +01:00
|
|
|
/*const ListenerRssPackage = require("../index");
|
2021-01-30 14:02:59 +01:00
|
|
|
|
|
|
|
const Listeners = ListenerRssPackage.ListenerRss
|
2021-02-07 13:01:48 +01:00
|
|
|
const ListenerRRSInfo = ListenerRssPackage.ListenerRssInfos*/
|
|
|
|
const Listeners = require('../src/ListenerRss')
|
|
|
|
const ListenerRRSInfo = require('../src/Models/ListenerRSSInfos')
|
2021-01-30 14:02:59 +01:00
|
|
|
|
|
|
|
// Unit test
|
2020-12-20 18:53:35 +01:00
|
|
|
const chai = require("chai");
|
|
|
|
const sinon = require("sinon");
|
2021-01-19 13:12:44 +01:00
|
|
|
const sinon_chai = require("sinon-chai");
|
|
|
|
chai.use(sinon_chai);
|
|
|
|
|
|
|
|
|
2020-12-20 18:53:35 +01:00
|
|
|
const expect = chai.expect;
|
|
|
|
|
2021-01-19 13:12:44 +01:00
|
|
|
describe("test class RSS: jsonfile", function () {
|
|
|
|
let myListener = undefined;
|
|
|
|
|
2021-02-07 13:01:48 +01:00
|
|
|
const infosListener = new ListenerRRSInfo('my-test-service', 'fake.rss.service', 15, [
|
|
|
|
['description', ['media:group', 'media:description']],
|
|
|
|
['icon', ['media:group', 'media:thumbnail']]
|
|
|
|
]);
|
2021-01-19 13:12:44 +01:00
|
|
|
|
|
|
|
// parseURL tests
|
|
|
|
let stubParser;
|
|
|
|
|
|
|
|
const mockedRSSOutput = {
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
'title': 'my title 00',
|
|
|
|
'media:group': {
|
|
|
|
'media:description': 'my description 00',
|
|
|
|
'media:thumbnail': [{$: {height: 360, width: 420, url: 'my_image00.jpg'}}],
|
|
|
|
},
|
|
|
|
'link': 'my_url_00.com',
|
|
|
|
'pubDate': 'myDate00'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'title': 'my title 01',
|
|
|
|
'media:group' : {
|
|
|
|
'media:description': 'my description 01',
|
|
|
|
'media:thumbnail': [{$: { height: 360, width: 420, url: 'my_image01.jpg'}}],
|
|
|
|
},
|
|
|
|
'link': 'my_url_01.com',
|
|
|
|
'pubDate': 'myDate01'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'title': 'my title 02',
|
|
|
|
'media:group' : {
|
|
|
|
'media:description': 'my description 02',
|
|
|
|
'media:thumbnail': [{$: { height: 360, width: 420, url: 'my_image02.jpg'}}],
|
|
|
|
},
|
|
|
|
'link': 'my_url_02.com',
|
|
|
|
'pubDate': 'myDate02'
|
|
|
|
},
|
|
|
|
]
|
2020-12-20 18:53:35 +01:00
|
|
|
};
|
|
|
|
|
2021-01-19 13:12:44 +01:00
|
|
|
beforeEach(function () {
|
|
|
|
// stubs
|
2021-02-07 13:01:48 +01:00
|
|
|
stubParser = sinon.stub(Parser.prototype, 'parseURL');
|
|
|
|
stubParser.withArgs(infosListener.address)
|
2021-01-19 13:12:44 +01:00
|
|
|
.resolves(mockedRSSOutput);
|
2021-02-07 13:01:48 +01:00
|
|
|
stubParser.withArgs('bad.rss.service')
|
|
|
|
.resolves(new Error('connect ECONNREFUSED 127.0.0.1:80'));
|
2021-01-19 13:12:44 +01:00
|
|
|
|
|
|
|
// constructor
|
2021-01-30 14:02:59 +01:00
|
|
|
myListener = undefined;
|
2021-01-19 13:12:44 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
// restore stubs
|
|
|
|
Parser.prototype.parseURL.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("Building Ytb listener", function () {
|
2021-02-07 13:01:48 +01:00
|
|
|
it("The build without issues (infosListener parameters)", function () {
|
|
|
|
myListener = new Listeners(infosListener);
|
|
|
|
|
|
|
|
// assertions
|
|
|
|
// myListener data
|
|
|
|
expect(myListener.timeloop).to.eql(15);
|
|
|
|
expect(myListener.name).to.eql('my-test-service');
|
|
|
|
expect(myListener.address).to.eql('fake.rss.service');
|
|
|
|
expect(myListener.customfields).to.eql([
|
|
|
|
['description', ['media:group', 'media:description']],
|
|
|
|
['icon', ['media:group', 'media:thumbnail']]
|
|
|
|
]);
|
|
|
|
expect(myListener.parser.options.customFields).to.eql({
|
|
|
|
feed: [],
|
|
|
|
item: [
|
|
|
|
'media:group',
|
|
|
|
'media:group'
|
|
|
|
]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it("The build without issues (raw infos : 4 params)", function () {
|
|
|
|
myListener = new Listeners('my-test-service', 'fake.rss.service', 15, [
|
|
|
|
['description', ['media:group', 'media:description']],
|
|
|
|
['icon', ['media:group', 'media:thumbnail']]
|
|
|
|
]);
|
2021-01-19 13:12:44 +01:00
|
|
|
|
|
|
|
// assertions
|
2021-01-30 14:02:59 +01:00
|
|
|
// myListener data
|
2021-02-07 13:01:48 +01:00
|
|
|
expect(myListener.timeloop).to.eql(15);
|
|
|
|
expect(myListener.name).to.eql('my-test-service');
|
|
|
|
expect(myListener.address).to.eql('fake.rss.service');
|
|
|
|
expect(myListener.customfields).to.eql([
|
|
|
|
['description', ['media:group', 'media:description']],
|
|
|
|
['icon', ['media:group', 'media:thumbnail']]
|
|
|
|
]);
|
|
|
|
expect(myListener.parser.options.customFields).to.eql({
|
|
|
|
feed: [],
|
|
|
|
item: [
|
|
|
|
'media:group',
|
|
|
|
'media:group'
|
|
|
|
]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it("The build without issues (raw infos : just 2 params)", function () {
|
|
|
|
myListener = new Listeners('my-test-service', 'fake.rss.service');
|
|
|
|
|
|
|
|
// assertions
|
|
|
|
// myListener data
|
|
|
|
expect(myListener.timeloop).to.eql(5*60);
|
|
|
|
expect(myListener.name).to.eql('my-test-service');
|
|
|
|
expect(myListener.address).to.eql('fake.rss.service');
|
|
|
|
expect(myListener.customfields).to.eql([]);
|
|
|
|
expect(myListener.parser.options.customFields).to.eql({
|
|
|
|
feed: [],
|
|
|
|
item: []
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it("The build without issues (raw infos : just 3 params (no custom fields))", function () {
|
|
|
|
myListener = new Listeners('my-test-service', 'fake.rss.service', 15);
|
|
|
|
|
|
|
|
// assertions
|
|
|
|
// myListener data
|
|
|
|
expect(myListener.timeloop).to.eql(15);
|
|
|
|
expect(myListener.name).to.eql('my-test-service');
|
|
|
|
expect(myListener.address).to.eql('fake.rss.service');
|
|
|
|
expect(myListener.customfields).to.eql([]);
|
|
|
|
expect(myListener.parser.options.customFields).to.eql({
|
|
|
|
feed: [],
|
|
|
|
item: []
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it("The build without issues (raw infos : just 3 params (no timeloop))", function () {
|
|
|
|
myListener = new Listeners('my-test-service', 'fake.rss.service', undefined, [
|
|
|
|
['description', ['media:group', 'media:description']],
|
|
|
|
['icon', ['media:group', 'media:thumbnail']]
|
|
|
|
]);
|
|
|
|
|
|
|
|
// assertions
|
|
|
|
// myListener data
|
|
|
|
expect(myListener.timeloop).to.eql(5*60);
|
|
|
|
expect(myListener.name).to.eql('my-test-service');
|
|
|
|
expect(myListener.address).to.eql('fake.rss.service');
|
2021-01-30 14:02:59 +01:00
|
|
|
expect(myListener.customfields).to.eql([
|
2021-01-19 13:12:44 +01:00
|
|
|
['description', ['media:group', 'media:description']],
|
|
|
|
['icon', ['media:group', 'media:thumbnail']]
|
|
|
|
]);
|
2021-02-07 13:01:48 +01:00
|
|
|
expect(myListener.parser.options.customFields).to.eql({
|
|
|
|
feed: [],
|
|
|
|
item: [
|
|
|
|
'media:group',
|
|
|
|
'media:group'
|
|
|
|
]
|
|
|
|
});
|
2021-01-19 13:12:44 +01:00
|
|
|
});
|
2020-12-20 18:53:35 +01:00
|
|
|
});
|
|
|
|
|
2021-01-30 14:02:59 +01:00
|
|
|
describe("fetch some data", function () {
|
2021-02-07 13:01:48 +01:00
|
|
|
it("fetch without issues", function () {
|
2021-01-30 14:02:59 +01:00
|
|
|
// classic build
|
2021-02-07 13:01:48 +01:00
|
|
|
myListener = new Listeners(infosListener);
|
|
|
|
// fetch
|
|
|
|
let res = myListener.fetchRSS();
|
2021-01-30 14:02:59 +01:00
|
|
|
|
|
|
|
//assertion
|
2021-02-07 13:01:48 +01:00
|
|
|
// calls
|
2021-01-30 14:02:59 +01:00
|
|
|
expect(stubParser).to.have.been.calledOnce;
|
|
|
|
expect(stubParser).to.have.been.calledWith(infosListener._address);
|
2021-02-07 13:01:48 +01:00
|
|
|
// Promise
|
|
|
|
//await expect(Promise.resolve(res)).to.be.eql(mockedRSSOutput);
|
|
|
|
res.then((obj, err) => {
|
|
|
|
expect(obj).to.be.eql(mockedRSSOutput);
|
|
|
|
expect(err).to.be.undefined
|
|
|
|
})
|
|
|
|
})
|
|
|
|
it("fetch with bad address", function () {
|
|
|
|
// classic build
|
|
|
|
myListener = new Listeners('my-test-service', 'bad.rss.service', undefined, [
|
|
|
|
['description', ['media:group', 'media:description']],
|
|
|
|
['icon', ['media:group', 'media:thumbnail']]
|
|
|
|
]);
|
|
|
|
// fetch
|
|
|
|
let res = myListener.fetchRSS();
|
|
|
|
|
|
|
|
//assertion
|
|
|
|
// calls
|
|
|
|
expect(stubParser).to.have.been.calledOnce;
|
|
|
|
expect(stubParser).to.have.been.calledWith('bad.rss.service');
|
|
|
|
// Promise
|
|
|
|
res.then((obj, err) => {
|
|
|
|
expect(obj).to.be.undefined
|
|
|
|
expect(err).to.be.eql(new Error('connect ECONNREFUSED 127.0.0.1:80'))
|
|
|
|
});
|
2021-01-30 14:02:59 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-02-07 13:01:48 +01:00
|
|
|
describe("start", function () {
|
|
|
|
it("Let's start the timer", async function () {
|
|
|
|
//custom timeout
|
|
|
|
this.timeout(15000);
|
|
|
|
|
|
|
|
// classic build
|
|
|
|
myListener = new Listeners('my-test-service', 'fake.rss.service', 2, [
|
|
|
|
['description', ['media:group', 'media:description']],
|
|
|
|
['icon', ['media:group', 'media:thumbnail']]
|
|
|
|
]);
|
|
|
|
|
|
|
|
//spy
|
|
|
|
const fun_spy = sinon.spy();
|
2021-01-19 13:12:44 +01:00
|
|
|
|
2021-02-07 13:01:48 +01:00
|
|
|
// start timer
|
|
|
|
myListener.start(fun_spy);
|
2021-01-19 13:12:44 +01:00
|
|
|
|
2021-02-07 13:01:48 +01:00
|
|
|
await new Promise(res => setTimeout(res, 5 * 1000));
|
|
|
|
|
|
|
|
myListener.stop();
|
|
|
|
|
|
|
|
//assertion
|
|
|
|
// calls
|
|
|
|
expect(1).to.be.eql(1);
|
|
|
|
expect(fun_spy).to.have.been.callCount(3);
|
|
|
|
expect(fun_spy).to.have.been.calledWith(mockedRSSOutput, undefined);
|
|
|
|
});
|
|
|
|
it("Let's start the timer (with a bad address)", async function () {
|
|
|
|
//custom timeout
|
|
|
|
this.timeout(15000)
|
|
|
|
|
|
|
|
// classic build
|
|
|
|
myListener = new Listeners('my-test-service', 'bad.rss.service', 2, [
|
|
|
|
['description', ['media:group', 'media:description']],
|
|
|
|
['icon', ['media:group', 'media:thumbnail']]
|
|
|
|
]);
|
|
|
|
|
|
|
|
//spy
|
|
|
|
const fun_spy = sinon.spy();
|
|
|
|
|
|
|
|
// start timer
|
|
|
|
myListener.start(fun_spy);
|
|
|
|
|
|
|
|
await new Promise(res => setTimeout(res, 5 * 1000));
|
|
|
|
|
|
|
|
myListener.stop();
|
|
|
|
//assertion
|
|
|
|
// calls
|
|
|
|
expect(1).to.be.eql(1);
|
|
|
|
expect(fun_spy).to.have.been.callCount(3);
|
|
|
|
expect(fun_spy).to.have.been.calledWith(undefined, Error); //yagni
|
2021-01-19 13:12:44 +01:00
|
|
|
});
|
|
|
|
});
|
2020-12-20 18:53:35 +01:00
|
|
|
});
|