Skip to content

Commit

Permalink
feat(putUser): take roles in account
Browse files Browse the repository at this point in the history
Closes pouchdb-community#114

Roles can now be modified via putUser by passing ops.roles.
  • Loading branch information
ptitjes committed Nov 17, 2017
1 parent b1ea26a commit 7f44c9e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ function putUser(db, user, opts, callback) {
user = assign(user, opts.metadata);
}

if (opts.roles) {
user = assign(user, {roles: opts.roles});
}

var url = getUsersUrl(db) + '/' + encodeURIComponent(user._id);
var ajaxOpts = assign({
method : 'PUT',
Expand Down Expand Up @@ -80,7 +84,7 @@ plugin.signup = toPromise(function (username, password, opts, callback) {
var user = {
name : username,
password : password,
roles : opts.roles || [],
roles : [],
type : 'user',
_id : userId
};
Expand Down
44 changes: 44 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,50 @@ testCases.forEach(function (testCase) {
});
});

it('Test that admin can change roles', function () {
var roles = ['sidekick'];
var newRoles = ['superhero', 'villain'];
return db.signup('robin', 'dickgrayson', {roles: roles}).then(function (res) {
res.ok.should.equal(true);
return db.getUser('robin');
}).then(function (user) {
user.roles.should.deep.equal(roles);
}).then(function () {
return db.putUser('robin', {roles: newRoles});
}).then(function (res) {
res.ok.should.equal(true);
return db.getUser('robin');
}).then(function (user) {
user.roles.should.deep.equal(newRoles);
}).catch(function (err) {
should.not.exist(err);
});
});

it('Test that user cannot change roles', function () {
var roles = ['sidekick'];
var newRoles = ['superhero', 'villain'];
// We can't test for initial roles as we are in admin party
// Let us have faith in CouchDB
return db.signup('robin', 'dickgrayson', {roles: roles}).then(function (res) {
res.ok.should.equal(true);
return db.login('robin', 'dickgrayson');
}).then(function () {
return db.getUser('robin');
}).then(function (user) {
user.roles.should.deep.equal(roles);
}).then(function () {
return db.putUser('robin', {roles: newRoles});
}).then(function (res) {
res.ok.should.not.equal(true);
return db.getUser('robin').then(function (user) {
user.roles.should.deep.equal(roles);
});
}).catch(function (err) {
should.exist(err);
});
});

it('Test wrong user for getUser', function () {
return db.signup('robin', 'dickgrayson').then(function (res) {
return db.signup('aquaman', 'sleeps_with_fishes');
Expand Down

0 comments on commit 7f44c9e

Please sign in to comment.