Skip to content

Commit

Permalink
Fix test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew W. Harn <[email protected]>
  • Loading branch information
awharn committed Feb 3, 2025
1 parent 28e3d8c commit d0553d0
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { setupConfigToLoad } from "../../../__tests__/src/TestUtil";
import { EnvFileUtils } from "../../utilities";
import { join } from "path";
import { Config } from "../../config";
import { LoggerUtils } from "../../logger/src/LoggerUtils";
import { Censor } from "../../censor";

jest.mock("../src/syntax/SyntaxValidator");
jest.mock("../src/utils/SharedOptions");
Expand Down Expand Up @@ -790,7 +790,7 @@ describe("Command Processor", () => {
expect(commandResponse.error?.additionalDetails).toEqual("Syntax validation error!");
});

it("should mask sensitive CLI options like user and password in log output", async () => {
it("should mask sensitive CLI options like user and password in log output 1", async () => {
// Allocate the command processor
const processor: CommandProcessor = new CommandProcessor({
envVariablePrefix: ENV_VAR_PREFIX,
Expand Down Expand Up @@ -824,6 +824,40 @@ describe("Command Processor", () => {
expect(logOutput).toContain("--user fakeUser --password **** --token-value **** --cert-file-passphrase **** --cert-key-file /fake/path");
});

it("should mask sensitive CLI options like user and password in log output 2", async () => {
// Allocate the command processor
const processor: CommandProcessor = new CommandProcessor({
envVariablePrefix: ENV_VAR_PREFIX,
fullDefinition: SAMPLE_COMPLEX_COMMAND,
definition: SAMPLE_COMMAND_DEFINITION,
helpGenerator: FAKE_HELP_GENERATOR,
rootCommandName: SAMPLE_ROOT_COMMAND,
commandLine: "-u fakeUser --password fakePass --token-value fakeToken " +
"--cert-file-passphrase fakePassphrase --cert-key-file /fake/path",
promptPhrase: "dummydummy"
});

// Mock log.info call
let logOutput: string = "";
const mockLogInfo = jest.fn((line) => {
logOutput += line + "\n";
});
Object.defineProperty(processor, "log", {
get: () => ({
debug: jest.fn(),
error: jest.fn(),
info: mockLogInfo,
trace: jest.fn()
})
});

const parms: any = { arguments: { _: [], $0: "", syntaxThrow: true }, responseFormat: "json", silent: true };
const commandResponse: ICommandResponse = await processor.invoke(parms);

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note test

Unused variable commandResponse.

expect(mockLogInfo).toHaveBeenCalled();
expect(logOutput).toContain("-u fakeUser --password **** --token-value **** --cert-file-passphrase **** --cert-key-file /fake/path");
});

it("should handle not being able to instantiate the handler", async () => {
// Allocate the command processor
const processor: CommandProcessor = new CommandProcessor({
Expand Down Expand Up @@ -1448,7 +1482,7 @@ describe("Command Processor", () => {
expect(commandResponse.data.requiredProfiles).toBeUndefined();
});

it.each(LoggerUtils.SECURE_PROMPT_OPTIONS)("should mask input value for secure parm %s when --show-inputs-only flag is set", async (propName) => {
it.each(Censor.SECURE_PROMPT_OPTIONS)("should mask input value for secure parm %s when --show-inputs-only flag is set", async (propName) => {

// values to test
const parm1Key = CliUtils.getOptionFormat(propName).kebabCase;
Expand Down
8 changes: 6 additions & 2 deletions packages/imperative/src/logger/__tests__/Logger.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe("Logger tests", () => {

it("Should allow all service function to store message in memory", () => {
const logger = Logger.getImperativeLogger();
const expectedSize = 6;
const expectedSize = 7;
Logger.setLogInMemory(true);

logger.trace("test");
Expand All @@ -111,6 +111,7 @@ describe("Logger tests", () => {
logger.warn("test");
logger.error("test");
logger.fatal("test");
logger.simple("test");

expect(LoggerManager.instance.QueuedMessages.length).toBe(expectedSize);
});
Expand Down Expand Up @@ -169,6 +170,7 @@ describe("Logger tests", () => {
(logger as any).logService.warn = jest.fn<string, any>((data: string) => data);
(logger as any).logService.error = jest.fn<string, any>((data: string) => data);
(logger as any).logService.fatal = jest.fn<string, any>((data: string) => data);
(logger as any).logService.simple = jest.fn<string, any>((data: string) => data);

const error = new ImperativeError({msg: "sample error"});

Expand All @@ -180,6 +182,7 @@ describe("Logger tests", () => {
expect((logger as any).logService.warn).not.toHaveBeenCalledTimes(1);
expect((logger as any).logService.fatal).not.toHaveBeenCalled();
expect((logger as any).logService.error).toHaveBeenCalledTimes(2);
expect((logger as any).logService.simple).not.toHaveBeenCalled();
});

it("Should get the correct requested logger appender", () => {
Expand Down Expand Up @@ -270,7 +273,7 @@ describe("Logger tests", () => {

it("Should support writing all of the message in memory to file", () => {
const logger = Logger.getImperativeLogger();
const expectedSize = 6;
const expectedSize = 7;
Logger.setLogInMemory(true);

logger.trace("test");
Expand All @@ -279,6 +282,7 @@ describe("Logger tests", () => {
logger.warn("test");
logger.error("test");
logger.fatal("test");
logger.simple("test");

expect(LoggerManager.instance.QueuedMessages.length).toBe(expectedSize);

Expand Down
24 changes: 24 additions & 0 deletions packages/imperative/src/logger/__tests__/LoggerUtils.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jest.mock("../../config/src/Config");
import { EnvironmentalVariableSettings } from "../../imperative/src/env/EnvironmentalVariableSettings";
import { LoggerUtils } from "../src/LoggerUtils";
import { ImperativeConfig } from "../../utilities/src/ImperativeConfig";
import { Censor } from "../..";

afterAll(() => {
jest.restoreAllMocks();
Expand Down Expand Up @@ -145,4 +146,27 @@ describe("LoggerUtils tests", () => {
});
});
});

describe("isSpecialValue", () => {
let impConfigSpy: jest.SpyInstance = null;

beforeEach(() => {
(Censor as any).mSchema = null;
impConfigSpy = jest.spyOn(ImperativeConfig, "instance", "get");
});

afterAll(() => {
(Censor as any).mSchema = null;
});

it("should check if user is a special value", () => {
expect(LoggerUtils.isSpecialValue("profiles.test.properties.user")).toBe(true);
});
});

it("should get profile schemas from Censor", () => {
const schemaSpy = jest.spyOn(Censor, "profileSchemas", "get");
expect(LoggerUtils.profileSchemas).toEqual(Censor.profileSchemas);
expect(schemaSpy).toHaveBeenCalledTimes(2);
});
});

0 comments on commit d0553d0

Please sign in to comment.