diff --git a/routers/users.js b/routers/users.js index 8bed407..7acffac 100644 --- a/routers/users.js +++ b/routers/users.js @@ -109,11 +109,17 @@ 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(user); + } 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