From a2dd36af5a0cc05b5bd63e0e6f9b8264c06d3cd4 Mon Sep 17 00:00:00 2001 From: "Wright, Christopher R" Date: Wed, 19 Sep 2018 16:59:38 -0400 Subject: [PATCH] #21 - Add new test for the InvalidCredentialManager Signed-off-by: Wright, Christopher R --- .../InvalidCredentialManager.test.ts | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 packages/security/__tests__/InvalidCredentialManager.test.ts diff --git a/packages/security/__tests__/InvalidCredentialManager.test.ts b/packages/security/__tests__/InvalidCredentialManager.test.ts new file mode 100644 index 000000000..a4c70b6aa --- /dev/null +++ b/packages/security/__tests__/InvalidCredentialManager.test.ts @@ -0,0 +1,48 @@ +/* + * This program and the accompanying materials are made available under the terms of the * + * Eclipse Public License v2.0 which accompanies this distribution, and is available at * + * https://www.eclipse.org/legal/epl-v20.html * + * * + * SPDX-License-Identifier: EPL-2.0 * + * * + * Copyright Contributors to the Zowe Project. * + * * + */ + +import { InvalidCredentialManager } from "../src/InvalidCredentialManager"; +import { BadCredentialManagerError } from ".."; + +describe("InvalidCredentialMangager", () => { + it("should throw an error for every available method on the class", async () => { + const error = new Error("This is a test"); + const invalidManager = new InvalidCredentialManager("cli", error); + const ignoreMethods = [ "constructor" ]; + + // Loops through the InvalidCredentialManager class's direct methods and will + // check that each one throws the expected BadCredentialManagerError object. + // If more methods are ever added to the invalid manager, they will be checked + // to see that they do throw errors. + for (const method of Object.getOwnPropertyNames(Object.getPrototypeOf(invalidManager))) { + // We will ignore any methods specified in the ignoreMethods array and + // only look at functions of the class. + if (ignoreMethods.indexOf(method) === -1 && typeof (invalidManager as any)[method] === "function") { + let caughtError: Error; + + try { + // Try to invoke the method. We don't care about the parameters because neither should + // the class throwing errors. + await (invalidManager as any)[method](); + } catch (e) { + caughtError = e; + } + + // Check to see if this method fails the test. + if (caughtError == null || !(caughtError instanceof BadCredentialManagerError)) { + fail(`InvalidCredentialManager.${method} does not properly throw a valid BadCredentialError.`); + } else if (caughtError.causeErrors !== error) { + fail(`InvalidCredentialManager.${method} does not preserve the error set in the constructor.`); + } + } + } + }); +});