2017-07-16 15:37:13 +00:00
|
|
|
|
2017-10-06 22:09:42 +00:00
|
|
|
import { EmailsRetriever } from "../../src/lib/ldap/EmailsRetriever";
|
|
|
|
import { LdapConfiguration } from "../../src/lib/configuration/Configuration";
|
2017-07-16 15:37:13 +00:00
|
|
|
|
2017-09-02 20:38:26 +00:00
|
|
|
import Sinon = require("sinon");
|
2017-07-16 15:37:13 +00:00
|
|
|
import BluebirdPromise = require("bluebird");
|
2017-09-02 20:38:26 +00:00
|
|
|
import Assert = require("assert");
|
2017-07-16 15:37:13 +00:00
|
|
|
|
2017-09-02 20:38:26 +00:00
|
|
|
import { ClientFactoryStub } from "../mocks/ldap/ClientFactoryStub";
|
|
|
|
import { ClientStub } from "../mocks/ldap/ClientStub";
|
2017-07-16 15:37:13 +00:00
|
|
|
|
|
|
|
describe("test emails retriever", function () {
|
2017-09-02 20:38:26 +00:00
|
|
|
const USERNAME = "username";
|
|
|
|
const ADMIN_USER_DN = "cn=admin,dc=example,dc=com";
|
|
|
|
const ADMIN_PASSWORD = "password";
|
|
|
|
|
|
|
|
let clientFactoryStub: ClientFactoryStub;
|
|
|
|
let adminClientStub: ClientStub;
|
|
|
|
|
2017-07-16 15:37:13 +00:00
|
|
|
let emailsRetriever: EmailsRetriever;
|
|
|
|
let ldapConfig: LdapConfiguration;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
2017-09-02 20:38:26 +00:00
|
|
|
clientFactoryStub = new ClientFactoryStub();
|
|
|
|
adminClientStub = new ClientStub();
|
2017-07-16 15:37:13 +00:00
|
|
|
|
|
|
|
ldapConfig = {
|
2017-09-02 20:38:26 +00: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 15:37:13 +00:00
|
|
|
};
|
|
|
|
|
2017-09-02 20:38:26 +00:00
|
|
|
emailsRetriever = new EmailsRetriever(ldapConfig, clientFactoryStub);
|
2017-07-16 15:37:13 +00:00
|
|
|
});
|
|
|
|
|
2017-09-02 20:38:26 +00:00
|
|
|
describe("success", function () {
|
|
|
|
it("should retrieve emails successfully", function () {
|
|
|
|
clientFactoryStub.createStub.withArgs(ADMIN_USER_DN, ADMIN_PASSWORD)
|
|
|
|
.returns(adminClientStub);
|
2017-07-16 15:37:13 +00:00
|
|
|
|
2017-09-02 20:38:26 +00:00
|
|
|
// admin connects successfully
|
|
|
|
adminClientStub.openStub.returns(BluebirdPromise.resolve());
|
|
|
|
adminClientStub.closeStub.returns(BluebirdPromise.resolve());
|
2017-07-16 15:37:13 +00:00
|
|
|
|
2017-09-02 20:38:26 +00:00
|
|
|
adminClientStub.searchEmailsStub.withArgs(USERNAME)
|
|
|
|
.returns(BluebirdPromise.resolve(["user@example.com"]));
|
2017-07-16 15:37:13 +00:00
|
|
|
|
2017-09-02 20:38:26 +00:00
|
|
|
return emailsRetriever.retrieve(USERNAME);
|
2017-07-16 15:37:13 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("failure", function () {
|
2017-11-01 15:30:47 +00:00
|
|
|
it("should fail retrieving emails when search operation fails",
|
|
|
|
function () {
|
|
|
|
clientFactoryStub.createStub.withArgs(ADMIN_USER_DN, ADMIN_PASSWORD)
|
|
|
|
.returns(adminClientStub);
|
|
|
|
|
|
|
|
// admin connects successfully
|
|
|
|
adminClientStub.openStub.returns(BluebirdPromise.resolve());
|
|
|
|
adminClientStub.closeStub.returns(BluebirdPromise.resolve());
|
|
|
|
|
|
|
|
adminClientStub.searchEmailsStub.withArgs(USERNAME)
|
|
|
|
.rejects(new Error("Error while searching emails"));
|
|
|
|
|
|
|
|
return emailsRetriever.retrieve(USERNAME)
|
|
|
|
.then(function () {
|
|
|
|
return BluebirdPromise.reject(new Error("Should not be here"));
|
|
|
|
})
|
|
|
|
.catch(function () {
|
|
|
|
return BluebirdPromise.resolve();
|
|
|
|
});
|
|
|
|
});
|
2017-07-16 15:37:13 +00:00
|
|
|
});
|
|
|
|
});
|