UNPKG

tm-apps-list-api

Version:

132 lines (107 loc) 4.17 kB
'use strict'; //process.setMaxListeners(0); const assert = require('chai').assert; const hl = require('highland'); const db = require('tm-apps-db')(require('config').db); const R = require('ramda'); let rewire = require("rewire"); let app = rewire("../../src/server"); let request = require("supertest"); let config = require("config"); let nock = require("nock"); let server = request(app(config, void 0, db)); const createGenericArticleAuthor = R.curry((publication_id, author_id, tuple) => { const published_date = tuple[1]; const article_id = tuple[0]; const article_author = { author_id, name: 'Mirror Chicken', article_id, publication_id, published_date }; return hl(db.article_author.upsert(article_author)); }); const createArticleAuthor = createGenericArticleAuthor('nationals.2','nationals.35572'); const createArticleAuthorWrongPub = createGenericArticleAuthor('nationals.32','nationals.35572'); const createBasicArticle = id => { return { "data": { "attributes": { }, "id": Number(id) } }; }; function randomDate(id) { const now = new Date(); const since = new Date(2016, 10, 10); return [id, new Date(since.getTime() + Math.random() * (now.getTime() - since.getTime()))]; } before(() => { nock("http://apps-test-endpoint.com") .persist() .get(/\/(?:nationals|regionals)\/\d+\/articles\/(\d+)/) .reply(200, uri => createBasicArticle(R.match(/\/(?:nationals|regionals)\/\d+\/articles\/(\d+)/, uri)[1])); }); after(() => { nock.restore(); }); describe("Authors Component", () => { before(function (done) { this.timeout(6000); db.sequelize.sync({force: true}).then(function () { done(); }); }); const arrayOfIdsAndDates = R.range(200, 260).map(integer => `nationals.${integer}`).map(randomDate); const sortedIds = R.take(50, R.sort((tuple1, tuple2) => { return tuple2[1] - tuple1[1]; }, arrayOfIdsAndDates)); before(done => { const createArticleAuthorStreams = arrayOfIdsAndDates.map(createArticleAuthor); let now = new Date(); hl(createArticleAuthorStreams) .merge() .collect() // right publication, wrong author .flatMap(() => createGenericArticleAuthor('nationals.2','nationals.12345', ['nationals.10000', new Date()])) // right pubblication, right author but in the future .flatMap( () => createArticleAuthor([ "nationals.20000", now.setDate(now.getDate() + 14)]) ) // right author, wrong publication .map( () => R.range(5000,5050)) .sequence() .flatMap(x => createArticleAuthorWrongPub(randomDate(`nationals.${x}`))) .collect() .toCallback(done); }); it("Gets the latest articles given an author id", done => { server .get("/nationals/2/authors/35572") .end((err, res) => { // check that articles published by other authors aren't included assert.notEqual(res.body.included[0].data.id, 10000); // check that articles published in the future aren't included assert.notEqual(res.body.included[0].data.id, 20000); assert.equal(res.header['content-type'], 'application/json; charset=utf-8'); assert.equal(res.body.data.type, "articles"); const cached = res.body.included.length; for (let i = 0; i < cached; i += 1) { assert.equal(`nationals.${res.body.included[i].data.id}`, sortedIds[i][0]); } done(); }); }); it("Handles 404s correctly", done => { server .get("/nationals/2/authors/6814768416841") .end((err, res) => { assert.equal(res.body.message, "nationals/2/authors/6814768416841 not available"); assert.equal(res.body.code, "NotFoundError"); done(); }); }); });