Skip to content

Commit

Permalink
Merge pull request #1 from killmenot/patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
robtweed committed Feb 13, 2018
2 parents 2bf8aab + 5934675 commit 3182bca
Show file tree
Hide file tree
Showing 5 changed files with 408 additions and 7 deletions.
16 changes: 12 additions & 4 deletions lib/microServiceRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
----------------------------------------------------------------------------
| qewd-microservice-router: Express Integration Module for QEWD |
| |
| Copyright (c) 2016-17 M/Gateway Developments Ltd, |
| Copyright (c) 2016-18 M/Gateway Developments Ltd, |
| Redhill, Surrey UK. |
| All rights reserved. |
| |
Expand All @@ -24,7 +24,7 @@
| limitations under the License. |
----------------------------------------------------------------------------
23 November 2017
31 January 2018
MicroService Routing Module
Expand Down Expand Up @@ -134,8 +134,10 @@ function handleMicroService(message, route, destination, handleResponse) {
// the nominal one must have the expiry updated

var token = this.jwt.handlers.getRestJWT(message);
debug('jwt: %s', token);
debug('route = %s', JSON.stringify(route));

if (token === '') {
if (token === '' || token === 'undefined') {
debug('using updated registration JWT');

token = microService.client.token;
Expand All @@ -145,7 +147,13 @@ function handleMicroService(message, route, destination, handleResponse) {
// prevents arbitrary application change attempts
sendToMicroService.call(this, token, message, microService, route, handleResponse);
}
else {
else if (route.bypassJWTCheck) {
debug('JWT check being bypassed');

// incoming JWT is to be ignored by primary; forward it to the MicroService
// which will be responsible for checking the JWT
sendToMicroService.call(this, token, message, microService, route, handleResponse);
} else {
debug('validating message JWT');

// JWT is first validated in worker (to reduce master process CPU load)
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "qewd-microservice-router",
"version": "1.0.0",
"version": "1.1.0",
"description": "MicroService Routing Module for QEWD",
"main": "index.js",
"engines": {
Expand Down Expand Up @@ -41,11 +41,11 @@
},
"devDependencies": {
"coveralls": "^3.0.0",
"jasmine": "^2.8.0",
"jasmine": "^2.9.0",
"jasmine-spec-reporter": "^4.1.1",
"jasmine-spy-matchers": "^1.2.0",
"jshint": "^2.9.5",
"nyc": "^11.2.1",
"nyc": "^11.4.1",
"pre-commit": "^1.2.2"
}
}
218 changes: 218 additions & 0 deletions spec/shared/destinations/multiple.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,224 @@ module.exports = function (boot) {
});
},

whenTokenHasValueUndefined: function () {
beforeEach(function (done) {
boot(function (_q, _messageObj, _handleResponse) {
q = _q;
messageObj = _messageObj;
handleResponse = _handleResponse;

done();
});
});

it('should call handleMessage 2 times with correct arguments', function () {
q.microServiceRouter(messageObj, handleResponse);

expect(q.handleMessage).toHaveBeenCalledTimes(2);
expect(q.handleMessage.calls.argsFor(0)[0]).toEqual({
type: 'ewd-jwt-updateExpiry',
params: {
jwt: 'token4',
application: 'sms-micro-service'
}
}, jasmine.any(Function));
expect(q.handleMessage.calls.argsFor(1)[0]).toEqual({
type: 'ewd-jwt-updateExpiry',
params: {
jwt: 'token5',
application: 'log-micro-service'
}
}, jasmine.any(Function));
});

it('should call microservice client #send 2 times with correct arguments', function () {
q.microServiceRouter(messageObj, handleResponse);

destinations.forEach(function (destination) {
/*jshint camelcase: false */
var microService = q.u_services.byDestination[destination];
/*jshint camelcase: true */

expect(microService.client.send).toHaveBeenCalledWith({
application: microService.application,
type: 'restRequest',
path: '/api/users',
pathTemplate: '/path/template',
method: 'POST',
headers: {
'x-foo': 'bar'
},
params: {
type: 'foo'
},
query: {
bar: 'baz'
},
body: {
login: 'johndoe',
password: 'secret'
},
ip: '127.0.0.1',
ips: ['client'],
token: 'updated-jwt-token',
args: {},
jwt: true
}, jasmine.any(Function));
});
});

it('should call handleResponse with composite result', function () {
/*jshint camelcase: false */
var expected = {
type: 'hello',
message: {
results: {
sms_service: {
application: 'sms-micro-service'
},
log_service: {
application: 'log-micro-service',
}
},
token: 'updated-jwt-token'
}
};
/*jshint camelcase: true */

destinations.forEach(function (destination) {
/*jshint camelcase: false */
var microService = q.u_services.byDestination[destination];
/*jshint camelcase: true */

microService.client.send.and.callFake(function (message, cb) {
var responseObj = {
message: {
application: message.application,
token: message.token
},
type: message.application + '-type',
finished: true
};
cb(responseObj);
});
});

q.microServiceRouter(messageObj, handleResponse);

expect(handleResponse).toHaveBeenCalledWith(expected);
});
},

whenRouteByPassJwtCheckEnabled: function () {
beforeEach(function (done) {
boot(function (_q, _messageObj, _handleResponse) {
q = _q;
messageObj = _messageObj;
handleResponse = _handleResponse;

done();
});
});

it('should call handleMessage 2 times with correct arguments', function () {
q.microServiceRouter(messageObj, handleResponse);

expect(q.handleMessage).toHaveBeenCalledTimes(2);
expect(q.handleMessage.calls.argsFor(0)[0]).toEqual({
type: 'ewd-jwt-updateExpiry',
params: {
jwt: 'jwt-token',
application: 'sms-micro-service'
}
}, jasmine.any(Function));
expect(q.handleMessage.calls.argsFor(1)[0]).toEqual({
type: 'ewd-jwt-updateExpiry',
params: {
jwt: 'jwt-token',
application: 'log-micro-service'
}
}, jasmine.any(Function));
});

it('should call microservice client #send 2 times with correct arguments', function () {
q.microServiceRouter(messageObj, handleResponse);

destinations.forEach(function (destination) {
/*jshint camelcase: false */
var microService = q.u_services.byDestination[destination];
/*jshint camelcase: true */

expect(microService.client.send).toHaveBeenCalledWith({
application: microService.application,
type: 'restRequest',
path: '/api/users',
pathTemplate: '/path/template',
method: 'POST',
headers: {
'x-foo': 'bar'
},
params: {
type: 'foo'
},
query: {
bar: 'baz'
},
body: {
login: 'johndoe',
password: 'secret'
},
ip: '127.0.0.1',
ips: ['client'],
token: 'updated-jwt-token',
args: {},
jwt: true
}, jasmine.any(Function));
});
});

it('should call handleResponse with composite result', function () {
/*jshint camelcase: false */
var expected = {
type: 'hello',
message: {
results: {
sms_service: {
application: 'sms-micro-service'
},
log_service: {
application: 'log-micro-service',
}
},
token: 'updated-jwt-token'
}
};
/*jshint camelcase: true */

destinations.forEach(function (destination) {
/*jshint camelcase: false */
var microService = q.u_services.byDestination[destination];
/*jshint camelcase: true */

microService.client.send.and.callFake(function (message, cb) {
var responseObj = {
message: {
application: message.application,
token: message.token
},
type: message.application + '-type',
finished: true
};
cb(responseObj);
});
});

q.microServiceRouter(messageObj, handleResponse);

expect(handleResponse).toHaveBeenCalledWith(expected);
});
},

whenTokenReturned: function () {
beforeEach(function (done) {
boot(function (_q, _messageObj, _handleResponse) {
Expand Down

0 comments on commit 3182bca

Please sign in to comment.