ft-next-barrier-component
Advanced tools
Comparing version 3.6.0 to 4.0.0
{ | ||
"name": "ft-next-barrier-component", | ||
"version": "3.6.0", | ||
"version": "4.0.0", | ||
"description": "Barriers for Next", | ||
@@ -5,0 +5,0 @@ "main": "server/main.js", |
@@ -6,3 +6,2 @@ 'use strict'; | ||
var middleware = require('./middleware'); | ||
var helper = require('./helper'); | ||
@@ -12,4 +11,3 @@ | ||
middleware : middleware, | ||
helper : helper, | ||
partials : path.resolve(__dirname, '../templates') | ||
}; |
'use strict'; | ||
/*global fetch */ | ||
var debug = require('debug')('ft-next-barrier-component'); | ||
require('isomorphic-fetch'); | ||
var UserModel = require('./models/user'); | ||
var BarrierModel = require('./models/barrier'); | ||
var BarriersModel = require('./models/barriers'); | ||
var barrierAPIClient = require('./barrierAPIClient'); | ||
var barrierTypes = require('./barrierTypes'); | ||
@@ -14,14 +13,13 @@ function fallbackBarrier(req, res, next){ | ||
function middleware(req, res, next){ | ||
res.locals.barrier = null; | ||
res.locals.barrier = false; | ||
res.locals.barriers = {}; | ||
// handle anonymous users | ||
// todo: need to validate this at some point | ||
res.locals.user = new UserModel({ | ||
anonymous : res.get('X-FT-Anonymous-User') === "true" | ||
}); | ||
var authGateResult = req.get('X-FT-Auth-Gate-Result'); | ||
var barrierType = req.get('X-FT-Barrier-Type'); | ||
var userIsAnonymous = req.get('X-FT-Anonymous-User'); | ||
var authGateResult = req.get('X-FT-Auth-Gate-Result'); | ||
res.vary('X-FT-Anonymous-User'); | ||
if(authGateResult !== 'DENIED'){ | ||
debug('Auth Gate Result is "%s" ,so no barrier to show',authGateResult ); | ||
debug('Auth Gate Result is "%s" ,so no barrier to show', authGateResult); | ||
return next(); | ||
@@ -34,43 +32,15 @@ } | ||
res.locals.barrier = (barrierType !== null); | ||
res.locals.barrier = new BarrierModel(req); | ||
if(!res.locals.barrier || !res.locals.barrier.type){ | ||
debug('Access check is %s, but Barrier-Type is %s so falling back to default redirect', authGateResult, res.locals.barrier); | ||
return fallbackBarrier(req, res, next); | ||
//todo remove this when we have a real barrier type from API | ||
if(userIsAnonymous && barrierType !== null){ | ||
barrierType = barrierTypes.REGISTER_PLUS; | ||
} | ||
fetch( | ||
'http://barrier-app-test.memb.ft.com/memb/barrier/v1', | ||
{ | ||
headers: res.locals.barrier.getApiRequestHeaders() | ||
} | ||
).then(function(response){ | ||
if(!response.ok){ | ||
debug("Fetch call failed. status=%s", response.status); | ||
return Promise.reject(); | ||
} | ||
return response.json(); | ||
}).then(function(json) { | ||
debug('Response from Barriers component %j', json); | ||
res.locals.barrier.viewModel.hydrate(json); | ||
debug('ViewModel is now %j', res.locals.barrier.viewModel); | ||
return true; | ||
}).then(function(){ | ||
barrierAPIClient.getBarrierData(req).then(function(json){ | ||
res.locals.barriers = new BarriersModel(barrierType, json); | ||
next(); | ||
}) | ||
.catch(function(err){ | ||
debug(err); | ||
next(err); | ||
}); | ||
}).catch(next); | ||
} | ||
module.exports = middleware; |
@@ -8,20 +8,8 @@ 'use strict'; | ||
request.Test.prototype.endPromise = function () { | ||
return new Promise(function (resolve, reject) { | ||
this.end(function (err, res) { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(res); | ||
} | ||
}); | ||
}.bind(this)); | ||
}; | ||
var fetchMock = require('fetch-mock'); | ||
var middleware = require('../server/main').middleware; | ||
describe('Middleware', function(){ | ||
describe.only('Middleware', function(){ | ||
@@ -71,10 +59,2 @@ var app, routeHandler, routeHandlerSpy, locals; | ||
it('Should redirect if no barrier is specified', function(done){ | ||
request(app) | ||
.get('/blah') | ||
.set('X-FT-Auth-Gate-Result', 'DENIED') | ||
.expect('Location', 'https://registration.ft.com/registration/barrier/login?location=http://next.ft.com/blah') | ||
.expect(302, done); | ||
}); | ||
it('Should redirect if barriers flag is off', function(done){ | ||
@@ -90,12 +70,2 @@ barriersFlag = false; | ||
it('Should set a local variable based on the X-FT-Anonymous-User header', function(done){ | ||
request(app) | ||
.get('/blah') | ||
.set('X-FT-Anonymous-User', "false") | ||
.expect(function(){ | ||
expect(locals.user.anonymous).to.be.false; | ||
}) | ||
.expect(200, done); | ||
}); | ||
it('Should vary on the X-FT-Anonymous-User header', function(done){ | ||
@@ -107,75 +77,17 @@ setup() | ||
describe('res.locals.barrier', function(){ | ||
it('Should set a barrier property to true if there is a barrier to show', function(done){ | ||
setup() | ||
.expect(function(){ | ||
expect(locals.barrier).to.be.true; | ||
}) | ||
.end(done); | ||
}); | ||
before(function(){ | ||
barriersFlag = true; | ||
}); | ||
it('Should set type to the value of the X-FT-Barrier-Type header', function(done){ | ||
setup() | ||
.expect(function(){ | ||
expect(locals.barrier.type).to.equal(barrierType); | ||
}) | ||
.expect(200, done); | ||
}); | ||
it('Should set sessionId to the value of the X-FT-Session-Token header', function(done){ | ||
setup() | ||
.expect(function(){ | ||
expect(locals.barrier.requestData.sessionId).to.equal(sessionId); | ||
}) | ||
.expect(200, done); | ||
}); | ||
it('Should set asyc to the value of the X-FT-AYSC header', function(done){ | ||
setup() | ||
.expect(function(){ | ||
expect(locals.barrier.requestData.aysc).to.equal(asyc); | ||
}) | ||
.expect(200, done); | ||
}); | ||
it('Should set countyCode to the value of the Country-Code header', function(done){ | ||
setup() | ||
.expect(function(){ | ||
expect(locals.barrier.requestData.countryCode).to.equal(countryCode); | ||
}) | ||
.expect(200, done); | ||
}); | ||
it('Should set contentClassification to the value of the X-FT-Content-Classification header', function(done){ | ||
setup() | ||
.expect(function(){ | ||
expect(locals.barrier.requestData.contentClassification).to.equal(contentClassification); | ||
}) | ||
.expect(200, done); | ||
}); | ||
it('Should call the Barriers API to get data to put into the view', function(done) { | ||
fetchMock.mock({ | ||
routes: { | ||
name: "barrier", | ||
matcher: /barrier-app-test\.memb\.ft\.com/, | ||
response: { | ||
foo : 'bar' | ||
} | ||
} | ||
}); | ||
setup() | ||
.end(function (err) { | ||
if(err) return done(err); | ||
expect(locals.barrier.viewModel.foo).to.equal('bar'); | ||
expect(fetchMock.called('barrier')).to.equal(true); | ||
fetchMock.restore(); | ||
done(); | ||
}); | ||
}); | ||
it('Should add a barriers model to res.locals', function(done){ | ||
setup() | ||
.expect(function(){ | ||
expect(locals.barriers).to.have.property('premiumBarrier'); | ||
}) | ||
.end(done); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
25
67889
2182