From a6b4a4287d10efcdf898093b207450d7a8848941 Mon Sep 17 00:00:00 2001 From: Ashwani Yadav Date: Tue, 10 Mar 2020 20:03:55 +0530 Subject: [PATCH] updated function of query and added tests --- app/api/server/v1/roles.js | 25 +++++++++++-------------- tests/end-to-end/api/13-roles.js | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/app/api/server/v1/roles.js b/app/api/server/v1/roles.js index aba5442fb1f0..b72de646fa12 100644 --- a/app/api/server/v1/roles.js +++ b/app/api/server/v1/roles.js @@ -7,28 +7,25 @@ import { getUsersInRole, hasPermission } from '../../../authorization/server'; API.v1.addRoute('roles.list', { authRequired: true }, { get() { - const roles = Roles.find({}, { fields: { _updatedAt: 0 } }).fetch(); + const { updatedSince } = this.queryParams; - return API.v1.success({ roles }); - }, -}); + if (!updatedSince) { + const roles = Roles.find({}, { fields: { _updatedAt: 0 } }).fetch(); -// api to to get updated roles after a date(in ISODate format) -API.v1.addRoute('roles.listByUpdatedDate', { authRequired: true }, { - get() { - const { updatedAfter } = this.queryParams; + return API.v1.success({ roles }); + } - let updatedAfterDate; - if (updatedAfter) { - if (isNaN(Date.parse(updatedAfter))) { - throw new Meteor.Error('error-updatedAfter-param-invalid', 'The "updatedAfter" query parameter must be a valid date.'); + let updatedSinceDate; + if (updatedSince) { + if (isNaN(Date.parse(updatedSince))) { + throw new Meteor.Error('error-updatedSince-param-invalid', 'The "updatedSince" query parameter must be a valid date.'); } else { - updatedAfterDate = new Date(updatedAfter); + updatedSinceDate = new Date(updatedSince); } } return API.v1.success({ - roles: Roles.findByUpdatedDate(updatedAfterDate, { fields: API.v1.defaultFieldsToExclude }).fetch(), + roles: Roles.findByUpdatedDate(updatedSinceDate, { fields: API.v1.defaultFieldsToExclude }).fetch(), }); }, }); diff --git a/tests/end-to-end/api/13-roles.js b/tests/end-to-end/api/13-roles.js index 26b867ee154a..9ef3f6ce4823 100644 --- a/tests/end-to-end/api/13-roles.js +++ b/tests/end-to-end/api/13-roles.js @@ -33,6 +33,30 @@ describe('[Roles]', function() { }) .end(done); }); + + it('should return an array of roles which are updated after updatedSice date when search by "updatedSince" query parameter', (done) => { + request.get(api('roles.list?updatedSince=2018-11-27T13:52:01Z')) + .set(credentials) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('update').and.to.be.an('array'); + expect(res.body).to.have.property('remove').and.to.be.an('array'); + }) + .end(done); + }); + + it('should return an error when updatedSince query parameter is not a valid ISODate string', (done) => { + request.get(api('roles.list?updatedSince=fsafdf')) + .set(credentials) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + }) + .end(done); + }); }); describe('POST [/roles.create]', () => {