UNPKG

@tlrg/middleware-js

Version:
819 lines (676 loc) 25.8 kB
var express = require('express'); var meta = require('../lib/middlewares/meta'); var brand = require('../lib/middlewares/brand'); var request = require('supertest'); var moment = require('moment'); var cookieParser = require('cookie-parser'); var domain = require('../lib/middlewares/domain'); describe('Moonstick meta', function () { var app; var customMiddleware; var today = moment().startOf('day'); var tomorrow = moment().add(1, 'days').startOf('day'); var dayAfterTomorrow = moment().add(2, 'days').startOf('day'); var yesterday = moment().add(-1, 'days').startOf('day'); var nextYear = moment().add(365, 'days').startOf('day'); beforeEach(function () { app = express(); customMiddleware = false; app.use(function moonstickSetup(req, res, next) { req.moonstick = req.moonstick || {}; req.moonstick.errorParams = req.params; req.moonstick.params = req.params; next(); }); app.use(brand); app.use(cookieParser()); app.use(domain); app.use(function (req, res, next) { if (customMiddleware) { customMiddleware(req, res, next); } else { next(); } }); app.use(meta); }); describe('Date', function () { it('should add todays date as start date if none are present in request', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.date.toJSON()); }); request(app) .get('/') .expect(200) .expect(today.toJSON(), done); }); it('should ensure that start date is not in the past', function (done) { var search = { Date: yesterday.format('YYYYMMDD') }; app.get('/', function (req, res) { res.send(req.moonstick.meta.date.toJSON()); }); request(app) .get('/?d=' + yesterday.format('YYYYMMDD')) .set({ Cookie: createCookie('search', search) }) .expect(200) .expect(today.toJSON(), done); }); it('should ensure that start date is not higher than 365 days', function (done) { var search = { Date: nextYear.format('YYYYMMDD') }; app.get('/', function (req, res) { res.send(req.moonstick.meta.date.toJSON()); }); request(app) .get('/?d=' + nextYear.format('YYYYMMDD')) .set({ Cookie: createCookie('search', search) }) .expect(200) .expect(today.toJSON(), done); }); it('should handle malformed date YYYYMDD to YYYYMMDD', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.date.toJSON()); }); request(app) .get('/?d=2016224') .expect(200) .expect(today.toJSON(), done); }); describe('laterooms and moonstick', function () { it('should add start date from cookie', function (done) { var search = { Date: tomorrow.format('YYYYMMDD') }; app.get('/', function (req, res) { res.send(req.moonstick.meta.date.toJSON()); }); request(app) .get('/') .set({ Cookie: createCookie('search', search) }) .expect(200) .expect(tomorrow.toJSON(), done); }); it('should add start date from qs if present even if cookie is present', function (done) { var search = { Date: tomorrow.format('YYYYMMDD') }; app.get('/', function (req, res) { res.send(req.moonstick.meta.date.toJSON()); }); request(app) .get('/?d=' + dayAfterTomorrow.format('YYYYMMDD')) .set({ Cookie: createCookie('search', search) }) .expect(200) .expect(dayAfterTomorrow.toJSON(), done); }); }); describe('asiarooms', function () { it('should add start date from cookie', function (done) { var arrivalDate = tomorrow.format('YYYY-MM-DD'); app.get('/', function (req, res) { res.send(req.moonstick.meta.date.toJSON()); }); request(app) .get('/') .set({ Cookie: createCookie('arrivalDate', arrivalDate), Host: 'www.asiarooms.com' }) .expect(200) .expect(tomorrow.toJSON(), done); }); it('should add start date from qs if present even if cookie is present', function (done) { var arrivalDate = tomorrow.format('YYYY-MM-DD'); app.get('/', function (req, res) { res.send(req.moonstick.meta.date.toJSON()); }); request(app) .get('/?d=' + dayAfterTomorrow.format('YYYYMMDD')) .set({ Cookie: createCookie('arrivalDate', arrivalDate), Host: 'www.asiarooms.com' }) .expect(200) .expect(dayAfterTomorrow.toJSON(), done); }); }); }); describe('Destination', function () { it('should use req.query.parsedSearch by default', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.destination); }); var search = { Destination: 'DefNotManchester' }; request(app) .get('/?parsedSearch=Manchester&k=NotManchester') .set('Cookie', createCookie('search', search)) .expect(200) .expect('Manchester', done); }); it('should fallback to the k querystring value', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.destination); }); var search = { Destination: 'DefNotManchester' }; request(app) .get('/?k=NotManchester') .set('Cookie', createCookie('search', search)) .expect(200) .expect('NotManchester', done); }); it('should fallback to the search cookie if no querystring keys match', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.destination); }); var search = { Destination: 'DefNotManchester' }; request(app) .get('/?foo=bar') .set('Cookie', createCookie('search', search)) .expect(200) .expect('DefNotManchester', done); }); it('should escape any string provided from Cookie', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.destination); }); var search = { Destination: '</script><script>alert(document.cookie)</script>' }; request(app) .get('/?foo=bar') .set('Cookie', createCookie('search', search)) .expect(200) .expect('&lt;/script&gt;&lt;script&gt;alert(document.cookie)&lt;/script&gt;', done); }); it('should escape any string provided from querystring k', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.destination); }); request(app) .get('/?k=</script><script>alert(document.cookie)</script>') .expect(200) .expect('&lt;/script&gt;&lt;script&gt;alert(document.cookie)&lt;/script&gt;', done); }); it('should escape any string provided from parsedSearch', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.destination); }); request(app) .get('/?parsedSearch=</script><script>alert(document.cookie)</script>') .expect(200) .expect('&lt;/script&gt;&lt;script&gt;alert(document.cookie)&lt;/script&gt;', done); }); }); describe('Nights', function () { it('should set the nights to 1 as default', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.nights.toString()); }); request(app) .get('/') .expect(200) .expect('1', done); }); describe('laterooms and moonstick', function () { it('should set the nights from the cookie', function (done) { var search = { Nights: 2 }; app.get('/', function (req, res) { res.send(req.moonstick.meta.nights.toString()); }); request(app) .get('/') .set({ Cookie: createCookie('search', search) }) .expect(200) .expect('2', done); }); it('should set the nights from the qs over the cookie where the qs is nights', function (done) { var search = { Nights: 2 }; app.get('/', function (req, res) { res.send(req.moonstick.meta.nights.toString()); }); request(app) .get('/?n=22') .set({ Cookie: createCookie('search', search) }) .expect(200) .expect('22', done); }); it('should set the nights to 1 if cookie value is under 1', function (done) { var search = { Nights: -1 }; app.get('/', function (req, res) { res.send(req.moonstick.meta.nights.toString()); }); request(app) .get('/') .set({ Cookie: createCookie('search', search) }) .expect(200) .expect('1', done); }); it('should set the nights to 1 if under 1', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.nights.toString()); }); request(app) .get('/?n=0') .expect(200) .expect('1', done); }); it('should set the nights to 28 if over 28', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.nights.toString()); }); request(app) .get('/?n=897') .expect(200) .expect('28', done); }); it('should set nights from the qs where the qs ia a date', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.nights.toString()); }); request(app) .get('/?dd=' + dayAfterTomorrow.format('YYYY-MM-DD')) .set({ Cookie: createCookie('departureDate', tomorrow.format('YYYY-MM-DD')) }) .expect(200) .expect('2', done); }); }); describe('Asiarooms', function () { it('should set the nights from the cookie', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.nights.toString()); }); request(app) .get('/') .set({ Cookie: createCookie('departureDate', dayAfterTomorrow.format('YYYY-MM-DD')), Host: 'www.asiarooms.com' }) .expect(200) .expect('2', done); }); }); }); describe('Adults', function () { it('should default adults to 2', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.adults.toString()); }); request(app) .get('/') .expect(200) .expect('2', done); }); it('should take adults from the cookie if present', function (done) { var search = { Adults: 3 }; app.get('/', function (req, res) { res.send(req.moonstick.meta.adults.toString()); }); request(app) .get('/') .set({ Cookie: createCookie('search', search) }) .expect(200) .expect('3', done); }); it('should take adults from the qs over cookie if present', function (done) { var search = { Adults: 3 }; app.get('/', function (req, res) { res.send(req.moonstick.meta.adults.toString()); }); request(app) .get('/?rt-adult=4') .set({ Cookie: createCookie('search', search) }) .expect(200) .expect('4', done); }); it('should limit adults to 8', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.adults.toString()); }); request(app) .get('/?rt-adult=15') .expect(200) .expect('8', done); }); it('should cast strings to default', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.adults.toString()); }); request(app) .get('/?rt-adult=esrdfb') .expect(200) .expect('2', done); }); }); describe('Children', function () { it('should default children to 0', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.children.toString()); }); request(app) .get('/') .expect(200) .expect('0', done); }); it('should take children from the cookie if present', function (done) { var search = { Children: 3 }; app.get('/', function (req, res) { res.send(req.moonstick.meta.children.toString()); }); request(app) .get('/') .set({ Cookie: createCookie('search', search) }) .expect(200) .expect('3', done); }); it('should take children from the qs over cookie if present', function (done) { var search = { Children: 2 }; app.get('/', function (req, res) { res.send(req.moonstick.meta.children.toString()); }); request(app) .get('/?rt-child=3') .set({ Cookie: createCookie('search', search) }) .expect(200) .expect('3', done); }); it('should limit the children to 3', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.children.toString()); }); request(app) .get('/?rt-child=20') .expect(200) .expect('3', done); }); it('should cast strings to default', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.children.toString()); }); request(app) .get('/?rt-child=awesgb') .expect(200) .expect('0', done); }); }); describe('Guests', function () { it('should return the total of adults + children', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.guests.toString()); }); request(app) .get('/?rt-child=2&rt-adult=2') .expect(200) .expect('4', done); }); }); describe('RT', function () { it('should return adult and children in Adult-Children format', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.rt); }); request(app) .get('/') .expect(200) .expect('2-0', done); }); it('should honor rt if present in the qs', function (done) { var search = { Adults: 3, Children: 2 }; app.get('/', function (req, res) { res.send(req.moonstick.meta.adults.toString() + '-' + req.moonstick.meta.children.toString()); }); request(app) .get('/?rt=2-3') .set({ Cookie: createCookie('search', search) }) .expect(200) .expect('2-3', done); }); it('should honor the first rt if it is present twice in the qs', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.adults.toString() + '-' + req.moonstick.meta.children.toString()); }); request(app) .get('/?rt=2-3&rt=2-3') .expect(200) .expect('2-3', done); }); }); describe('Departure date', function () { it('should return departure date which reflects start and nights', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.departureDate.toJSON()); }); request(app) .get('/') .expect(200) .expect(tomorrow.toJSON(), done); }); }); describe('Format', function () { it('should return the YYYYMMDD as the format for laterooms', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.format); }); request(app) .get('/') .expect(200) .expect('YYYYMMDD', done); }); it('should return the YYYY-MM-DD as the format for asiarooms', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.format); }); request(app) .get('/') .set({ Host: 'www.asiarooms.com' }) .expect(200) .expect('YYYY-MM-DD', done); }); it('should return YYYYMMDD as default', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.format); }); request(app) .get('/') .expect(200) .expect('YYYYMMDD', done); }); }); describe('Currency', function () { it('should return GBP as default currency for laterooms', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.currency); }); request(app) .get('/') .expect(200) .expect('GBP') .end(done); }); it('should return USD as default currency for asiarooms', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.currency); }); request(app) .get('/') .set({ Host: 'www.asiarooms.com' }) .expect(200) .expect('USD') .end(done); }); it('should return default currency when URL param is invalid ie.ZZZ', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.currency); }); request(app) .get('/?curr=ZZZ') .set({ Host: 'www.laterooms.com.au' }) .expect(200) .expect('AUD') .end(done); }); it('should use first currency if more than one specified', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.currency); }); request(app) .get('/?curr=GBP&curr=USD') .set({ Host: 'www.laterooms.com.au' }) .expect(200) .expect('GBP') .end(done); }); it('should use the URLs parameter when passed via URL', function (done) { var params = ['cur', 'curr', 'currency', 'currencies']; app.get('/', function (req, res) { res.send(req.moonstick.meta.currency); }); params.forEach(function (param) { request(app) .get('/?' + param + '=EUR') .set({ Host: 'www.laterooms.com.au' }) .expect(200) .expect('EUR'); }); done(); }); it('should use the currency defined on LateroomsProfile cookie', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.currency); }); request(app) .get('/') .set({ Cookie: createCookie('LateroomsProfile', { currencycode: 'BRL' }) }) .expect(200) .expect('BRL') .end(done); }); it('should return AUD as default currency for laterooms.com.au', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.currency); }); request(app) .get('/') .set({ Host: 'www.laterooms.com.au' }) .expect(200) .expect('AUD') .end(done); }); }); describe('Logged In', function () { it('should indicate user is logged in when they are', function (done) { customMiddleware = function fakePassport(req, res, next) { req.user = {}; next(); }; app.get('/', function (req, res) { res.send(req.moonstick.meta.isLoggedIn); }); request(app) .get('/') .set('Cookie', ['moomin-cmid=pezza']) .expect(200) .expect('true') .end(done); }); it('should indicate when a user is not logged in - missing cookie', function (done) { customMiddleware = function fakePassport(req, res, next) { req.user = {}; next(); }; app.get('/', function (req, res) { res.send(req.moonstick.meta.isLoggedIn); }); request(app) .get('/') .expect(200) .expect('false') .end(done); }); it('should indicate when a user is not logged in - missing user object', function (done) { app.get('/', function (req, res) { res.send(req.moonstick.meta.isLoggedIn); }); request(app) .get('/') .set('Cookie', ['moomin-cmid=pezza']) .expect(200) .expect('false') .end(done); }); }); function createCookie(name, valueObj) { var cookie = name + '='; if (typeof valueObj === 'string') { return cookie += valueObj; } return cookie += encodeURIComponent(JSON.stringify(valueObj)); } });