diff --git a/lib/login.js b/lib/login.js index 4a4483d5e..628c1ec68 100644 --- a/lib/login.js +++ b/lib/login.js @@ -19,16 +19,17 @@ async function getCredentials(opts) { password: await read({ prompt: 'Password:', silent: true }), project: opts.project || (await read({ prompt: 'Project:', default: configStore.get('project') })), + token: opts['2fa'] && await read({ prompt: '2fa token:' }) }; } exports.run = async function({ opts }) { - let { email, password, project } = opts; + let { email, password, project, token } = opts; // We only want to prompt for input outside of the test environment /* istanbul ignore next */ if (!testing) { - ({ email, password, project } = await getCredentials(opts)); + ({ email, password, project, token } = await getCredentials(opts)); } if (!project) { @@ -49,7 +50,7 @@ exports.run = async function({ opts }) { return request .post(`${config.host}/api/v1/login`, { - json: { email, password, project }, + json: { email, password, project, token }, }) .then(res => { configStore.set('apiKey', res.apiKey); diff --git a/test/login.test.js b/test/login.test.js index b6749dc55..b063b7678 100644 --- a/test/login.test.js +++ b/test/login.test.js @@ -60,5 +60,40 @@ describe('login command', () => { }); }); + it('should error if missing two factor token', done => { + const email = 'dom@readme.io'; + const password = '123456'; + const project = 'subdomain'; + + const mock = nock(config.host) + .post('/api/v1/login', { email, password, project }) + .reply(400, { + description: 'You must provide a Two Factor Code', + error: 'Bad Request', + }); + + return login([], { email, password, project }).catch(err => { + mock.done(); + assert.equal(err.error, 'Bad Request'); + assert.equal(err.description, 'You must provide a Two Factor Code'); + return done(); + }); + }); + + it('should send 2fa token if provided', () => { + const email = 'dom@readme.io'; + const password = '123456'; + const project = 'subdomain'; + const token = '123456'; + + const mock = nock(config.host) + .post('/api/v1/login', { email, password, project, token }) + .reply(200, { apiKey: 123 }); + + return login([], { email, password, project, token }).then(() => { + mock.done(); + }); + }); + it('should error if trying to access a project that is not yours', () => {}); });