Skip to content

Commit 72023a2

Browse files
committed
Add role validation to User creator
1 parent f1e6bd1 commit 72023a2

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v6.1.13 (May 18, 2019)
2+
3+
- Add role validation to User creator, Fixes [#35](https://github.com/davellanedam/node-express-mongodb-jwt-rest-api-skeleton/issues/35)
4+
15
## v6.1.12 (May 17, 2019)
26

37
- NPM updated

app/controllers/users.validate.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ exports.createItem = [
3535
.withMessage('MISSING')
3636
.not()
3737
.isEmpty()
38-
.withMessage('IS_EMPTY'),
38+
.withMessage('IS_EMPTY')
39+
.isIn(['user', 'admin'])
40+
.withMessage('USER_NOT_IN_KNOWN_ROLE'),
3941
check('phone')
4042
.exists()
4143
.withMessage('MISSING')

test/users.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,25 @@ describe('*********** USERS ***********', () => {
131131
done()
132132
})
133133
})
134+
it('it should NOT POST a user with not known role', done => {
135+
const user = {
136+
name: faker.random.words(),
137+
email,
138+
password: faker.random.words(),
139+
role: faker.random.words()
140+
}
141+
chai
142+
.request(server)
143+
.post('/users')
144+
.set('Authorization', `Bearer ${token}`)
145+
.send(user)
146+
.end((err, res) => {
147+
res.should.have.status(422)
148+
res.body.should.be.a('object')
149+
res.body.should.have.property('errors')
150+
done()
151+
})
152+
})
134153
})
135154
describe('/GET/:id user', () => {
136155
it('it should GET a user by the given id', done => {

0 commit comments

Comments
 (0)