Skip to content

Commit

Permalink
Merge pull request #348 from pushpabrol/master
Browse files Browse the repository at this point in the history
Add method to assign users to a role
  • Loading branch information
luisrudge authored Mar 27, 2019
2 parents 8320a45 + dd79ab7 commit e8c3f3e
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 1 deletion.
47 changes: 47 additions & 0 deletions src/management/RolesManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,51 @@ RolesManager.prototype.getUsers = function(params, callback) {
return this.users.getAll(params, callback);
};

/**
* Assign users to a role
*
* @method assignUsers
* @memberOf module:management.RolesManager.prototype
*
* @example
* var params = { id :'ROLE_ID'};
* var data = { "users" : ["userId1","userId2"]};
*
* management.roles.assignUsers(params, data, function (err, user) {
* if (err) {
* // Handle error.
* }
*
* // permissions added.
* });
*
* @param {String} params.id ID of the Role.
* @param {Object} data permissions data
* @param {String} data.permissions Array of permissions
* @param {String} data.permissions.permission_name Name of a permission
* @param {String} data.permissions.resource_server_identifier Identifier for a resource
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/

RolesManager.prototype.assignUsers = function(params, data, cb) {
data = data || {};
params = params || {};

// Require a user ID.
if (!params.id) {
throw new ArgumentError('The roleId passed in params cannot be null or undefined');
}
if (typeof params.id !== 'string') {
throw new ArgumentError('The role Id has to be a string');
}

if (cb && cb instanceof Function) {
return this.permissions.create(params, data, cb);
}

return this.users.create(params, data);
};

module.exports = RolesManager;
96 changes: 95 additions & 1 deletion test/management/roles.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ describe('RolesManager', function() {
'getPermissions',
'addPermissions',
'removePermissions',
'getUsers'
'getUsers',
'assignUsers'
];

methods.forEach(function(method) {
Expand Down Expand Up @@ -700,4 +701,97 @@ describe('RolesManager', function() {
});
});
});

describe('#assignUsers', function() {
beforeEach(function() {
this.data = {
id: 'rol_ID'
};
this.body = { users: ['userID1'] };

this.request = nock(API_URL)
.post('/roles/' + this.data.id + '/users')
.reply(200);
});

it('should validate empty roleId', function() {
var _this = this;
expect(function() {
_this.roles.assignUsers({ id: null }, _this.body, function() {});
}).to.throw('The roleId passed in params cannot be null or undefined');
});

it('should validate non-string roleId', function() {
var _this = this;
expect(function() {
_this.roles.assignUsers({ id: 123 }, _this.body, function() {});
}).to.throw('The role Id has to be a string');
});

it('should accept a callback', function(done) {
this.roles.assignUsers(this.data, {}, function() {
done();
});
});

it('should return a promise if no callback is given', function(done) {
this.roles
.assignUsers(this.data, {})
.then(done.bind(null, null))
.catch(done.bind(null, null));
});

it('should pass any errors to the promise catch handler', function(done) {
nock.cleanAll();

var request = nock(API_URL)
.post('/roles/' + this.data.id + '/users')
.reply(500);

this.roles.assignUsers(this.data, {}).catch(function(err) {
expect(err).to.exist;

done();
});
});

it('should perform a POST request to /api/v2/roles/rol_ID/users', function(done) {
var request = this.request;

this.roles.assignUsers(this.data, {}).then(function() {
expect(request.isDone()).to.be.true;

done();
});
});

it('should pass the data in the body of the request', function(done) {
nock.cleanAll();

var request = nock(API_URL)
.post('/roles/' + this.data.id + '/users', this.body)
.reply(200);

this.roles.assignUsers(this.data, this.body).then(function() {
expect(request.isDone()).to.be.true;

done();
});
});

it('should include the token in the Authorization header', function(done) {
nock.cleanAll();

var request = nock(API_URL)
.post('/roles/' + this.data.id + '/users')
.matchHeader('Authorization', 'Bearer ' + this.token)
.reply(200);

this.roles.assignUsers(this.data, {}).then(function() {
expect(request.isDone()).to.be.true;

done();
});
});
});
});

0 comments on commit e8c3f3e

Please sign in to comment.