2023-06-21 11:56:59 +03:00
|
|
|
const assert = require('assert/strict');
|
2022-04-21 15:29:52 +03:00
|
|
|
const sinon = require('sinon');
|
|
|
|
const errors = require('@tryghost/errors');
|
|
|
|
|
|
|
|
const versionMismatchMW = require('../index');
|
|
|
|
|
|
|
|
describe('mw-api-version-mismatch', function () {
|
|
|
|
it('Does call handle mismatch when a generic RequestNotAcceptableError is used', function (done) {
|
|
|
|
const APIVersionCompatibilityService = {
|
|
|
|
handleMismatch: sinon.stub().resolves()
|
|
|
|
};
|
|
|
|
const req = {
|
2022-05-09 10:33:40 +03:00
|
|
|
originalUrl: '/api/admin/posts/1',
|
|
|
|
headers: {
|
|
|
|
'accept-version': 'v3.28',
|
|
|
|
'user-agent': 'Zapier/2.1 GhostAdminSDK/3.28'
|
|
|
|
}
|
2022-04-21 15:29:52 +03:00
|
|
|
};
|
|
|
|
const res = {
|
2022-05-09 10:33:40 +03:00
|
|
|
locals: {
|
|
|
|
safeVersion: '4.46'
|
|
|
|
}
|
2022-04-21 15:29:52 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
versionMismatchMW(APIVersionCompatibilityService)(new errors.RequestNotAcceptableError({
|
|
|
|
code: 'UPDATE_CLIENT'
|
|
|
|
}), req, res, () => {
|
|
|
|
assert.equal(APIVersionCompatibilityService.handleMismatch.called, true);
|
2022-05-10 11:44:18 +03:00
|
|
|
assert.deepEqual(Object.keys(APIVersionCompatibilityService.handleMismatch.args[0][0]), [
|
|
|
|
'acceptVersion',
|
|
|
|
'contentVersion',
|
|
|
|
'requestURL',
|
|
|
|
'userAgent',
|
|
|
|
'apiKeyValue',
|
|
|
|
'apiKeyType'
|
|
|
|
], 'handleMismatch called with wrong arguments');
|
|
|
|
|
|
|
|
assert.equal(APIVersionCompatibilityService.handleMismatch.args[0][0].acceptVersion, 'v3.28');
|
|
|
|
assert.equal(APIVersionCompatibilityService.handleMismatch.args[0][0].contentVersion, 'v4.46');
|
|
|
|
assert.equal(APIVersionCompatibilityService.handleMismatch.args[0][0].requestURL, '/api/admin/posts/1');
|
|
|
|
assert.equal(APIVersionCompatibilityService.handleMismatch.args[0][0].userAgent, 'Zapier/2.1 GhostAdminSDK/3.28');
|
|
|
|
assert.equal(APIVersionCompatibilityService.handleMismatch.args[0][0].apiKeyValue, null);
|
|
|
|
assert.equal(APIVersionCompatibilityService.handleMismatch.args[0][0].apiKeyType, null);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Does call handle mismatch when with correct API key values when identification information is in the request', function (done) {
|
|
|
|
const APIVersionCompatibilityService = {
|
|
|
|
handleMismatch: sinon.stub().resolves()
|
|
|
|
};
|
|
|
|
const req = {
|
2022-05-11 05:40:09 +03:00
|
|
|
originalUrl: '/api/admin/posts/1?tim_me=please',
|
2022-05-10 11:44:18 +03:00
|
|
|
query: {
|
|
|
|
key: 'content_api_key_secret'
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
'accept-version': 'v3.28',
|
|
|
|
'user-agent': 'Zapier/2.1 GhostAdminSDK/3.28'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const res = {
|
|
|
|
locals: {
|
|
|
|
safeVersion: '4.46'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
versionMismatchMW(APIVersionCompatibilityService)(new errors.RequestNotAcceptableError({
|
|
|
|
code: 'UPDATE_CLIENT'
|
|
|
|
}), req, res, () => {
|
|
|
|
assert.equal(APIVersionCompatibilityService.handleMismatch.called, true);
|
|
|
|
assert.deepEqual(Object.keys(APIVersionCompatibilityService.handleMismatch.args[0][0]), [
|
|
|
|
'acceptVersion',
|
|
|
|
'contentVersion',
|
|
|
|
'requestURL',
|
|
|
|
'userAgent',
|
|
|
|
'apiKeyValue',
|
|
|
|
'apiKeyType'
|
|
|
|
], 'handleMismatch called with wrong arguments');
|
|
|
|
|
2022-05-09 10:33:40 +03:00
|
|
|
assert.equal(APIVersionCompatibilityService.handleMismatch.args[0][0].acceptVersion, 'v3.28');
|
|
|
|
assert.equal(APIVersionCompatibilityService.handleMismatch.args[0][0].contentVersion, 'v4.46');
|
2022-05-11 05:40:09 +03:00
|
|
|
assert.equal(APIVersionCompatibilityService.handleMismatch.args[0][0].requestURL, '/api/admin/posts/1', 'trims query string');
|
2022-05-09 10:33:40 +03:00
|
|
|
assert.equal(APIVersionCompatibilityService.handleMismatch.args[0][0].userAgent, 'Zapier/2.1 GhostAdminSDK/3.28');
|
2022-05-10 11:44:18 +03:00
|
|
|
assert.equal(APIVersionCompatibilityService.handleMismatch.args[0][0].apiKeyValue, 'content_api_key_secret');
|
|
|
|
assert.equal(APIVersionCompatibilityService.handleMismatch.args[0][0].apiKeyType, 'content');
|
2022-05-09 10:33:40 +03:00
|
|
|
|
2022-04-21 15:29:52 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Does NOT call handle mismatch when a generic RequestNotAcceptableError is used', function (done) {
|
|
|
|
const APIVersionCompatibilityService = {
|
|
|
|
handleMismatch: sinon.stub().resolves()
|
|
|
|
};
|
|
|
|
|
|
|
|
versionMismatchMW(APIVersionCompatibilityService)(new errors.RequestNotAcceptableError(), {}, {}, () => {
|
|
|
|
assert.equal(APIVersionCompatibilityService.handleMismatch.called, false);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|