-
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.
- Added contoller for the admin to create a role. - Added middleware to check if user is admin. - Added test for role. - Added role in the token. - Changed role data type in the user model.
- Loading branch information
Showing
18 changed files
with
151 additions
and
13 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
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,3 @@ | ||
const validateRoles = data => (data.permissions && data.role && data.tablesAllowed) | ||
&& data; | ||
export default validateRoles; |
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,19 @@ | ||
/* eslint-disable require-jsdoc */ | ||
import model from '../models'; | ||
|
||
const { Permissions } = model; | ||
|
||
class PermitionsController { | ||
static async createRole(req, res) { | ||
try { | ||
const permissions = req.body.permissions.split(','); | ||
const tablesAllowed = req.body.tablesAllowed.split(','); | ||
const role = await Permissions.create({ role: req.body.role, permissions, tablesAllowed }); | ||
return role && res.status(201).json({ message: 'Role successfully created' }); | ||
} catch (error) { | ||
return res.status(400).json({ error: 'tableAllowed, role, and permissions are required ' }); | ||
} | ||
} | ||
} | ||
|
||
export default PermitionsController; |
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,7 @@ | ||
|
||
const checkAdmin = (req, res, next) => ( | ||
req.user.roles.includes('admin') | ||
? next() | ||
: res.status(403).json({ message: 'Not allowed to perform the action' })); | ||
|
||
export default checkAdmin; |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module.exports = { up: (queryInterface, Sequelize) => queryInterface.createTable('Permissions', { id: { allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER }, | ||
role: { type: Sequelize.STRING }, | ||
permissions: { allowNull: false, type: Sequelize.ARRAY(Sequelize.STRING) }, | ||
tablesAllowed: { allowNull: false, type: Sequelize.ARRAY(Sequelize.STRING) }, | ||
createdAt: { allowNull: false, | ||
type: Sequelize.DATE }, | ||
updatedAt: { allowNull: false, | ||
type: Sequelize.DATE } }), | ||
down: (queryInterface, Sequelize) => queryInterface.dropTable('Permissions') }; |
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,11 @@ | ||
|
||
|
||
module.exports = (sequelize, DataTypes) => { | ||
const Permissions = sequelize.define('Permissions', { role: { type: DataTypes.STRING, | ||
allowNull: false }, | ||
tablesAllowed: { type: DataTypes.ARRAY(DataTypes.STRING), | ||
allowNull: false }, | ||
permissions: { type: DataTypes.ARRAY(DataTypes.STRING), | ||
allowNull: false } }, {}); | ||
return Permissions; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import express from 'express'; | ||
import Auth from '../middlewares/Auth'; | ||
import checkAdmin from '../middlewares/roleCheck'; | ||
import roles from '../controllers/rolesController'; | ||
|
||
const role = express.Router(); | ||
|
||
const { verifyToken } = Auth; | ||
|
||
role.post('/role', verifyToken, checkAdmin, roles.createRole); | ||
|
||
export default role; |
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,11 @@ | ||
module.exports = { up: queryInterface => queryInterface.bulkInsert('User', [{ username: 'john', | ||
firstName: 'kagabo', | ||
lastName: 'prince', | ||
email: 'faustin.kagabo@andela.com', | ||
password: '$2b$12$ohLYwcyFvN9o/fnRSd4G1.vcdNvt6SDJpiyTpOxiz38Y/wG4hNeza', | ||
following: true, | ||
bio: 'bios', | ||
image: 'image', | ||
roles: ['admin'] }], {}), | ||
|
||
down: queryInterface => queryInterface.bulkDelete('User', null, {}) }; |
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,52 @@ | ||
import chai from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import app from '../index'; | ||
import mock from './mock/users'; | ||
|
||
chai.use(chaiHttp); | ||
chai.should(); | ||
let tokenGen; | ||
describe('role', () => { | ||
it('admin signin', (done) => { | ||
chai.request(app) | ||
.post('/api/users/login') | ||
.send(mock.userAdmin) | ||
.end((req, res) => { | ||
// eslint-disable-next-line prefer-destructuring | ||
res.should.have.status(200); | ||
res.body.data.should.have.property('token'); | ||
tokenGen = res.body.data.token; | ||
done(); | ||
}); | ||
}); | ||
|
||
it('admin should create a role', (done) => { | ||
const role = { tablesAllowed: 'Articles,User', | ||
role: 'admin', | ||
permissions: 'GET,DELETE' }; | ||
chai.request(app) | ||
.post('/api/role') | ||
.set('token', tokenGen) | ||
.send(role) | ||
.end((req, res) => { | ||
res.should.have.status(201); | ||
res.body.should.have.property('message'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should not create a role if user is not an admin', (done) => { | ||
const role = { tablesAllowed: 'Articles,User', | ||
role: 'admin', | ||
permissions: 'GET,DELETE' }; | ||
chai.request(app) | ||
.post('/api/role') | ||
.set('token', `${tokenGen}dgfwe`) | ||
.send(role) | ||
.end((req, res) => { | ||
res.should.have.status(401); | ||
res.body.should.have.property('error'); | ||
done(); | ||
}); | ||
}); | ||
}); |
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