ft-next-barrier-component
Advanced tools
Comparing version
{ | ||
"name": "ft-next-barrier-component", | ||
"version": "3.2.0", | ||
"version": "3.3.0", | ||
"description": "Barriers for Next", | ||
@@ -20,2 +20,3 @@ "main": "server/main.js", | ||
"express-handlebars": "^2.0.1", | ||
"fetch-mock": "^1.1.3", | ||
"handlebars": "^3.0.2", | ||
@@ -30,4 +31,5 @@ "mocha": "^2.2.4", | ||
"debug": "^2.1.1", | ||
"handlebars": "^3.0.2" | ||
"handlebars": "^3.0.2", | ||
"isomorphic-fetch": "^2.0.2" | ||
} | ||
} |
'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'); | ||
function fallbackBarrier(req, res, next){ | ||
res.redirect('https://registration.ft.com/registration/barrier/login?location=http://next.ft.com' + req.url); | ||
return next(); | ||
} | ||
function middleware(req, res, next){ | ||
res.locals.barrier = null; | ||
// 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'); | ||
res.vary('X-FT-Anonymous-User'); | ||
if(authGateResult !== 'DENIED'){ | ||
debug('Auth Gate Result is "%s" ,so no barrier to show',authGateResult ); | ||
next(); | ||
return; | ||
return next(); | ||
} | ||
if(res.locals.flags && res.locals.flags.barrier){ | ||
res.locals.barrier = { | ||
type : req.get('X-FT-Barrier-Type'), | ||
sessionId : req.get('X-FT-Session-Token'), | ||
aysc : req.get('X-FT-AYSC'), | ||
countryCode : req.get('Country-Code'), | ||
contentClassification : req.get('X-FT-Content-Classification') | ||
}; | ||
if(!res.locals.flags.barrier){ | ||
return fallbackBarrier(req, res, next); | ||
} | ||
// if we've got this far then they have failed the Access check but we don't have a barrier to show them | ||
// So use the default | ||
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 defaulrt redirect', authGateResult, res.locals.barrier); | ||
res.redirect('https://registration.ft.com/registration/barrier/login?location=http://next.ft.com' + req.url); | ||
return; | ||
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); | ||
} | ||
next(); | ||
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(){ | ||
next(); | ||
}) | ||
.catch(function(err){ | ||
debug(err); | ||
next(err); | ||
}); | ||
} | ||
module.exports = middleware; |
@@ -8,2 +8,17 @@ '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; | ||
@@ -35,11 +50,59 @@ | ||
routeHandlerSpy.reset(); | ||
barriersFlag = true; | ||
}); | ||
var barrierType = "PREMIUM", | ||
sessionId = "kjvbjkvbrv", | ||
asyc = "dvsvsv", | ||
countryCode = "GBR", | ||
contentClassification = "PREMIUM_CONTENT"; | ||
function setup(){ | ||
return request(app) | ||
.get('/blah') | ||
.set('X-FT-Auth-Gate-Result', 'DENIED') | ||
.set('X-FT-Barrier-Type', barrierType) | ||
.set('X-FT-Session-Token', sessionId) | ||
.set('X-FT-AYSC', asyc) | ||
.set('Country-Code', countryCode) | ||
.set('X-FT-Content-Classification', contentClassification); | ||
} | ||
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){ | ||
barriersFlag = false; | ||
request(app) | ||
.get('/blah') | ||
.set('X-FT-Auth-Gate-Result', 'DENIED') | ||
.set('X-FT-Barrier-Type', 'PREMIUM') | ||
.expect('Location', 'https://registration.ft.com/registration/barrier/login?location=http://next.ft.com/blah') | ||
.expect(302, done); | ||
}); | ||
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){ | ||
setup() | ||
.expect('Vary', /X-FT-Anonymous-User/) | ||
.expect(200, done); | ||
}); | ||
describe('res.locals.barrier', function(){ | ||
var barrierType = "PREMIUM", | ||
sessionId = "kjvbjkvbrv", | ||
asyc = "dvsvsv", | ||
countryCode = "GBR", | ||
contentClassification = "PREMIUM_CONTENT"; | ||
@@ -50,12 +113,2 @@ before(function(){ | ||
function setup(){ | ||
return request(app) | ||
.get('/blah') | ||
.set('X-FT-Auth-Gate-Result', 'DENIED') | ||
.set('X-FT-Barrier-Type', barrierType) | ||
.set('X-FT-Session-Token', sessionId) | ||
.set('X-FT-AYSC', asyc) | ||
.set('Country-Code', countryCode) | ||
.set('X-FT-Content-Classification', contentClassification); | ||
} | ||
@@ -73,3 +126,3 @@ it('Should set type to the value of the X-FT-Barrier-Type header', function(done){ | ||
.expect(function(){ | ||
expect(locals.barrier.sessionId).to.equal(sessionId); | ||
expect(locals.barrier.requestData.sessionId).to.equal(sessionId); | ||
}) | ||
@@ -82,3 +135,3 @@ .expect(200, done); | ||
.expect(function(){ | ||
expect(locals.barrier.aysc).to.equal(asyc); | ||
expect(locals.barrier.requestData.aysc).to.equal(asyc); | ||
}) | ||
@@ -91,3 +144,3 @@ .expect(200, done); | ||
.expect(function(){ | ||
expect(locals.barrier.countryCode).to.equal(countryCode); | ||
expect(locals.barrier.requestData.countryCode).to.equal(countryCode); | ||
}) | ||
@@ -100,3 +153,3 @@ .expect(200, done); | ||
.expect(function(){ | ||
expect(locals.barrier.contentClassification).to.equal(contentClassification); | ||
expect(locals.barrier.requestData.contentClassification).to.equal(contentClassification); | ||
}) | ||
@@ -106,20 +159,24 @@ .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' | ||
} | ||
} | ||
}); | ||
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); | ||
}); | ||
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 redirect if barriers flag is off', function(done){ | ||
barriersFlag = false; | ||
request(app) | ||
.get('/blah') | ||
.set('X-FT-Auth-Gate-Result', 'DENIED') | ||
.set('X-FT-Barrier-Type', 'PREMIUM') | ||
.expect('Location', 'https://registration.ft.com/registration/barrier/login?location=http://next.ft.com/blah') | ||
.expect(302, done); | ||
}); | ||
}); | ||
@@ -126,0 +183,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
68268
7.93%2245
7.98%3
50%10
11.11%2
100%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added