2017-01-19 07:01:37 +07:00
|
|
|
|
|
|
|
var sinon = require('sinon');
|
|
|
|
var Promise = require('bluebird');
|
|
|
|
var assert = require('assert');
|
2017-01-22 23:54:45 +07:00
|
|
|
var winston = require('winston');
|
2017-01-19 07:01:37 +07:00
|
|
|
var first_factor = require('../../../src/lib/routes/first_factor');
|
|
|
|
|
|
|
|
describe('test the first factor validation route', function() {
|
2017-01-21 23:41:06 +07:00
|
|
|
var req, res;
|
|
|
|
var ldap_interface_mock;
|
2017-01-22 23:54:45 +07:00
|
|
|
var search_res_ok;
|
2017-01-21 23:41:06 +07:00
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
ldap_interface_mock = {
|
2017-01-22 23:54:45 +07:00
|
|
|
bind: sinon.stub(),
|
|
|
|
search: sinon.stub()
|
2017-01-21 23:41:06 +07:00
|
|
|
}
|
|
|
|
var config = {
|
|
|
|
ldap_users_dn: 'dc=example,dc=com'
|
|
|
|
}
|
|
|
|
|
2017-01-22 23:54:45 +07:00
|
|
|
var search_doc = {
|
|
|
|
object: {
|
|
|
|
mail: 'test_ok@example.com'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var search_res_ok = {};
|
|
|
|
search_res_ok.on = sinon.spy(function(event, fn) {
|
|
|
|
if(event != 'error') fn(search_doc);
|
|
|
|
});
|
|
|
|
ldap_interface_mock.search.yields(undefined, search_res_ok);
|
|
|
|
|
2017-01-21 23:41:06 +07:00
|
|
|
var app_get = sinon.stub();
|
|
|
|
app_get.withArgs('ldap client').returns(ldap_interface_mock);
|
|
|
|
app_get.withArgs('config').returns(ldap_interface_mock);
|
2017-01-22 23:54:45 +07:00
|
|
|
app_get.withArgs('logger').returns(winston);
|
|
|
|
|
2017-01-21 23:41:06 +07:00
|
|
|
req = {
|
|
|
|
app: {
|
|
|
|
get: app_get
|
|
|
|
},
|
|
|
|
body: {
|
|
|
|
username: 'username',
|
|
|
|
password: 'password'
|
|
|
|
},
|
|
|
|
session: {
|
|
|
|
auth_session: {
|
|
|
|
first_factor: false,
|
|
|
|
second_factor: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res = {
|
|
|
|
send: sinon.spy(),
|
|
|
|
status: sinon.spy()
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-01-19 07:01:37 +07:00
|
|
|
it('should return status code 204 when LDAP binding succeeds', function() {
|
2017-01-21 23:41:06 +07:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
res.send = sinon.spy(function(data) {
|
|
|
|
assert.equal('username', req.session.auth_session.userid);
|
|
|
|
assert.equal(204, res.status.getCall(0).args[0]);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
ldap_interface_mock.bind.yields(undefined);
|
|
|
|
first_factor(req, res);
|
|
|
|
});
|
2017-01-19 07:01:37 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return status code 401 when LDAP binding fails', function() {
|
2017-01-21 23:41:06 +07:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
res.send = sinon.spy(function(data) {
|
|
|
|
assert.equal(401, res.status.getCall(0).args[0]);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
ldap_interface_mock.bind.yields('Bad credentials');
|
|
|
|
first_factor(req, res);
|
|
|
|
});
|
2017-01-19 07:01:37 +07:00
|
|
|
});
|
2017-01-22 23:54:45 +07:00
|
|
|
|
|
|
|
it('should return status code 500 when LDAP binding fails', function() {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
res.send = sinon.spy(function(data) {
|
|
|
|
assert.equal(500, res.status.getCall(0).args[0]);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
ldap_interface_mock.bind.yields(undefined);
|
|
|
|
ldap_interface_mock.search.yields('error');
|
|
|
|
first_factor(req, res);
|
|
|
|
});
|
|
|
|
});
|
2017-01-19 07:01:37 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
|