|
|
|
|
@ -31,6 +31,26 @@ function createRole(name, scope, description) { |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function addUserToRole(roleId, username) { |
|
|
|
|
return new Promise((resolve) => { |
|
|
|
|
request.post(api('roles.addUserToRole')) |
|
|
|
|
.set(credentials) |
|
|
|
|
.send({ |
|
|
|
|
roleName: roleId, |
|
|
|
|
username, |
|
|
|
|
}) |
|
|
|
|
.expect('Content-Type', 'application/json') |
|
|
|
|
.expect(200) |
|
|
|
|
.expect((res) => { |
|
|
|
|
expect(res.body).to.have.property('success', true); |
|
|
|
|
expect(res.body).to.have.nested.property('role._id', roleId); |
|
|
|
|
}) |
|
|
|
|
.end((err, req) => { |
|
|
|
|
resolve(req.body.role); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
describe('[Roles]', function() { |
|
|
|
|
this.retries(0); |
|
|
|
|
|
|
|
|
|
@ -310,4 +330,59 @@ describe('[Roles]', function() { |
|
|
|
|
.end(done); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
describe('POST [/roles.delete]', () => { |
|
|
|
|
let roleWithUser; |
|
|
|
|
let roleWithoutUser; |
|
|
|
|
before(async () => { |
|
|
|
|
roleWithUser = await createRole(`roleWithUser-${ Date.now() }`, 'Users'); |
|
|
|
|
roleWithoutUser = await createRole(`roleWithoutUser-${ Date.now() }`, 'Users'); |
|
|
|
|
|
|
|
|
|
await addUserToRole(roleWithUser._id, login.user); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should delete a role that it is not being used', (done) => { |
|
|
|
|
request.post(api('roles.delete')) |
|
|
|
|
.set(credentials) |
|
|
|
|
.send({ |
|
|
|
|
roleId: roleWithoutUser._id, |
|
|
|
|
}) |
|
|
|
|
.expect('Content-Type', 'application/json') |
|
|
|
|
.expect(200) |
|
|
|
|
.expect((res) => { |
|
|
|
|
expect(res.body).to.have.property('success', true); |
|
|
|
|
}) |
|
|
|
|
.end(done); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should NOT delete a role that it is protected', (done) => { |
|
|
|
|
request.post(api('roles.delete')) |
|
|
|
|
.set(credentials) |
|
|
|
|
.send({ |
|
|
|
|
roleId: 'admin', |
|
|
|
|
}) |
|
|
|
|
.expect('Content-Type', 'application/json') |
|
|
|
|
.expect(400) |
|
|
|
|
.expect((res) => { |
|
|
|
|
expect(res.body).to.have.property('success', false); |
|
|
|
|
expect(res.body).to.have.nested.property('error', 'Cannot delete a protected role [error-role-protected]'); |
|
|
|
|
}) |
|
|
|
|
.end(done); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should NOT delete a role that it is being used', (done) => { |
|
|
|
|
request.post(api('roles.delete')) |
|
|
|
|
.set(credentials) |
|
|
|
|
.send({ |
|
|
|
|
roleId: roleWithUser._id, |
|
|
|
|
}) |
|
|
|
|
.expect('Content-Type', 'application/json') |
|
|
|
|
.expect(400) |
|
|
|
|
.expect((res) => { |
|
|
|
|
expect(res.body).to.have.property('success', false); |
|
|
|
|
expect(res.body).to.have.nested.property('error', 'Cannot delete role because it\'s in use [error-role-in-use]'); |
|
|
|
|
}) |
|
|
|
|
.end(done); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
|