-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added coverage and tests for msal-browser package
- Loading branch information
1 parent
e828446
commit e0b4773
Showing
8 changed files
with
960 additions
and
3 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import { expect } from "chai"; | ||
import { Configuration, buildConfiguration } from "../../src/app/Configuration"; | ||
import { TEST_CONFIG, TEST_URIS } from "../utils/StringConstants"; | ||
|
||
/** | ||
* Defaults for the Configuration Options | ||
*/ | ||
const FRAME_TIMEOUT = 6000; | ||
const OFFSET = 300; | ||
const NAVIGATE_FRAME_WAIT = 500; | ||
|
||
/** | ||
* Test values for the Configuration Options | ||
*/ | ||
const TEST_FRAME_TIMEOUT = 3000; | ||
const TEST_OFFSET = 100; | ||
const TEST_NAVIGATE_FRAME_WAIT = 200; | ||
|
||
describe("MsalPublicClientSPAConfiguration.ts Class Unit Tests", () => { | ||
|
||
it("buildConfiguration assigns default values", () => { | ||
let emptyConfig: Configuration = buildConfiguration({auth: null}); | ||
// Auth config checks | ||
expect(emptyConfig.auth).to.be.not.null; | ||
expect(emptyConfig.auth.clientId).to.be.empty; | ||
expect(emptyConfig.auth.clientSecret).to.be.empty; | ||
expect(emptyConfig.auth.authority).to.be.null; | ||
expect(emptyConfig.auth.validateAuthority).to.be.true; | ||
let redirUriResult: string; | ||
if (emptyConfig.auth.redirectUri instanceof Function) { | ||
redirUriResult = emptyConfig.auth.redirectUri(); | ||
} else { | ||
redirUriResult = emptyConfig.auth.redirectUri; | ||
} | ||
let postLogoutRediUriResult: string; | ||
if (emptyConfig.auth.postLogoutRedirectUri instanceof Function) { | ||
postLogoutRediUriResult = emptyConfig.auth.postLogoutRedirectUri(); | ||
} else { | ||
postLogoutRediUriResult = emptyConfig.auth.postLogoutRedirectUri; | ||
} | ||
expect(redirUriResult).to.be.eq(TEST_URIS.TEST_REDIR_URI); | ||
expect(postLogoutRediUriResult).to.be.eq(TEST_URIS.TEST_REDIR_URI); | ||
expect(emptyConfig.auth.navigateToLoginRequestUrl).to.be.true; | ||
// Cache config checks | ||
expect(emptyConfig.cache).to.be.not.null; | ||
expect(emptyConfig.cache.cacheLocation).to.be.not.null; | ||
expect(emptyConfig.cache.cacheLocation).to.be.eq("sessionStorage"); | ||
expect(emptyConfig.cache.storeAuthStateInCookie).to.be.not.null; | ||
expect(emptyConfig.cache.storeAuthStateInCookie).to.be.false; | ||
// System config checks | ||
expect(emptyConfig.system).to.be.not.null; | ||
expect(emptyConfig.system.loadFrameTimeout).to.be.not.null; | ||
expect(emptyConfig.system.loadFrameTimeout).to.be.eq(FRAME_TIMEOUT); | ||
expect(emptyConfig.system.tokenRenewalOffsetSeconds).to.be.not.null; | ||
expect(emptyConfig.system.tokenRenewalOffsetSeconds).to.be.eq(OFFSET); | ||
expect(emptyConfig.system.navigateFrameWait).to.be.not.null; | ||
expect(emptyConfig.system.navigateFrameWait).to.be.eq(NAVIGATE_FRAME_WAIT); | ||
expect(emptyConfig.system.telemetry).to.be.undefined; | ||
// Framework config checks | ||
expect(emptyConfig.framework).to.be.not.null; | ||
expect(emptyConfig.framework.isAngular).to.be.not.null; | ||
expect(emptyConfig.framework.isAngular).to.be.false; | ||
expect(emptyConfig.framework.unprotectedResources).to.be.not.null; | ||
expect(emptyConfig.framework.unprotectedResources).to.be.empty; | ||
expect(emptyConfig.framework.protectedResourceMap).to.be.not.null; | ||
expect(emptyConfig.framework.protectedResourceMap).to.be.empty; | ||
}); | ||
|
||
const testAppName = "MSAL.js App"; | ||
const testAppVersion = "v1.0.0"; | ||
const testUnprotectedResources = ["resource1"]; | ||
let testProtectedResourceMap = new Map<string, Array<string>>(); | ||
testProtectedResourceMap.set("testResource1", ["resourceUri1"]); | ||
it("buildConfiguration correctly assigns new values", () => { | ||
let newConfig: Configuration = buildConfiguration({ | ||
auth: { | ||
clientId: TEST_CONFIG.MSAL_CLIENT_ID, | ||
clientSecret: TEST_CONFIG.MSAL_CLIENT_SECRET, | ||
authority: TEST_CONFIG.validAuthority, | ||
validateAuthority: false, | ||
redirectUri: TEST_URIS.TEST_ALTERNATE_REDIR_URI, | ||
postLogoutRedirectUri: TEST_URIS.TEST_LOGOUT_URI, | ||
navigateToLoginRequestUrl: false | ||
}, | ||
cache: { | ||
cacheLocation: "localStorage", | ||
storeAuthStateInCookie: true | ||
}, | ||
system: { | ||
loadFrameTimeout: TEST_FRAME_TIMEOUT, | ||
navigateFrameWait: TEST_NAVIGATE_FRAME_WAIT, | ||
tokenRenewalOffsetSeconds: TEST_OFFSET, | ||
telemetry: { | ||
applicationName: testAppName, | ||
applicationVersion: testAppVersion | ||
} | ||
}, | ||
framework: { | ||
isAngular: true, | ||
unprotectedResources: testUnprotectedResources, | ||
protectedResourceMap: testProtectedResourceMap | ||
} | ||
}); | ||
// Auth config checks | ||
expect(newConfig.auth).to.be.not.null; | ||
expect(newConfig.auth.clientId).to.be.eq(TEST_CONFIG.MSAL_CLIENT_ID); | ||
expect(newConfig.auth.clientSecret).to.be.eq(TEST_CONFIG.MSAL_CLIENT_SECRET); | ||
expect(newConfig.auth.authority).to.be.eq(TEST_CONFIG.validAuthority); | ||
expect(newConfig.auth.validateAuthority).to.be.false; | ||
expect(newConfig.auth.redirectUri).to.be.eq(TEST_URIS.TEST_ALTERNATE_REDIR_URI); | ||
expect(newConfig.auth.postLogoutRedirectUri).to.be.eq(TEST_URIS.TEST_LOGOUT_URI); | ||
expect(newConfig.auth.navigateToLoginRequestUrl).to.be.false; | ||
// Cache config checks | ||
expect(newConfig.cache).to.be.not.null; | ||
expect(newConfig.cache.cacheLocation).to.be.not.null; | ||
expect(newConfig.cache.cacheLocation).to.be.eq("localStorage"); | ||
expect(newConfig.cache.storeAuthStateInCookie).to.be.not.null; | ||
expect(newConfig.cache.storeAuthStateInCookie).to.be.true; | ||
// System config checks | ||
expect(newConfig.system).to.be.not.null; | ||
expect(newConfig.system.loadFrameTimeout).to.be.not.null; | ||
expect(newConfig.system.loadFrameTimeout).to.be.eq(TEST_FRAME_TIMEOUT); | ||
expect(newConfig.system.tokenRenewalOffsetSeconds).to.be.not.null; | ||
expect(newConfig.system.tokenRenewalOffsetSeconds).to.be.eq(TEST_OFFSET); | ||
expect(newConfig.system.navigateFrameWait).to.be.not.null; | ||
expect(newConfig.system.navigateFrameWait).to.be.eq(TEST_NAVIGATE_FRAME_WAIT); | ||
expect(newConfig.system.telemetry).to.be.not.null; | ||
expect(newConfig.system.telemetry.applicationName).to.be.eq(testAppName); | ||
expect(newConfig.system.telemetry.applicationVersion).to.be.eq(testAppVersion); | ||
// Framework config checks | ||
expect(newConfig.framework).to.be.not.null; | ||
expect(newConfig.framework.isAngular).to.be.not.null; | ||
expect(newConfig.framework.isAngular).to.be.true; | ||
expect(newConfig.framework.unprotectedResources).to.be.not.null; | ||
expect(newConfig.framework.unprotectedResources).to.be.eq(testUnprotectedResources); | ||
expect(newConfig.framework.protectedResourceMap).to.be.not.null; | ||
expect(newConfig.framework.protectedResourceMap).to.be.eq(testProtectedResourceMap); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,93 @@ | ||
import * as Mocha from "mocha"; | ||
import sinon from "sinon"; | ||
import { expect } from "chai"; | ||
import { PublicClientApplication, authCallback } from "../../src/app/PublicClientApplication"; | ||
import { TEST_CONFIG, TEST_URIS } from "../utils/StringConstants"; | ||
import { AuthError, AuthResponse } from "msal-common"; | ||
|
||
describe("PublicClientApplication.ts Class Unit Tests", () => { | ||
let pca = new PublicClientApplication({ | ||
auth: { | ||
clientId: TEST_CONFIG.MSAL_CLIENT_ID, | ||
clientSecret: TEST_CONFIG.MSAL_CLIENT_SECRET | ||
} | ||
}); | ||
|
||
const authCallback: authCallback = (authErr: AuthError, response: AuthResponse) => { | ||
if (authErr) { | ||
console.error(authErr); | ||
} else if (response) { | ||
console.log(response); | ||
} else { | ||
console.log("This shouldn't print, check the test"); | ||
} | ||
}; | ||
|
||
describe("Constructor tests", () => { | ||
|
||
it("passes null check", () => { | ||
expect(null).to.be.null; | ||
expect(pca).to.be.not.null; | ||
expect(pca instanceof PublicClientApplication).to.be.true; | ||
}); | ||
}); | ||
|
||
describe("Redirect Flow Unit tests", () => { | ||
|
||
it("handleRedirectCallback throws not implemented error", () => { | ||
expect(() => pca.handleRedirectCallback(authCallback)).to.throw("Method not implemented."); | ||
expect(() => pca.handleRedirectCallback(authCallback)).to.throw(Error); | ||
}); | ||
|
||
it("loginRedirect throws throws not implemented error", () => { | ||
expect(() => pca.loginRedirect({})).to.throw("Method not implemented."); | ||
expect(() => pca.loginRedirect({})).to.throw(Error); | ||
}); | ||
|
||
it("acquireTokenRedirect throws throws not implemented error", () => { | ||
expect(() => pca.acquireTokenRedirect({})).to.throw("Method not implemented."); | ||
expect(() => pca.acquireTokenRedirect({})).to.throw(Error); | ||
}); | ||
}); | ||
|
||
describe("Popup Flow Unit tests", () => { | ||
it("loginPopup throws throws not implemented error", () => { | ||
expect(() => pca.loginPopup({})).to.throw("Method not implemented."); | ||
expect(() => pca.loginPopup({})).to.throw(Error); | ||
}); | ||
|
||
it("acquireTokenPopup throws throws not implemented error", () => { | ||
expect(() => pca.acquireTokenPopup({})).to.throw("Method not implemented."); | ||
expect(() => pca.acquireTokenPopup({})).to.throw(Error); | ||
}); | ||
}); | ||
|
||
describe("Acquire Token Silent (Iframe) Tests", () => { | ||
|
||
it("acquireTokenSilent throws throws not implemented error", () => { | ||
expect(() => pca.acquireTokenSilent({})).to.throw("Method not implemented."); | ||
expect(() => pca.acquireTokenSilent({})).to.throw(Error); | ||
}); | ||
}); | ||
|
||
describe("Getters and Setters Unit Tests", () => { | ||
|
||
let pca_alternate_redirUris = new PublicClientApplication({ | ||
auth: { | ||
clientId: TEST_CONFIG.MSAL_CLIENT_ID, | ||
clientSecret: TEST_CONFIG.MSAL_CLIENT_SECRET, | ||
redirectUri: TEST_URIS.TEST_ALTERNATE_REDIR_URI, | ||
postLogoutRedirectUri: TEST_URIS.TEST_LOGOUT_URI | ||
} | ||
}); | ||
|
||
it("getRedirectUri returns the currently configured redirect uri", () => { | ||
expect(pca.getRedirectUri()).to.be.eq(TEST_URIS.TEST_REDIR_URI); | ||
expect(pca_alternate_redirUris.getRedirectUri()).to.be.eq(TEST_URIS.TEST_ALTERNATE_REDIR_URI); | ||
}); | ||
|
||
it("getPostLogoutRedirectUri returns the currently configured post logout redirect uri", () => { | ||
expect(pca.getPostLogoutRedirectUri()).to.be.eq(TEST_URIS.TEST_REDIR_URI); | ||
expect(pca_alternate_redirUris.getPostLogoutRedirectUri()).to.be.eq(TEST_URIS.TEST_LOGOUT_URI); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
require("@babel/register")({ extensions: ['.js', '.jsx', '.ts', '.tsx'] }); | ||
require("jsdom-global")("", { | ||
url: "https://localhost:8081/index.html" | ||
}); | ||
require("msal-common"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { expect } from "chai"; | ||
import { BrowserUtils } from "../../src/utils/BrowserUtils" | ||
import { TEST_URIS } from "./StringConstants"; | ||
|
||
describe("BrowserUtils.ts Function Unit Tests", () => { | ||
|
||
it("getDefaultRedirectUri returns current location uri of browser", () => { | ||
expect(BrowserUtils.getDefaultRedirectUri()).to.be.eq(TEST_URIS.TEST_REDIR_URI); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* This file contains the string constants used by the test classes. | ||
*/ | ||
// Test URIs | ||
export const TEST_URIS = { | ||
DEFAULT_INSTANCE: "https://login.microsoftonline.com/", | ||
ALTERNATE_INSTANCE: "https://login.windows.net/", | ||
TEST_REDIR_URI: "https://localhost:8081/index.html", | ||
TEST_ALTERNATE_REDIR_URI: "https://localhost:8081/index2.html", | ||
TEST_LOGOUT_URI: "https://localhost:8081/logout.html", | ||
TEST_AUTH_ENDPT: "https://login.microsoftonline.com/common/oauth2/v2.0/authorize" | ||
}; | ||
|
||
// Test MSAL config params | ||
export const TEST_CONFIG = { | ||
TENANT: "common", | ||
MSAL_CLIENT_ID: "0813e1d1-ad72-46a9-8665-399bba48c201", | ||
MSAL_CLIENT_SECRET: "ThisIsASecret", | ||
MSAL_TENANT_ID: "3338040d-6c67-4c5b-b112-36a304b66dad", | ||
validAuthority: TEST_URIS.DEFAULT_INSTANCE + "common", | ||
alternateValidAuthority: TEST_URIS.ALTERNATE_INSTANCE + "common", | ||
applicationName: "msal.js-tests", | ||
applicationVersion: "msal.js-tests.1.0.fake", | ||
STATE: "1234" | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters