Skip to content

Commit

Permalink
added coverage and tests for msal-browser package
Browse files Browse the repository at this point in the history
  • Loading branch information
pkanher617 committed Nov 18, 2019
1 parent e828446 commit e0b4773
Show file tree
Hide file tree
Showing 8 changed files with 960 additions and 3 deletions.
694 changes: 694 additions & 0 deletions lib/msal-browser/package-lock.json

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions lib/msal-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
"scripts": {
"clean": "shx rm -rf dist lib",
"lint": "eslint src --ext .ts",
"pretest": "npm link msal-common",
"test": "mocha",
"test:coverage": "nyc mocha",
"test:report": "nyc report | coveralls",
"build:all": "npm run build:common && npm link msal-common && npm run build",
"build:common": "cd ../msal-common && npm run build && npm link",
"build:modules": "rollup -c",
Expand All @@ -62,8 +64,11 @@
"@typescript-eslint/parser": "^2.4.0",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"coveralls": "^3.0.7",
"eslint": "^6.5.1",
"husky": "^3.0.9",
"jsdom": "^15.2.1",
"jsdom-global": "^3.0.2",
"mocha": "^6.2.2",
"nyc": "^14.1.1",
"rollup": "^1.24.0",
Expand Down
139 changes: 139 additions & 0 deletions lib/msal-browser/test/app/Configuration.spec.ts
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);
});
});
82 changes: 81 additions & 1 deletion lib/msal-browser/test/app/PublicClientApplication.spec.ts
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);
});
});
});
4 changes: 4 additions & 0 deletions lib/msal-browser/test/mochaSetup.js
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");
10 changes: 10 additions & 0 deletions lib/msal-browser/test/utils/BrowserUtils.spec.ts
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);
});
});
25 changes: 25 additions & 0 deletions lib/msal-browser/test/utils/StringConstants.ts
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"
};
4 changes: 2 additions & 2 deletions lib/msal-common/test/app/module/AuthModule.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as Mocha from "mocha";
import chai from "chai";
import chaiAsPromised from "chai-as-promised";
const expect = chai.expect;
chai.use(chaiAsPromised);
import { AuthModule } from "../../../src/app/module/AuthModule";
import { MsalConfiguration } from "../../../src/app/config/MsalConfiguration";
import { AuthenticationParameters } from "../../../src/request/AuthenticationParameters";
import { TEST_HASHES } from "../../utils/StringConstants";
const expect = chai.expect;
chai.use(chaiAsPromised);

class TestAuthModule extends AuthModule {

Expand Down

0 comments on commit e0b4773

Please sign in to comment.