Skip to content

Commit

Permalink
Fix Web extension context integration test cases (#805)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyaginidhi authored Jan 9, 2024
1 parent 4c2f234 commit 5f84121
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 97 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"cSpell.words": [
"lcid",
"nupkg",
"powerapps",
"powerpages",
Expand Down
6 changes: 3 additions & 3 deletions l10n/bundle.l10n.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
"{0} represents the name of the file"
]
},
"Enter the name of the web template": "Enter the name of the web template",
"Please enter a name for the web template.": "Please enter a name for the web template.",
"A webtemplate with the same name already exists. Please enter a different name.": "A webtemplate with the same name already exists. Please enter a different name.",
"No page templates found": "No page templates found",
"No webpages found": "No webpages found",
"New Webpage": "New Webpage",
Expand All @@ -85,9 +88,6 @@
"File(s) already exist. No new files to add": "File(s) already exist. No new files to add",
"Web files": "Web files",
"Webfile(s) added successfully": "Webfile(s) added successfully",
"Enter the name of the web template": "Enter the name of the web template",
"Please enter a name for the web template.": "Please enter a name for the web template.",
"A webtemplate with the same name already exists. Please enter a different name.": "A webtemplate with the same name already exists. Please enter a different name.",
"Page Template name cannot be empty.": "Page Template name cannot be empty.",
"New Page Template": "New Page Template",
"Choose web template": "Choose web template",
Expand Down
18 changes: 9 additions & 9 deletions src/web/client/WebExtensionContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,14 +345,14 @@ class WebExtensionContext implements IWebExtensionContext {
await this.setWebsiteLanguageCode();

// Getting website Id to populate shared workspace for Co-Presence
const websiteid = this.urlParametersMap.get(
const websiteId = this.urlParametersMap.get(
Constants.queryParameters.WEBSITE_ID
) as string;

const headers = getCommonHeaders(this._dataverseAccessToken);

// Populate shared workspace for Co-Presence
await this.populateSharedworkspace(headers, dataverseOrgUrl, websiteid);
await this.populateSharedWorkspace(headers, dataverseOrgUrl, websiteId);
}

public async dataverseAuthentication(firstTimeAuth = false) {
Expand Down Expand Up @@ -729,24 +729,24 @@ class WebExtensionContext implements IWebExtensionContext {
this._worker = worker;
}

private async populateSharedworkspace(
private async populateSharedWorkspace(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
headers: any,
dataverseOrgUrl: string,
websiteid: string
websiteId: string
) {
try {
const sharedworkspace = await getOrCreateSharedWorkspace({
const sharedWorkspace = await getOrCreateSharedWorkspace({
headers,
dataverseOrgUrl,
websiteid,
websiteId: websiteId,
});

const sharedWorkSpaceParamsMap = new Map<string, string>();
for (const key in sharedworkspace) {
for (const key in sharedWorkspace) {
sharedWorkSpaceParamsMap.set(
String(key).trim().toLocaleLowerCase(),
String(sharedworkspace[key]).trim()
String(sharedWorkspace[key]).trim()
);
}

Expand All @@ -759,7 +759,7 @@ class WebExtensionContext implements IWebExtensionContext {
} catch (error) {
this.telemetry.sendErrorTelemetry(
telemetryEventNames.WEB_EXTENSION_POPULATE_SHARED_WORKSPACE_SYSTEM_ERROR,
this.populateSharedworkspace.name,
this.populateSharedWorkspace.name,
Constants.WEB_EXTENSION_POPULATE_SHARED_WORKSPACE_SYSTEM_ERROR,
error as Error
);
Expand Down
129 changes: 45 additions & 84 deletions src/web/client/test/integration/WebExtensionContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ describe("WebExtensionContext", () => {
assert.calledOnceWithExactly(
sendErrorTelemetry,
telemetryEventNames.WEB_EXTENSION_DATAVERSE_AUTHENTICATION_MISSING,
'triggeredMethod'
WebExtensionContext.dataverseAuthentication.name
);
expect(WebExtensionContext.websiteIdToLanguage).empty;
expect(WebExtensionContext.languageIdCodeMap).empty;
Expand Down Expand Up @@ -186,7 +186,8 @@ describe("WebExtensionContext", () => {
fileName,
attributePath,
encodeAsBase64,
mimeType
mimeType,
true
);

//Assert
Expand All @@ -204,6 +205,9 @@ describe("WebExtensionContext", () => {
_encodeAsBase64: true,
_mimeType: "pdf",
_hasDirtyChanges: false,
_hasDiffViewTriggered: false,
_isContentLoaded: true,
_logicalEntityName: undefined
};
expect(fileMap).deep.eq(expectedResult);
});
Expand Down Expand Up @@ -253,12 +257,15 @@ describe("WebExtensionContext", () => {
_encodeAsBase64: encodeAsBase64,
_hasDirtyChanges: false,
_mimeType: undefined,
_hasDiffViewTriggered: false,
_isContentLoaded: undefined,
_logicalEntityName: undefined
};

expect(fileMap).deep.eq(expectedResult);
});

it("updateEntityDetailsInContext_whenPassAllParamsExcpet_shouldSetColumnContent", async () => {
it("updateEntityDetailsInContext_whenPassAllParamsExcept_shouldSetColumnContent", async () => {
//Act
const attributePaths = {
relativePath: "relativePath",
Expand Down Expand Up @@ -334,7 +341,7 @@ describe("WebExtensionContext", () => {
assert.calledOnceWithExactly(
sendErrorTelemetry,
telemetryEventNames.WEB_EXTENSION_DATAVERSE_AUTHENTICATION_MISSING,
'triggeredMethod'
WebExtensionContext.dataverseAuthentication.name
);
expect(WebExtensionContext.websiteIdToLanguage).length(0);
expect(WebExtensionContext.languageIdCodeMap).length(0);
Expand Down Expand Up @@ -404,6 +411,15 @@ describe("WebExtensionContext", () => {
"getWebsiteLanguageIdToPortalLanguageIdMap"
).returns(websiteLanguageIdToPortalLanguageMap);

const sharedWorkSpaceParamsMap = new Map<string, string>([
[
"workspaceKey",
"d8b40829-17c8-4082-9e3f-89d60dc0ab7e", // some guid value
],
]);
const getOrCreateSharedWorkspace = stub(urlBuilderUtil, "getOrCreateSharedWorkspace")
.resolves(sharedWorkSpaceParamsMap);

const portalLanguageIdCodeMap = new Map<string, string>([
["d8b40829-17c8-4082-9e3f-89d60dc0ab7e", "1033"],
]);
Expand Down Expand Up @@ -447,7 +463,7 @@ describe("WebExtensionContext", () => {
);

expect(WebExtensionContext.dataverseAccessToken).eq(accessToken);
assert.calledOnceWithExactly(dataverseAuthentication, ORG_URL);
assert.calledOnceWithExactly(dataverseAuthentication, ORG_URL, true);
assert.callCount(sendAPISuccessTelemetry, 3);
assert.calledOnceWithExactly(
getLcidCodeMap,
Expand Down Expand Up @@ -508,9 +524,9 @@ describe("WebExtensionContext", () => {
Constants.initializationEntityName.WEBSITELANGUAGE
);

const thrirdGetCustomRequestURLCall = getCustomRequestURL.getCalls()[2];
expect(thrirdGetCustomRequestURLCall.args[0]).eq(ORG_URL);
expect(thrirdGetCustomRequestURLCall.args[1]).eq(
const thirdGetCustomRequestURLCall = getCustomRequestURL.getCalls()[2];
expect(thirdGetCustomRequestURLCall.args[0]).eq(ORG_URL);
expect(thirdGetCustomRequestURLCall.args[1]).eq(
Constants.initializationEntityName.PORTALLANGUAGE
);
//#endregion
Expand All @@ -531,6 +547,15 @@ describe("WebExtensionContext", () => {
expect(thirdFetchCall.args[1]?.headers).deep.eq(header);

//#endregion

assert.calledOnceWithExactly(
getOrCreateSharedWorkspace,
{
headers: header,
dataverseOrgUrl: ORG_URL,
websiteId: undefined
}
);
});

it("authenticateAndUpdateDataverseProperties_whenFetchCallFails_languageMapsShouldStillBeUpdated", async () => {
Expand Down Expand Up @@ -558,21 +583,6 @@ describe("WebExtensionContext", () => {
"getCustomRequestURL"
).returns(requestUrl);

const sendAPIFailureTelemetry = stub(
WebExtensionContext.telemetry,
"sendAPIFailureTelemetry"
);

const sendAPITelemetry = stub(
WebExtensionContext.telemetry,
"sendAPITelemetry"
);

const sendAPISuccessTelemetry = stub(
WebExtensionContext.telemetry,
"sendAPISuccessTelemetry"
);

const languageIdCodeMap = new Map<string, string>([["1033", "en-US"]]);
const getLcidCodeMap = stub(schemaHelperUtil, "getLcidCodeMap").returns(
languageIdCodeMap
Expand Down Expand Up @@ -631,54 +641,11 @@ describe("WebExtensionContext", () => {
);

expect(WebExtensionContext.dataverseAccessToken).eq(accessToken);
assert.calledOnceWithExactly(dataverseAuthentication, ORG_URL);
assert.calledThrice(sendAPISuccessTelemetry);
assert.calledOnceWithExactly(
getLcidCodeMap,
{ value: "value" },
SCHEMA_VERSION.toLowerCase()
);
assert.calledOnceWithExactly(
getWebsiteIdToLcidMap,
{ value: "value" },
SCHEMA_VERSION.toLowerCase()
);
assert.calledOnceWithExactly(
getPortalLanguageIdToLcidMap,
{ value: "value" },
SCHEMA_VERSION.toLowerCase()
);
assert.calledOnceWithExactly(
getWebsiteLanguageIdToPortalLanguageIdMap,
{ value: "value" },
SCHEMA_VERSION.toLowerCase()
);

assert.calledThrice(sendAPIFailureTelemetry);

//#region sendAPITelemetry calls
assert.calledThrice(sendAPITelemetry);
const firstSendAPITelemetryCall = sendAPITelemetry.getCalls()[0];
expect(firstSendAPITelemetryCall.args[0]).eq(requestUrl);
expect(firstSendAPITelemetryCall.args[1]).eq(
Constants.initializationEntityName.WEBSITE
);
expect(firstSendAPITelemetryCall.args[2]).eq(Constants.httpMethod.GET);

const secondSendAPITelemetryCall = sendAPITelemetry.getCalls()[1];
expect(secondSendAPITelemetryCall.args[0]).eq(requestUrl);
expect(secondSendAPITelemetryCall.args[1]).eq(
Constants.initializationEntityName.WEBSITELANGUAGE
);
expect(secondSendAPITelemetryCall.args[2]).eq(Constants.httpMethod.GET);

const thirdSendAPITelemetryCall = sendAPITelemetry.getCalls()[2];
expect(thirdSendAPITelemetryCall.args[0]).eq(requestUrl);
expect(thirdSendAPITelemetryCall.args[1]).eq(
Constants.initializationEntityName.PORTALLANGUAGE
);
expect(thirdSendAPITelemetryCall.args[2]).eq(Constants.httpMethod.GET);
//#endregion
assert.calledOnceWithExactly(dataverseAuthentication, ORG_URL, true);
assert.notCalled(getLcidCodeMap);
assert.notCalled(getWebsiteIdToLcidMap);
assert.notCalled(getPortalLanguageIdToLcidMap);
assert.notCalled(getWebsiteLanguageIdToPortalLanguageIdMap);

//#region getCustomRequestURL calls
assert.calledThrice(getCustomRequestURL);
Expand All @@ -694,9 +661,9 @@ describe("WebExtensionContext", () => {
Constants.initializationEntityName.WEBSITELANGUAGE
);

const thrirdGetCustomRequestURLCall = getCustomRequestURL.getCalls()[2];
expect(thrirdGetCustomRequestURLCall.args[0]).eq(ORG_URL);
expect(thrirdGetCustomRequestURLCall.args[1]).eq(
const thirdGetCustomRequestURLCall = getCustomRequestURL.getCalls()[2];
expect(thirdGetCustomRequestURLCall.args[0]).eq(ORG_URL);
expect(thirdGetCustomRequestURLCall.args[1]).eq(
Constants.initializationEntityName.PORTALLANGUAGE
);
//#endregion
Expand Down Expand Up @@ -744,11 +711,6 @@ describe("WebExtensionContext", () => {
"getCustomRequestURL"
).returns(requestUrl);

const sendAPIFailureTelemetry = stub(
WebExtensionContext.telemetry,
"sendAPIFailureTelemetry"
);

const _mockFetch = stub(fetch, "default").throws();
const languageIdCodeMap = new Map<string, string>([["1033", "en-US"]]);
stub(schemaHelperUtil, "getLcidCodeMap").returns(languageIdCodeMap);
Expand Down Expand Up @@ -795,7 +757,7 @@ describe("WebExtensionContext", () => {
);
expect(WebExtensionContext.dataverseAccessToken).eq(accessToken);

assert.calledOnceWithExactly(dataverseAuthentication, ORG_URL);
assert.calledOnceWithExactly(dataverseAuthentication, ORG_URL, true);
//#region Fetch
const header = getCommonHeaders(accessToken);
assert.calledThrice(_mockFetch);
Expand All @@ -810,7 +772,6 @@ describe("WebExtensionContext", () => {
const thirdFetchCall = _mockFetch.getCalls()[2];
expect(thirdFetchCall.args[0], requestUrl);
expect(thirdFetchCall.args[1]?.headers).deep.eq(header);
assert.calledThrice(sendAPIFailureTelemetry);

//#endregion

Expand All @@ -828,9 +789,9 @@ describe("WebExtensionContext", () => {
Constants.initializationEntityName.WEBSITELANGUAGE
);

const thrirdGetCustomRequestURLCall = getCustomRequestURL.getCalls()[2];
expect(thrirdGetCustomRequestURLCall.args[0]).eq(ORG_URL);
expect(thrirdGetCustomRequestURLCall.args[1]).eq(
const thirdGetCustomRequestURLCall = getCustomRequestURL.getCalls()[2];
expect(thirdGetCustomRequestURLCall.args[0]).eq(ORG_URL);
expect(thirdGetCustomRequestURLCall.args[1]).eq(
Constants.initializationEntityName.PORTALLANGUAGE
);
//#endregion
Expand Down
2 changes: 1 addition & 1 deletion src/web/client/utilities/urlBuilderUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ export async function getOrCreateSharedWorkspace(config: any) {
method: "POST",
body: JSON.stringify({
target: {
name: getWorkSpaceName(config.websiteid),
name: getWorkSpaceName(config.websiteId),
}
}),
}
Expand Down

0 comments on commit 5f84121

Please sign in to comment.