UNPKG

tm-apps-list-api

Version:

289 lines (240 loc) 10.2 kB
"use strict"; let sinon = require("sinon"); let assert = require("chai").assert; let hl = require("highland"); let R = require("ramda"); let utils = require("../../src/utils"); let nock = require("nock"); describe("List Utils", () => { describe('setCacheEndpoint', () => { it('returns the same url for dev endpoints', () => { const actual = utils.setCacheEndpoint('apps-api-prod.tm-dev-awx.com'); assert.equal(actual, 'apps-api-prod.tm-dev-awx.com'); }); it("returns orgles url for prod when it isn't there", () => { const actual = utils.setCacheEndpoint('apps-api-prod.tm-awx.com'); assert.equal(actual, 'apps-api-prod.tm-awx.com'); }); it('returns orgles url for prod when it is there', () => { const actual = utils.setCacheEndpoint('org-apps-api-prod.tm-awx.com'); assert.equal(actual, 'apps-api-prod.tm-awx.com'); }); }); describe("addMeta", () => { it("adds the basic meta to the list", () => { const actual = utils.addMeta([]); assert.equal(actual.data.type, "articles"); assert.equal(R.test(/^\d{10}000$/, actual.data.attributes.timestamp), true); assert.deepEqual(actual.included, []); }); }); describe("sendResponse()", () => { const res = { "charSet": sinon.stub(), "send": sinon.stub() }; const logger = { "error": sinon.stub(), "timeEnd": sinon.stub() }; const next = sinon.stub(); beforeEach(() => { res.send.reset(); logger.error.reset(); next.reset(); }); it("send a 200 response if no error and there is data", () => { utils.sendResponse(logger, res, next)(null, "result"); assert.equal(logger.timeEnd.callCount, 1); assert.isTrue(logger.timeEnd.calledWith("Finished Sections Request")); assert.equal(res.send.callCount, 1); assert.isTrue(res.send.calledWith("result")); }); it("sends a specific status (404 in this case)", () => { const error = { "status": "NotFoundError", "message": "404 error" }; utils.sendResponse(logger, res, next)(error); assert.equal(logger.error.callCount, 1); assert.isTrue(logger.error.calledWith("Sections Request Error")); assert.equal(next.firstCall.args[0].body.code, "NotFoundError"); assert.equal(next.firstCall.args[0].body.message, "404 error"); }); it("called with a different error", () => { utils.sendResponse(logger, res, next)("Another error"); assert.equal(logger.error.callCount, 1); assert.isTrue(logger.error.calledWith("Sections Request Error")); assert.equal(next.firstCall.args[0].body.code, "InternalServerError"); }); }); describe("notFound()", () => { it("pushes an error onto stream and logs", done => { utils.notFound("404 message") .toCallback(err => { assert.equal(err.message, "404 message"); assert.equal(err.status, "NotFoundError"); done(); }); }); }); describe("getArticlesInParallel()", () => { let logger = { "error": sinon.stub() }; let getArticleStub = sinon.stub(); let getArticle = x => { getArticleStub(x); return isNaN(x) ? hl([null]) : hl([x]); }; let notFound = sinon.stub(); beforeEach(() => { logger.error.reset(); notFound.reset(); getArticleStub.reset(); }); it("streams the data", done => { utils.getArticlesInParallel(logger, getArticle, notFound, hl(["nationals.1"])) .toCallback((err, res) => { assert.equal(notFound.callCount, 0); assert.equal(res[0], 1); done(); }); }); it("Only calls getArticle 40 times, even if more than 40 ids", done => { const ids = R.range(1, 41) .map(id => `nationals.${id}`); utils.getArticlesInParallel(logger, getArticle, notFound, hl(ids)) .toCallback((err, res) => { assert.equal(notFound.callCount, 0); assert.equal(getArticleStub.callCount, 40); assert.equal(res.length, 40); done(); }); }); it("filters out articles that not found", done => { const ids = R.range(1, 41) .map(id => `nationals.${id}`); ids[1] = "nationals.null"; utils.getArticlesInParallel(logger, getArticle, notFound, hl(ids)) .toCallback((err, res) => { assert.equal(notFound.callCount, 0); assert.equal(getArticleStub.callCount, 40); assert.equal(res.length, 39); done(); }); }); it("if there is an error from getArticles, it's filtered out and an error is logged", done => { const ids = R.range(1, 41) .map(id => `nationals.${id}`); ids[1] = "nationals.null"; const erroringGetArticle = x => { getArticleStub(x); if (isNaN(x)) throw "error"; else return hl([x]); }; utils.getArticlesInParallel(logger, erroringGetArticle, notFound, hl(ids)) .toCallback((err, res) => { assert.equal(notFound.callCount, 0); assert.equal(getArticleStub.callCount, 40); assert.equal(res.length, 39); assert.equal(logger.error.callCount, 1); assert.equal(logger.error.firstCall.args[1], "error"); assert.equal(logger.error.firstCall.args[0], "Sections Request. Error retrieving article"); done(); }); }); it("if there are duplicate ids should be filtered out", done => { const ids = [1,2,1,3,4,5].map(id => `nationals.${id}`); utils.getArticlesInParallel(logger, getArticle, notFound, hl(ids)) .toCallback((err, res) => { assert.deepEqual(res, [1,2,3,4,5]); done(); }); }); }); describe("makeArticleRequest()", function() { this.timeout(5000); const logger = { "error": sinon.stub(), "timeEnd": sinon.stub() }; beforeEach(() => { logger.error.reset(); }); it("makes a requeset to the articles endpoint", done => { nock("http://url_to_articles.com") .get("/nationals/2/articles/1") .reply(200, { "data": "success" }); utils.makeArticleRequest(logger, "http://url_to_articles.com", "nationals", 2)(1) .tap(res => assert.equal(res.data, "success")) .toCallback(done); }); it("should timeout after 3 seconds", done => { nock("http://url_to_articles.com") .get("/nationals/2/articles/123") .delay(4000) .reply(200, { "data": "success" }); utils.makeArticleRequest(logger, "http://url_to_articles.com", "nationals", 2)(123) .toCallback((err, res) => { assert.isUndefined(res); assert.strictEqual(err.code, 'ECONNABORTED'); assert.strictEqual(err.message, 'Timeout of 3000ms exceeded'); const loggerArgs = logger.error.firstCall.args; assert.strictEqual(loggerArgs[0], 'error requesting article'); assert.strictEqual(loggerArgs[1].err_msg, 'Timeout of 3000ms exceeded'); assert.strictEqual(loggerArgs[1].article_id, 123); done(); }); }); it("sends the correct error on 404", done => { nock("http://url_to_articles.com") .get("/nationals/2/articles/1") .reply(404, { "status": "NotFoundError", "message": "404 error" }); utils.makeArticleRequest(logger, "http://url_to_articles.com", "nationals", 2)(1) .toCallback(err => { assert.equal(err.message, "404 error"); assert.equal(err.status, "NotFoundError"); done(); }); }); it("sends the correct error on other errors", done => { utils.makeArticleRequest(logger, "http://url_to_articles.com", "nationals", 2)(1) .toCallback(err => { assert.equal(err.message, "Nock: No match for request GET http://url_to_articles.com/nationals/2/articles/1 "); done(); }); }); }); describe("shouldBeExcluded()", () => { it("should return true if article has exclude true flag", () => { const article = { data: { attributes: { exclude: true } }, included: [] }; assert.isTrue(utils.shouldBeExcluded(article)); }); it("should return false if article has exclude false flag", () => { const article = { data: { attributes: { exclude: false } }, included: [] }; assert.isFalse(utils.shouldBeExcluded(article)); }); }); });