Skip to content

Commit 6a519bc

Browse files
committed
added tests for PlatformAuthDOMHandler
1 parent 63e27f6 commit 6a519bc

File tree

3 files changed

+537
-39
lines changed

3 files changed

+537
-39
lines changed

lib/msal-browser/src/broker/nativeBroker/PlatformAuthDOMHandler.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export class PlatformAuthDOMHandler implements IPlatformAuthHandler {
103103

104104
try {
105105
const platformDOMRequest: PlatformDOMTokenRequest =
106-
await this.initializePlatformDOMRequest(request);
106+
this.initializePlatformDOMRequest(request);
107107
const response: object =
108108
// @ts-ignore
109109
await window.navigator.platformAuthentication.executeGetToken(
@@ -118,9 +118,9 @@ export class PlatformAuthDOMHandler implements IPlatformAuthHandler {
118118
}
119119
}
120120

121-
private async initializePlatformDOMRequest(
121+
private initializePlatformDOMRequest(
122122
request: PlatformBrokerRequest
123-
): Promise<PlatformDOMTokenRequest> {
123+
): PlatformDOMTokenRequest {
124124
this.logger.trace(
125125
"NativeInteractionClient: initializeNativeDOMRequest called"
126126
);
@@ -180,7 +180,11 @@ export class PlatformAuthDOMHandler implements IPlatformAuthHandler {
180180
);
181181
} else if (response.hasOwnProperty("error")) {
182182
const errorResponse = response as PlatformDOMTokenResponse;
183-
if (errorResponse.isSuccess === false) {
183+
if (
184+
errorResponse.isSuccess === false &&
185+
errorResponse.error &&
186+
errorResponse.error.code
187+
) {
184188
this.logger.trace(
185189
"PlatformDOMHandler: platform broker returned error response"
186190
);
@@ -216,7 +220,7 @@ export class PlatformAuthDOMHandler implements IPlatformAuthHandler {
216220
scope: response.scopes,
217221
state: response.state || "",
218222
properties: response.properties || {},
219-
extendedLifetimeToken: response.extendedLifetimeToken,
223+
extendedLifetimeToken: response.extendedLifetimeToken ?? false,
220224
shr: response.proofOfPossessionPayload,
221225
};
222226

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

+70-34
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ import {
118118
} from "msal-test-utils";
119119
import { INTERACTION_TYPE } from "../../src/utils/BrowserConstants.js";
120120
import { BaseOperatingContext } from "../../src/operatingcontext/BaseOperatingContext.js";
121+
import { PlatformAuthDOMHandler } from "../../src/broker/nativeBroker/PlatformAuthDOMHandler.js";
122+
import { config } from "process";
121123

122124
const cacheConfig = {
123125
temporaryCacheLocation: BrowserCacheLocation.SessionStorage,
@@ -138,7 +140,7 @@ let testAppConfig = {
138140
},
139141
};
140142

141-
function stubProvider(config: Configuration) {
143+
function stubExtensionProvider(config: Configuration) {
142144
const browserEnvironment = typeof window !== "undefined";
143145

144146
const newConfig = buildConfiguration(config, browserEnvironment);
@@ -148,7 +150,6 @@ function stubProvider(config: Configuration) {
148150
"unittest"
149151
);
150152
const performanceClient = newConfig.telemetry.client;
151-
152153
return jest
153154
.spyOn(PlatformAuthExtensionHandler, "createProvider")
154155
.mockImplementation(async () => {
@@ -161,6 +162,28 @@ function stubProvider(config: Configuration) {
161162
});
162163
}
163164

165+
function stubDOMProvider(config: Configuration) {
166+
const browserEnvironment = typeof window !== "undefined";
167+
168+
const newConfig = buildConfiguration(config, browserEnvironment);
169+
const logger = new Logger(
170+
newConfig.system.loggerOptions,
171+
"unittest",
172+
"unittest"
173+
);
174+
const performanceClient = newConfig.telemetry.client;
175+
console.log("stubDOMProvider");
176+
return jest
177+
.spyOn(PlatformAuthDOMHandler, "createProvider")
178+
.mockImplementation(async () => {
179+
return new PlatformAuthDOMHandler(
180+
logger,
181+
performanceClient,
182+
"test-correlation-id"
183+
);
184+
});
185+
}
186+
164187
const testRequest: CommonAuthorizationUrlRequest = {
165188
redirectUri: `${TEST_URIS.DEFAULT_INSTANCE}/`,
166189
scopes: TEST_CONFIG.DEFAULT_SCOPES,
@@ -451,7 +474,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
451474
}
452475
});
453476

454-
it("creates extension provider if allowPlatformBroker is true", async () => {
477+
it("creates platform auth extension handler if allowPlatformBroker is true", async () => {
455478
const config = {
456479
auth: {
457480
clientId: TEST_CONFIG.MSAL_CLIENT_ID,
@@ -467,42 +490,55 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
467490
).mockImplementation(() => {
468491
return false;
469492
});
493+
494+
const getPlatformAuthProviderSpy = jest.spyOn(
495+
PlatformAuthProvider,
496+
"getPlatformAuthProvider"
497+
);
498+
470499
pca = new PublicClientApplication(config);
471500

472-
const createProviderSpy = stubProvider(config);
501+
const createExtensionProviderSpy = stubExtensionProvider(config);
473502

474503
await pca.initialize();
475504

476505
// Implementation of PCA was moved to controller.
477506
// eslint-disable-next-line @typescript-eslint/no-explicit-any
478507
pca = (pca as any).controller;
479508

480-
expect(createProviderSpy).toHaveBeenCalled();
509+
expect(getPlatformAuthProviderSpy).toHaveBeenCalled();
510+
expect(createExtensionProviderSpy).toHaveBeenCalled();
481511
// @ts-ignore
482512
expect(pca.platformAuthProvider).toBeInstanceOf(
483513
PlatformAuthExtensionHandler
484514
);
485515
});
486516

487517
it("does not create extension provider if allowPlatformBroker is false", async () => {
488-
const createProviderSpy = jest.spyOn(
489-
PlatformAuthExtensionHandler,
490-
"createProvider"
518+
const getPlatformAuthProviderSpy = jest.spyOn(
519+
PlatformAuthProvider,
520+
"getPlatformAuthProvider"
491521
);
492-
pca = new PublicClientApplication({
522+
523+
const config = {
493524
auth: {
494525
clientId: TEST_CONFIG.MSAL_CLIENT_ID,
495526
},
496527
system: {
497528
allowPlatformBroker: false,
498529
},
499-
});
530+
};
531+
532+
pca = new PublicClientApplication(config);
533+
const createProviderSpy = stubExtensionProvider(config);
534+
500535
await pca.initialize();
501536

502537
//Implementation of PCA was moved to controller.
503538
pca = (pca as any).controller;
504539

505540
expect(createProviderSpy).toHaveBeenCalledTimes(0);
541+
expect(getPlatformAuthProviderSpy).not.toHaveBeenCalled();
506542
// @ts-ignore
507543
expect(pca.platformAuthProvider).toBeUndefined();
508544
});
@@ -777,7 +813,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
777813
};
778814
pca = new PublicClientApplication(config);
779815

780-
stubProvider(config);
816+
stubExtensionProvider(config);
781817
await pca.initialize();
782818

783819
// Implementation of PCA was moved to controller.
@@ -871,7 +907,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
871907
},
872908
};
873909
pca = new PublicClientApplication(config);
874-
stubProvider(config);
910+
stubExtensionProvider(config);
875911

876912
pca.initialize().then(() => {
877913
const callbackId = pca.addPerformanceCallback((events) => {
@@ -970,7 +1006,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
9701006
};
9711007
pca = new PublicClientApplication(config);
9721008

973-
stubProvider(config);
1009+
stubExtensionProvider(config);
9741010

9751011
//@ts-ignore
9761012
pca.controller.browserStorage.setInteractionInProgress(true);
@@ -1571,7 +1607,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
15711607
};
15721608
pca = new PublicClientApplication(config);
15731609

1574-
stubProvider(config);
1610+
stubExtensionProvider(config);
15751611
await pca.initialize();
15761612

15771613
//Implementation of PCA was moved to controller.
@@ -1626,7 +1662,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
16261662
};
16271663
pca = new PublicClientApplication(config);
16281664

1629-
stubProvider(config);
1665+
stubExtensionProvider(config);
16301666

16311667
const callbackId = pca.addPerformanceCallback((events) => {
16321668
expect(events.length).toBeGreaterThanOrEqual(1);
@@ -1665,7 +1701,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
16651701
pca = new PublicClientApplication(config);
16661702

16671703
await pca.initialize();
1668-
stubProvider(config);
1704+
stubExtensionProvider(config);
16691705

16701706
//Implementation of PCA was moved to controller.
16711707
pca = (pca as any).controller;
@@ -1707,7 +1743,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
17071743
};
17081744
pca = new PublicClientApplication(config);
17091745

1710-
stubProvider(config);
1746+
stubExtensionProvider(config);
17111747
await pca.initialize();
17121748

17131749
// Implementation of PCA was moved to controller.
@@ -1754,7 +1790,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
17541790
};
17551791
pca = new PublicClientApplication(config);
17561792

1757-
stubProvider(config);
1793+
stubExtensionProvider(config);
17581794
await pca.initialize();
17591795

17601796
// Implementation of PCA was moved to controller.
@@ -1803,7 +1839,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
18031839
};
18041840
pca = new PublicClientApplication(config);
18051841

1806-
stubProvider(config);
1842+
stubExtensionProvider(config);
18071843
await pca.initialize();
18081844

18091845
//PCA implementation moved to controller
@@ -2107,7 +2143,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
21072143
},
21082144
};
21092145

2110-
stubProvider(config);
2146+
stubExtensionProvider(config);
21112147
pca = new PublicClientApplication({
21122148
...config,
21132149
telemetry: {
@@ -2519,7 +2555,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
25192555
};
25202556
pca = new PublicClientApplication(config);
25212557

2522-
stubProvider(config);
2558+
stubExtensionProvider(config);
25232559
await pca.initialize();
25242560

25252561
//Implementation of PCA was moved to controller.
@@ -2582,7 +2618,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
25822618
};
25832619
pca = new PublicClientApplication(config);
25842620

2585-
stubProvider(config);
2621+
stubExtensionProvider(config);
25862622
await pca.initialize();
25872623

25882624
const testAccount: AccountInfo = {
@@ -2637,7 +2673,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
26372673
};
26382674
pca = new PublicClientApplication(config);
26392675

2640-
stubProvider(config);
2676+
stubExtensionProvider(config);
26412677
await pca.initialize();
26422678

26432679
//Implementation of PCA was moved to controller.
@@ -2695,7 +2731,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
26952731
};
26962732
pca = new PublicClientApplication(config);
26972733

2698-
stubProvider(config);
2734+
stubExtensionProvider(config);
26992735
await pca.initialize();
27002736

27012737
//Implementation of PCA was moved to controller.
@@ -2755,7 +2791,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
27552791
};
27562792
pca = new PublicClientApplication(config);
27572793

2758-
stubProvider(config);
2794+
stubExtensionProvider(config);
27592795
await pca.initialize();
27602796

27612797
//PCA implementation moved to controller
@@ -3308,7 +3344,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
33083344
};
33093345
pca = new PublicClientApplication(config);
33103346

3311-
stubProvider(config);
3347+
stubExtensionProvider(config);
33123348
await pca.initialize();
33133349

33143350
//Implementation of PCA was moved to controller.
@@ -3365,7 +3401,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
33653401
};
33663402
pca = new PublicClientApplication(config);
33673403

3368-
stubProvider(config);
3404+
stubExtensionProvider(config);
33693405
await pca.initialize();
33703406

33713407
//Implementation of PCA was moved to controller.
@@ -3423,7 +3459,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
34233459
};
34243460
pca = new PublicClientApplication(config);
34253461

3426-
stubProvider(config);
3462+
stubExtensionProvider(config);
34273463
await pca.initialize();
34283464

34293465
//Implementation of PCA was moved to controller.
@@ -3714,7 +3750,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
37143750
};
37153751
pca = new PublicClientApplication(config);
37163752

