0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Added helper to check for content API (#10104)

no issue

* Added helper to check if API called is content API
This commit is contained in:
Rishabh Garg 2018-11-06 17:36:22 +05:30 committed by GitHub
parent 22911b5812
commit 3345618731
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 0 deletions

View file

@ -9,5 +9,16 @@ module.exports = {
get validators() {
return require('./validators');
},
/**
* TODO: We need to check for public context as permission stage overrides
* the whole context object currently: https://github.com/TryGhost/Ghost/issues/10099
*/
isContentAPI: (frame) => {
return !!(Object.keys(frame.options.context).length === 0 ||
(!frame.options.context.user && frame.options.context.api_key_id) ||
frame.options.context.public
);
}
};

View file

@ -0,0 +1,55 @@
const should = require('should');
const sinon = require('sinon');
const utils = require('../../../../server/api/v2/utils');
const sandbox = sinon.sandbox.create();
describe('Unit: v2/utils/index', function () {
afterEach(function () {
sandbox.restore();
});
describe('isContentAPI', function () {
it('is truthy when having api key and no user', function () {
const frame = {
options: {
context: {
api_key_id: 'api_key'
}
}
};
should(utils.isContentAPI(frame)).equal(true);
});
it('is falsy when having api key and a user', function () {
const frame = {
options: {
context: {
user: {},
api_key_id: 'api_key'
}
}
};
should(utils.isContentAPI(frame)).equal(false);
});
it('is truthy when context is empty', function () {
const frame = {
options: {
context: {
}
}
};
should(utils.isContentAPI(frame)).equal(true);
});
it('is truthy when context is public', function () {
const frame = {
options: {
context: {
public: true
}
}
};
should(utils.isContentAPI(frame)).equal(true);
});
});
});