2017-09-03 03:38:26 +07:00
|
|
|
import Sinon = require("sinon");
|
2017-07-16 22:37:13 +07:00
|
|
|
import BluebirdPromise = require("bluebird");
|
2017-09-03 03:38:26 +07:00
|
|
|
import Assert = require("assert");
|
2017-10-20 05:42:33 +07:00
|
|
|
import { PasswordUpdater } from "../../src/lib/ldap/PasswordUpdater";
|
|
|
|
import { LdapConfiguration } from "../../src/lib/configuration/Configuration";
|
2017-09-03 03:38:26 +07:00
|
|
|
import { ClientFactoryStub } from "../mocks/ldap/ClientFactoryStub";
|
|
|
|
import { ClientStub } from "../mocks/ldap/ClientStub";
|
2017-10-20 05:42:33 +07:00
|
|
|
import { HashGenerator } from "../../src/lib/utils/HashGenerator";
|
2017-07-16 22:37:13 +07:00
|
|
|
|
|
|
|
describe("test password update", function () {
|
2017-09-03 03:38:26 +07:00
|
|
|
const USERNAME = "username";
|
|
|
|
const NEW_PASSWORD = "new-password";
|
|
|
|
|
|
|
|
const ADMIN_USER_DN = "cn=admin,dc=example,dc=com";
|
|
|
|
const ADMIN_PASSWORD = "password";
|
|
|
|
|
|
|
|
let clientFactoryStub: ClientFactoryStub;
|
|
|
|
let adminClientStub: ClientStub;
|
2017-07-16 22:37:13 +07:00
|
|
|
let passwordUpdater: PasswordUpdater;
|
|
|
|
let ldapConfig: LdapConfiguration;
|
2017-10-20 05:42:33 +07:00
|
|
|
let ssha512HashGenerator: Sinon.SinonStub;
|
2017-07-16 22:37:13 +07:00
|
|
|
|
|
|
|
beforeEach(function () {
|
2017-09-03 03:38:26 +07:00
|
|
|
clientFactoryStub = new ClientFactoryStub();
|
|
|
|
adminClientStub = new ClientStub();
|
2017-07-16 22:37:13 +07:00
|
|
|
|
|
|
|
ldapConfig = {
|
2017-09-03 03:38:26 +07:00
|
|
|
url: "http://ldap",
|
|
|
|
user: ADMIN_USER_DN,
|
|
|
|
password: ADMIN_PASSWORD,
|
|
|
|
users_dn: "ou=users,dc=example,dc=com",
|
|
|
|
groups_dn: "ou=groups,dc=example,dc=com",
|
|
|
|
group_name_attribute: "cn",
|
|
|
|
groups_filter: "cn={0}",
|
|
|
|
mail_attribute: "mail",
|
|
|
|
users_filter: "cn={0}"
|
2017-07-16 22:37:13 +07:00
|
|
|
};
|
|
|
|
|
2017-10-20 05:42:33 +07:00
|
|
|
ssha512HashGenerator = Sinon.stub(HashGenerator, "ssha512");
|
2017-11-01 22:30:47 +07:00
|
|
|
clientFactoryStub.createStub.withArgs(ADMIN_USER_DN, ADMIN_PASSWORD)
|
|
|
|
.returns(adminClientStub);
|
|
|
|
|
2017-09-03 03:38:26 +07:00
|
|
|
passwordUpdater = new PasswordUpdater(ldapConfig, clientFactoryStub);
|
2017-07-16 22:37:13 +07:00
|
|
|
});
|
|
|
|
|
2017-10-20 05:42:33 +07:00
|
|
|
afterEach(function () {
|
|
|
|
ssha512HashGenerator.restore();
|
|
|
|
});
|
|
|
|
|
2017-07-16 22:37:13 +07:00
|
|
|
describe("success", function () {
|
|
|
|
it("should update the password successfully", function () {
|
2017-11-01 22:30:47 +07:00
|
|
|
ssha512HashGenerator
|
|
|
|
.returns("{CRYPT}$6$abcdefghijklm$AQmxaKfobGY9HSQa6aDYkAWOgPGNhGYn");
|
|
|
|
adminClientStub.modifyPasswordStub.withArgs(USERNAME, NEW_PASSWORD)
|
|
|
|
.returns(BluebirdPromise.resolve());
|
2017-09-03 03:38:26 +07:00
|
|
|
adminClientStub.openStub.returns(BluebirdPromise.resolve());
|
|
|
|
adminClientStub.closeStub.returns(BluebirdPromise.resolve());
|
|
|
|
|
|
|
|
return passwordUpdater.updatePassword(USERNAME, NEW_PASSWORD);
|
2017-07-16 22:37:13 +07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("failure", function () {
|
2017-11-01 22:30:47 +07:00
|
|
|
it("should fail updating password when modify operation fails",
|
|
|
|
function () {
|
|
|
|
ssha512HashGenerator
|
|
|
|
.returns("{CRYPT}$6$abcdefghijklm$AQmxaKfobGY9HSQa6aDYkAWOgPGNhGYn");
|
|
|
|
adminClientStub.modifyPasswordStub.withArgs(USERNAME, NEW_PASSWORD)
|
|
|
|
.rejects(new Error("Error while updating password"));
|
|
|
|
adminClientStub.openStub.returns(BluebirdPromise.resolve());
|
|
|
|
adminClientStub.closeStub.returns(BluebirdPromise.resolve());
|
2017-09-03 03:38:26 +07:00
|
|
|
|
2017-11-01 22:30:47 +07:00
|
|
|
return passwordUpdater.updatePassword(USERNAME, NEW_PASSWORD)
|
|
|
|
.then(function () {
|
|
|
|
return BluebirdPromise.reject(new Error("should not be here"));
|
|
|
|
})
|
|
|
|
.catch(function(err: Error) {
|
|
|
|
return BluebirdPromise.resolve();
|
|
|
|
});
|
|
|
|
});
|
2017-07-16 22:37:13 +07:00
|
|
|
});
|
|
|
|
});
|