From 1e6895503ea46ca31c4cd49e7d0678e66274c3b2 Mon Sep 17 00:00:00 2001 From: Jon Ursenbach Date: Fri, 2 Aug 2019 22:28:02 -0700 Subject: [PATCH] Adding a new `whoami` command. --- cmds/login.js | 2 +- cmds/open.js | 3 ++- cmds/whoami.js | 23 +++++++++++++++++++++++ test/cli.test.js | 2 +- test/cmds/open.test.js | 3 ++- test/cmds/whoami.test.js | 38 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 cmds/whoami.js create mode 100644 test/cmds/whoami.test.js diff --git a/cmds/login.js b/cmds/login.js index 52cb41d9f..836857066 100644 --- a/cmds/login.js +++ b/cmds/login.js @@ -72,7 +72,7 @@ exports.run = async function(opts) { configStore.set('email', email); configStore.set('project', project); - return `Successfully logged in as ${email.green} in the ${project.blue} project`; + return `Successfully logged in as ${email.green} to the ${project.blue} project.`; }) .catch(badRequest); }; diff --git a/cmds/open.js b/cmds/open.js index bea884141..81c1975ef 100644 --- a/cmds/open.js +++ b/cmds/open.js @@ -1,6 +1,7 @@ const config = require('config'); const open = require('opn'); const configStore = require('../lib/configstore'); +const loginCmd = require('./login'); exports.command = 'open'; exports.usage = 'open'; @@ -13,7 +14,7 @@ exports.args = []; exports.run = function(opts) { const project = configStore.get('project'); if (!project) { - return Promise.reject(new Error(`Please login using \`${config.cli} ${exports.usage}\`.`)); + return Promise.reject(new Error(`Please login using \`${config.cli} ${loginCmd.command}\`.`)); } return (opts.mockOpen || open)(config.hub.replace('{project}', project), { diff --git a/cmds/whoami.js b/cmds/whoami.js new file mode 100644 index 000000000..1c9b7bcd8 --- /dev/null +++ b/cmds/whoami.js @@ -0,0 +1,23 @@ +const config = require('config'); +const configStore = require('../lib/configstore'); +const loginCmd = require('./login'); + +exports.command = 'whoami'; +exports.usage = 'whoami'; +exports.description = 'Displays the current user and project authenticated with ReadMe.'; +exports.category = 'admin'; +exports.weight = 2; + +exports.args = []; + +exports.run = () => { + if (!configStore.has('email') || !configStore.has('project')) { + return Promise.reject(new Error(`Please login using \`${config.cli} ${loginCmd.command}\`.`)); + } + + return Promise.resolve( + `You are currently logged in as ${configStore.get('email').green} to the ${ + configStore.get('project').blue + } project.`, + ); +}; diff --git a/test/cli.test.js b/test/cli.test.js index cd4a011f1..403eb9c44 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -88,7 +88,7 @@ describe('cli', () => { }); it('should not show related commands on commands that have none', () => { - cli(['login', '--help']).then(output => { + cli(['swagger', '--help']).then(output => { assert.ok(output.indexOf('Related commands') === -1); }); }); diff --git a/test/cmds/open.test.js b/test/cmds/open.test.js index 88865c541..05b3fb5fa 100644 --- a/test/cmds/open.test.js +++ b/test/cmds/open.test.js @@ -2,13 +2,14 @@ const assert = require('assert'); const config = require('config'); const configStore = require('../../lib/configstore'); const cmd = require('../../cmds/open'); +const loginCmd = require('../../cmds/login'); describe('open command', () => { it('should error if no project provided', done => { configStore.delete('project'); cmd.run({}).catch(err => { - assert.equal(err.message, `Please login using \`${config.cli} ${cmd.usage}\`.`); + assert.equal(err.message, `Please login using \`${config.cli} ${loginCmd.command}\`.`); return done(); }); }); diff --git a/test/cmds/whoami.test.js b/test/cmds/whoami.test.js new file mode 100644 index 000000000..2cabd0232 --- /dev/null +++ b/test/cmds/whoami.test.js @@ -0,0 +1,38 @@ +const assert = require('assert'); +const config = require('config'); +const configStore = require('../../lib/configstore'); +const cmd = require('../../cmds/whoami'); +const loginCmd = require('../../cmds/login'); + +describe('whoami command', () => { + it('should error if user is not authenticate', done => { + configStore.delete('email'); + configStore.delete('project'); + + cmd + .run({}) + .then(() => { + assert.ok(false, 'unauthenticated error message not displayed'); + }) + .catch(err => { + assert.equal(err.message, `Please login using \`${config.cli} ${loginCmd.command}\`.`); + return done(); + }); + }); + + it('should return the authenticated user', done => { + configStore.set('email', 'email@example.com'); + configStore.set('project', 'subdomain'); + + cmd + .run({}) + .then(() => { + assert.ok(true); + return done(); + }) + .catch(err => { + assert.ok(false, err); + return done(); + }); + }); +});