From e3e12357558cc072ebcaf1793096c40a3a239a80 Mon Sep 17 00:00:00 2001 From: Clement Michaud Date: Wed, 1 Nov 2017 16:30:47 +0100 Subject: [PATCH] Fix unhandled error exception thrown by Bluebirds in tests --- package.json | 5 +- server/src/lib/ldap/GroupsRetriever.ts | 39 ------------ server/src/lib/ldap/IGroupsRetriever.ts | 6 -- server/src/lib/ldap/PasswordUpdater.ts | 12 ++-- server/test/ldap/Authenticator.test.ts | 12 ++-- server/test/ldap/EmailsRetriever.test.ts | 29 +++++---- server/test/ldap/GroupsRetriever.test.ts | 75 ------------------------ server/test/ldap/PasswordUpdater.test.ts | 41 +++++++------ 8 files changed, 58 insertions(+), 161 deletions(-) delete mode 100644 server/src/lib/ldap/GroupsRetriever.ts delete mode 100644 server/src/lib/ldap/IGroupsRetriever.ts delete mode 100644 server/test/ldap/GroupsRetriever.test.ts diff --git a/package.json b/package.json index bfb6fede6..1b05cf3e6 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,7 @@ "@types/request": "^2.0.5", "@types/request-promise": "^4.1.38", "@types/selenium-webdriver": "^3.0.4", - "@types/sinon": "^2.2.1", + "@types/sinon": "^2.3.7", "@types/tmp": "0.0.33", "@types/winston": "^2.3.2", "@types/yamljs": "^0.2.30", @@ -101,8 +101,7 @@ "request-promise": "^4.2.2", "selenium-webdriver": "^3.5.0", "should": "^11.1.1", - "sinon": "^2.3.8", - "sinon-promise": "^0.1.3", + "sinon": "^4.0.2", "tmp": "0.0.31", "ts-node": "^3.3.0", "tslint": "^5.2.0", diff --git a/server/src/lib/ldap/GroupsRetriever.ts b/server/src/lib/ldap/GroupsRetriever.ts deleted file mode 100644 index f5b02df97..000000000 --- a/server/src/lib/ldap/GroupsRetriever.ts +++ /dev/null @@ -1,39 +0,0 @@ -import BluebirdPromise = require("bluebird"); -import exceptions = require("../Exceptions"); -import ldapjs = require("ldapjs"); -import { IClient } from "./IClient"; - -import { IClientFactory } from "./IClientFactory"; -import { IGroupsRetriever } from "./IGroupsRetriever"; -import { LdapConfiguration } from "../configuration/Configuration"; - - -export class GroupsRetriever implements IGroupsRetriever { - private options: LdapConfiguration; - private clientFactory: IClientFactory; - - constructor(options: LdapConfiguration, clientFactory: IClientFactory) { - this.options = options; - this.clientFactory = clientFactory; - } - - retrieve(username: string, client?: IClient): BluebirdPromise { - client = this.clientFactory.create(this.options.user, this.options.password); - let groups: string[]; - - return client.open() - .then(function () { - return client.searchGroups(username); - }) - .then(function (groups_: string[]) { - groups = groups_; - return client.close(); - }) - .then(function () { - return BluebirdPromise.resolve(groups); - }) - .catch(function (err: Error) { - return BluebirdPromise.reject(new exceptions.LdapError("Failed during groups retrieval: " + err.message)); - }); - } -} diff --git a/server/src/lib/ldap/IGroupsRetriever.ts b/server/src/lib/ldap/IGroupsRetriever.ts deleted file mode 100644 index ce43ac948..000000000 --- a/server/src/lib/ldap/IGroupsRetriever.ts +++ /dev/null @@ -1,6 +0,0 @@ -import BluebirdPromise = require("bluebird"); -import { IClient } from "./IClient"; - -export interface IGroupsRetriever { - retrieve(username: string): BluebirdPromise; -} \ No newline at end of file diff --git a/server/src/lib/ldap/PasswordUpdater.ts b/server/src/lib/ldap/PasswordUpdater.ts index 4e00034db..e492f03b5 100644 --- a/server/src/lib/ldap/PasswordUpdater.ts +++ b/server/src/lib/ldap/PasswordUpdater.ts @@ -17,8 +17,10 @@ export class PasswordUpdater implements IPasswordUpdater { this.clientFactory = clientFactory; } - updatePassword(username: string, newPassword: string): BluebirdPromise { - const adminClient = this.clientFactory.create(this.options.user, this.options.password); + updatePassword(username: string, newPassword: string) + : BluebirdPromise { + const adminClient = this.clientFactory.create(this.options.user, + this.options.password); return adminClient.open() .then(function () { @@ -27,8 +29,10 @@ export class PasswordUpdater implements IPasswordUpdater { .then(function () { return adminClient.close(); }) - .error(function (err: Error) { - return BluebirdPromise.reject(new exceptions.LdapError("Failed during password update: " + err.message)); + .catch(function (err: Error) { + return BluebirdPromise.reject( + new exceptions.LdapError( + "Error while updating password: " + err.message)); }); } } diff --git a/server/test/ldap/Authenticator.test.ts b/server/test/ldap/Authenticator.test.ts index 59682d268..5300d352e 100644 --- a/server/test/ldap/Authenticator.test.ts +++ b/server/test/ldap/Authenticator.test.ts @@ -87,11 +87,13 @@ describe("test ldap authentication", function () { .returns(BluebirdPromise.resolve("cn=" + USERNAME + ",ou=users,dc=example,dc=com")); // user connects successfully - userClientStub.openStub.returns(BluebirdPromise.reject(new Error("Error while binding"))); + userClientStub.openStub.rejects(new Error("Error while binding")); userClientStub.closeStub.returns(BluebirdPromise.resolve()); return authenticator.authenticate(USERNAME, PASSWORD) - .then(function () { return BluebirdPromise.reject("Should not be here!"); }) + .then(function () { + return BluebirdPromise.reject("Should not be here!"); + }) .catch(function () { return BluebirdPromise.resolve(); }); @@ -118,10 +120,12 @@ describe("test ldap authentication", function () { adminClientStub.searchEmailsStub.returns(BluebirdPromise.resolve(["group1"])); // admin retrieves emails and groups of user adminClientStub.searchGroupsStub - .returns(BluebirdPromise.reject(new Error("Error while retrieving emails and groups"))); + .rejects(new Error("Error while retrieving emails and groups")); return authenticator.authenticate(USERNAME, PASSWORD) - .then(function () { return BluebirdPromise.reject("Should not be here!"); }) + .then(function () { + return BluebirdPromise.reject("Should not be here!"); + }) .catch(function () { return BluebirdPromise.resolve(); }); diff --git a/server/test/ldap/EmailsRetriever.test.ts b/server/test/ldap/EmailsRetriever.test.ts index 2826c4625..fcc8846d3 100644 --- a/server/test/ldap/EmailsRetriever.test.ts +++ b/server/test/ldap/EmailsRetriever.test.ts @@ -56,20 +56,25 @@ describe("test emails retriever", function () { }); describe("failure", function () { - it("should fail retrieving emails when search operation fails", function () { - clientFactoryStub.createStub.withArgs(ADMIN_USER_DN, ADMIN_PASSWORD) - .returns(adminClientStub); + 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()); + // admin connects successfully + adminClientStub.openStub.returns(BluebirdPromise.resolve()); + adminClientStub.closeStub.returns(BluebirdPromise.resolve()); - adminClientStub.searchEmailsStub.withArgs(USERNAME) - .returns(BluebirdPromise.reject(new Error("Error while searching emails"))); + 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(); }); - }); + return emailsRetriever.retrieve(USERNAME) + .then(function () { + return BluebirdPromise.reject(new Error("Should not be here")); + }) + .catch(function () { + return BluebirdPromise.resolve(); + }); + }); }); }); \ No newline at end of file diff --git a/server/test/ldap/GroupsRetriever.test.ts b/server/test/ldap/GroupsRetriever.test.ts deleted file mode 100644 index 922383aea..000000000 --- a/server/test/ldap/GroupsRetriever.test.ts +++ /dev/null @@ -1,75 +0,0 @@ - -import { GroupsRetriever } from "../../src/lib/ldap/GroupsRetriever"; -import { LdapConfiguration } from "../../src/lib/configuration/Configuration"; - -import Sinon = require("sinon"); -import BluebirdPromise = require("bluebird"); -import Assert = require("assert"); - -import { ClientFactoryStub } from "../mocks/ldap/ClientFactoryStub"; -import { ClientStub } from "../mocks/ldap/ClientStub"; - -describe("test groups retriever", function () { - const USERNAME = "username"; - const ADMIN_USER_DN = "cn=admin,dc=example,dc=com"; - const ADMIN_PASSWORD = "password"; - - let clientFactoryStub: ClientFactoryStub; - let adminClientStub: ClientStub; - - let groupsRetriever: GroupsRetriever; - let ldapConfig: LdapConfiguration; - - beforeEach(function () { - clientFactoryStub = new ClientFactoryStub(); - adminClientStub = new ClientStub(); - - ldapConfig = { - 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: "member=cn={0},ou=users,dc=example,dc=com", - mail_attribute: "mail", - users_filter: "cn={0}" - }; - - groupsRetriever = new GroupsRetriever(ldapConfig, clientFactoryStub); - }); - - describe("success", function () { - it("should retrieve groups successfully", 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.searchGroupsStub.withArgs(USERNAME) - .returns(BluebirdPromise.resolve(["user@example.com"])); - - return groupsRetriever.retrieve(USERNAME); - }); - }); - - describe("failure", function () { - it("should fail retrieving groups 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.searchGroupsStub.withArgs(USERNAME) - .returns(BluebirdPromise.reject(new Error("Error while searching groups"))); - - return groupsRetriever.retrieve(USERNAME) - .then(function () { return BluebirdPromise.reject(new Error("Should not be here")); }) - .catch(function () { return BluebirdPromise.resolve(); }); - }); - }); -}); \ No newline at end of file diff --git a/server/test/ldap/PasswordUpdater.test.ts b/server/test/ldap/PasswordUpdater.test.ts index aa8752844..e88192614 100644 --- a/server/test/ldap/PasswordUpdater.test.ts +++ b/server/test/ldap/PasswordUpdater.test.ts @@ -37,6 +37,9 @@ describe("test password update", function () { }; ssha512HashGenerator = Sinon.stub(HashGenerator, "ssha512"); + clientFactoryStub.createStub.withArgs(ADMIN_USER_DN, ADMIN_PASSWORD) + .returns(adminClientStub); + passwordUpdater = new PasswordUpdater(ldapConfig, clientFactoryStub); }); @@ -46,11 +49,10 @@ describe("test password update", function () { describe("success", function () { it("should update the password successfully", function () { - clientFactoryStub.createStub.withArgs(ADMIN_USER_DN, ADMIN_PASSWORD) - .returns(adminClientStub); - - ssha512HashGenerator.returns("{CRYPT}$6$abcdefghijklm$AQmxaKfobGY9HSQa6aDYkAWOgPGNhGYn"); - adminClientStub.modifyPasswordStub.withArgs(USERNAME, NEW_PASSWORD).returns(BluebirdPromise.resolve()); + ssha512HashGenerator + .returns("{CRYPT}$6$abcdefghijklm$AQmxaKfobGY9HSQa6aDYkAWOgPGNhGYn"); + adminClientStub.modifyPasswordStub.withArgs(USERNAME, NEW_PASSWORD) + .returns(BluebirdPromise.resolve()); adminClientStub.openStub.returns(BluebirdPromise.resolve()); adminClientStub.closeStub.returns(BluebirdPromise.resolve()); @@ -59,19 +61,22 @@ describe("test password update", function () { }); describe("failure", function () { - it("should fail updating password when modify operation fails", function () { - clientFactoryStub.createStub.withArgs(ADMIN_USER_DN, ADMIN_PASSWORD) - .returns(adminClientStub); + 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()); - ssha512HashGenerator.returns("{CRYPT}$6$abcdefghijklm$AQmxaKfobGY9HSQa6aDYkAWOgPGNhGYn"); - adminClientStub.modifyPasswordStub.withArgs(USERNAME, NEW_PASSWORD) - .returns(BluebirdPromise.reject(new Error("Error while updating password"))); - adminClientStub.openStub.returns(BluebirdPromise.resolve()); - adminClientStub.closeStub.returns(BluebirdPromise.resolve()); - - return passwordUpdater.updatePassword(USERNAME, NEW_PASSWORD) - .then(function () { return BluebirdPromise.reject(new Error("should not be here")); }) - .catch(function () { return BluebirdPromise.resolve(); }); - }); + return passwordUpdater.updatePassword(USERNAME, NEW_PASSWORD) + .then(function () { + return BluebirdPromise.reject(new Error("should not be here")); + }) + .catch(function(err: Error) { + return BluebirdPromise.resolve(); + }); + }); }); }); \ No newline at end of file