mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-01 23:37:43 +03:00
Remove invalid fields prior to fetch
closes #5601 - Remove invalid fields prior to fetch - Adds initial tests for fields
This commit is contained in:
parent
e09cd70cd0
commit
372907890f
@ -277,6 +277,11 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
|
||||
// Run specific conversion of model query options to where options
|
||||
options = this.processOptions(itemCollection, options);
|
||||
|
||||
// Ensure only valid fields/columns are added to query
|
||||
if (options.columns) {
|
||||
options.columns = _.intersection(options.columns, this.prototype.permittedAttributes());
|
||||
}
|
||||
|
||||
// Prefetch filter objects
|
||||
return Promise.all(baseUtils.filtering.preFetch(filterObjects)).then(function doQuery() {
|
||||
// If there are `where` conditionals specified, add those to the query.
|
||||
|
@ -241,6 +241,43 @@ describe('Post API', function () {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('with context.user can fetch a single field', function (done) {
|
||||
PostAPI.browse({context: {user: 1}, status: 'all', limit: 5, fields: 'title'}).then(function (results) {
|
||||
should.exist(results.posts);
|
||||
|
||||
results.posts[0].title.should.exist;
|
||||
should.not.exist(results.posts[0].slug);
|
||||
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
|
||||
it('with context.user can fetch multiple fields', function (done) {
|
||||
PostAPI.browse({context: {user: 1}, status: 'all', limit: 5, fields: 'slug,published_at'}).then(function (results) {
|
||||
should.exist(results.posts);
|
||||
|
||||
results.posts[0].published_at.should.exist;
|
||||
results.posts[0].slug.should.exist;
|
||||
should.not.exist(results.posts[0].title);
|
||||
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
|
||||
it('with context.user can fetch a field and not return invalid field', function (done) {
|
||||
PostAPI.browse({context: {user: 1}, status: 'all', limit: 5, fields: 'foo,title'}).then(function (results) {
|
||||
var objectKeys;
|
||||
should.exist(results.posts);
|
||||
|
||||
results.posts[0].title.should.exist;
|
||||
should.not.exist(results.posts[0].foo);
|
||||
objectKeys = _.keys(results.posts[0]);
|
||||
objectKeys.length.should.eql(1);
|
||||
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Read', function () {
|
||||
|
Loading…
Reference in New Issue
Block a user