Skip to content

Commit

Permalink
pencilblue#970 added more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brianhyder committed Dec 4, 2016
1 parent 1b504d5 commit f4c2f95
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 13 deletions.
6 changes: 3 additions & 3 deletions include/http/middleware/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ module.exports = function(pb) {
static parseRequestBody (req, res, next) {
req.handler.parseBody(req.themeRoute.request_body, function (err, body) {
if (util.isError(err)) {
err.code = 400;
err.code = HttpStatus.BAD_REQUEST;
}
req.body = body;
next(err);
Expand Down Expand Up @@ -440,7 +440,7 @@ module.exports = function(pb) {
* @param {Response} res The response object that compliments the current request
* @param {function} next (Error) Callback function that takes a single parameter, an error if it occurred
*/
static render (req, res, next) {console.log(req.themeRoute.handler ? req.themeRoute.handler : 'render');
static render (req, res, next) {
req.controllerInstance[req.themeRoute.handler ? req.themeRoute.handler : 'render'](function (result) {
if (util.isError(result)) {
return next(result);
Expand All @@ -464,7 +464,7 @@ module.exports = function(pb) {
try {
cookies.set(pb.SessionHandler.COOKIE_NAME, req.session.uid, pb.SessionHandler.getSessionCookie(req.session));
}
catch (e) {
catch (e) {console.log(e);
pb.log.error('RequestHandler: Failed to set cookie: %s', e.stack);
}
}
Expand Down
192 changes: 182 additions & 10 deletions test/include/http/middleware/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
var Url = require('url');
var should = require('should');
var sinon = require('sinon');
var Cookies = require('cookies');
var HttpStatusCodes = require('http-status-codes');
var TestHelpers = require('../../../test_helpers.js');

Expand Down Expand Up @@ -580,36 +581,207 @@ describe('Middleware', function() {

describe('instantiateController', function() {

it('', function(done) {
done();
it('should create a new instance of the controller from the route theme properties: site, theme, HTTP method', function(done) {
function ControllerSample(){}
req.routeTheme = {
site: 'global',
theme: 'pencilblue',
method: 'get'
};
req.route = {
themes: {
global: {
pencilblue: {
'get': {
controller: ControllerSample
}
}
}
}
};
this.pb.Middleware.instantiateController(req, res, function(err) {
should(err).eql(undefined);
(req.controllerInstance instanceof ControllerSample).should.eql(true);
done();
});
});
});

describe('parseRequestBody', function() {

it('', function(done) {
done();
it('should call back with a bad request error when the request body fails to parse', function(done) {
var expectedError = new Error('expected');
req.themeRoute = {
request_body: 'application/json'
};
sandbox.stub(req.handler, 'parseBody')
.withArgs(req.themeRoute.request_body, sinon.match.func)
.callsArgWith(1, expectedError);
this.pb.Middleware.parseRequestBody(req, res, function(err) {
err.code.should.eql(HttpStatusCodes.BAD_REQUEST);
should(req.body).eql(undefined);
req.handler.parseBody.calledOnce.should.eql(true);
done();
});
});

it('should set the parsed body object on the request when the body is valid', function(done) {
var expectedBody = { hello: 'world' };
req.themeRoute = {
request_body: 'application/json'
};
sandbox.stub(req.handler, 'parseBody')
.withArgs(req.themeRoute.request_body, sinon.match.func)
.callsArgWith(1, null, expectedBody);
this.pb.Middleware.parseRequestBody(req, res, function(err) {
should(err).eql(null);
req.body.should.eql(expectedBody);
req.handler.parseBody.calledOnce.should.eql(true);
done();
});
});
});

describe('initializeController', function() {

it('', function(done) {
done();
it('should call back with an error when the controller fails to initialize', function(done) {
var expectedError = new Error('expected');
var expectedContext = { hello: 'world' };
req.controllerInstance = {
init: function(){}
};
sandbox.stub(this.pb.RequestHandler, 'buildControllerContext')
.withArgs(req, res)
.returns(expectedContext);
sandbox.stub(req.controllerInstance, 'init')
.withArgs(expectedContext, sinon.match.func)
.callsArgWith(1, expectedError);
var self = this;
this.pb.Middleware.initializeController(req, res, function(err) {
err.should.eql(expectedError);
self.pb.RequestHandler.buildControllerContext.calledOnce.should.eql(true);
req.controllerInstance.init.calledOnce.should.eql(true);
done();
});
});

it('should build the controller context and initialize the controller', function(done) {
var expectedContext = { hello: 'world' };
req.controllerInstance = {
init: function(){}
};
sandbox.stub(this.pb.RequestHandler, 'buildControllerContext')
.withArgs(req, res)
.returns(expectedContext);
sandbox.stub(req.controllerInstance, 'init')
.withArgs(expectedContext, sinon.match.func)
.callsArgWith(1, null);
var self = this;
this.pb.Middleware.initializeController(req, res, function(err) {
should(err).eql(null);
self.pb.RequestHandler.buildControllerContext.calledOnce.should.eql(true);
req.controllerInstance.init.calledOnce.should.eql(true);
done();
});
});
});

describe('render', function() {

it('', function(done) {
done();
it('should call back with an error when the controller result is an error', function(done) {
var expectedError = new Error('expected');
req.controllerInstance = {
render: function(){}
};
req.themeRoute = { handler: 'render' };
sandbox.stub(req.controllerInstance, 'render')
.withArgs(sinon.match.any)
.callsArgWith(0, expectedError);
this.pb.Middleware.render(req, res, function(err) {
err.should.eql(expectedError);
done();
});
});

it('should default to "render" when the handler name is not specified', function(done) {
var expectedResult = '<html><body>Hello World!</body></html>';
req.controllerInstance = {
render: function(){}
};
req.themeRoute = {};
sandbox.stub(req.controllerInstance, 'render')
.withArgs(sinon.match.any)
.callsArgWith(0, expectedResult);
this.pb.Middleware.render(req, res, function(err) {
should(err).eql(undefined);
req.controllerResult.should.eql(expectedResult);
req.controllerInstance.render.calledOnce.should.eql(true);
done();
});
});

it('should execute the controller with the specified handler name', function(done) {
var expectedResult = '<html><body>Hello World!</body></html>';
req.controllerInstance = {
doSomething: function(){}
};
req.themeRoute = { handler: 'doSomething' };
sandbox.stub(req.controllerInstance, 'doSomething')
.withArgs(sinon.match.any)
.callsArgWith(0, expectedResult);
this.pb.Middleware.render(req, res, function(err) {
should(err).eql(undefined);
req.controllerResult.should.eql(expectedResult);
req.controllerInstance.doSomething.calledOnce.should.eql(true);
done();
});
});
});

describe('writeSessionCookie', function() {

it('', function(done) {
done();
it('should catch the synchronous error that is thrown when the attempt to write the session cookie fails', function(done) {
var expectedError = new Error('expected');
req.setSessionCookie = true;
req.session = { hello: 'world' };
sandbox.stub(this.pb.SessionHandler, 'getSessionCookie')
.withArgs(req.session).throws(expectedError);
sandbox.stub(this.pb.log, 'error').withArgs(sinon.match.string, sinon.match.string);
var self = this;
this.pb.Middleware.writeSessionCookie(req, res, function(err) {
should(err).eql(undefined);
self.pb.SessionHandler.getSessionCookie.calledOnce.should.eql(true);
self.pb.log.error.calledOnce.should.eql(true);
done();
});
});

it('should skip writting the session cookie if the flag setSessionCookie is not set on the request object', function(done) {
req.setSessionCookie = false;
sandbox.stub(this.pb.SessionHandler, 'getSessionCookie');
var self = this;
this.pb.Middleware.writeSessionCookie(req, res, function(err) {
should(err).eql(undefined);
self.pb.SessionHandler.getSessionCookie.called.should.eql(false);
done();
});
});

it('should set the cookie when the flag setSessionCookie is set on the request object', function(done) {
req.setSessionCookie = true;
req.session = { uid: 'abc123' };
sandbox.stub(this.pb.SessionHandler, 'getSessionCookie')
.withArgs(req.session).returns();
sandbox.stub(this.pb.log, 'error');
sandbox.stub(Cookies.prototype, 'set')
.withArgs(this.pb.SessionHandler.COOKIE_NAME, req.session.uid, sinon.match.any);
var self = this;
this.pb.Middleware.writeSessionCookie(req, res, function(err) {
should(err).eql(undefined);
self.pb.SessionHandler.getSessionCookie.calledOnce.should.eql(true);
self.pb.log.error.called.should.eql(false);
done();
});
});
});

Expand Down

0 comments on commit f4c2f95

Please sign in to comment.