Skip to content

Commit d18088c

Browse files
authored
Merge pull request #1449 from AzureAD/sagonzal/msal-node-tests
[msal-node][msal-common] unit tests for msal-node changes
2 parents eb60610 + d923b93 commit d18088c

37 files changed

+1138
-284
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright (c) Microsoft Corporation. All rights reserved.
33
* Licensed under the MIT License.
44
*/
5-
import { Account, PublicClientSPA, AuthenticationParameters, INetworkModule, TokenResponse, UrlString, TemporaryCacheKeys, TokenRenewParameters, StringUtils } from "@azure/msal-common";
5+
import { Account, SPAClient, AuthenticationParameters, INetworkModule, TokenResponse, UrlString, TemporaryCacheKeys, TokenRenewParameters, StringUtils } from "@azure/msal-common";
66
import { Configuration, buildConfiguration } from "../config/Configuration";
77
import { BrowserStorage } from "../cache/BrowserStorage";
88
import { CryptoOps } from "../crypto/CryptoOps";
@@ -24,7 +24,7 @@ export class PublicClientApplication {
2424
private config: Configuration;
2525

2626
// auth functions imported from @azure/msal-common module
27-
private authModule: PublicClientSPA;
27+
private authModule: SPAClient;
2828

2929
// callback for error/token response
3030
private authCallback: AuthCallback = null;
@@ -73,7 +73,7 @@ export class PublicClientApplication {
7373
this.browserStorage = new BrowserStorage(this.config.auth.clientId, this.config.cache);
7474

7575
// Create auth module.
76-
this.authModule = new PublicClientSPA({
76+
this.authModule = new SPAClient({
7777
auth: this.config.auth,
7878
systemOptions: {
7979
tokenRenewalOffsetSeconds: this.config.system.tokenRenewalOffsetSeconds,

lib/msal-browser/src/interaction_handler/InteractionHandler.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
* Copyright (c) Microsoft Corporation. All rights reserved.
33
* Licensed under the MIT License.
44
*/
5-
import { PublicClientSPA, TokenResponse } from "@azure/msal-common";
5+
import { SPAClient, TokenResponse } from "@azure/msal-common";
66
import { BrowserStorage } from "../cache/BrowserStorage";
77

88
/**
99
* Abstract class which defines operations for a browser interaction handling class.
1010
*/
1111
export abstract class InteractionHandler {
1212

13-
protected authModule: PublicClientSPA;
13+
protected authModule: SPAClient;
1414
protected browserStorage: BrowserStorage;
1515

16-
constructor(authCodeModule: PublicClientSPA, storageImpl: BrowserStorage) {
16+
constructor(authCodeModule: SPAClient, storageImpl: BrowserStorage) {
1717
this.authModule = authCodeModule;
1818
this.browserStorage = storageImpl;
1919
}

lib/msal-browser/src/interaction_handler/PopupHandler.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright (c) Microsoft Corporation. All rights reserved.
33
* Licensed under the MIT License.
44
*/
5-
import { UrlString, StringUtils, Constants, TokenResponse, PublicClientSPA } from "@azure/msal-common";
5+
import { UrlString, StringUtils, Constants, TokenResponse, SPAClient } from "@azure/msal-common";
66
import { InteractionHandler } from "./InteractionHandler";
77
import { BrowserAuthError } from "../error/BrowserAuthError";
88
import { BrowserConstants } from "../utils/BrowserConstants";
@@ -16,7 +16,7 @@ export class PopupHandler extends InteractionHandler {
1616

1717
private currentWindow: Window;
1818

19-
constructor(authCodeModule: PublicClientSPA, storageImpl: BrowserStorage) {
19+
constructor(authCodeModule: SPAClient, storageImpl: BrowserStorage) {
2020
super(authCodeModule, storageImpl);
2121

2222
// Properly sets this reference for the unload event.

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

+30-26
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
Account,
3030
TokenExchangeParameters,
3131
IdTokenClaims,
32-
PublicClientSPA
32+
SPAClient
3333
} from "@azure/msal-common";
3434

3535
import { AuthCallback } from "../../src/types/AuthCallback";
@@ -133,13 +133,15 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
133133
};
134134
window.sessionStorage.setItem(`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.REQUEST_PARAMS}`, b64Encode.encode(JSON.stringify(testTokenReq)));
135135
const testServerTokenResponse = {
136-
token_type: TEST_CONFIG.TOKEN_TYPE_BEARER,
137-
scope: TEST_CONFIG.DEFAULT_SCOPES.join(" "),
138-
expires_in: TEST_TOKEN_LIFETIMES.DEFAULT_EXPIRES_IN,
139-
ext_expires_in: TEST_TOKEN_LIFETIMES.DEFAULT_EXPIRES_IN,
140-
access_token: TEST_TOKENS.ACCESS_TOKEN,
141-
refresh_token: TEST_TOKENS.REFRESH_TOKEN,
142-
id_token: TEST_TOKENS.IDTOKEN_V2
136+
body : {
137+
token_type: TEST_CONFIG.TOKEN_TYPE_BEARER,
138+
scope: TEST_CONFIG.DEFAULT_SCOPES.join(" "),
139+
expires_in: TEST_TOKEN_LIFETIMES.DEFAULT_EXPIRES_IN,
140+
ext_expires_in: TEST_TOKEN_LIFETIMES.DEFAULT_EXPIRES_IN,
141+
access_token: TEST_TOKENS.ACCESS_TOKEN,
142+
refresh_token: TEST_TOKENS.REFRESH_TOKEN,
143+
id_token: TEST_TOKENS.IDTOKEN_V2
144+
}
143145
};
144146
const testIdTokenClaims: IdTokenClaims = {
145147
"ver": "2.0",
@@ -232,13 +234,15 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
232234
};
233235
window.sessionStorage.setItem(`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.REQUEST_PARAMS}`, b64Encode.encode(JSON.stringify(testTokenReq)));
234236
const testServerTokenResponse = {
235-
token_type: TEST_CONFIG.TOKEN_TYPE_BEARER,
236-
scope: TEST_CONFIG.DEFAULT_SCOPES.join(" "),
237-
expires_in: TEST_TOKEN_LIFETIMES.DEFAULT_EXPIRES_IN,
238-
ext_expires_in: TEST_TOKEN_LIFETIMES.DEFAULT_EXPIRES_IN,
239-
access_token: TEST_TOKENS.ACCESS_TOKEN,
240-
refresh_token: TEST_TOKENS.REFRESH_TOKEN,
241-
id_token: TEST_TOKENS.IDTOKEN_V2
237+
body : {
238+
token_type: TEST_CONFIG.TOKEN_TYPE_BEARER,
239+
scope: TEST_CONFIG.DEFAULT_SCOPES.join(" "),
240+
expires_in: TEST_TOKEN_LIFETIMES.DEFAULT_EXPIRES_IN,
241+
ext_expires_in: TEST_TOKEN_LIFETIMES.DEFAULT_EXPIRES_IN,
242+
access_token: TEST_TOKENS.ACCESS_TOKEN,
243+
refresh_token: TEST_TOKENS.REFRESH_TOKEN,
244+
id_token: TEST_TOKENS.IDTOKEN_V2
245+
}
242246
};
243247
const testIdTokenClaims: IdTokenClaims = {
244248
"ver": "2.0",
@@ -317,7 +321,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
317321
});
318322

319323
it("loginRedirect navigates to created login url", async () => {
320-
sinon.stub(PublicClientSPA.prototype, "createLoginUrl").resolves(testNavUrl);
324+
sinon.stub(SPAClient.prototype, "createLoginUrl").resolves(testNavUrl);
321325
sinon.stub(RedirectHandler.prototype, "showUI").callsFake((navigateUrl): Window => {
322326
expect(navigateUrl).to.be.eq(testNavUrl);
323327
return window;
@@ -338,7 +342,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
338342
window.sessionStorage.setItem(`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.ORIGIN_URI}`, TEST_URIS.TEST_REDIR_URI);
339343
window.sessionStorage.setItem(`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.REQUEST_STATE}`, RANDOM_TEST_GUID);
340344
window.sessionStorage.setItem(`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.NONCE_IDTOKEN}${Constants.RESOURCE_DELIM}${RANDOM_TEST_GUID}`, "123523");
341-
sinon.stub(PublicClientSPA.prototype, "createLoginUrl").throws(testError);
345+
sinon.stub(SPAClient.prototype, "createLoginUrl").throws(testError);
342346
try {
343347
pca.loginRedirect({});
344348
} catch (e) {
@@ -365,7 +369,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
365369
});
366370

367371
it("acquireTokenRedirect navigates to created login url", async () => {
368-
sinon.stub(PublicClientSPA.prototype, "createAcquireTokenUrl").resolves(testNavUrl);
372+
sinon.stub(SPAClient.prototype, "createAcquireTokenUrl").resolves(testNavUrl);
369373
sinon.stub(RedirectHandler.prototype, "showUI").callsFake((navigateUrl): Window => {
370374
expect(navigateUrl).to.be.eq(testNavUrl);
371375
return window;
@@ -387,7 +391,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
387391
window.sessionStorage.setItem(`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.ORIGIN_URI}`, TEST_URIS.TEST_REDIR_URI);
388392
window.sessionStorage.setItem(`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.REQUEST_STATE}`, RANDOM_TEST_GUID);
389393
window.sessionStorage.setItem(`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.NONCE_IDTOKEN}${Constants.RESOURCE_DELIM}${RANDOM_TEST_GUID}`, "123523");
390-
sinon.stub(PublicClientSPA.prototype, "createAcquireTokenUrl").throws(testError);
394+
sinon.stub(SPAClient.prototype, "createAcquireTokenUrl").throws(testError);
391395
try {
392396
pca.acquireTokenRedirect({
393397
scopes: TEST_CONFIG.DEFAULT_SCOPES
@@ -444,7 +448,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
444448
account: testAccount,
445449
userRequestState: ""
446450
};
447-
sinon.stub(PublicClientSPA.prototype, "createLoginUrl").resolves(testNavUrl);
451+
sinon.stub(SPAClient.prototype, "createLoginUrl").resolves(testNavUrl);
448452
sinon.stub(PopupHandler.prototype, "showUI").callsFake((requestUrl: string): Window => {
449453
expect(requestUrl).to.be.eq(testNavUrl);
450454
return window;
@@ -460,7 +464,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
460464
window.sessionStorage.setItem(`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.ORIGIN_URI}`, TEST_URIS.TEST_REDIR_URI);
461465
window.sessionStorage.setItem(`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.REQUEST_STATE}`, RANDOM_TEST_GUID);
462466
window.sessionStorage.setItem(`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.NONCE_IDTOKEN}${Constants.RESOURCE_DELIM}${RANDOM_TEST_GUID}`, "123523");
463-
sinon.stub(PublicClientSPA.prototype, "createLoginUrl").resolves(testNavUrl);
467+
sinon.stub(SPAClient.prototype, "createLoginUrl").resolves(testNavUrl);
464468
sinon.stub(PopupHandler.prototype, "showUI").throws(testError);
465469
try {
466470
const tokenResp = await pca.loginPopup({});
@@ -513,7 +517,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
513517
account: testAccount,
514518
userRequestState: ""
515519
};
516-
sinon.stub(PublicClientSPA.prototype, "createAcquireTokenUrl").resolves(testNavUrl);
520+
sinon.stub(SPAClient.prototype, "createAcquireTokenUrl").resolves(testNavUrl);
517521
sinon.stub(PopupHandler.prototype, "showUI").callsFake((requestUrl: string): Window => {
518522
expect(requestUrl).to.be.eq(testNavUrl);
519523
return window;
@@ -531,7 +535,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
531535
window.sessionStorage.setItem(`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.ORIGIN_URI}`, TEST_URIS.TEST_REDIR_URI);
532536
window.sessionStorage.setItem(`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.REQUEST_STATE}`, RANDOM_TEST_GUID);
533537
window.sessionStorage.setItem(`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.NONCE_IDTOKEN}${Constants.RESOURCE_DELIM}${RANDOM_TEST_GUID}`, "123523");
534-
sinon.stub(PublicClientSPA.prototype, "createAcquireTokenUrl").resolves(testNavUrl);
538+
sinon.stub(SPAClient.prototype, "createAcquireTokenUrl").resolves(testNavUrl);
535539
sinon.stub(PopupHandler.prototype, "showUI").throws(testError);
536540
try {
537541
const tokenResp = await pca.acquireTokenPopup({
@@ -581,7 +585,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
581585
account: testAccount,
582586
userRequestState: ""
583587
};
584-
sinon.stub(PublicClientSPA.prototype, "renewToken").resolves(testTokenResponse);
588+
sinon.stub(SPAClient.prototype, "renewToken").resolves(testTokenResponse);
585589
const tokenResp = await pca.acquireTokenSilent({
586590
scopes: TEST_CONFIG.DEFAULT_SCOPES
587591
});
@@ -590,7 +594,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
590594

591595
it("throws error that renewToken throws", async () => {
592596
const testError = "Error in creating a login url";
593-
sinon.stub(PublicClientSPA.prototype, "renewToken").throws(testError);
597+
sinon.stub(SPAClient.prototype, "renewToken").throws(testError);
594598
try {
595599
const tokenResp = await pca.acquireTokenSilent({
596600
scopes: TEST_CONFIG.DEFAULT_SCOPES
@@ -605,7 +609,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
605609
describe("logout", () => {
606610

607611
it("passes logoutUri from authModule to window nav util", () => {
608-
sinon.stub(PublicClientSPA.prototype, "logout").resolves(testLogoutUrl);
612+
sinon.stub(SPAClient.prototype, "logout").resolves(testLogoutUrl);
609613
sinon.stub(BrowserUtils, "navigateWindow").callsFake((urlNavigate: string, noHistory: boolean) => {
610614
expect(urlNavigate).to.be.eq(testLogoutUrl);
611615
console.log(noHistory);

lib/msal-browser/test/interaction_handler/InteractionHandler.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { expect } from "chai";
22
import { InteractionHandler } from "../../src/interaction_handler/InteractionHandler";
3-
import { PublicClientSPA, PkceCodes, NetworkRequestOptions, LogLevel } from "@azure/msal-common";
3+
import { SPAClient, PkceCodes, NetworkRequestOptions, LogLevel } from "@azure/msal-common";
44
import { Configuration, buildConfiguration } from "../../src/config/Configuration";
55
import { TEST_CONFIG } from "../utils/StringConstants";
66
import { BrowserStorage } from "../../src/cache/BrowserStorage";
77

88
class TestInteractionHandler extends InteractionHandler {
99

10-
constructor(authCodeModule: PublicClientSPA, storageImpl: BrowserStorage) {
10+
constructor(authCodeModule: SPAClient, storageImpl: BrowserStorage) {
1111
super(authCodeModule, storageImpl);
1212
}
1313

@@ -52,7 +52,7 @@ describe("InteractionHandler.ts Unit Tests", () => {
5252
}
5353
};
5454
const configObj = buildConfiguration(appConfig);
55-
const authCodeModule = new PublicClientSPA({
55+
const authCodeModule = new SPAClient({
5656
auth: configObj.auth,
5757
systemOptions: {
5858
tokenRenewalOffsetSeconds: configObj.system.tokenRenewalOffsetSeconds,

lib/msal-browser/test/interaction_handler/PopupHandler.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import chai from "chai";
22
import chaiAsPromised from "chai-as-promised"
33
chai.use(chaiAsPromised);
44
const expect = chai.expect;
5-
import { PkceCodes, PublicClientSPA, NetworkRequestOptions, LogLevel, Account, TokenResponse, CodeResponse } from "@azure/msal-common";
5+
import { PkceCodes, SPAClient, NetworkRequestOptions, LogLevel, Account, TokenResponse, CodeResponse } from "@azure/msal-common";
66
import { PopupHandler } from "../../src/interaction_handler/PopupHandler";
77
import { BrowserStorage } from "../../src/cache/BrowserStorage";
88
import { Configuration, buildConfiguration } from "../../src/config/Configuration";
@@ -48,7 +48,7 @@ describe("PopupHandler.ts Unit Tests", () => {
4848
}
4949
};
5050
const configObj = buildConfiguration(appConfig);
51-
const authCodeModule = new PublicClientSPA({
51+
const authCodeModule = new SPAClient({
5252
auth: configObj.auth,
5353
systemOptions: {
5454
tokenRenewalOffsetSeconds: configObj.system.tokenRenewalOffsetSeconds,
@@ -181,8 +181,8 @@ describe("PopupHandler.ts Unit Tests", () => {
181181
uniqueId: idTokenClaims.oid,
182182
userRequestState: "testState"
183183
};
184-
sinon.stub(PublicClientSPA.prototype, "handleFragmentResponse").returns(testCodeResponse);
185-
sinon.stub(PublicClientSPA.prototype, "acquireToken").resolves(testTokenResponse);
184+
sinon.stub(SPAClient.prototype, "handleFragmentResponse").returns(testCodeResponse);
185+
sinon.stub(SPAClient.prototype, "acquireToken").resolves(testTokenResponse);
186186

187187
const tokenResponse = await popupHandler.handleCodeResponse(TEST_HASHES.TEST_SUCCESS_CODE_HASH);
188188
expect(tokenResponse).to.deep.eq(testTokenResponse);

lib/msal-browser/test/interaction_handler/RedirectHandler.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import chaiAsPromised from "chai-as-promised"
33
chai.use(chaiAsPromised);
44
const expect = chai.expect;
55
import { Configuration, buildConfiguration } from "../../src/config/Configuration";
6-
import { PublicClientSPA, PkceCodes, NetworkRequestOptions, LogLevel, TemporaryCacheKeys, CodeResponse, TokenResponse, Account } from "@azure/msal-common";
6+
import { SPAClient, PkceCodes, NetworkRequestOptions, LogLevel, TemporaryCacheKeys, CodeResponse, TokenResponse, Account } from "@azure/msal-common";
77
import { TEST_CONFIG, TEST_URIS, TEST_TOKENS, TEST_DATA_CLIENT_INFO, RANDOM_TEST_GUID, TEST_HASHES, TEST_TOKEN_LIFETIMES } from "../utils/StringConstants";
88
import { BrowserStorage } from "../../src/cache/BrowserStorage";
99
import { RedirectHandler } from "../../src/interaction_handler/RedirectHandler";
@@ -47,7 +47,7 @@ describe("RedirectHandler.ts Unit Tests", () => {
4747
}
4848
};
4949
const configObj = buildConfiguration(appConfig);
50-
const authCodeModule = new PublicClientSPA({
50+
const authCodeModule = new SPAClient({
5151
auth: configObj.auth,
5252
systemOptions: {
5353
tokenRenewalOffsetSeconds: configObj.system.tokenRenewalOffsetSeconds,
@@ -184,8 +184,8 @@ describe("RedirectHandler.ts Unit Tests", () => {
184184
};
185185
browserStorage.setItem(BrowserConstants.INTERACTION_STATUS_KEY, BrowserConstants.INTERACTION_IN_PROGRESS_VALUE);
186186
browserStorage.setItem(TemporaryCacheKeys.URL_HASH, TEST_HASHES.TEST_SUCCESS_CODE_HASH);
187-
sinon.stub(PublicClientSPA.prototype, "handleFragmentResponse").returns(testCodeResponse);
188-
sinon.stub(PublicClientSPA.prototype, "acquireToken").resolves(testTokenResponse);
187+
sinon.stub(SPAClient.prototype, "handleFragmentResponse").returns(testCodeResponse);
188+
sinon.stub(SPAClient.prototype, "acquireToken").resolves(testTokenResponse);
189189

190190
const tokenResponse = await redirectHandler.handleCodeResponse(TEST_HASHES.TEST_SUCCESS_CODE_HASH);
191191
expect(tokenResponse).to.deep.eq(testTokenResponse);

0 commit comments

Comments
 (0)