2023-02-27 20:34:03 +03:00
|
|
|
require('should');
|
2020-04-25 23:06:33 +03:00
|
|
|
const sinon = require('sinon');
|
2023-02-27 20:34:03 +03:00
|
|
|
|
|
|
|
const versionMatch = require('../');
|
2016-06-09 18:14:36 +03:00
|
|
|
|
|
|
|
describe('Version Mismatch', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
let req;
|
|
|
|
let res;
|
|
|
|
let getStub;
|
|
|
|
let nextStub;
|
2020-04-25 23:06:33 +03:00
|
|
|
|
2016-06-09 18:14:36 +03:00
|
|
|
afterEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.restore();
|
2016-06-09 18:14:36 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
getStub = sinon.stub();
|
|
|
|
nextStub = sinon.stub();
|
2016-06-09 18:14:36 +03:00
|
|
|
|
|
|
|
req = {
|
|
|
|
get: getStub
|
|
|
|
};
|
|
|
|
res = {
|
2017-08-22 12:59:01 +03:00
|
|
|
locals: {}
|
2016-06-09 18:14:36 +03:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2017-08-22 12:59:01 +03:00
|
|
|
function testVersionMatch(serverVersion, clientVersion) {
|
|
|
|
// Set the server version
|
|
|
|
res.locals.version = serverVersion;
|
|
|
|
|
|
|
|
if (clientVersion) {
|
|
|
|
// Optionally set the client version
|
|
|
|
getStub.returns(clientVersion);
|
|
|
|
}
|
|
|
|
|
2016-06-09 18:14:36 +03:00
|
|
|
versionMatch(req, res, nextStub);
|
2017-08-22 12:59:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
it('should call next if request does not include a version', function () {
|
2020-04-25 23:06:33 +03:00
|
|
|
const server = '1.5.1';
|
2017-08-22 12:59:01 +03:00
|
|
|
|
|
|
|
testVersionMatch(server);
|
2016-06-09 18:14:36 +03:00
|
|
|
|
|
|
|
nextStub.calledOnce.should.be.true();
|
|
|
|
nextStub.firstCall.args.should.be.empty();
|
|
|
|
});
|
|
|
|
|
2017-08-22 12:59:01 +03:00
|
|
|
it('should call next if versions are an exact match', function () {
|
2020-04-25 23:06:33 +03:00
|
|
|
const server = '1.5.0';
|
|
|
|
const client = '1.5';
|
2017-08-22 12:59:01 +03:00
|
|
|
|
|
|
|
testVersionMatch(server, client);
|
2016-06-09 18:14:36 +03:00
|
|
|
|
|
|
|
nextStub.calledOnce.should.be.true();
|
|
|
|
nextStub.firstCall.args.should.be.empty();
|
|
|
|
});
|
|
|
|
|
2017-08-22 12:59:01 +03:00
|
|
|
it('should call next if client version is earlier than server', function () {
|
2020-04-25 23:06:33 +03:00
|
|
|
const server = '1.5.0';
|
|
|
|
const client = '1.3';
|
2017-08-22 12:59:01 +03:00
|
|
|
|
|
|
|
testVersionMatch(server, client);
|
|
|
|
|
|
|
|
nextStub.calledOnce.should.be.true();
|
|
|
|
nextStub.firstCall.args.should.be.empty();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw VersionMismatchError if client version is earlier by a major version', function () {
|
2020-04-25 23:06:33 +03:00
|
|
|
const server = '2.5.0';
|
|
|
|
const client = '1.3';
|
2017-08-22 12:59:01 +03:00
|
|
|
|
|
|
|
testVersionMatch(server, client);
|
|
|
|
|
|
|
|
nextStub.calledOnce.should.be.true();
|
|
|
|
nextStub.firstCall.args.should.have.lengthOf(1);
|
|
|
|
nextStub.firstCall.args[0].should.have.property('errorType', 'VersionMismatchError');
|
|
|
|
nextStub.firstCall.args[0].should.have.property('statusCode', 400);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw VersionMismatchError if client version is later than server', function () {
|
2020-04-25 23:06:33 +03:00
|
|
|
const server = '1.3.0';
|
|
|
|
const client = '1.5';
|
2017-08-22 12:59:01 +03:00
|
|
|
|
|
|
|
testVersionMatch(server, client);
|
|
|
|
|
|
|
|
nextStub.calledOnce.should.be.true();
|
|
|
|
nextStub.firstCall.args.should.have.lengthOf(1);
|
|
|
|
nextStub.firstCall.args[0].should.have.property('errorType', 'VersionMismatchError');
|
|
|
|
nextStub.firstCall.args[0].should.have.property('statusCode', 400);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw VersionMismatchError if client version is later by a major version', function () {
|
2020-04-25 23:06:33 +03:00
|
|
|
const server = '1.5.0';
|
|
|
|
const client = '2.3';
|
2017-08-22 12:59:01 +03:00
|
|
|
|
|
|
|
testVersionMatch(server, client);
|
2016-06-09 18:14:36 +03:00
|
|
|
|
|
|
|
nextStub.calledOnce.should.be.true();
|
|
|
|
nextStub.firstCall.args.should.have.lengthOf(1);
|
|
|
|
nextStub.firstCall.args[0].should.have.property('errorType', 'VersionMismatchError');
|
|
|
|
nextStub.firstCall.args[0].should.have.property('statusCode', 400);
|
|
|
|
});
|
2018-01-03 22:20:18 +03:00
|
|
|
|
|
|
|
it('should call next if pre-release is allowed', function () {
|
2020-04-25 23:06:33 +03:00
|
|
|
const server = '1.5.0-pre';
|
|
|
|
const client = '1.4';
|
2018-01-03 22:20:18 +03:00
|
|
|
|
|
|
|
testVersionMatch(server, client);
|
|
|
|
|
|
|
|
nextStub.calledOnce.should.be.true();
|
|
|
|
nextStub.firstCall.args.should.be.empty();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws error if server is a pre-release, but later by major version', function () {
|
2020-04-25 23:06:33 +03:00
|
|
|
const server = '2.0.0-alpha';
|
|
|
|
const client = '1.5';
|
2018-01-03 22:20:18 +03:00
|
|
|
|
|
|
|
testVersionMatch(server, client);
|
|
|
|
|
|
|
|
nextStub.calledOnce.should.be.true();
|
|
|
|
nextStub.firstCall.args.should.have.lengthOf(1);
|
|
|
|
nextStub.firstCall.args[0].should.have.property('errorType', 'VersionMismatchError');
|
|
|
|
nextStub.firstCall.args[0].should.have.property('statusCode', 400);
|
|
|
|
});
|
2016-06-09 18:14:36 +03:00
|
|
|
});
|