Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions routers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion test/users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
});
});