From 7a996ecbe713642a0f5c6507c92b8deedcad5738 Mon Sep 17 00:00:00 2001 From: cobbspur Date: Thu, 22 Oct 2015 15:08:15 +0100 Subject: [PATCH] Simplify fields and includes prior to fetch No Issue - allows comma separated include and field parameters to also have a space - allows capitals in include and field parameters --- core/server/api/utils.js | 25 ++++++------ core/test/integration/api/api_posts_spec.js | 44 +++++++++++++++++++++ 2 files changed, 58 insertions(+), 11 deletions(-) diff --git a/core/server/api/utils.js b/core/server/api/utils.js index 1d3ca57df7..9072298074 100644 --- a/core/server/api/utils.js +++ b/core/server/api/utils.js @@ -113,7 +113,7 @@ utils = { slug: {isSlug: true}, page: {matches: /^\d+$/}, limit: {matches: /^\d+|all$/}, - fields: {matches: /^[a-z0-9_,]+$/}, + fields: {matches: /^[\w, ]+$/}, name: {} }, // these values are sanitised/validated separately @@ -218,20 +218,23 @@ utils = { }; }, - prepareInclude: function prepareInclude(include, allowedIncludes) { - include = include || ''; - include = _.intersection(include.split(','), allowedIncludes); + trimAndLowerCase: function trimAndLowerCase(params) { + params = params || ''; + if (_.isString(params)) { + params = params.split(','); + } - return include; + return _.map(params, function (item) { + return item.trim().toLowerCase(); + }); + }, + + prepareInclude: function prepareInclude(include, allowedIncludes) { + return _.intersection(this.trimAndLowerCase(include), allowedIncludes); }, prepareFields: function prepareFields(fields) { - fields = fields || ''; - if (_.isString(fields)) { - fields = fields.split(','); - } - - return fields; + return this.trimAndLowerCase(fields); }, /** diff --git a/core/test/integration/api/api_posts_spec.js b/core/test/integration/api/api_posts_spec.js index 904d620d9a..23e84f041b 100644 --- a/core/test/integration/api/api_posts_spec.js +++ b/core/test/integration/api/api_posts_spec.js @@ -210,6 +210,26 @@ describe('Post API', function () { }).catch(done); }); + it('can include author and be case insensitive', function (done) { + PostAPI.browse({context: {user: 1}, status: 'all', include: 'Author'}).then(function (results) { + should.exist(results.posts); + should.exist(results.posts[0].author.name); + results.posts[0].author.name.should.eql('Joe Bloggs'); + + done(); + }).catch(done); + }); + + it('can include author and ignore space in include', function (done) { + PostAPI.browse({context: {user: 1}, status: 'all', include: ' author'}).then(function (results) { + should.exist(results.posts); + should.exist(results.posts[0].author.name); + results.posts[0].author.name.should.eql('Joe Bloggs'); + + done(); + }).catch(done); + }); + it('can fetch all posts for an author', function (done) { PostAPI.browse({context: {user: 1}, status: 'all', author: 'joe-bloggs'}).then(function (results) { should.exist(results.posts); @@ -265,6 +285,30 @@ describe('Post API', function () { }).catch(done); }); + it('with context.user can fetch multiple fields and be case insensitive', 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 multiple fields ignoring spaces', 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;