Skip to content
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

fix(): update clientKey to empty string in updateDomain/addDomain #1157

Merged
merged 11 commits into from
Aug 11, 2023
4 changes: 4 additions & 0 deletions packages/common/src/sites/domains/add-domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export function addDomain(
if (typeof title === "number") {
domainEntry.siteTitle = title.toString();
}
// update client key to empty string if we have one, as it won't pass schema validation
if (domainEntry.clientKey) {
domainEntry.clientKey = "";
}
return fetch(url, {
method: "POST",
headers,
Expand Down
6 changes: 6 additions & 0 deletions packages/common/src/sites/domains/update-domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ export function updateDomain(
if (typeof title === "number") {
domainEntry.siteTitle = title.toString();
}

// update client key to empty string if we have one, as it won't pass schema validation
if (domainEntry.clientKey) {
domainEntry.clientKey = "";
}

return fetch(url, {
method: "PUT",
headers,
Expand Down
81 changes: 81 additions & 0 deletions packages/common/test/sites/domains/add-domain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ describe("addDomain", function () {
const res = await addDomain(domainEntry, ro);
expect(fetchMock.done()).toBeTruthy("fetch should have been called once");
expect(res.success).toBeTruthy("json parsed and response returned");
const opts = fetchMock.lastOptions("end:api/v3/domains");
const body = JSON.parse(opts.body as string);
expect(getProp(body, "clientKey")).toBe(
"",
"should update clientKey to empty string"
);
});

it("converts title to string", async function () {
Expand Down Expand Up @@ -64,9 +70,84 @@ describe("addDomain", function () {
"1234",
"should coerce numeric title to a string"
);
expect(getProp(body, "clientKey")).toBe(
"",
"should update clientKey to empty string"
);
expect(res.success).toBeTruthy("json parsed and response returned");
});

it("converts title to string without client key", async function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For these tests, I'd expect to see passing in the clientKey, and verify that it does not make it into the body of the request (or is present as "" )

const entry = {
domain: "zebra-dc.hubqa.arcgis.com",
hostname: "zebra-dc.hubqa.arcgis.com",
id: "146663",
orgId: "97KLIFOSt5CxbiRI",
orgKey: "dc",
orgTitle: "Washington, DC R&D Center (QA)",
permanentRedirect: false,
siteId: "9697f67b6d6343fa823dcdbe2d172073",
siteTitle: 1234,
sslOnly: true,
} as unknown as IDomainEntry;
const ro = { isPortal: false } as IHubRequestOptions;

spyOn(
_checkStatusAndParseJsonModule,
"_checkStatusAndParseJson"
).and.returnValue(Promise.resolve({ success: true }));

fetchMock.post("end:api/v3/domains", {});

const res = await addDomain(entry, ro);
expect(fetchMock.done()).toBeTruthy("fetch should have been called once");
const opts = fetchMock.lastOptions("end:api/v3/domains");
const body = JSON.parse(opts.body as string);
expect(getProp(body, "siteTitle")).toBe(
"1234",
"should coerce numeric title to a string"
);
dbouwman marked this conversation as resolved.
Show resolved Hide resolved
expect(getProp(body, "clientKey")).toBe(
undefined,
"should not have client key"
);
expect(res.success).toBeTruthy("json parsed and response returned");
});

it("function runs as expected without a client key (code coverage)", async function () {
const ro = { isPortal: false } as IHubRequestOptions;
dbouwman marked this conversation as resolved.
Show resolved Hide resolved

spyOn(
_checkStatusAndParseJsonModule,
"_checkStatusAndParseJson"
).and.returnValue(Promise.resolve({ success: true }));

fetchMock.post("end:api/v3/domains", {});

const entry = {
domain: "zebra-dc.hubqa.arcgis.com",
hostname: "zebra-dc.hubqa.arcgis.com",
id: "146663",
orgId: "97KLIFOSt5CxbiRI",
orgKey: "dc",
orgTitle: "Washington, DC R&D Center (QA)",
permanentRedirect: false,
siteId: "9697f67b6d6343fa823dcdbe2d172073",
siteTitle: "Zebra",
sslOnly: true,
};

const res = await addDomain(entry, ro);
expect(fetchMock.done()).toBeTruthy("fetch should have been called once");
expect(res.success).toBeTruthy("json parsed and response returned");
const opts = fetchMock.lastOptions("end:api/v3/domains");
const body = JSON.parse(opts.body as string);
expect(getProp(body, "clientKey")).toBe(
undefined,
"should not have clientKey"
);
});

it("throws error on portal", async function () {
const ro = { isPortal: true } as IHubRequestOptions;

Expand Down
80 changes: 80 additions & 0 deletions packages/common/test/sites/domains/update-domain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ describe("updateDomain", function () {
const res = await updateDomain(domainEntry, ro);
expect(fetchMock.done()).toBeTruthy("fetch should have been called once");
expect(res.success).toBeTruthy("json parsed and response returned");
const opts = fetchMock.lastOptions(`end:api/v3/domains/${domainEntry.id}`);
const body = JSON.parse(opts.body as string);
expect(getProp(body, "clientKey")).toBe(
"",
"should update clientKey to empty string"
);
});

it("converts title to a string", async function () {
Expand Down Expand Up @@ -64,6 +70,47 @@ describe("updateDomain", function () {
"1234",
"should coerce numeric title to a string"
);
expect(getProp(body, "clientKey")).toBe(
"",
"should update clientKey to empty string"
);
expect(res.success).toBeTruthy("json parsed and response returned");
});

it("converts title to a string without client key", async function () {
const entry = {
domain: "zebra-dc.hubqa.arcgis.com",
hostname: "zebra-dc.hubqa.arcgis.com",
id: "146663",
orgId: "97KLIFOSt5CxbiRI",
orgKey: "dc",
orgTitle: "Washington, DC R&D Center (QA)",
permanentRedirect: false,
siteId: "9697f67b6d6343fa823dcdbe2d172073",
siteTitle: 1234,
sslOnly: true,
} as unknown as IDomainEntry;
const ro = { isPortal: false } as IHubRequestOptions;

spyOn(
_checkStatusAndParseJsonModule,
"_checkStatusAndParseJson"
).and.returnValue(Promise.resolve({ success: true }));

fetchMock.put(`end:api/v3/domains/${entry.id}`, {});

const res = await updateDomain(entry, ro);
expect(fetchMock.done()).toBeTruthy("fetch should have been called once");
const opts = fetchMock.lastOptions(`end:api/v3/domains/${entry.id}`);
const body = JSON.parse(opts.body as string);
expect(getProp(body, "siteTitle")).toBe(
"1234",
"should coerce numeric title to a string"
);
expect(getProp(body, "clientKey")).toBe(
undefined,
"should not have client key"
);
expect(res.success).toBeTruthy("json parsed and response returned");
});

Expand All @@ -84,4 +131,37 @@ describe("updateDomain", function () {
"fetch should NOT have been called"
);
});

it("function runs as expected without a client key (code coverage)", async function () {
const ro = { isPortal: false } as IHubRequestOptions;

spyOn(
_checkStatusAndParseJsonModule,
"_checkStatusAndParseJson"
).and.returnValue(Promise.resolve({ success: true }));

const entry = {
domain: "zebra-dc.hubqa.arcgis.com",
hostname: "zebra-dc.hubqa.arcgis.com",
id: "146663",
orgId: "97KLIFOSt5CxbiRI",
orgKey: "dc",
orgTitle: "Washington, DC R&D Center (QA)",
permanentRedirect: false,
siteId: "9697f67b6d6343fa823dcdbe2d172073",
siteTitle: "Zebra",
sslOnly: true,
};
fetchMock.put(`end:api/v3/domains/${entry.id}`, {});

const res = await updateDomain(entry, ro);
expect(fetchMock.done()).toBeTruthy("fetch should have been called once");
expect(res.success).toBeTruthy("json parsed and response returned");
const opts = fetchMock.lastOptions(`end:api/v3/domains/${entry.id}`);
const body = JSON.parse(opts.body as string);
expect(getProp(body, "clientKey")).toBe(
undefined,
"should not have clientKey"
);
});
});