Skip to content

[msal-node][msal-common] unit tests for msal-node changes #1449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 9, 2020
6 changes: 3 additions & 3 deletions lib/msal-browser/src/client/PublicClientApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { Account, PublicClientSPA, AuthenticationParameters, INetworkModule, TokenResponse, UrlString, TemporaryCacheKeys, TokenRenewParameters, StringUtils } from "@azure/msal-common";
import { Account, SPAClient, AuthenticationParameters, INetworkModule, TokenResponse, UrlString, TemporaryCacheKeys, TokenRenewParameters, StringUtils } from "@azure/msal-common";
import { Configuration, buildConfiguration } from "../config/Configuration";
import { BrowserStorage } from "../cache/BrowserStorage";
import { CryptoOps } from "../crypto/CryptoOps";
Expand All @@ -24,7 +24,7 @@ export class PublicClientApplication {
private config: Configuration;

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

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

// Create auth module.
this.authModule = new PublicClientSPA({
this.authModule = new SPAClient({
auth: this.config.auth,
systemOptions: {
tokenRenewalOffsetSeconds: this.config.system.tokenRenewalOffsetSeconds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { PublicClientSPA, TokenResponse } from "@azure/msal-common";
import { SPAClient, TokenResponse } from "@azure/msal-common";
import { BrowserStorage } from "../cache/BrowserStorage";

/**
* Abstract class which defines operations for a browser interaction handling class.
*/
export abstract class InteractionHandler {

protected authModule: PublicClientSPA;
protected authModule: SPAClient;
protected browserStorage: BrowserStorage;

constructor(authCodeModule: PublicClientSPA, storageImpl: BrowserStorage) {
constructor(authCodeModule: SPAClient, storageImpl: BrowserStorage) {
this.authModule = authCodeModule;
this.browserStorage = storageImpl;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/msal-browser/src/interaction_handler/PopupHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { UrlString, StringUtils, Constants, TokenResponse, PublicClientSPA } from "@azure/msal-common";
import { UrlString, StringUtils, Constants, TokenResponse, SPAClient } from "@azure/msal-common";
import { InteractionHandler } from "./InteractionHandler";
import { BrowserAuthError } from "../error/BrowserAuthError";
import { BrowserConstants } from "../utils/BrowserConstants";
Expand All @@ -16,7 +16,7 @@ export class PopupHandler extends InteractionHandler {

private currentWindow: Window;

constructor(authCodeModule: PublicClientSPA, storageImpl: BrowserStorage) {
constructor(authCodeModule: SPAClient, storageImpl: BrowserStorage) {
super(authCodeModule, storageImpl);

// Properly sets this reference for the unload event.
Expand Down
56 changes: 30 additions & 26 deletions lib/msal-browser/test/client/PublicClientApplication.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
Account,
TokenExchangeParameters,
IdTokenClaims,
PublicClientSPA
SPAClient
} from "@azure/msal-common";

import { AuthCallback } from "../../src/types/AuthCallback";
Expand Down Expand Up @@ -133,13 +133,15 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
};
window.sessionStorage.setItem(`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.REQUEST_PARAMS}`, b64Encode.encode(JSON.stringify(testTokenReq)));
const testServerTokenResponse = {
token_type: TEST_CONFIG.TOKEN_TYPE_BEARER,
scope: TEST_CONFIG.DEFAULT_SCOPES.join(" "),
expires_in: TEST_TOKEN_LIFETIMES.DEFAULT_EXPIRES_IN,
ext_expires_in: TEST_TOKEN_LIFETIMES.DEFAULT_EXPIRES_IN,
access_token: TEST_TOKENS.ACCESS_TOKEN,
refresh_token: TEST_TOKENS.REFRESH_TOKEN,
id_token: TEST_TOKENS.IDTOKEN_V2
body : {
token_type: TEST_CONFIG.TOKEN_TYPE_BEARER,
scope: TEST_CONFIG.DEFAULT_SCOPES.join(" "),
expires_in: TEST_TOKEN_LIFETIMES.DEFAULT_EXPIRES_IN,
ext_expires_in: TEST_TOKEN_LIFETIMES.DEFAULT_EXPIRES_IN,
access_token: TEST_TOKENS.ACCESS_TOKEN,
refresh_token: TEST_TOKENS.REFRESH_TOKEN,
id_token: TEST_TOKENS.IDTOKEN_V2
}
};
const testIdTokenClaims: IdTokenClaims = {
"ver": "2.0",
Expand Down Expand Up @@ -232,13 +234,15 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
};
window.sessionStorage.setItem(`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.REQUEST_PARAMS}`, b64Encode.encode(JSON.stringify(testTokenReq)));
const testServerTokenResponse = {
token_type: TEST_CONFIG.TOKEN_TYPE_BEARER,
scope: TEST_CONFIG.DEFAULT_SCOPES.join(" "),
expires_in: TEST_TOKEN_LIFETIMES.DEFAULT_EXPIRES_IN,
ext_expires_in: TEST_TOKEN_LIFETIMES.DEFAULT_EXPIRES_IN,
access_token: TEST_TOKENS.ACCESS_TOKEN,
refresh_token: TEST_TOKENS.REFRESH_TOKEN,
id_token: TEST_TOKENS.IDTOKEN_V2
body : {
token_type: TEST_CONFIG.TOKEN_TYPE_BEARER,
scope: TEST_CONFIG.DEFAULT_SCOPES.join(" "),
expires_in: TEST_TOKEN_LIFETIMES.DEFAULT_EXPIRES_IN,
ext_expires_in: TEST_TOKEN_LIFETIMES.DEFAULT_EXPIRES_IN,
access_token: TEST_TOKENS.ACCESS_TOKEN,
refresh_token: TEST_TOKENS.REFRESH_TOKEN,
id_token: TEST_TOKENS.IDTOKEN_V2
}
};
const testIdTokenClaims: IdTokenClaims = {
"ver": "2.0",
Expand Down Expand Up @@ -317,7 +321,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
});

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

it("acquireTokenRedirect navigates to created login url", async () => {
sinon.stub(PublicClientSPA.prototype, "createAcquireTokenUrl").resolves(testNavUrl);
sinon.stub(SPAClient.prototype, "createAcquireTokenUrl").resolves(testNavUrl);
sinon.stub(RedirectHandler.prototype, "showUI").callsFake((navigateUrl): Window => {
expect(navigateUrl).to.be.eq(testNavUrl);
return window;
Expand All @@ -387,7 +391,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
window.sessionStorage.setItem(`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.ORIGIN_URI}`, TEST_URIS.TEST_REDIR_URI);
window.sessionStorage.setItem(`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.REQUEST_STATE}`, RANDOM_TEST_GUID);
window.sessionStorage.setItem(`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.NONCE_IDTOKEN}${Constants.RESOURCE_DELIM}${RANDOM_TEST_GUID}`, "123523");
sinon.stub(PublicClientSPA.prototype, "createAcquireTokenUrl").throws(testError);
sinon.stub(SPAClient.prototype, "createAcquireTokenUrl").throws(testError);
try {
pca.acquireTokenRedirect({
scopes: TEST_CONFIG.DEFAULT_SCOPES
Expand Down Expand Up @@ -444,7 +448,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
account: testAccount,
userRequestState: ""
};
sinon.stub(PublicClientSPA.prototype, "createLoginUrl").resolves(testNavUrl);
sinon.stub(SPAClient.prototype, "createLoginUrl").resolves(testNavUrl);
sinon.stub(PopupHandler.prototype, "showUI").callsFake((requestUrl: string): Window => {
expect(requestUrl).to.be.eq(testNavUrl);
return window;
Expand All @@ -460,7 +464,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
window.sessionStorage.setItem(`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.ORIGIN_URI}`, TEST_URIS.TEST_REDIR_URI);
window.sessionStorage.setItem(`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.REQUEST_STATE}`, RANDOM_TEST_GUID);
window.sessionStorage.setItem(`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.NONCE_IDTOKEN}${Constants.RESOURCE_DELIM}${RANDOM_TEST_GUID}`, "123523");
sinon.stub(PublicClientSPA.prototype, "createLoginUrl").resolves(testNavUrl);
sinon.stub(SPAClient.prototype, "createLoginUrl").resolves(testNavUrl);
sinon.stub(PopupHandler.prototype, "showUI").throws(testError);
try {
const tokenResp = await pca.loginPopup({});
Expand Down Expand Up @@ -513,7 +517,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
account: testAccount,
userRequestState: ""
};
sinon.stub(PublicClientSPA.prototype, "createAcquireTokenUrl").resolves(testNavUrl);
sinon.stub(SPAClient.prototype, "createAcquireTokenUrl").resolves(testNavUrl);
sinon.stub(PopupHandler.prototype, "showUI").callsFake((requestUrl: string): Window => {
expect(requestUrl).to.be.eq(testNavUrl);
return window;
Expand All @@ -531,7 +535,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
window.sessionStorage.setItem(`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.ORIGIN_URI}`, TEST_URIS.TEST_REDIR_URI);
window.sessionStorage.setItem(`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.REQUEST_STATE}`, RANDOM_TEST_GUID);
window.sessionStorage.setItem(`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.NONCE_IDTOKEN}${Constants.RESOURCE_DELIM}${RANDOM_TEST_GUID}`, "123523");
sinon.stub(PublicClientSPA.prototype, "createAcquireTokenUrl").resolves(testNavUrl);
sinon.stub(SPAClient.prototype, "createAcquireTokenUrl").resolves(testNavUrl);
sinon.stub(PopupHandler.prototype, "showUI").throws(testError);
try {
const tokenResp = await pca.acquireTokenPopup({
Expand Down Expand Up @@ -581,7 +585,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
account: testAccount,
userRequestState: ""
};
sinon.stub(PublicClientSPA.prototype, "renewToken").resolves(testTokenResponse);
sinon.stub(SPAClient.prototype, "renewToken").resolves(testTokenResponse);
const tokenResp = await pca.acquireTokenSilent({
scopes: TEST_CONFIG.DEFAULT_SCOPES
});
Expand All @@ -590,7 +594,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {

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

it("passes logoutUri from authModule to window nav util", () => {
sinon.stub(PublicClientSPA.prototype, "logout").resolves(testLogoutUrl);
sinon.stub(SPAClient.prototype, "logout").resolves(testLogoutUrl);
sinon.stub(BrowserUtils, "navigateWindow").callsFake((urlNavigate: string, noHistory: boolean) => {
expect(urlNavigate).to.be.eq(testLogoutUrl);
console.log(noHistory);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { expect } from "chai";
import { InteractionHandler } from "../../src/interaction_handler/InteractionHandler";
import { PublicClientSPA, PkceCodes, NetworkRequestOptions, LogLevel } from "@azure/msal-common";
import { SPAClient, PkceCodes, NetworkRequestOptions, LogLevel } from "@azure/msal-common";
import { Configuration, buildConfiguration } from "../../src/config/Configuration";
import { TEST_CONFIG } from "../utils/StringConstants";
import { BrowserStorage } from "../../src/cache/BrowserStorage";

class TestInteractionHandler extends InteractionHandler {

constructor(authCodeModule: PublicClientSPA, storageImpl: BrowserStorage) {
constructor(authCodeModule: SPAClient, storageImpl: BrowserStorage) {
super(authCodeModule, storageImpl);
}

Expand Down Expand Up @@ -52,7 +52,7 @@ describe("InteractionHandler.ts Unit Tests", () => {
}
};
const configObj = buildConfiguration(appConfig);
const authCodeModule = new PublicClientSPA({
const authCodeModule = new SPAClient({
auth: configObj.auth,
systemOptions: {
tokenRenewalOffsetSeconds: configObj.system.tokenRenewalOffsetSeconds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import chai from "chai";
import chaiAsPromised from "chai-as-promised"
chai.use(chaiAsPromised);
const expect = chai.expect;
import { PkceCodes, PublicClientSPA, NetworkRequestOptions, LogLevel, Account, TokenResponse, CodeResponse } from "@azure/msal-common";
import { PkceCodes, SPAClient, NetworkRequestOptions, LogLevel, Account, TokenResponse, CodeResponse } from "@azure/msal-common";
import { PopupHandler } from "../../src/interaction_handler/PopupHandler";
import { BrowserStorage } from "../../src/cache/BrowserStorage";
import { Configuration, buildConfiguration } from "../../src/config/Configuration";
Expand Down Expand Up @@ -48,7 +48,7 @@ describe("PopupHandler.ts Unit Tests", () => {
}
};
const configObj = buildConfiguration(appConfig);
const authCodeModule = new PublicClientSPA({
const authCodeModule = new SPAClient({
auth: configObj.auth,
systemOptions: {
tokenRenewalOffsetSeconds: configObj.system.tokenRenewalOffsetSeconds,
Expand Down Expand Up @@ -181,8 +181,8 @@ describe("PopupHandler.ts Unit Tests", () => {
uniqueId: idTokenClaims.oid,
userRequestState: "testState"
};
sinon.stub(PublicClientSPA.prototype, "handleFragmentResponse").returns(testCodeResponse);
sinon.stub(PublicClientSPA.prototype, "acquireToken").resolves(testTokenResponse);
sinon.stub(SPAClient.prototype, "handleFragmentResponse").returns(testCodeResponse);
sinon.stub(SPAClient.prototype, "acquireToken").resolves(testTokenResponse);

const tokenResponse = await popupHandler.handleCodeResponse(TEST_HASHES.TEST_SUCCESS_CODE_HASH);
expect(tokenResponse).to.deep.eq(testTokenResponse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import chaiAsPromised from "chai-as-promised"
chai.use(chaiAsPromised);
const expect = chai.expect;
import { Configuration, buildConfiguration } from "../../src/config/Configuration";
import { PublicClientSPA, PkceCodes, NetworkRequestOptions, LogLevel, TemporaryCacheKeys, CodeResponse, TokenResponse, Account } from "@azure/msal-common";
import { SPAClient, PkceCodes, NetworkRequestOptions, LogLevel, TemporaryCacheKeys, CodeResponse, TokenResponse, Account } from "@azure/msal-common";
import { TEST_CONFIG, TEST_URIS, TEST_TOKENS, TEST_DATA_CLIENT_INFO, RANDOM_TEST_GUID, TEST_HASHES, TEST_TOKEN_LIFETIMES } from "../utils/StringConstants";
import { BrowserStorage } from "../../src/cache/BrowserStorage";
import { RedirectHandler } from "../../src/interaction_handler/RedirectHandler";
Expand Down Expand Up @@ -47,7 +47,7 @@ describe("RedirectHandler.ts Unit Tests", () => {
}
};
const configObj = buildConfiguration(appConfig);
const authCodeModule = new PublicClientSPA({
const authCodeModule = new SPAClient({
auth: configObj.auth,
systemOptions: {
tokenRenewalOffsetSeconds: configObj.system.tokenRenewalOffsetSeconds,
Expand Down Expand Up @@ -184,8 +184,8 @@ describe("RedirectHandler.ts Unit Tests", () => {
};
browserStorage.setItem(BrowserConstants.INTERACTION_STATUS_KEY, BrowserConstants.INTERACTION_IN_PROGRESS_VALUE);
browserStorage.setItem(TemporaryCacheKeys.URL_HASH, TEST_HASHES.TEST_SUCCESS_CODE_HASH);
sinon.stub(PublicClientSPA.prototype, "handleFragmentResponse").returns(testCodeResponse);
sinon.stub(PublicClientSPA.prototype, "acquireToken").resolves(testTokenResponse);
sinon.stub(SPAClient.prototype, "handleFragmentResponse").returns(testCodeResponse);
sinon.stub(SPAClient.prototype, "acquireToken").resolves(testTokenResponse);

const tokenResponse = await redirectHandler.handleCodeResponse(TEST_HASHES.TEST_SUCCESS_CODE_HASH);
expect(tokenResponse).to.deep.eq(testTokenResponse);
Expand Down
Loading