Skip to content

Commit

Permalink
#205 usermanager.js now covered with tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zsolt Lattmann committed Mar 7, 2015
1 parent 83e74b6 commit 5f95638
Show file tree
Hide file tree
Showing 2 changed files with 355 additions and 159 deletions.
140 changes: 83 additions & 57 deletions src/bin/usermanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,21 @@ main = function (argv) {
setupGMEAuth(options.parent.db);

// TODO: we may need to use a module like 'prompt' to get user password
auth.addUser(username, email, password, options.canCreate, {overwrite: true})
.then(mainDeferred.resolve)
.catch(mainDeferred.reject)
.finally(auth.unload);
if (username && email && password) {
auth.addUser(username, email, password, options.canCreate, {overwrite: true})
.then(mainDeferred.resolve)
.catch(mainDeferred.reject)
.finally(auth.unload);
} else {
mainDeferred.reject(new SyntaxError('username, email, and password parameters are required'));
}
})
.on('--help', function () {
console.log(' Examples:');
console.log();
console.log(' $ node usermanager.js useradd brubble brubble@example.com Password.123');
console.log(' $ node usermanager.js useradd --canCreate brubble brubble@example.com Password.123');
console.log();
// FIXME: resolve promise
});

program
Expand All @@ -90,22 +93,25 @@ main = function (argv) {
.action(function (username, options) {
setupGMEAuth(options.parent.db);

return auth.getAllUserAuthInfo(username)
.then(function (userObject) {
// TODO: pretty print users
console.log(userObject);
mainDeferred.resolve();
})
.catch(mainDeferred.reject)
.finally(auth.unload);
if (username) {
auth.getAllUserAuthInfo(username)
.then(function (userObject) {
// TODO: pretty print users
console.log(userObject);
mainDeferred.resolve();
})
.catch(mainDeferred.reject)
.finally(auth.unload);
} else {
mainDeferred.reject(new SyntaxError('username parameter is required'));
}
})
.on('--help', function () {
console.log(' Examples:');
console.log();
console.log(' $ node usermanager.js userlist');
console.log(' $ node usermanager.js userlist user23');
console.log();
// FIXME: resolve promise
});

program
Expand All @@ -115,21 +121,24 @@ main = function (argv) {
setupGMEAuth(options.parent.db);

// TODO: we may need to use a module like 'prompt' to get user password
return auth.getAllUserAuthInfo(username)
.then(function (userObject) {
return auth.addUser(username, userObject.email, password, userObject.canCreate, {overwrite: true});
})
.then(mainDeferred.resolve)
.catch(mainDeferred.reject)
.finally(auth.unload);
if (username && password) {
auth.getAllUserAuthInfo(username)
.then(function (userObject) {
return auth.addUser(username, userObject.email, password, userObject.canCreate, {overwrite: true});
})
.then(mainDeferred.resolve)
.catch(mainDeferred.reject)
.finally(auth.unload);
} else {
mainDeferred.reject(new SyntaxError('username and password parameters are required'));
}

})
.on('--help', function () {
console.log(' Examples:');
console.log();
console.log(' $ node usermanager.js passwd brubble NewPass.123');
console.log();
// FIXME: resolve promise
});

program
Expand All @@ -138,36 +147,42 @@ main = function (argv) {
.action(function (username, options) {
setupGMEAuth(options.parent.db);

return auth.removeUserByUserId(username)
.then(mainDeferred.resolve)
.catch(mainDeferred.reject)
.finally(auth.unload);
if (username) {
auth.removeUserByUserId(username)
.then(mainDeferred.resolve)
.catch(mainDeferred.reject)
.finally(auth.unload);
} else {
mainDeferred.reject(new SyntaxError('username parameter is missing'));
}
})
.on('--help', function () {
console.log(' Examples:');
console.log();
console.log(' $ node usermanager.js userdel brubble');
console.log();
// FIXME: resolve promise
});

program
.command('organizationadd <orgname>')
.command('organizationadd <organizationname>')
.description('adds a new organization')
.action(function (orgname, options) {
.action(function (organizationname, options) {
setupGMEAuth(options.parent.db);

return auth.addOrganization(orgname)
.then(mainDeferred.resolve)
.catch(mainDeferred.reject)
.finally(auth.unload);
if (organizationname) {
auth.addOrganization(organizationname)
.then(mainDeferred.resolve)
.catch(mainDeferred.reject)
.finally(auth.unload);
} else {
mainDeferred.reject(new SyntaxError('organizationname parameter is missing'));
}
})
.on('--help', function () {
console.log(' Examples:');
console.log();
console.log(' $ node usermanager.js organizationadd neworg');
console.log();
// FIXME: resolve promise
});

program
Expand All @@ -176,17 +191,20 @@ main = function (argv) {
.action(function (organizationname, options) {
setupGMEAuth(options.parent.db);

return auth.removeOrganizationByOrgId(organizationname)
.then(mainDeferred.resolve)
.catch(mainDeferred.reject)
.finally(auth.unload);
if (organizationname) {
auth.removeOrganizationByOrgId(organizationname)
.then(mainDeferred.resolve)
.catch(mainDeferred.reject)
.finally(auth.unload);
} else {
mainDeferred.reject(new SyntaxError('organizationname parameter is missing'));
}
})
.on('--help', function () {
console.log(' Examples:');
console.log();
console.log(' $ node usermanager.js organizationdel sample_organization');
console.log();
// FIXME: resolve promise
});

var authUserOrGroup = function (id, projectname, options, fn) {
Expand Down Expand Up @@ -216,7 +234,11 @@ main = function (argv) {
.option('-a, --authorize <mode>', 'mode is rwd, read, write, delete', 'rwd')
.option('-d, --deauthorize', 'deauthorizes user', false)
.action(function (username, projectname, options) {
return authUserOrGroup(username, projectname, options, 'authorizeByUserId');
if (username && projectname) {
authUserOrGroup(username, projectname, options, 'authorizeByUserId');
} else {
mainDeferred.reject(new SyntaxError('username and projectname parameter are missing'));
}
})
.on('--help', function () {
console.log(' Examples:');
Expand All @@ -228,8 +250,6 @@ main = function (argv) {
console.log(' $ node usermanager.js usermod_auth --deauthorize user23 project42');
console.log(' $ node usermanager.js usermod_auth -d user23 project42');
console.log();
// FIXME: resolve promise

});

program
Expand All @@ -238,12 +258,14 @@ main = function (argv) {
.option('-a, --authorize <mode>', 'mode is rwd, read, write, delete', 'rwd')
.option('-d, --deauthorize', 'deauthorizes user', false)
.action(function (orgname, projectname, options) {
return authUserOrGroup(orgname, projectname, options, 'authorizeOrganization');
if (orgname && projectname) {
authUserOrGroup(orgname, projectname, options, 'authorizeOrganization');
} else {
mainDeferred.reject(new SyntaxError('orgname and projectname parameter are missing'));
}
})
.on('--help', function () {
console.log(' Organizations are authorized like users are authorized. See also: usermod_auth');
// FIXME: resolve promise

});

program
Expand All @@ -252,18 +274,20 @@ main = function (argv) {
.action(function (username, organizationname, options) {
setupGMEAuth(options.parent.db);

return auth.addUserToOrganization(username, organizationname)
.then(mainDeferred.resolve)
.catch(mainDeferred.reject)
.finally(auth.unload);
if (username && organizationname) {
auth.addUserToOrganization(username, organizationname)
.then(mainDeferred.resolve)
.catch(mainDeferred.reject)
.finally(auth.unload);
} else {
mainDeferred.reject(new SyntaxError('username and organizationname parameter are missing'));
}
})
.on('--help', function () {
console.log(' Examples:');
console.log();
console.log(' $ node usermanager.js usermod_organization_add user23 organization123');
console.log();
// FIXME: resolve promise

});

program
Expand All @@ -272,18 +296,20 @@ main = function (argv) {
.action(function (username, organizationname, options) {
setupGMEAuth(options.parent.db);

return auth.removeUserFromOrganization(username, organizationname)
.then(mainDeferred.resolve)
.catch(mainDeferred.reject)
.finally(auth.unload);
if (username && organizationname) {
auth.removeUserFromOrganization(username, organizationname)
.then(mainDeferred.resolve)
.catch(mainDeferred.reject)
.finally(auth.unload);
} else {
mainDeferred.reject(new SyntaxError('username and organizationname parameter are missing'));
}
})
.on('--help', function () {
console.log(' Examples:');
console.log();
console.log(' $ node usermanager.js usermod_organization_del user23 organization123');
console.log();
// FIXME: resolve promise

});


Expand Down
Loading

0 comments on commit 5f95638

Please sign in to comment.