Skip to content

Commit

Permalink
Update client-capabilities sample with initialize poller
Browse files Browse the repository at this point in the history
  • Loading branch information
hectormmg committed Jul 19, 2023
1 parent 9933c50 commit 80ba1e4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ let accountId = "";
// configuration parameters are located at authConfig.js
const myMSALObj = new msal.PublicClientApplication(msalConfig);

// Redirect: once login is successful and redirects with tokens, call Graph API
myMSALObj.handleRedirectPromise().then(handleResponse).catch(err => {
console.error(err);
myMSALObj.initialize().then(() => {
setInitializedFlagTrue(); // Used as a flag in the test to ensure that MSAL has been initialized
// Redirect: once login is successful and redirects with tokens, call Graph API
myMSALObj.handleRedirectPromise().then(handleResponse).catch(err => {
console.error(err);
});
});

function setInitializedFlagTrue() {
document.getElementById("pca-initialized").innerHTML = "true";
}

function handleResponse(resp) {
if (resp !== null) {
accountId = resp.account.homeAccountId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ <h5 class="card-title" id="WelcomeMessage">Please sign-in to see your profile an
</div>
<br>
<br>
<div hidden id="pca-initialized">
false
</div>

<!-- importing bootstrap.js and supporting js libraries -->
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import * as puppeteer from "puppeteer";
import {
Screenshot,
createFolder,
setupCredentials,
enterCredentials,
storagePoller,
ONE_SECOND_IN_MS,
getBrowser,
getHomeUrl
Screenshot,
createFolder,
setupCredentials,
enterCredentials,
storagePoller,
ONE_SECOND_IN_MS,
getBrowser,
getHomeUrl,
pcaInitializedPoller,
} from "e2e-test-utils/src/TestUtils";
import { BrowserCacheUtils } from "e2e-test-utils/src/BrowserCacheTestUtils";
import { LabApiQueryParams } from "e2e-test-utils/src/LabApiQueryParams";
Expand All @@ -26,15 +27,20 @@ describe("Browser tests", function () {
createFolder(SCREENSHOT_BASE_FOLDER_NAME);
const labApiParams: LabApiQueryParams = {
azureEnvironment: AzureEnvironments.CLOUD,
appType: AppTypes.CLOUD
appType: AppTypes.CLOUD,
};

browser = await getBrowser();
sampleHomeUrl = getHomeUrl();

const labClient = new LabClient();
const envResponse = await labClient.getVarsByCloudEnvironment(labApiParams);
[username, accountPwd] = await setupCredentials(envResponse[0], labClient);
const envResponse = await labClient.getVarsByCloudEnvironment(
labApiParams
);
[username, accountPwd] = await setupCredentials(
envResponse[0],
labClient
);
});

let context: puppeteer.BrowserContext;
Expand All @@ -43,9 +49,10 @@ describe("Browser tests", function () {
beforeEach(async () => {
context = await browser.createIncognitoBrowserContext();
page = await context.newPage();
page.setDefaultTimeout(ONE_SECOND_IN_MS*5);
page.setDefaultTimeout(ONE_SECOND_IN_MS * 5);
BrowserCache = new BrowserCacheUtils(page, "sessionStorage");
await page.goto(sampleHomeUrl);
await pcaInitializedPoller(page, 5000);
});

afterEach(async () => {
Expand All @@ -59,7 +66,9 @@ describe("Browser tests", function () {

it("Performs loginRedirect, acquires and validates CAE token", async () => {
const testName = "redirectBaseCase";
const screenshot = new Screenshot(`${SCREENSHOT_BASE_FOLDER_NAME}/${testName}`);
const screenshot = new Screenshot(
`${SCREENSHOT_BASE_FOLDER_NAME}/${testName}`
);

// Home Page
await page.waitForSelector("#SignIn");
Expand Down Expand Up @@ -88,20 +97,30 @@ describe("Browser tests", function () {
expect(tokenStore.idTokens).toHaveLength(1);
expect(tokenStore.accessTokens).toHaveLength(1);
expect(tokenStore.refreshTokens).toHaveLength(1);
const cachedAccount = await BrowserCache.getAccountFromCache(tokenStore.idTokens[0]);
const defaultCachedToken = await BrowserCache.accessTokenForScopesExists(tokenStore.accessTokens, ["openid", "profile", "user.read"]);
const cachedAccount = await BrowserCache.getAccountFromCache(
tokenStore.idTokens[0]
);
const defaultCachedToken =
await BrowserCache.accessTokenForScopesExists(
tokenStore.accessTokens,
["openid", "profile", "user.read"]
);
expect(cachedAccount).toBeDefined();
expect(defaultCachedToken).toBeTruthy();

// Check cae token
const accessToken = JSON.parse((await BrowserCache.getWindowStorage())[(tokenStore.accessTokens[0])]).secret;
const accessToken = JSON.parse(
(await BrowserCache.getWindowStorage())[tokenStore.accessTokens[0]]
).secret;
const decodedToken: any = JWT.decode(accessToken);
expect(decodedToken.xms_cc).toEqual([ "CP1" ]);
expect(decodedToken.xms_cc).toEqual(["CP1"]);
});

it("Performs loginRedirect, acquires and validates CAE PoP token", async () => {
const testName = "redirectBaseCase";
const screenshot = new Screenshot(`${SCREENSHOT_BASE_FOLDER_NAME}/${testName}`);
const screenshot = new Screenshot(
`${SCREENSHOT_BASE_FOLDER_NAME}/${testName}`
);

// Home Page
await page.waitForSelector("#SignIn");
Expand Down Expand Up @@ -132,23 +151,31 @@ describe("Browser tests", function () {
await storagePoller(async () => {
const tokenStore = await BrowserCache.getTokens();
expect(tokenStore.accessTokens).toHaveLength(2);
}, ONE_SECOND_IN_MS*5);
}, ONE_SECOND_IN_MS * 5);

const tokenStore = await BrowserCache.getTokens();
const cachedBearerToken = await BrowserCache.accessTokenForScopesExists(tokenStore.accessTokens, ["openid", "profile", "user.read"]);
const cachedBearerToken = await BrowserCache.accessTokenForScopesExists(
tokenStore.accessTokens,
["openid", "profile", "user.read"]
);
expect(cachedBearerToken).toBeTruthy();

const cachedPopToken = await BrowserCache.popAccessTokenForScopesExists(tokenStore.accessTokens, ["openid", "profile", "user.read"]);
const cachedPopToken = await BrowserCache.popAccessTokenForScopesExists(
tokenStore.accessTokens,
["openid", "profile", "user.read"]
);
expect(cachedPopToken).toBeTruthy();

const storage = await BrowserCache.getWindowStorage();
const caePopTokenKey = tokenStore.accessTokens.find(key => key.includes("accesstoken_with_authscheme"));
const caePopTokenKey = tokenStore.accessTokens.find((key) =>
key.includes("accesstoken_with_authscheme")
);
const accessToken = JSON.parse(storage[caePopTokenKey]).secret;

// Check cae pop token
const decodedToken: any = JWT.decode(accessToken);
expect(decodedToken.xms_cc).toEqual([ "CP1" ]);
expect(typeof decodedToken.cnf.kid).toEqual('string');
expect(typeof decodedToken.cnf.xms_ksl).toEqual('string');
expect(decodedToken.xms_cc).toEqual(["CP1"]);
expect(typeof decodedToken.cnf.kid).toEqual("string");
expect(typeof decodedToken.cnf.xms_ksl).toEqual("string");
});
});

0 comments on commit 80ba1e4

Please sign in to comment.