From bbd2d9774f1aaf7cd58c39d1f589339acc73d680 Mon Sep 17 00:00:00 2001 From: Shinh18 Date: Sat, 12 Dec 2020 02:49:45 -0500 Subject: [PATCH 1/2] test get all users route --- routers/users.js | 6 ++++++ test/users.test.js | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/routers/users.js b/routers/users.js index 8bed407..f9c89ca 100644 --- a/routers/users.js +++ b/routers/users.js @@ -114,6 +114,12 @@ userRouter.get('/s/:search',[param('search').isString({ min: 5, max: 100 })], as if (!users) { throw new Error(`Can not find a user by this username`); } + return res.json(users); + } catch (e) { + console.error(e); + return res.sendStatus(500); + } + }); /** * @api {get} /api/users Fetch all the available users diff --git a/test/users.test.js b/test/users.test.js index a0d4526..af2e096 100644 --- a/test/users.test.js +++ b/test/users.test.js @@ -3,7 +3,7 @@ const app = require('./../server'); var assert = require('assert'); /** - * Testing get books + * Testing get user by id */ describe('GET /api/users/:id', function () { it('check with invalid user id', function (done) { @@ -18,3 +18,16 @@ describe('GET /api/users/:id', function () { }); }); }); + +/** + * Testing get users + */ +describe('GET /api/users', function () { + it('respond with all the users', function (done) { + request(app) + .get('/api/users') + .set('Accept', 'application/json') + .expect('Content-Type', /json/) + .expect(200, done) + }); +}); \ No newline at end of file From 7956c2e99ea18f4779998cf7366583bc47af5c4a Mon Sep 17 00:00:00 2001 From: Shinh18 Date: Sat, 12 Dec 2020 16:38:29 -0500 Subject: [PATCH 2/2] return object instead of array in search user route --- routers/users.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/routers/users.js b/routers/users.js index f9c89ca..7acffac 100644 --- a/routers/users.js +++ b/routers/users.js @@ -109,12 +109,12 @@ userRouter.get('/s/:search',[param('search').isString({ min: 5, max: 100 })], as return res.status(400).json({ errors: errors.array() }); } - const users = await userDoc.find({username : req.params.search}); + const user = await userDoc.find({username : req.params.search}); - if (!users) { + if (!user) { throw new Error(`Can not find a user by this username`); } - return res.json(users); + return res.json(user); } catch (e) { console.error(e); return res.sendStatus(500);