0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00
ghost/test/unit/web/api/middleware/version-match_spec.js
Hannah Wolfe 41c3b4e92b Moved version-match mw into api app
- Moved version-match from shared to api as it is not shared (except within the API)
- This file is only used in one part of the app, this updates the code structure to reflect this
- This is one of many similar changes needed to make it easier to refactor to the existing setup
2020-04-22 07:12:25 +01:00

121 lines
3.8 KiB
JavaScript

var should = require('should'),
sinon = require('sinon'),
versionMatch = require('../../../../../core/server/web/api/middleware/version-match');
describe('Version Mismatch', function () {
var req, res, getStub, nextStub;
afterEach(function () {
sinon.restore();
});
beforeEach(function () {
getStub = sinon.stub();
nextStub = sinon.stub();
req = {
get: getStub
};
res = {
locals: {}
};
});
function testVersionMatch(serverVersion, clientVersion) {
// Set the server version
res.locals.version = serverVersion;
if (clientVersion) {
// Optionally set the client version
getStub.returns(clientVersion);
}
versionMatch(req, res, nextStub);
}
it('should call next if request does not include a version', function () {
var server = '1.5.1';
testVersionMatch(server);
nextStub.calledOnce.should.be.true();
nextStub.firstCall.args.should.be.empty();
});
it('should call next if versions are an exact match', function () {
var server = '1.5.0',
client = '1.5';
testVersionMatch(server, client);
nextStub.calledOnce.should.be.true();
nextStub.firstCall.args.should.be.empty();
});
it('should call next if client version is earlier than server', function () {
var server = '1.5.0',
client = '1.3';
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 () {
var server = '2.5.0',
client = '1.3';
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 () {
var server = '1.3.0',
client = '1.5';
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 () {
var server = '1.5.0',
client = '2.3';
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 call next if pre-release is allowed', function () {
var server = '1.5.0-pre',
client = '1.4';
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 () {
var server = '2.0.0-alpha',
client = '1.5';
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);
});
});