Remove invalid fields prior to fetch

closes #5601

- Remove invalid fields prior to fetch
- Adds initial tests for fields
This commit is contained in:
cobbspur 2015-10-19 18:32:51 +01:00
parent e09cd70cd0
commit 372907890f
2 changed files with 42 additions and 0 deletions

View File

@ -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.

View File

@ -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 () {