-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
320 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { getAllUsersService } from '../services/user.services'; | ||
import Helper from '../services/helper'; | ||
|
||
/** | ||
* @description Get list of all users | ||
* @param {object} request | ||
* @param {object} response | ||
* @returns {object} List of all users | ||
*/ | ||
const getUsers = async (request, response) => { | ||
const value = await getAllUsersService(); | ||
|
||
return Helper.successResponse(response, 200, value); | ||
}; | ||
|
||
export default { getUsers }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* @description Wraps a controller function in a try-catch block | ||
* @param {object} request | ||
* @param {object} response | ||
* @returns Status code and error message if an error is thrown | ||
*/ | ||
|
||
/* istanbul ignore next */ | ||
const tryCatch = controller => async (request, response) => { | ||
try { | ||
await controller(request, response); | ||
} catch (error) { | ||
return response | ||
.status(500) | ||
.json({ status: 'error', message: error.message }); | ||
} | ||
return true; | ||
}; | ||
|
||
export default tryCatch; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import auth from './auth.route'; | ||
import users from './user.routes'; | ||
|
||
export default app => { | ||
app.use('/api/v1/users', auth); | ||
app.use('/api/v1/users/', users); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import express from 'express'; | ||
import authentication from '../../middlewares/auth.middleware'; | ||
import userController from '../../controllers/user.controller'; | ||
import tryCatch from '../../helpers/tryCatch.helper'; | ||
|
||
const router = express.Router(); | ||
|
||
router.get('/', authentication.verifyToken, tryCatch(userController.getUsers)); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import model from '../db/models'; | ||
|
||
const { User } = model; | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export const getAllUsersService = () => { | ||
const users = User.findAll({ | ||
attributes: { | ||
exclude: ['password', 'confirmEmailCode'] | ||
} | ||
}); | ||
return users; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import chai from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import sinonChai from 'sinon-chai'; | ||
import { getUser } from '../utils/db.utils'; | ||
import app from '../../index'; | ||
|
||
const { expect, request } = chai; | ||
|
||
chai.use(chaiHttp); | ||
chai.use(sinonChai); | ||
let userToken; | ||
describe('User API endpoints', () => { | ||
let responseHook; | ||
before('sign up a random user', async () => { | ||
const randomUser = getUser(); | ||
responseHook = await request(app) | ||
.post('/api/v1/users/signup') | ||
.send(randomUser); | ||
}); | ||
describe('GET /users', async () => { | ||
it('should get all users', done => { | ||
userToken = responseHook.body.data.token; | ||
request(app) | ||
.get('/api/v1/users') | ||
.set({ Authorization: `Bearer ${userToken}` }) | ||
.end((error, response) => { | ||
expect(response.status).to.be.equal(200); | ||
expect(response.body.data).to.be.an('array'); | ||
expect(response.body.data[0]).to.have.property('id'); | ||
expect(response.body.data[0]).to.have.property('firstName'); | ||
done(); | ||
}); | ||
}); | ||
it('should not get all users if token is invalid', done => { | ||
request(app) | ||
.get('/api/v1/users') | ||
.end((error, response) => { | ||
expect(response.status).to.be.equal(400); | ||
expect(response.body).to.have.property('error'); | ||
expect(response.body.error).to.eq( | ||
'No token provided, you do not have access to this page' | ||
); | ||
done(); | ||
console.log(response.body); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,7 +61,6 @@ const UserValidator = { | |
]; | ||
|
||
default: | ||
return []; | ||
} | ||
}, | ||
|
||
|
Oops, something went wrong.