From 1a3ae578af6b446b1ec9e1b55715dd730559d14c Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Mon, 16 Nov 2015 18:16:59 +0000 Subject: [PATCH] Add filter param for tags & users refs #5604 - `filter` is missing from tags & users - add it in and add tests which show it works --- core/server/models/tag.js | 2 +- core/server/models/user.js | 2 +- .../integration/api/advanced_browse_spec.js | 75 ++++++++++++++++++- .../test/utils/fixtures/filter-param/index.js | 27 ++++++- 4 files changed, 99 insertions(+), 7 deletions(-) diff --git a/core/server/models/tag.js b/core/server/models/tag.js index 408832e7c4..b891b3827b 100644 --- a/core/server/models/tag.js +++ b/core/server/models/tag.js @@ -79,7 +79,7 @@ Tag = ghostBookshelf.Model.extend({ // whitelists for the `options` hash argument on methods, by method name. // these are the only options that can be passed to Bookshelf / Knex. validOptions = { - findPage: ['page', 'limit', 'columns', 'order'] + findPage: ['page', 'limit', 'columns', 'filter', 'order'] }; if (validOptions[methodName]) { diff --git a/core/server/models/user.js b/core/server/models/user.js index 904fd527a8..8809de22c5 100644 --- a/core/server/models/user.js +++ b/core/server/models/user.js @@ -228,7 +228,7 @@ User = ghostBookshelf.Model.extend({ findOne: ['withRelated', 'status'], setup: ['id'], edit: ['withRelated', 'id'], - findPage: ['page', 'limit', 'columns', 'order', 'status'] + findPage: ['page', 'limit', 'columns', 'filter', 'order', 'status'] }; if (validOptions[methodName]) { diff --git a/core/test/integration/api/advanced_browse_spec.js b/core/test/integration/api/advanced_browse_spec.js index cbf1073acd..8125281d41 100644 --- a/core/test/integration/api/advanced_browse_spec.js +++ b/core/test/integration/api/advanced_browse_spec.js @@ -178,7 +178,7 @@ describe('Filter Param Spec', function () { describe('5. Users - filter="posts.tags:photo" order="posts.count DESC" limit="3"', function () { it('Will fetch the 3 most prolific users who write posts with the tag `photo` ordered by most posts.', function (done) { - UserAPI.browse({filter: 'posts.tags:photo', order: 'posts.count DESC', limit: 3}).then(function (result) { + UserAPI.browse({filter: 'posts.tags:special', order: 'posts.count DESC', limit: 3}).then(function (result) { var ids; // 1. Result should have the correct base structure should.exist(result); @@ -230,6 +230,79 @@ describe('Filter Param Spec', function () { }).catch(done); }); }); + + describe('7. Users filter: "website:-null", order: "website"', function () { + it('Will fetch users that have a website and order them by website', function (done) { + UserAPI.browse({filter: 'website:-null', order: 'website ASC'}).then(function (result) { + var ids; + // 1. Result should have the correct base structure + should.exist(result); + result.should.have.property('users'); + result.should.have.property('meta'); + + // 2. The data part of the response should be correct + // We should have 2 matching items + result.users.should.be.an.Array.with.lengthOf(2); + + ids = _.pluck(result.users, 'id'); + ids.should.eql([2, 1]); + + should.exist(result.users[0].website); + should.exist(result.users[1].website); + + // 3. The meta object should contain the right details + result.meta.should.have.property('pagination'); + result.meta.pagination.should.be.an.Object.with.properties(['page', 'limit', 'pages', 'total', 'next', 'prev']); + result.meta.pagination.page.should.eql(1); + result.meta.pagination.limit.should.eql(15); + result.meta.pagination.pages.should.eql(1); + result.meta.pagination.total.should.eql(2); + should.equal(result.meta.pagination.next, null); + should.equal(result.meta.pagination.prev, null); + + done(); + }).catch(done); + }); + }); + + describe('8. Tags filter: "image:-null+description:-null"', function () { + it('Will fetch tags which have an image and a description', function (done) { + TagAPI.browse({filter: 'image:-null+description:-null', order: 'name ASC'}).then(function (result) { + var ids; + // 1. Result should have the correct base structure + should.exist(result); + result.should.have.property('tags'); + result.should.have.property('meta'); + + // 2. The data part of the response should be correct + // We should have 3 matching items + result.tags.should.be.an.Array.with.lengthOf(3); + + ids = _.pluck(result.tags, 'id'); + ids.should.eql([4, 3, 2]); + + should.exist(result.tags[0].image); + should.exist(result.tags[1].image); + should.exist(result.tags[2].image); + + should.exist(result.tags[0].description); + should.exist(result.tags[1].description); + should.exist(result.tags[2].description); + + // 3. The meta object should contain the right details + result.meta.should.have.property('pagination'); + result.meta.pagination.should.be.an.Object.with.properties(['page', 'limit', 'pages', 'total', 'next', 'prev']); + result.meta.pagination.page.should.eql(1); + result.meta.pagination.limit.should.eql(15); + result.meta.pagination.pages.should.eql(1); + result.meta.pagination.total.should.eql(3); + should.equal(result.meta.pagination.next, null); + should.equal(result.meta.pagination.prev, null); + + done(); + }).catch(done); + }); + }); }); describe.skip('Count capabilities', function () { diff --git a/core/test/utils/fixtures/filter-param/index.js b/core/test/utils/fixtures/filter-param/index.js index d3e957b9b8..7bab5d3ae0 100644 --- a/core/test/utils/fixtures/filter-param/index.js +++ b/core/test/utils/fixtures/filter-param/index.js @@ -14,22 +14,33 @@ data.tags = [ { name: 'photo', slug: 'photo', + image: 'some/image/path.jpg', + description: 'Photo posts', created_by: 2 }, { name: 'Video', slug: 'video', + image: 'some/image/path.jpg', + description: 'Video posts', created_by: 1 }, { name: 'Audio', slug: 'audio', + image: 'some/image/path.jpg', + description: 'Audio posts', created_by: 1 }, { name: 'No Posts', slug: 'no-posts', created_by: 2 + }, + { + name: 'Special', + slug: 'special', + created_by: 2 } ]; @@ -39,12 +50,20 @@ data.users = [ name: 'Leslie Jones', slug: 'leslie', email: 'ljones@nothere.com', - password: '$2a$10$.pZeeBE0gHXd0PTnbT/ph.GEKgd0Wd3q2pWna3ynTGBkPKnGIKZL6' + password: '$2a$10$.pZeeBE0gHXd0PTnbT/ph.GEKgd0Wd3q2pWna3ynTGBkPKnGIKZL6', + website: 'http://twitter.com/ljonestestuser' }, { name: 'Pat Smith', slug: 'pat-smith', email: 'pat-smith@nothere.com', + password: '$2a$10$.pZeeBE0gHXd0PTnbT/ph.GEKgd0Wd3q2pWna3ynTGBkPKnGIKZL6', + website: 'http://github.com/patsmithtestuser' + }, + { + name: 'Cameron Howe', + slug: 'camhowe', + email: 'camhowe@c-e-is-real.com', password: '$2a$10$.pZeeBE0gHXd0PTnbT/ph.GEKgd0Wd3q2pWna3ynTGBkPKnGIKZL6' } ]; @@ -64,7 +83,7 @@ data.posts = [ markdown: 'Hello World!', featured: false, author_id: 2, - tags: [2, 3, 4] + tags: [2, 3, 4, 6] }, { title: 'Third Post', @@ -88,7 +107,7 @@ data.posts = [ markdown: 'Hello World!', featured: true, author_id: 2, - tags: [] + tags: [6] }, { title: 'Sixth Post', @@ -97,7 +116,7 @@ data.posts = [ featured: false, author_id: 2, image: 'some/image/path.jpg', - tags: [1, 4] + tags: [1, 4, 6] }, { title: 'Seventh Post',