Skip to content

Commit 78a21ac

Browse files
Add PCAFactory and correlation id optional params to createNestablePublicClientApplication
1 parent 0d498d4 commit 78a21ac

File tree

3 files changed

+98
-37
lines changed

3 files changed

+98
-37
lines changed

lib/msal-browser/apiReview/msal-browser.api.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,11 +489,13 @@ export type Configuration = {
489489
// @public
490490
function createGuid(): string;
491491

492+
// Warning: (tsdoc-param-tag-missing-hyphen) The @param block should be followed by a parameter name and then a hyphen
493+
// Warning: (tsdoc-param-tag-missing-hyphen) The @param block should be followed by a parameter name and then a hyphen
492494
// Warning: (tsdoc-param-tag-missing-hyphen) The @param block should be followed by a parameter name and then a hyphen
493495
// Warning: (ae-missing-release-tag) "createNestablePublicClientApplication" is part of the package's API, but it is missing a release tag (@alpha, @beta, @public, or @internal)
494496
//
495497
// @public
496-
export function createNestablePublicClientApplication(configuration: Configuration): Promise<IPublicClientApplication>;
498+
export function createNestablePublicClientApplication(configuration: Configuration, correlationId?: string, pcaFactory?: (configuration: Configuration, controller: IController) => IPublicClientApplication): Promise<IPublicClientApplication>;
497499

498500
// Warning: (tsdoc-param-tag-missing-hyphen) The @param block should be followed by a parameter name and then a hyphen
499501
// Warning: (ae-missing-release-tag) "createStandardPublicClientApplication" is part of the package's API, but it is missing a release tag (@alpha, @beta, @public, or @internal)

lib/msal-browser/src/app/PublicClientApplication.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -381,18 +381,20 @@ export class PublicClientApplication implements IPublicClientApplication {
381381
export async function createNestablePublicClientApplication(
382382
configuration: Configuration,
383383
correlationId?: string,
384-
pcaFactory?: (configuration: Configuration, controller: IController) => IPublicClientApplication,
384+
pcaFactory?: (
385+
configuration: Configuration,
386+
controller: IController
387+
) => IPublicClientApplication
385388
): Promise<IPublicClientApplication> {
386389
const cid = correlationId || createNewGuid();
387390
const nestedAppAuth = new NestedAppOperatingContext(configuration);
388391
await nestedAppAuth.initialize(cid);
389392

390393
if (nestedAppAuth.isAvailable()) {
391394
const controller = new NestedAppAuthController(nestedAppAuth);
392-
const nestablePCA = pcaFactory ? pcaFactory(configuration, controller) : new PublicClientApplication(
393-
configuration,
394-
controller
395-
);
395+
const nestablePCA = pcaFactory
396+
? pcaFactory(configuration, controller)
397+
: new PublicClientApplication(configuration, controller);
396398
await nestablePCA.initialize({ correlationId: cid });
397399
return nestablePCA;
398400
}

lib/msal-browser/test/app/createNestablePublicClientApplication.spec.ts

Lines changed: 88 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ import {
88
createNestablePublicClientApplication,
99
createStandardPublicClientApplication,
1010
} from "../../src/app/PublicClientApplication.js";
11-
import {
12-
TEST_CONFIG,
13-
RANDOM_TEST_GUID,
14-
} from "../utils/StringConstants.js";
11+
import { TEST_CONFIG, RANDOM_TEST_GUID } from "../utils/StringConstants.js";
1512
import { NestedAppOperatingContext } from "../../src/operatingcontext/NestedAppOperatingContext.js";
1613
import { NestedAppAuthController } from "../../src/controllers/NestedAppAuthController.js";
1714
import { Configuration, IPublicClientApplication } from "../../src/index.js";
@@ -52,17 +49,33 @@ describe("createNestablePublicClientApplication tests", () => {
5249
mockNestedAppAuthController = {} as any;
5350

5451
// Mock constructors
55-
(NestedAppOperatingContext as jest.MockedClass<typeof NestedAppOperatingContext>).mockImplementation(() => mockNestedAppOperatingContext);
56-
(NestedAppAuthController as jest.MockedClass<typeof NestedAppAuthController>).mockImplementation(() => mockNestedAppAuthController);
52+
(
53+
NestedAppOperatingContext as jest.MockedClass<
54+
typeof NestedAppOperatingContext
55+
>
56+
).mockImplementation(() => mockNestedAppOperatingContext);
57+
(
58+
NestedAppAuthController as jest.MockedClass<
59+
typeof NestedAppAuthController
60+
>
61+
).mockImplementation(() => mockNestedAppAuthController);
5762

5863
// Mock PublicClientApplication
59-
jest.spyOn(PublicClientApplication.prototype, "initialize").mockResolvedValue(undefined);
64+
jest.spyOn(
65+
PublicClientApplication.prototype,
66+
"initialize"
67+
).mockResolvedValue(undefined);
6068

6169
// Mock createNewGuid
62-
createNewGuidSpy = jest.spyOn(BrowserCrypto, "createNewGuid").mockReturnValue(RANDOM_TEST_GUID);
70+
createNewGuidSpy = jest
71+
.spyOn(BrowserCrypto, "createNewGuid")
72+
.mockReturnValue(RANDOM_TEST_GUID);
6373

6474
// Get the mocked function
65-
createStandardSpy = createStandardPublicClientApplication as jest.MockedFunction<typeof createStandardPublicClientApplication>;
75+
createStandardSpy =
76+
createStandardPublicClientApplication as jest.MockedFunction<
77+
typeof createStandardPublicClientApplication
78+
>;
6679
});
6780

6881
afterEach(() => {
@@ -76,39 +89,58 @@ describe("createNestablePublicClientApplication tests", () => {
7689

7790
describe("Without pcaFactory parameter", () => {
7891
it("should create a nestable PCA using default PublicClientApplication constructor", async () => {
79-
const result = await createNestablePublicClientApplication(testConfig);
92+
const result = await createNestablePublicClientApplication(
93+
testConfig
94+
);
8095

8196
// Verify NestedAppOperatingContext was created with config
82-
expect(NestedAppOperatingContext).toHaveBeenCalledWith(testConfig);
97+
expect(NestedAppOperatingContext).toHaveBeenCalledWith(
98+
testConfig
99+
);
83100

84101
// Verify initialize was called with generated correlation ID
85-
expect(mockNestedAppOperatingContext.initialize).toHaveBeenCalledWith(RANDOM_TEST_GUID);
102+
expect(
103+
mockNestedAppOperatingContext.initialize
104+
).toHaveBeenCalledWith(RANDOM_TEST_GUID);
86105

87106
// Verify isAvailable was called
88-
expect(mockNestedAppOperatingContext.isAvailable).toHaveBeenCalled();
107+
expect(
108+
mockNestedAppOperatingContext.isAvailable
109+
).toHaveBeenCalled();
89110

90111
// Verify NestedAppAuthController was created with the operating context
91-
expect(NestedAppAuthController).toHaveBeenCalledWith(mockNestedAppOperatingContext);
112+
expect(NestedAppAuthController).toHaveBeenCalledWith(
113+
mockNestedAppOperatingContext
114+
);
92115

93116
// Verify the result is a PublicClientApplication instance
94117
expect(result).toBeInstanceOf(PublicClientApplication);
95118

96119
// Verify initialize was called on the PCA instance
97-
expect(result.initialize).toHaveBeenCalledWith({ correlationId: RANDOM_TEST_GUID });
120+
expect(result.initialize).toHaveBeenCalledWith({
121+
correlationId: RANDOM_TEST_GUID,
122+
});
98123
});
99124

100125
it("should use provided correlation ID when specified", async () => {
101126
// Create a fresh spy for this test to avoid interference
102127
const freshGuidSpy = jest.spyOn(BrowserCrypto, "createNewGuid");
103128
const customCorrelationId = "custom-correlation-id";
104129

105-
const result = await createNestablePublicClientApplication(testConfig, customCorrelationId);
130+
const result = await createNestablePublicClientApplication(
131+
testConfig,
132+
customCorrelationId
133+
);
106134

107135
// Verify initialize was called with custom correlation ID
108-
expect(mockNestedAppOperatingContext.initialize).toHaveBeenCalledWith(customCorrelationId);
136+
expect(
137+
mockNestedAppOperatingContext.initialize
138+
).toHaveBeenCalledWith(customCorrelationId);
109139

110140
// Verify initialize was called on PCA with custom correlation ID
111-
expect(result.initialize).toHaveBeenCalledWith({ correlationId: customCorrelationId });
141+
expect(result.initialize).toHaveBeenCalledWith({
142+
correlationId: customCorrelationId,
143+
});
112144

113145
// Verify createNewGuid was not called since correlation ID was provided
114146
expect(freshGuidSpy).not.toHaveBeenCalled();
@@ -123,7 +155,9 @@ describe("createNestablePublicClientApplication tests", () => {
123155
expect(createNewGuidSpy).toHaveBeenCalled();
124156

125157
// Verify initialize was called with generated ID
126-
expect(mockNestedAppOperatingContext.initialize).toHaveBeenCalledWith(RANDOM_TEST_GUID);
158+
expect(
159+
mockNestedAppOperatingContext.initialize
160+
).toHaveBeenCalledWith(RANDOM_TEST_GUID);
127161
});
128162
});
129163

@@ -142,13 +176,18 @@ describe("createNestablePublicClientApplication tests", () => {
142176
);
143177

144178
// Verify pcaFactory was called with config and controller
145-
expect(mockPcaFactory).toHaveBeenCalledWith(testConfig, mockNestedAppAuthController);
179+
expect(mockPcaFactory).toHaveBeenCalledWith(
180+
testConfig,
181+
mockNestedAppAuthController
182+
);
146183

147184
// Verify the returned PCA is the one from factory
148185
expect(result).toBe(mockPCA);
149186

150187
// Verify initialize was called on the factory-created PCA
151-
expect(mockPCA.initialize).toHaveBeenCalledWith({ correlationId: RANDOM_TEST_GUID });
188+
expect(mockPCA.initialize).toHaveBeenCalledWith({
189+
correlationId: RANDOM_TEST_GUID,
190+
});
152191
});
153192

154193
it("should use pcaFactory with custom correlation ID", async () => {
@@ -168,10 +207,15 @@ describe("createNestablePublicClientApplication tests", () => {
168207
);
169208

170209
// Verify pcaFactory was called with config and controller
171-
expect(mockPcaFactory).toHaveBeenCalledWith(testConfig, mockNestedAppAuthController);
210+
expect(mockPcaFactory).toHaveBeenCalledWith(
211+
testConfig,
212+
mockNestedAppAuthController
213+
);
172214

173215
// Verify initialize was called with custom correlation ID
174-
expect(mockPCA.initialize).toHaveBeenCalledWith({ correlationId: customCorrelationId });
216+
expect(mockPCA.initialize).toHaveBeenCalledWith({
217+
correlationId: customCorrelationId,
218+
});
175219

176220
// Verify createNewGuid was not called
177221
expect(freshGuidSpy).not.toHaveBeenCalled();
@@ -181,7 +225,10 @@ describe("createNestablePublicClientApplication tests", () => {
181225

182226
it("should handle pcaFactory returning different PCA implementation", async () => {
183227
class CustomPCA implements IPublicClientApplication {
184-
constructor(public config: Configuration, public controller: IController) {}
228+
constructor(
229+
public config: Configuration,
230+
public controller: IController
231+
) {}
185232
initialize = jest.fn().mockResolvedValue(undefined);
186233
// Add other required methods with minimal implementations
187234
acquireTokenPopup = jest.fn();
@@ -211,9 +258,11 @@ describe("createNestablePublicClientApplication tests", () => {
211258
clearCache = jest.fn();
212259
}
213260

214-
const mockPcaFactory = jest.fn().mockImplementation((config, controller) => {
215-
return new CustomPCA(config, controller);
216-
});
261+
const mockPcaFactory = jest
262+
.fn()
263+
.mockImplementation((config, controller) => {
264+
return new CustomPCA(config, controller);
265+
});
217266

218267
const result = await createNestablePublicClientApplication(
219268
testConfig,
@@ -223,7 +272,9 @@ describe("createNestablePublicClientApplication tests", () => {
223272

224273
// Verify result is instance of CustomPCA
225274
expect(result).toBeInstanceOf(CustomPCA);
226-
expect(result.initialize).toHaveBeenCalledWith({ correlationId: RANDOM_TEST_GUID });
275+
expect(result.initialize).toHaveBeenCalledWith({
276+
correlationId: RANDOM_TEST_GUID,
277+
});
227278
});
228279
});
229280
});
@@ -237,11 +288,17 @@ describe("createNestablePublicClientApplication tests", () => {
237288
const mockStandardPCA = { type: "standard" } as any;
238289
createStandardSpy.mockResolvedValue(mockStandardPCA);
239290

240-
const result = await createNestablePublicClientApplication(testConfig);
291+
const result = await createNestablePublicClientApplication(
292+
testConfig
293+
);
241294

242295
// Should have initialized NestedAppOperatingContext first
243-
expect(mockNestedAppOperatingContext.initialize).toHaveBeenCalledWith(RANDOM_TEST_GUID);
244-
expect(mockNestedAppOperatingContext.isAvailable).toHaveBeenCalled();
296+
expect(
297+
mockNestedAppOperatingContext.initialize
298+
).toHaveBeenCalledWith(RANDOM_TEST_GUID);
299+
expect(
300+
mockNestedAppOperatingContext.isAvailable
301+
).toHaveBeenCalled();
245302

246303
// Should return a result (whether mocked or real)
247304
expect(result).toBeDefined();

0 commit comments

Comments
 (0)