UNPKG

@tlrg/middleware-js

Version:
148 lines (124 loc) 4.34 kB
require('chai').should(); var request = require('supertest'); var express = require('express'); var cookieParser = require('cookie-parser'); var proxyquire = require('proxyquire').noCallThru(); var Promise = require('bluebird'); var mockLogger = function () { return { info: function () {} }; }; function createCookie (name, valueObj) { var cookie = name + '='; if (typeof valueObj === 'string') { return cookie += valueObj; } return cookie += encodeURIComponent(JSON.stringify(valueObj)); } describe('Predictive Analytics Middleware', function () { var app; function makeFake (fake) { var fakeAnalytics = proxyquire('../lib/middlewares/predictiveAnalytics', { 'request-promise': function () { return fake || Promise.resolve('ABC'); }, 'tlrg-logger': mockLogger }); app.get('/', fakeAnalytics, function (req, res) { res.status(200).json(req.moonstick); }); } beforeEach(function () { app = express(); app.use(cookieParser()); app.use(function moonstickSetup (req, res, next) { req.moonstick = req.moonstick || {}; next(); }); }); it('it should set a req.moonstick.predictiveAnalytics if it is not set', function (done) { makeFake(); request(app) .get('/') .expect(200) .expect({ predictiveAnalytics: 'ABC' }, done); }); it('it should not set req.moonstick.predictiveAnalytics if it is set', function (done) { app.use(function moonstickSetup (req, res, next) { req.cookies.PACookie = 'Cookie Set'; next(); }); makeFake(); request(app) .get('/') .expect(200) .expect({ predictiveAnalytics: 'Cookie Set' }, done); }); it('it should not set req.moonstick.predictiveAnalytics if its already set and the API doesn`t work', function (done) { app.use(function moonstickSetup (req, res, next) { req.cookies.PACookie = 'Cookie Set'; next(); }); makeFake(Promise.reject('Error from external API')); request(app) .get('/') .expect(200) .expect({ predictiveAnalytics: 'Cookie Set' }, done); }); it('it should not set req.moonstick.predictiveAnalytics if its not set and the API doesn`t work', function (done) { makeFake(Promise.reject('Error from external API')); request(app) .get('/') .expect(200) .expect({}, done); }); it('it should set a cookie if it is not set', function (done) { makeFake(); request(app) .get('/') .expect(function (res) { /PACookie=ABC/.test(res.headers['set-cookie']).should.equal(true); }) .expect(200, done); }); it('it should not set a cookie if it is already set', function (done) { makeFake(); request(app) .get('/') .set({ Cookie: createCookie('PACookie', 'Cookiepreset') }) .expect(function (res) { /PACookie=ABC/.test(res.headers['set-cookie']).should.equal(false); }) .expect(200, done); }); it('it should not set a cookie if its already set and the API doesn`t work', function (done) { makeFake(Promise.reject('Error from external API')); request(app) .get('/') .set({ Cookie: createCookie('PACookie', 'Cookiepreset') }) .expect(function (res) { /PACookie=ABC/.test(res.headers['set-cookie']).should.equal(false); }) .expect(200, done); }); it('it should not set a cookie if it is not already set and the API doesn`t work', function (done) { makeFake(Promise.reject('Error from external API')); request(app) .get('/') .expect(function (res) { /PACookie=ABC/.test(res.headers['set-cookie']).should.equal(false); }) .expect(200, done); }); });