authelia/test/server/LdapClient.test.ts

243 lines
7.5 KiB
TypeScript
Raw Normal View History

2017-05-21 06:15:34 +07:00
import LdapClient = require("../../src/server/lib/LdapClient");
import { LdapConfiguration } from "../../src/types/Configuration";
2017-05-21 06:15:34 +07:00
import sinon = require("sinon");
import BluebirdPromise = require("bluebird");
import assert = require("assert");
import ldapjs = require("ldapjs");
import winston = require("winston");
import { EventEmitter } from "events";
import { LdapjsMock, LdapjsClientMock } from "./mocks/ldapjs";
describe("test ldap validation", function () {
let ldap: LdapClient.LdapClient;
let ldapClient: LdapjsClientMock;
2017-05-21 06:15:34 +07:00
let ldapjs: LdapjsMock;
let ldapConfig: LdapConfiguration;
2017-05-21 06:15:34 +07:00
beforeEach(function () {
ldapClient = LdapjsClientMock();
2017-05-21 06:15:34 +07:00
ldapjs = LdapjsMock();
ldapjs.createClient.returns(ldapClient);
2017-05-21 06:15:34 +07:00
ldapConfig = {
2017-05-21 06:15:34 +07:00
url: "http://localhost:324",
user: "admin",
password: "password",
base_dn: "dc=example,dc=com",
additional_user_dn: "ou=users"
};
ldap = new LdapClient.LdapClient(ldapConfig, ldapjs, winston);
2017-05-21 06:15:34 +07:00
});
describe("test checking password", test_checking_password);
2017-05-21 06:15:34 +07:00
describe("test get emails from username", test_get_emails);
describe("test get groups from username", test_get_groups);
describe("test update password", test_update_password);
function test_checking_password() {
function test_check_password_internal() {
2017-05-21 06:15:34 +07:00
const username = "username";
const password = "password";
return ldap.checkPassword(username, password);
2017-05-21 06:15:34 +07:00
}
it("should bind the user if good credentials provided", function () {
ldapClient.bind.yields();
ldapClient.unbind.yields();
return test_check_password_internal();
2017-05-21 06:15:34 +07:00
});
it("should bind the user with correct DN", function () {
ldapConfig.user_name_attribute = "uid";
2017-05-21 06:15:34 +07:00
const username = "user";
const password = "password";
ldapClient.bind.withArgs("uid=user,ou=users,dc=example,dc=com").yields();
ldapClient.unbind.yields();
return ldap.checkPassword(username, password);
2017-05-21 06:15:34 +07:00
});
it("should default to cn user search filter if no filter provided", function () {
const username = "user";
const password = "password";
ldapClient.bind.withArgs("cn=user,ou=users,dc=example,dc=com").yields();
ldapClient.unbind.yields();
return ldap.checkPassword(username, password);
2017-05-21 06:15:34 +07:00
});
it("should not bind the user if wrong credentials provided", function () {
ldapClient.bind.yields("wrong credentials");
const promise = test_check_password_internal();
2017-05-21 06:15:34 +07:00
return promise.catch(function () {
return BluebirdPromise.resolve();
2017-05-21 06:15:34 +07:00
});
});
}
function test_get_emails() {
let res_emitter: any;
let expected_doc: any;
beforeEach(function () {
expected_doc = {
object: {
mail: "user@example.com"
}
};
res_emitter = {
on: sinon.spy(function (event: string, fn: (doc: any) => void) {
if (event != "error") fn(expected_doc);
})
};
});
it("should retrieve the email of an existing user", function () {
ldapClient.search.yields(undefined, res_emitter);
2017-05-21 06:15:34 +07:00
return ldap.retrieveEmails("user")
2017-05-21 06:15:34 +07:00
.then(function (emails) {
assert.deepEqual(emails, [expected_doc.object.mail]);
return BluebirdPromise.resolve();
2017-05-21 06:15:34 +07:00
});
});
it("should retrieve email for user with uid name attribute", function () {
ldapConfig.user_name_attribute = "uid";
ldapClient.search.withArgs("uid=username,ou=users,dc=example,dc=com").yields(undefined, res_emitter);
return ldap.retrieveEmails("username")
2017-05-21 06:15:34 +07:00
.then(function (emails) {
assert.deepEqual(emails, ["user@example.com"]);
return BluebirdPromise.resolve();
2017-05-21 06:15:34 +07:00
});
});
it("should fail on error with search method", function () {
const expected_doc = {
mail: ["user@example.com"]
};
ldapClient.search.yields("Error while searching mails");
2017-05-21 06:15:34 +07:00
return ldap.retrieveEmails("user")
2017-05-21 06:15:34 +07:00
.catch(function () {
return BluebirdPromise.resolve();
2017-05-21 06:15:34 +07:00
});
});
}
function test_get_groups() {
let res_emitter: any;
let expected_doc1: any, expected_doc2: any;
beforeEach(function () {
expected_doc1 = {
object: {
cn: "group1"
}
};
expected_doc2 = {
object: {
cn: "group2"
}
};
res_emitter = {
on: sinon.spy(function (event: string, fn: (doc: any) => void) {
if (event != "error") fn(expected_doc1);
if (event != "error") fn(expected_doc2);
})
};
});
it("should retrieve the groups of an existing user", function () {
ldapClient.search.yields(undefined, res_emitter);
return ldap.retrieveGroups("user")
2017-05-21 06:15:34 +07:00
.then(function (groups) {
assert.deepEqual(groups, ["group1", "group2"]);
return BluebirdPromise.resolve();
2017-05-21 06:15:34 +07:00
});
});
it("should reduce the scope to additional_group_dn", function (done) {
ldapConfig.additional_group_dn = "ou=groups";
ldapClient.search.yields(undefined, res_emitter);
ldap.retrieveGroups("user")
2017-05-21 06:15:34 +07:00
.then(function() {
assert.equal(ldapClient.search.getCall(0).args[0], "ou=groups,dc=example,dc=com");
2017-05-21 06:15:34 +07:00
done();
});
});
it("should use default group_name_attr if not provided", function (done) {
ldapClient.search.yields(undefined, res_emitter);
ldap.retrieveGroups("user")
2017-05-21 06:15:34 +07:00
.then(function() {
assert.equal(ldapClient.search.getCall(0).args[0], "dc=example,dc=com");
assert.equal(ldapClient.search.getCall(0).args[1].filter, "member=cn=user,ou=users,dc=example,dc=com");
assert.deepEqual(ldapClient.search.getCall(0).args[1].attributes, ["cn"]);
2017-05-21 06:15:34 +07:00
done();
});
});
it("should fail on error with search method", function () {
ldapClient.search.yields("error");
return ldap.retrieveGroups("user")
2017-05-21 06:15:34 +07:00
.catch(function () {
return BluebirdPromise.resolve();
2017-05-21 06:15:34 +07:00
});
});
}
function test_update_password() {
it("should update the password successfully", function () {
const change = {
operation: "replace",
modification: {
userPassword: "new-password"
}
};
const userdn = "cn=user,ou=users,dc=example,dc=com";
ldapClient.bind.yields();
ldapClient.unbind.yields();
ldapClient.modify.yields();
2017-05-21 06:15:34 +07:00
return ldap.updatePassword("user", "new-password")
2017-05-21 06:15:34 +07:00
.then(function () {
assert.deepEqual(ldapClient.modify.getCall(0).args[0], userdn);
assert.deepEqual(ldapClient.modify.getCall(0).args[1].operation, change.operation);
2017-05-21 06:15:34 +07:00
const userPassword = ldapClient.modify.getCall(0).args[1].modification.userPassword;
2017-05-21 06:15:34 +07:00
assert(/{SSHA}/.test(userPassword));
return BluebirdPromise.resolve();
})
.catch(function(err) { return BluebirdPromise.reject(new Error("It should fail")); });
2017-05-21 06:15:34 +07:00
});
it("should fail when ldap throws an error", function () {
ldapClient.bind.yields(undefined);
ldapClient.modify.yields("Error");
2017-05-21 06:15:34 +07:00
return ldap.updatePassword("user", "new-password")
2017-05-21 06:15:34 +07:00
.catch(function () {
return BluebirdPromise.resolve();
2017-05-21 06:15:34 +07:00
});
});
it("should update password of user using particular user name attribute", function () {
ldapConfig.user_name_attribute = "uid";
2017-05-21 06:15:34 +07:00
ldapClient.bind.yields();
ldapClient.unbind.yields();
ldapClient.modify.withArgs("uid=username,ou=users,dc=example,dc=com").yields();
return ldap.updatePassword("username", "newpass");
2017-05-21 06:15:34 +07:00
});
}
});