Skip to content

Commit 58ca777

Browse files
committed
tests: cases for default cred mgr and AppSettings
Signed-off-by: Trae Yelovich <[email protected]>
1 parent 612f8c0 commit 58ca777

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed

packages/imperative/src/security/__tests__/DefaultCredentialManager.unit.test.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { Module } from "module";
1515
import { DefaultCredentialManager } from "..";
1616
import { keyring as keytar } from "@zowe/secrets-for-zowe-sdk";
1717
import { ImperativeError } from "../../error";
18-
import { PersistenceValue } from "../src/doc/IDefaultCredentialManagerOptions";
18+
import { PersistenceLevel, PersistenceValue } from "../src/doc/IDefaultCredentialManagerOptions";
1919

2020
const winMaxCredentialLength = 2560;
2121

@@ -31,6 +31,49 @@ describe("DefaultCredentialManager", () => {
3131
expect((manager as any).service).toEqual(service);
3232
});
3333

34+
describe("constructor switch statement", () => {
35+
it("should set persistValueWin32 to SessionOnly when persist option is SessionOnly", () => {
36+
const service = "imperative";
37+
const manager = new DefaultCredentialManager(service, "test manager", {
38+
persist: PersistenceLevel.SessionOnly
39+
});
40+
41+
expect((manager as any).persistValueWin32).toEqual(PersistenceValue.SessionOnly);
42+
});
43+
44+
it("should set persistValueWin32 to LocalMachine when persist option is LocalMachine", () => {
45+
const service = "imperative";
46+
const manager = new DefaultCredentialManager(service, "test manager", {
47+
persist: PersistenceLevel.LocalMachine
48+
});
49+
50+
expect((manager as any).persistValueWin32).toEqual(PersistenceValue.LocalMachine);
51+
});
52+
53+
it("should set persistValueWin32 to Enterprise when persist option is Enterprise", () => {
54+
const service = "imperative";
55+
const manager = new DefaultCredentialManager(service, "test manager", {
56+
persist: PersistenceLevel.Enterprise
57+
});
58+
59+
expect((manager as any).persistValueWin32).toEqual(PersistenceValue.Enterprise);
60+
});
61+
62+
it("should default to Enterprise when no persist option is provided", () => {
63+
const service = "imperative";
64+
const manager = new DefaultCredentialManager(service, "test manager");
65+
66+
expect((manager as any).persistValueWin32).toEqual(PersistenceValue.Enterprise);
67+
});
68+
69+
it("should default to Enterprise when options object is undefined", () => {
70+
const service = "imperative";
71+
const manager = new DefaultCredentialManager(service, "test manager", undefined);
72+
73+
expect((manager as any).persistValueWin32).toEqual(PersistenceValue.Enterprise);
74+
});
75+
});
76+
3477
describe("instance methods", () => {
3578
const service = DefaultCredentialManager.SVC_NAME;
3679

packages/imperative/src/settings/__tests__/AppSettings.unit.test.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,73 @@ describe("AppSettings", () => {
169169
});
170170
});
171171

172+
describe("set", () => {
173+
beforeEach(() => {
174+
jest.spyOn(JSONSettingsFilePersistence.prototype, "read").mockReturnValueOnce({
175+
overrides: {
176+
CredentialManager: "@zowe/cli"
177+
}
178+
});
179+
AppSettings.initialize("file", defaultSettings);
180+
});
181+
182+
it("sets a property in credentialManagerOptions with the given key/value pair", () => {
183+
expect(() => AppSettings.instance.set("credentialManagerOptions", "persist", PersistenceLevel.LocalMachine)).not.toThrow();
184+
});
185+
186+
it("throws an error if the given namespace does not exist", () => {
187+
expect(() => AppSettings.instance.set("nonexistent" as any, "blah", 123)).toThrow("Namespace nonexistent does not exist");
188+
});
189+
});
190+
191+
describe("get", () => {
192+
beforeEach(() => {
193+
jest.spyOn(JSONSettingsFilePersistence.prototype, "read").mockReturnValueOnce({
194+
overrides: {
195+
CredentialManager: false,
196+
},
197+
credentialManagerOptions: {}
198+
});
199+
AppSettings.initialize("file", defaultSettings);
200+
});
201+
202+
it("throws an error if the given namespace does not exist", () => {
203+
expect(() => AppSettings.instance.get("asdfghjkl" as any, "nonexistent_key")).toThrow("Namespace asdfghjkl does not exist");
204+
});
205+
206+
it("returns a property in credentialManagerOptions namespace when options are present", () => {
207+
AppSettings.instance.set("credentialManagerOptions", "persist", PersistenceLevel.LocalMachine);
208+
expect(AppSettings.instance.get("credentialManagerOptions", "persist")).not.toBeUndefined();
209+
expect(AppSettings.instance.get("credentialManagerOptions", "persist")).toStrictEqual(PersistenceLevel.LocalMachine);
210+
});
211+
212+
it("returns undefined for a credentialManagerOptions property when no options are present", () => {
213+
expect(AppSettings.instance.get("credentialManagerOptions", "persist")).toBeUndefined();
214+
});
215+
});
216+
217+
describe("getNamespace", () => {
218+
beforeEach(() => {
219+
jest.spyOn(JSONSettingsFilePersistence.prototype, "read").mockReturnValueOnce({
220+
overrides: {
221+
CredentialManager: false
222+
}
223+
});
224+
AppSettings.initialize("file", defaultSettings);
225+
});
226+
227+
it("returns the object for overrides namespace", () => {
228+
expect(AppSettings.instance.getNamespace("overrides")).not.toBeUndefined();
229+
expect(AppSettings.instance.getNamespace("overrides")).toStrictEqual(defaultSettings.overrides);
230+
});
231+
232+
it("returns the namespace for credentialManagerOptions when options are present", () => {
233+
AppSettings.instance.set("credentialManagerOptions", "persist", PersistenceLevel.LocalMachine);
234+
expect(AppSettings.instance.getNamespace("credentialManagerOptions")).not.toBeUndefined();
235+
expect(AppSettings.instance.getNamespace("credentialManagerOptions")).toStrictEqual({ persist: PersistenceLevel.LocalMachine });
236+
});
237+
});
238+
172239
describe("writing settings", () => {
173240
/**
174241
* Takes an app settings object and mocks the {@link IAppSettingsAllMethods#writeSettingsFile} method

0 commit comments

Comments
 (0)