-
Notifications
You must be signed in to change notification settings - Fork 4
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
7 changed files
with
253 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
import request from 'supertest'; | ||
import bcrypt from 'bcrypt'; | ||
import app from '../../app'; | ||
import { User } from '../../database/models'; | ||
import { urlPrefix } from '../mocks/variables.json'; | ||
import { signupUser, signupUser2 } from '../mocks/db.json'; | ||
|
||
let superAdmin; | ||
let user1; | ||
let user2; | ||
let admin; | ||
|
||
describe('RBAC', () => { | ||
beforeAll(async () => { | ||
const encryptedPassword = bcrypt.hashSync('123456', 10); | ||
await User.create({ | ||
username: 'superAdmin', | ||
userType: 'super-admin', | ||
email: 'superadmin@author.haven', | ||
password: encryptedPassword, | ||
confirmed: 'confirmed' | ||
}); | ||
await User.create({ | ||
...signupUser, | ||
confirmed: 'confirmed', | ||
password: encryptedPassword | ||
}); | ||
await User.create({ | ||
...signupUser2, | ||
confirmed: 'confirmed', | ||
password: encryptedPassword | ||
}); | ||
await User.create({ | ||
username: 'admin', | ||
userType: 'admin', | ||
email: 'admin@author.haven', | ||
password: encryptedPassword, | ||
confirmed: 'confirmed' | ||
}); | ||
const res1 = await request(app) | ||
.post(`${urlPrefix}/users/login`) | ||
.send({ | ||
user: { | ||
username: 'superadmin@author.haven', | ||
password: '123456' | ||
} | ||
}); | ||
superAdmin = res1.body.user; | ||
const res2 = await request(app) | ||
.post(`${urlPrefix}/users/login`) | ||
.send({ | ||
user: { | ||
username: signupUser.email, | ||
password: '123456' | ||
} | ||
}); | ||
user1 = res2.body.user; | ||
const res3 = await request(app) | ||
.post(`${urlPrefix}/users/login`) | ||
.send({ | ||
user: { | ||
username: signupUser2.email, | ||
password: '123456' | ||
} | ||
}); | ||
user2 = res3.body.user; | ||
const res4 = await request(app) | ||
.post(`${urlPrefix}/users/login`) | ||
.send({ | ||
user: { | ||
username: 'admin@author.haven', | ||
password: '123456' | ||
} | ||
}); | ||
admin = res4.body.user; | ||
}); | ||
|
||
afterAll(async () => { | ||
await User.destroy({ where: { id: superAdmin.id } }); | ||
await User.destroy({ where: { id: user1.id } }); | ||
await User.destroy({ where: { id: user2.id } }); | ||
await User.destroy({ where: { id: admin.id } }); | ||
}); | ||
|
||
test('should not grant access if user not admin', async () => { | ||
const res = await request(app) | ||
.put(`${urlPrefix}/users/${user1.username}/grant`) | ||
.set('authorization', user1.token) | ||
.send({ role: 'admin' }); | ||
|
||
expect(res.status).toBe(401); | ||
expect(res.body.status).toBe(401); | ||
expect(res.body.message).toBe('Not authorized'); | ||
}); | ||
|
||
test('should not grant access with invalid input', async () => { | ||
const res = await request(app) | ||
.put(`${urlPrefix}/users/${user1.username}/grant`) | ||
.set('authorization', superAdmin.token) | ||
.send({ role: 'dhfjs' }); | ||
|
||
expect(res.status).toBe(400); | ||
expect(res.body.message).toBe('Bad Request'); | ||
}); | ||
|
||
test('should grant access', async () => { | ||
const res = await request(app) | ||
.put(`${urlPrefix}/users/${user1.username}/grant`) | ||
.set('authorization', superAdmin.token) | ||
.send({ role: 'admin' }); | ||
|
||
expect(res.status).toBe(200); | ||
expect(res.body.status).toBe(200); | ||
expect(res.body.message).toBe('admin role granted'); | ||
expect(res.body.user.userType).toBe('admin'); | ||
}); | ||
|
||
test('should inform a user in case role is already granted', async () => { | ||
const res = await request(app) | ||
.put(`${urlPrefix}/users/${user1.username}/grant`) | ||
.set('authorization', superAdmin.token) | ||
.send({ role: 'admin' }); | ||
|
||
expect(res.status).toBe(409); | ||
expect(res.body.status).toBe(409); | ||
expect(res.body.message).toBe('already an admin'); | ||
}); | ||
|
||
test('should inform a user in case role is already granted', async () => { | ||
const res = await request(app) | ||
.put(`${urlPrefix}/users/${user2.username}/grant`) | ||
.set('authorization', superAdmin.token) | ||
.send({ role: 'user' }); | ||
|
||
expect(res.status).toBe(409); | ||
expect(res.body.status).toBe(409); | ||
expect(res.body.message).toBe('already a user'); | ||
}); | ||
|
||
test('should not grant access if user does not exist', async () => { | ||
const fakeName = 'rtvdr'; | ||
const res = await request(app) | ||
.put(`${urlPrefix}/users/${fakeName}/grant`) | ||
.set('authorization', superAdmin.token) | ||
.send({ role: 'admin' }); | ||
|
||
expect(res.status).toBe(404); | ||
expect(res.body.status).toBe(404); | ||
expect(res.body.message).toBe('User not found'); | ||
}); | ||
|
||
test('should not grant super-admin when you are an admin', async () => { | ||
const res = await request(app) | ||
.put(`${urlPrefix}/users/${user2.username}/grant`) | ||
.set('authorization', admin.token) | ||
.send({ role: 'super-admin' }); | ||
|
||
expect(res.status).toBe(401); | ||
expect(res.body.status).toBe(401); | ||
expect(res.body.message).toBe('Not authorized'); | ||
}); | ||
}); |
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,46 @@ | ||
import { User } from '../database/models'; | ||
|
||
/** | ||
* @description a class to grant roles to users | ||
*/ | ||
class grantRoleController { | ||
/** | ||
* @author Daniel | ||
* @param {*} req | ||
* @param {*} res | ||
* @returns {*} object | ||
*/ | ||
static async assignRole(req, res) { | ||
const { username } = req.params; | ||
const { currentUser } = req; | ||
const { role } = req.body; | ||
if (currentUser.userType === 'admin' && role === 'super-admin') { | ||
return res.status(401).send({ | ||
status: 401, | ||
message: 'Not authorized' | ||
}); | ||
} | ||
const user = await User.findOne({ where: { username } }); | ||
if (!user) { | ||
return res.status(404).send({ | ||
status: 404, | ||
message: 'User not found' | ||
}); | ||
} | ||
if (user.userType === role) { | ||
return res.status(409).send({ | ||
status: 409, | ||
message: `already ${role === 'admin' ? 'an' : 'a'} ${role}` | ||
}); | ||
} | ||
user.update({ userType: role }); | ||
const { password, confirmationCode, ...userData } = user.get(); | ||
return res.status(200).send({ | ||
status: 200, | ||
message: `${role} role granted`, | ||
user: userData | ||
}); | ||
} | ||
} | ||
|
||
export default grantRoleController; |
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
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,8 @@ | ||
import { Joi } from 'celebrate'; | ||
|
||
const role = Joi.object().keys({ | ||
role: Joi.string() | ||
.valid('admin', 'super-admin', 'user') | ||
.required() | ||
}); | ||
export default role; |