148 lines
4.2 KiB
JavaScript
148 lines
4.2 KiB
JavaScript
/**
|
|
* Unit testing file for ElevateSession command
|
|
*/
|
|
'use strict';
|
|
/* eslint max-nested-callbacks: ["error", 7] import/max-dependencies: ["error", {"max": 11}] */
|
|
// eslint-disable-next-line no-unused-vars
|
|
const testGlobals = require('../../tools/test/testGlobals.js');
|
|
const _ = require('lodash');
|
|
const Q = require('q');
|
|
const chai = require('chai');
|
|
const sinonChai = require('sinon-chai');
|
|
const chaiAsPromised = require('chai-as-promised');
|
|
|
|
const utils = require('../../ComServe/utils.js');
|
|
|
|
const {MockRequest} = require('../../utils/test/mock-request');
|
|
const {bridgeBodyParser} = require('../api_body_middleware.js');
|
|
|
|
/**
|
|
* Set up chai & sinon to simplify the tests
|
|
*/
|
|
const expect = chai.expect;
|
|
|
|
chai.use(sinonChai);
|
|
chai.use(chaiAsPromised);
|
|
|
|
/**
|
|
* Make a promise-style version of the middleware handler for easier testing
|
|
*/
|
|
const middlewareP = (req) => Q.nfcall(
|
|
bridgeBodyParser(),
|
|
req,
|
|
{} // Don't use res, so not defined
|
|
);
|
|
|
|
/**
|
|
* Valid values
|
|
*/
|
|
const PROTOCOL = 'https';
|
|
const PATH = '/api/v0/devices';
|
|
const METHOD = 'get';
|
|
|
|
const MOCK_REQUEST_OPTIONS = {
|
|
originalUrl: PATH,
|
|
protocol: PROTOCOL,
|
|
method: METHOD
|
|
};
|
|
|
|
const MOCK_REQUEST_BODY_OPTIONS = {
|
|
mockBody: '{\n' +
|
|
' "test": "value",\n' +
|
|
' "other": "value2"\n' +
|
|
'}'
|
|
};
|
|
|
|
/**
|
|
* Function to create a mock `req` objects that mimics the important parts of a
|
|
* real request object.
|
|
*
|
|
* @param {Object} resolvedSwagger - The **resolved** swagger object (i.e. all refs resolved)
|
|
* @param {Object} reqOptions - The additional fields to add to the request object
|
|
* @param {Object} bodyOptions - Additional params for creating the MockRequest (provides the body)
|
|
*/
|
|
function createMockReq(resolvedSwagger, reqOptions, bodyOptions) {
|
|
const req = new MockRequest(_.cloneDeep(bodyOptions));
|
|
_.merge(req, _.cloneDeep(reqOptions));
|
|
return req;
|
|
}
|
|
|
|
/**
|
|
* The tests
|
|
*/
|
|
describe('Custom body middleware', () => {
|
|
let resolvedSwagger;
|
|
let req;
|
|
|
|
describe('with valid JSON body', () => {
|
|
beforeEach(() => {
|
|
req = createMockReq(resolvedSwagger, MOCK_REQUEST_OPTIONS, MOCK_REQUEST_BODY_OPTIONS);
|
|
});
|
|
|
|
it('runs and completes', () => {
|
|
return expect(middlewareP(req)).to.eventually.be.fulfilled;
|
|
});
|
|
|
|
it('parses the JSON and stores it in req.body', () => {
|
|
return middlewareP(req)
|
|
.then(() => {
|
|
return expect(req.body)
|
|
.to.deep.equal({
|
|
test: 'value',
|
|
other: 'value2'
|
|
});
|
|
});
|
|
});
|
|
|
|
it('stores the raw body in req.bodyRaw', () => {
|
|
return middlewareP(req)
|
|
.then(() => {
|
|
return expect(req.bodyRaw)
|
|
.to.equal(MOCK_REQUEST_BODY_OPTIONS.mockBody);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('with no body', () => {
|
|
beforeEach(() => {
|
|
req = createMockReq(resolvedSwagger, MOCK_REQUEST_OPTIONS);
|
|
});
|
|
|
|
it('runs and completes', () => {
|
|
return expect(middlewareP(req)).to.eventually.be.fulfilled;
|
|
});
|
|
|
|
it('stores empty object in req.body', () => {
|
|
return middlewareP(req)
|
|
.then(() => {
|
|
return expect(req.body)
|
|
.to.deep.equal({});
|
|
});
|
|
});
|
|
|
|
it('leaves req.bodyRaw undefined', () => {
|
|
return middlewareP(req)
|
|
.then(() => {
|
|
return expect(req.bodyRaw)
|
|
.to.be.undefined;
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('with body that\'s too big', () => {
|
|
it('runs and fails', () => {
|
|
const tooManyAs = utils.maxPacketSize - 8 + 1; // 8 other chars in valid body
|
|
const mockBody = '{"b":"' +
|
|
'c'.repeat(tooManyAs) +
|
|
'"}';
|
|
req = createMockReq(
|
|
resolvedSwagger,
|
|
MOCK_REQUEST_OPTIONS, {
|
|
mockBody
|
|
});
|
|
|
|
return expect(middlewareP(req)).to.eventually.be.rejectedWith('too large');
|
|
});
|
|
});
|
|
});
|