Ghost/ghost/mw-api-version-mismatch/test/mw-api-version-mismatch.test.js
Hannah Wolfe 6161f94910
Updated to use assert/strict everywhere (#17047)
refs: https://github.com/TryGhost/Toolbox/issues/595

We're rolling out new rules around the node assert library, the first of which is enforcing the use of assert/strict. This means we don't need to use the strict version of methods, as the standard version will work that way by default.

This caught some gotchas in our existing usage of assert where the lack of strict mode had unexpected results:
- Url matching needs to be done on `url.href` see aa58b354a4
- Null and undefined are not the same thing,  there were a few cases of this being confused
- Particularly questionable changes in [PostExporter tests](c1a468744b) tracked [here](https://github.com/TryGhost/Team/issues/3505).
- A typo see eaac9c293a

Moving forward, using assert strict should help us to catch unexpected behaviour, particularly around nulls and undefineds during implementation.
2023-06-21 09:56:59 +01:00

104 lines
4.4 KiB
JavaScript

const assert = require('assert/strict');
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 = {
originalUrl: '/api/admin/posts/1',
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');
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 = {
originalUrl: '/api/admin/posts/1?tim_me=please',
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');
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', 'trims query string');
assert.equal(APIVersionCompatibilityService.handleMismatch.args[0][0].userAgent, 'Zapier/2.1 GhostAdminSDK/3.28');
assert.equal(APIVersionCompatibilityService.handleMismatch.args[0][0].apiKeyValue, 'content_api_key_secret');
assert.equal(APIVersionCompatibilityService.handleMismatch.args[0][0].apiKeyType, 'content');
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();
});
});
});