Skip to content
Browse files

[test] Refactor `test/commands/users-test.js` to use `prompt.override…

…` and `nock`
  • Loading branch information...
1 parent f3f5f2c commit 94ba7e14d0f315e0b67923354418bf6abcf1fafb @indexzero indexzero committed Feb 11, 2012
Showing with 95 additions and 85 deletions.
  1. +2 −1 .gitignore
  2. +8 −5 lib/jitsu/commands/users.js
  3. +85 −79 test/commands/users-test.js
View
3 .gitignore
@@ -1,2 +1,3 @@
node_modules/
-npm-debug.log
+npm-debug.log
+test/fixtures/test-jitsuconf
View
13 lib/jitsu/commands/users.js
@@ -51,10 +51,16 @@ users.create = function (username, callback) {
email: email,
password: password
}, function (err, result) {
+ if (err) {
+ jitsu.log.error('There was an error creating your account');
+ jitsu.log.error(err.message);
+ return callback(err, true, true);
+ }
+
jitsu.log.info('Account creation successful!');
jitsu.log.help('Your account has been created, but it is not yet active');
jitsu.log.help('Please check the email we sent to ' + email.grey + ' for further instructions');
- callback(err, result);
+ callback(null, result);
});
}
else {
@@ -277,20 +283,17 @@ users.confirm = function (username, inviteCode, callback) {
});
}
});
-
}
if (!callback && !inviteCode) {
callback = username;
return callback(new Error('username is required'), true);
}
- else if (typeof callback === 'function') {
+ else if (inviteCode && typeof callback === 'function') {
// They are providing an inviteCode so lets reset the password
return confirmUserHelper(username, inviteCode, callback);
}
- callback = inviteCode;
-
jitsu.prompt.get(['invite code'], function (err, result) {
confirmUserHelper(username, result['invite code'], callback);
});
View
164 test/commands/users-test.js
@@ -1,107 +1,113 @@
/*
- * users.js: Tests for `jitsu users *` command(s).
+ * env.js: Tests for `jitsu env *` command(s).
*
* (C) 2010, Nodejitsu Inc.
*
*/
var assert = require('assert'),
+ fs = require('fs'),
+ path = require('path'),
+ flatiron = require('flatiron'),
+ nock = require('nock'),
vows = require('vows'),
- mockRequest = require('mock-request'),
jitsu = require('../../lib/jitsu'),
- helper = require('../helpers/mock-helpers');
+ macros = require('../helpers/macros');
-var mockPrompt = helper.mockPrompt,
- runJitsuCommand = helper.runJitsuCommand;
+var shouldNodejitsuOk = macros.shouldNodejitsuOk;
+
+var elvis = {
+ username: 'elvis',
+ email: 'e@mailinator.com',
+ password: '12345'
+};
+
+var jimmy = {
+ username: "jimmy",
+ email: "j@mailinator.com",
+ password: "98765"
+};
vows.describe('jitsu/commands/users').addBatch({
- 'users create elvis': runJitsuCommand(
- mockPrompt({ email: 'e@mailinator.com', password: '12345', 'confirm password': '12345'}),
- mockRequest.mock(helper.mockOptions, helper.mockDefaults)
- .post('/users/elvis', {
- email: 'e@mailinator.com',
- password: '12345',
- username: 'elvis'
- }))
+ 'users create elvis': shouldNodejitsuOk(function setup() {
+ jitsu.prompt.override = flatiron.common.clone(elvis);
+ jitsu.prompt.override['confirm password'] = elvis.password;
+
+ nock('http://api.mockjitsu.com')
+ .post('/users/elvis', elvis)
+ .reply(200, '', { 'x-powered-by': 'Nodejitsu' })
+ })
}).addBatch({
- 'users create elvis': runJitsuCommand(
+ 'users create elvis': shouldNodejitsuOk(
'should respond with a 400 error',
function assertion (ign, err) {
err = ign;
assert.equal(err.statusCode, '400');
},
- mockPrompt({email: 'e@mailinator.com', password: '12345'}),
- mockRequest.mock(helper.mockOptions, helper.mockDefaults)
- .post('/users/elvis', {
- email: 'e@mailinator.com',
- password: '12345',
- username: 'elvis'
- })
- .respond({
- statusCode: 400
- }))
-}).addBatch({
- 'users create elvis': runJitsuCommand(
- 'should respond with a 403 error',
- function assertion (ign, err) {
- err = ign;
- assert.equal(err.statusCode, '403');
- },
- mockPrompt({ email: 'e@mailinator.com', password: '12345' }),
- mockRequest.mock(helper.mockOptions, helper.mockDefaults)
- .post('/users/elvis', {
- email: 'e@mailinator.com',
- password: '12345',
- username: 'elvis'
- })
- .respond({
- statusCode: 403
- }))
+ function setup() {
+ jitsu.prompt.override = flatiron.common.clone(elvis);
+ jitsu.prompt.override['confirm password'] = elvis.password;
+
+ nock('http://api.mockjitsu.com')
+ .post('/users/elvis', elvis)
+ .reply(400, '', { 'x-powered-by': 'Nodejitsu' })
+ })
}).addBatch({
- 'users create elvis': runJitsuCommand(
+ 'users create elvis': shouldNodejitsuOk(
'should respond with a 500 error',
function assertion (ign, err) {
err = ign;
assert.equal(err.statusCode, '500');
},
- mockPrompt({ email: 'e@mailinator.com', password: '12345' }),
- mockRequest.mock(helper.mockOptions, helper.mockDefaults)
- .post('/users/elvis', {
- email: 'e@mailinator.com',
- password: '12345',
- username: 'elvis'
- })
- .respond({
- statusCode: 500
- }))
+ function setup() {
+ jitsu.prompt.override = flatiron.common.clone(elvis);
+ jitsu.prompt.override['confirm password'] = elvis.password;
+
+ nock('http://api.mockjitsu.com')
+ .post('/users/elvis', elvis)
+ .reply(500, '', { 'x-powered-by': 'Nodejitsu' })
+ })
}).addBatch({
- 'users create jimmy': runJitsuCommand(
- mockPrompt({ email: 'j@mailinator.com', password: '98765' }),
- mockRequest.mock(helper.mockOptions, helper.mockDefaults)
- .post('/users/jimmy', {
- email: "j@mailinator.com",
- password: "98765",
- username: "jimmy"
- }))
-})*/.addBatch({
- 'users available jimmy': runJitsuCommand(
- mockRequest.mock(helper.mockOptions, helper.mockDefaults)
+ 'users create jimmy': shouldNodejitsuOk(function setup() {
+ jitsu.prompt.override = flatiron.common.clone(jimmy);
+ jitsu.prompt.override['confirm password'] = jimmy.password;
+
+ nock('http://api.mockjitsu.com')
+ .post('/users/jimmy', jimmy)
+ .reply(200, '', { 'x-powered-by': 'Nodejitsu' })
+ })
+}).addBatch({
+ 'users available jimmy': shouldNodejitsuOk(function setup() {
+ nock('http://api.mockjitsu.com')
.get('/users/jimmy/available')
- .respond({
- body: { available: true }
- }))
-})/*.addBatch({
- 'users confirm jimmy': runJitsuCommand(
- mockPrompt({'Invite code': 'f4387f4'}),
- mockRequest.mock(helper.mockOptions, helper.mockDefaults)
- .post('/users/jimmy/confirm')
- .respond({
- body: {
- username: 'jimmy',
- inviteCode: 'f4387f4'
- }
- }))
+ .reply(200, { available: true }, { 'x-powered-by': 'Nodejitsu' })
+ })
}).addBatch({
+ 'users confirm jimmy': shouldNodejitsuOk(function setup() {
+ jitsu.prompt.override['invite code'] = 'f4387f4';
+ jitsu.prompt.override['set password'] = '123456';
+ jitsu.prompt.override['confirm password'] = '123456';
+
+ var testConf = path.join(__dirname, '..', 'fixtures', 'test-jitsuconf'),
+ conf = path.join(__dirname, '..', 'fixtures', 'dot-jitsuconf');
+
+ fs.writeFileSync(
+ testConf,
+ fs.readFileSync(conf, 'utf8'),
+ 'utf8'
+ );
+
+ jitsu.config.stores.file.file = testConf;
+ jitsu.config.stores.file.loadSync();
+
+ nock('http://api.mockjitsu.com')
+ .post('/users/jimmy/confirm', { username: 'jimmy', inviteCode: 'f4387f4'})
+ .reply(200, {
+ username: 'jimmy',
+ inviteCode: 'f4387f4'
+ }, { 'x-powered-by': 'Nodejitsu' })
+ })
+})/*.addBatch({
'users confirm jimmy': runJitsuCommand(
mockPrompt({ 'Invite code': 'f4387f4' }),
mockRequest.mock(helper.mockOptions, helper.mockDefaults)
@@ -112,8 +118,8 @@ vows.describe('jitsu/commands/users').addBatch({
}
})
)
-})*/.addBatch({
+}).addBatch({
'users forgot jimmy': runJitsuCommand(
mockRequest.mock(helper.mockOptions, helper.mockDefaults)
.post('/users/jimmy/forgot'))
-}).export(module);
+})*/.export(module);

0 comments on commit 94ba7e1

Please sign in to comment.
Something went wrong with that request. Please try again.