-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dom Harrington
committed
Oct 1, 2018
1 parent
234bb18
commit de88b5e
Showing
8 changed files
with
146 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const request = require('request-promise-native'); | ||
const config = require('config'); | ||
const read = require('read'); | ||
const Configstore = require('configstore'); | ||
const { validate: isEmail } = require('isemail'); | ||
|
||
exports.desc = 'Login to a ReadMe project'; | ||
exports.category = 'services'; | ||
exports.weight = 1; | ||
|
||
const pkg = require('../package.json'); | ||
|
||
const conf = new Configstore(pkg.name); | ||
|
||
function getCredentials() { | ||
return new Promise((resolve, reject) => { | ||
read({ prompt: 'Email:', default: conf.get('email') }, (emailErr, email) => { | ||
if (emailErr) return reject(emailErr); | ||
|
||
return read({ prompt: 'Password:', silent: true }, (passwordErr, password) => { | ||
if (passwordErr) return reject(passwordErr); | ||
|
||
return resolve({ email, password }); | ||
}); | ||
}) | ||
}); | ||
} | ||
|
||
exports.run = async function({ opts }) { | ||
const { project } = opts; | ||
|
||
if (!project) { | ||
return Promise.reject(new Error('No project subdomain provided. Please use --project')); | ||
} | ||
|
||
let { email, password } = opts; | ||
|
||
if (!email) { | ||
({ email, password } = await getCredentials()); | ||
} | ||
|
||
if (!isEmail(email)) { | ||
return Promise.reject(new Error('You must provide a valid email address.')); | ||
} | ||
|
||
function badRequest(err) { | ||
if (err.statusCode === 400) { | ||
return Promise.reject(err.error); | ||
} | ||
|
||
return Promise.reject(err); | ||
} | ||
|
||
return request.post(`${config.host}/api/v1/login`, { | ||
json: { email, password, project }, | ||
}).then((res) => { | ||
conf.set('apiKey', res.apiKey); | ||
conf.set('email', email); | ||
conf.set('project', project); | ||
return `Successfully logged in as ${email.green} in the ${project.blue} project`; | ||
}).catch(badRequest); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const nock = require('nock'); | ||
const config = require('config'); | ||
const assert = require('assert'); | ||
const Configstore = require('configstore'); | ||
|
||
const pkg = require('../package'); | ||
|
||
const login = require('../cli').bind(null, 'login'); | ||
|
||
describe('login command', () => { | ||
beforeAll(() => nock.disableNetConnect()); | ||
afterAll(() => nock.cleanAll()); | ||
|
||
it('should error if no project provided', (done) => | ||
login([], {}).catch(err => { | ||
assert.equal(err.message, 'No project subdomain provided. Please use --project'); | ||
return done(); | ||
})); | ||
|
||
it('should error if email is invalid', (done) => | ||
login([], { project: 'subdomain', email: 'this-is-not-an-email' }).catch(err => { | ||
assert.equal(err.message, 'You must provide a valid email address.'); | ||
return done(); | ||
})); | ||
|
||
it('should post to /login on the API', () => { | ||
const email = '[email protected]'; | ||
const password = '123456'; | ||
const project = 'subdomain'; | ||
const apiKey = 'abcdefg'; | ||
|
||
const mock = nock(config.host) | ||
.post('/api/v1/login', { email, password, project }) | ||
.reply(200, { apiKey }); | ||
|
||
return login([], { email, password, project }).then(() => { | ||
mock.done(); | ||
const conf = new Configstore(pkg.name); | ||
assert.equal(conf.get('apiKey'), apiKey); | ||
assert.equal(conf.get('email'), email); | ||
assert.equal(conf.get('project'), project); | ||
}); | ||
}); | ||
|
||
it('should error if invalid credentials are given', (done) => { | ||
const email = '[email protected]'; | ||
const password = '123456'; | ||
const project = 'subdomain'; | ||
|
||
const mock = nock(config.host) | ||
.post('/api/v1/login', { email, password, project }) | ||
.reply(400, { | ||
description: 'Invalid email/password', | ||
error: 'Bad Request', | ||
}); | ||
|
||
return login([], { email, password, project }).catch((err) => { | ||
mock.done(); | ||
assert.equal(err.error, 'Bad Request'); | ||
assert.equal(err.description, 'Invalid email/password'); | ||
return done(); | ||
}); | ||
}); | ||
|
||
it('should error if trying to access a project that is not yours', () => {}); | ||
}); |