Unit-test login with passport.js and express.js
express, javascript, node.js, passport.js, unit-testing
Solution
I'm trying using this code and work as expected
var User, app, mongoose, request, server, should, user, agent;
should = require("should");
app = require("../server");
mongoose = require("mongoose");
User = mongoose.model("User");
request = require("supertest");
agent = request.agent(app)
describe('User', function () {
before(function(done) {
user = new User({
email : "user@user.com",
firstName: "Full Name",
lastName : "Last Name",
password : "pass11"
});
user.save(done)
});
describe('Login test', function () {
it('should redirect to /', function (done) {
agent
.post('/users/session')
.field('email', 'user@user.com')
.field('password', 'pass11')
.expect('Location','/')
.end(done)
})
after(function(done) {
User.remove().exec();
return done();
});
})
})
for more reference check this test-user
here my screenshot
Problem
I'm trying to test my local-login. I've implemented with `passport.js`, following its guide and following this MEAN skeleton. I'm pretty sure that the implementation is fine, but there is something wrong with the test that always fails authentication. If authentication fails it should be redirect to "/signin" if authentication is correct it should be go to "/" But when I test, the authentication always fails. This is routes.js: ``` module.exports = function(app, passport, auth) { var users = require('../app/controllers/users'); app.get('/signin', users.signin); app.post('/users/session', passport.authenticate('local', { failureRedirect: '/signin', failureFlash: 'Invalid email or password.' }), users.session); var index = require('../app/controllers/index'); app.get('/', index.render); ``` passport.js: ``` var mongoose = require('mongoose'), LocalStrategy = require('passport-local').Strategy, User = mongoose.model('User'), config = require('./config'); module.exports = function(passport) { //Serialize sessions passport.serializeUser(function(user, done) { done(null, user.id); }); passport.deserializeUser(function(id, done) { User.findOne({ _id: id }, '-salt -hashed_password', function(err, user) { done(err, user); }); }); //Use local strategy passport.use(new LocalStrategy({ usernameField: 'email', passwordField: 'password' }, function(email, password, done) { User.findOne({ email: email }, function(err, user) { if (err) { return done(err); } if (!user) { return done(null, false, { message: 'Unknown user' }); } if (!user.authenticate(password)) { return done(null, false, { message: 'Invalid password' }); } return done(null, user); }); } )); } ``` and test: api.js: ``` var User, app, mongoose, request, server, should, user; should = require("should"); app = require("../server"); mongoose = require("mongoose"); User = mongoose.model("User"); request = require("supertest"); server = request.agent("http://localhost:3000"); describe("<Unit Test>", function() { return describe("API User:", function() { before(function(done) { user = new User({ email : "user@user.com", firstName: "Full Name", lastName : "Last Name", password : "pass11" }); user.save(); return done(); }); describe("Authentication", function() { return it("Local login", function(done) { return server.post("/users/session").send({ email : "user@user.com", password: "pass11" }).end(function(err, res) { res.headers.location.should.have.equal("/"); return done(); }); }); }); return after(function(done) { User.remove().exec(); return done(); }); }); }); ``` This is what the terminal displays: ``` <Unit Test> API User: Authentication 1) Local login 0 passing (24ms) 1 failing 1) <Unit Test> API User: Authentication Local login: actual expected /signin ```