Skip to content

Commit

Permalink
More test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhanson committed Jan 14, 2014
1 parent 9faf408 commit 9474ffc
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 2 deletions.
59 changes: 57 additions & 2 deletions test/middleware/authenticate.fail.callback.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,64 @@ describe('middleware/authenticate', function() {
var passport = new Passport();
passport.use('fail', new Strategy());

var request, error, user, info;
var request, error, user, info, status;

before(function(done) {
function callback(e, u, i) {
function callback(e, u, i, s) {
error = e;
user = u;
info = i;
status = s;
done();
}

chai.connect.use(authenticate('fail', callback).bind(passport))
.req(function(req) {
request = req;
})
.dispatch();
});

it('should not error', function() {
expect(error).to.be.null;
});

it('should pass false to callback', function() {
expect(user).to.equal(false);
});

it('should pass info to callback', function() {
expect(info).to.be.an('object');
expect(info.message).to.equal('Invalid password');
});

it('should pass status to callback', function() {
expect(status).to.be.undefined;
});

it('should not set user on request', function() {
expect(request.user).to.be.undefined;
});
});

describe('fail with callback, passing info and status', function() {
function Strategy() {
}
Strategy.prototype.authenticate = function(req) {
this.fail({ message: 'Invalid password' }, 403);
}

var passport = new Passport();
passport.use('fail', new Strategy());

var request, error, user, info, status;

before(function(done) {
function callback(e, u, i, s) {
error = e;
user = u;
info = i;
status = s;
done();
}

Expand All @@ -84,6 +135,10 @@ describe('middleware/authenticate', function() {
expect(info.message).to.equal('Invalid password');
});

it('should pass status to callback', function() {
expect(status).to.equal(403);
});

it('should not set user on request', function() {
expect(request.user).to.be.undefined;
});
Expand Down
44 changes: 44 additions & 0 deletions test/middleware/authenticate.fail.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,48 @@ describe('middleware/authenticate', function() {
});
});

describe('fail with error, passing info to fail', function() {
function Strategy() {
}
Strategy.prototype.authenticate = function(req) {
this.fail({ message: 'Invalid credentials' });
}

var passport = new Passport();
passport.use('fail', new Strategy());

var request, response, error;

before(function(done) {
chai.connect.use('express', authenticate('fail', { failWithError: true }).bind(passport))
.req(function(req) {
request = req;
})
.res(function(res) {
response = res;
})
.next(function(err) {
error = err;
done();
})
.dispatch();
});

it('should error', function() {
expect(error).to.be.an.instanceOf(Error);
expect(error.constructor.name).to.equal('AuthenticationError')
expect(error.message).to.equal('Unauthorized');
});

it('should not set user', function() {
expect(request.user).to.be.undefined;
});

it('should not set body of response', function() {
expect(response.statusCode).to.equal(401);
expect(response.getHeader('Location')).to.be.undefined;
expect(response.body).to.be.undefined;
});
});

});

0 comments on commit 9474ffc

Please sign in to comment.