From 5c39adb7313e0696aabb4b71196ab7b0b378c359 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 16 Aug 2016 22:31:45 +0200 Subject: Server: add user list sort/pagination --- server/tests/api/check-params.js | 26 +++++++++++++ server/tests/api/users.js | 83 ++++++++++++++++++++++++++++++++++++++-- server/tests/utils/users.js | 15 ++++++++ 3 files changed, 120 insertions(+), 4 deletions(-) (limited to 'server/tests') diff --git a/server/tests/api/check-params.js b/server/tests/api/check-params.js index 882948fac..fc8b0a42a 100644 --- a/server/tests/api/check-params.js +++ b/server/tests/api/check-params.js @@ -459,6 +459,32 @@ describe('Test parameters validator', function () { let userId = null let userAccessToken = null + describe('When listing users', function () { + it('Should fail with a bad start pagination', function (done) { + request(server.url) + .get(path) + .query({ start: 'hello' }) + .set('Accept', 'application/json') + .expect(400, done) + }) + + it('Should fail with a bad count pagination', function (done) { + request(server.url) + .get(path) + .query({ count: 'hello' }) + .set('Accept', 'application/json') + .expect(400, done) + }) + + it('Should fail with an incorrect sort', function (done) { + request(server.url) + .get(path) + .query({ sort: 'hello' }) + .set('Accept', 'application/json') + .expect(400, done) + }) + }) + describe('When adding a new user', function () { it('Should fail with a too small username', function (done) { const data = { diff --git a/server/tests/api/users.js b/server/tests/api/users.js index a2557d2ab..c6c892bf2 100644 --- a/server/tests/api/users.js +++ b/server/tests/api/users.js @@ -209,17 +209,92 @@ describe('Test users', function () { usersUtils.getUsersList(server.url, function (err, res) { if (err) throw err - const users = res.body.data + const result = res.body + const total = result.total + const users = result.data + expect(total).to.equal(2) expect(users).to.be.an('array') expect(users.length).to.equal(2) - const rootUser = users[0] + const user = users[0] + expect(user.username).to.equal('user_1') + + const rootUser = users[1] expect(rootUser.username).to.equal('root') + userId = user.id + + done() + }) + }) + + it('Should list only the first user by username asc', function (done) { + usersUtils.getUsersListPaginationAndSort(server.url, 0, 1, 'username', function (err, res) { + if (err) throw err + + const result = res.body + const total = result.total + const users = result.data + + expect(total).to.equal(2) + expect(users.length).to.equal(1) - const user = users[1] + const user = users[0] + expect(user.username).to.equal('root') + + done() + }) + }) + + it('Should list only the first user by username desc', function (done) { + usersUtils.getUsersListPaginationAndSort(server.url, 0, 1, '-username', function (err, res) { + if (err) throw err + + const result = res.body + const total = result.total + const users = result.data + + expect(total).to.equal(2) + expect(users.length).to.equal(1) + + const user = users[0] expect(user.username).to.equal('user_1') - userId = user.id + + done() + }) + }) + + it('Should list only the second user by createdDate desc', function (done) { + usersUtils.getUsersListPaginationAndSort(server.url, 0, 1, '-createdDate', function (err, res) { + if (err) throw err + + const result = res.body + const total = result.total + const users = result.data + + expect(total).to.equal(2) + expect(users.length).to.equal(1) + + const user = users[0] + expect(user.username).to.equal('user_1') + + done() + }) + }) + + it('Should list all the users by createdDate asc', function (done) { + usersUtils.getUsersListPaginationAndSort(server.url, 0, 2, 'createdDate', function (err, res) { + if (err) throw err + + const result = res.body + const total = result.total + const users = result.data + + expect(total).to.equal(2) + expect(users.length).to.equal(2) + + expect(users[0].username).to.equal('root') + expect(users[1].username).to.equal('user_1') done() }) diff --git a/server/tests/utils/users.js b/server/tests/utils/users.js index 3b560e409..0cf4e4adb 100644 --- a/server/tests/utils/users.js +++ b/server/tests/utils/users.js @@ -6,6 +6,7 @@ const usersUtils = { createUser: createUser, getUserInformation: getUserInformation, getUsersList: getUsersList, + getUsersListPaginationAndSort: getUsersListPaginationAndSort, removeUser: removeUser, updateUser: updateUser } @@ -52,6 +53,20 @@ function getUsersList (url, end) { .end(end) } +function getUsersListPaginationAndSort (url, start, count, sort, end) { + const path = '/api/v1/users' + + request(url) + .get(path) + .query({ start: start }) + .query({ count: count }) + .query({ sort: sort }) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) + .end(end) +} + function removeUser (url, userId, accessToken, expectedStatus, end) { if (!end) { end = expectedStatus -- cgit v1.2.3