Skip to content

Commit

Permalink
Adding a new whoami command.
Browse files Browse the repository at this point in the history
  • Loading branch information
erunion committed Aug 3, 2019
1 parent f76ef57 commit 1e68955
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmds/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
3 changes: 2 additions & 1 deletion cmds/open.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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), {
Expand Down
23 changes: 23 additions & 0 deletions cmds/whoami.js
Original file line number Diff line number Diff line change
@@ -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.`,
);
};
2 changes: 1 addition & 1 deletion test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Expand Down
3 changes: 2 additions & 1 deletion test/cmds/open.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Expand Down
38 changes: 38 additions & 0 deletions test/cmds/whoami.test.js
Original file line number Diff line number Diff line change
@@ -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 protected]');
configStore.set('project', 'subdomain');

cmd
.run({})
.then(() => {
assert.ok(true);
return done();
})
.catch(err => {
assert.ok(false, err);
return done();
});
});
});

0 comments on commit 1e68955

Please sign in to comment.