3717-
stubProvider(config);
3753+
stubExtensionProvider(config);
37183754
await pca.initialize();
37193755

37203756
//Implementation of PCA was moved to controller.
@@ -3766,7 +3802,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
37663802
};
37673803
pca = new PublicClientApplication(config);
37683804

3769-
stubProvider(config);
3805+
stubExtensionProvider(config);
37703806
await pca.initialize();
37713807

37723808
//Implementation of PCA was moved to controller.
@@ -4211,7 +4247,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
42114247
};
42124248
pca = new PublicClientApplication(config);
42134249

4214-
stubProvider(config);
4250+
stubExtensionProvider(config);
42154251
await pca.initialize();
42164252

42174253
//Implementation of PCA was moved to controller.
@@ -4268,7 +4304,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
42684304
};
42694305
pca = new PublicClientApplication(config);
42704306

4271-
stubProvider(config);
4307+
stubExtensionProvider(config);
42724308
await pca.initialize();
42734309

42744310
//Implementation of PCA was moved to controller.
@@ -4328,7 +4364,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
43284364
};
43294365
pca = new PublicClientApplication(config);
43304366

4331-
stubProvider(config);
4367+
stubExtensionProvider(config);
43324368
await pca.initialize();
43334369
//Implementation of PCA was moved to controller.
43344370
pca = (pca as any).controller;
@@ -7311,7 +7347,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
73117347
};
73127348
pca = new PublicClientApplication(config);
73137349

7314-
stubProvider(config);
7350+
stubExtensionProvider(config);
73157351
await pca.initialize();
73167352

73177353
//Implementation of PCA was moved to controller.

0 commit comments

Comments
 (0)