New to NodeJS and Express3 ... working through the tutorial i found at:
http://www.scotchmedia.com/tutorials/express/authentication
It seems rather helpful but i seem to be having some difficulty with getting a supertest to pass.
This is the test:
describe('POST /signup', function () {
it('should redirect to "/account" if the form is valid', function (done) {
var post = {
givenName: 'Barrack',
familyName: 'Obama',
email: 'Bob@bob.com',
password: 'secret'
};
request(app)
.post('/signup')
.set('Content-Type', 'application/json')
.send(post)
.expect(302)
.end(function (err, res) {
should.not.exist(err);
// confirm the redirect
res.header.location.should.include('/account');
done();
});
});
});
The /signup route is handled by:
exports.signup = function (req, res) {
console.log("Calling Signup")
req.onValidationError(function (msg) {
//Redirect to `/signup` if validation fails
console.log("Validation Failed: " + msg + " " + req.param('email'));
return res.redirect('/signup');
});
req.check('password', 'Please enter a password with a length between 4 and 34 digits').len(4, 34);
req.check('givenName', 'Please enter your first name').len(1);
req.check('familyName', 'Please enter your last name').len(1);
req.check('email', 'Please enter a valid email').len(1).isEmail();
// If the form is valid craete a new user
var newUser = {
name: {
givenName: req.body.givenName,
familyName: req.body.familyName
},
email: req.body.email
};
}
The difficulty is that the validation is always failing.
my app.j contains the following use statements:
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
//
//Routes
app.get('/', routes.index);
app.post('/signup', users.signup);
Out of ideas ... seems like req.body.xxx is not bound ...
Appreciate any guidance to reduce my level of ignorance.
Cheers