Skip to content

Commit

Permalink
fix: address issue creating sites on portal (#1085)
Browse files Browse the repository at this point in the history
* fix: address issue creating sites on portal

* docs: clean up comments
  • Loading branch information
dbouwman authored Jun 8, 2023
1 parent 05d33ba commit 7243db8
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 103 deletions.
5 changes: 2 additions & 3 deletions packages/common/src/sites/HubSites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,13 @@ export async function createSite(
);

// Register as an app
// const registration = await registerSiteAsApplication(model, requestOptions);
// model.data.values.clientId = registration.client_id;
// NOTE: Site registration needs to happen via the hub api domain api calls
// See https://devtopia.esri.com/dc/hub/issues/6390 for info

// Register domain and at the same time register the site as an application
// for portal, this will return a single entry with just the clientKey
const domainResponses = await addSiteDomains(model, requestOptions);
model.data.values.clientId = domainResponses[0].client_id;
model.data.values.clientId = domainResponses[0].clientKey;

// update the model
const updatedModel = await updateModel(
Expand Down
6 changes: 5 additions & 1 deletion packages/common/src/sites/domains/addSiteDomains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { addDomain } from "./add-domain";
/**
* Given a Site Model, register the domains with the Domain Service.
*
* For Portal, this will return a sparse entry that contains just the portal clientKey
*
* This should only be used when creating a site. To update domains related
* to a site, use the `addDomain` and `removeDomain` functions directly
*
Expand All @@ -16,7 +18,9 @@ export function addSiteDomains(
hubRequestOptions: IHubRequestOptions
): Promise<any[]> {
if (hubRequestOptions.isPortal) {
return Promise.resolve([]);
// For enterprise we don't register the domain, but we do return a sparse entry
// that contains just the portal clientKey so that the caller can use it.
return Promise.resolve([{ clientKey: "arcgisonline" }]);
} else {
const props = ["defaultHostname", "customHostname"];
return Promise.all(
Expand Down
203 changes: 107 additions & 96 deletions packages/common/test/sites/HubSites.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,114 +348,125 @@ describe("HubSites:", () => {
const newModel = commonModule.cloneObject(m);
return Promise.resolve(newModel);
});

addDomainsSpy = spyOn(
require("../../src/sites/domains/addSiteDomains"),
"addSiteDomains"
).and.returnValue(Promise.resolve([{ clientKey: "FAKE_CLIENT_KEY" }]));
});
it("works with a sparse IHubSite", async () => {
const sparseSite: Partial<commonModule.IHubSite> = {
name: "my site",
orgUrlKey: "dcdev",
};

const chk = await commonModule.createSite(sparseSite, MOCK_HUB_REQOPTS);

expect(uniqueDomainSpy.calls.count()).toBe(1);
expect(createModelSpy.calls.count()).toBe(1);
expect(updateModelSpy.calls.count()).toBe(1);

expect(addDomainsSpy.calls.count()).toBe(1);

const modelToCreate = createModelSpy.calls.argsFor(0)[0];
expect(modelToCreate.item.title).toBe("my site");
expect(modelToCreate.item.type).toBe("Hub Site Application");
expect(modelToCreate.item.properties.slug).toBe("dcdev|my-site");
expect(modelToCreate.item.properties.orgUrlKey).toBe("org");

expect(chk.name).toBe("my site");
expect(chk.url).toBe("https://my-site-org.hubqa.arcgis.com");
});
it("works with a full IHubSite", async () => {
const site: Partial<commonModule.IHubSite> = {
name: "Special Site",
slug: "CuStOm-Slug",
subdomain: "custom-subdomain",
customHostname: "site.myorg.com",
theme: {
fake: "theme",
},
defaultExtent: {
xmax: 10,
ymax: 10,
xmin: 5,
ymin: 5,
spatialReference: {
wkid: 4326,
describe("online: ", () => {
beforeEach(() => {
addDomainsSpy = spyOn(
require("../../src/sites/domains/addSiteDomains"),
"addSiteDomains"
).and.returnValue(Promise.resolve([{ clientKey: "FAKE_CLIENT_KEY" }]));
});
it("works with a sparse IHubSite", async () => {
const sparseSite: Partial<commonModule.IHubSite> = {
name: "my site",
orgUrlKey: "dcdev",
};

const chk = await commonModule.createSite(sparseSite, MOCK_HUB_REQOPTS);

expect(uniqueDomainSpy.calls.count()).toBe(1);
expect(createModelSpy.calls.count()).toBe(1);
expect(updateModelSpy.calls.count()).toBe(1);

expect(addDomainsSpy.calls.count()).toBe(1);

const modelToCreate = createModelSpy.calls.argsFor(0)[0];
expect(modelToCreate.item.title).toBe("my site");
expect(modelToCreate.item.type).toBe("Hub Site Application");
expect(modelToCreate.item.properties.slug).toBe("dcdev|my-site");
expect(modelToCreate.item.properties.orgUrlKey).toBe("org");
const modelToUpdate = updateModelSpy.calls.argsFor(0)[0];
expect(modelToUpdate.data.values.clientId).toBe("FAKE_CLIENT_KEY");

expect(chk.name).toBe("my site");
expect(chk.url).toBe("https://my-site-org.hubqa.arcgis.com");
});
it("works with a full IHubSite", async () => {
const site: Partial<commonModule.IHubSite> = {
name: "Special Site",
slug: "CuStOm-Slug",
subdomain: "custom-subdomain",
customHostname: "site.myorg.com",
theme: {
fake: "theme",
},
},
culture: "fr-ca",
map: {
basemaps: {
primary: {
fake: "basemap",
defaultExtent: {
xmax: 10,
ymax: 10,
xmin: 5,
ymin: 5,
spatialReference: {
wkid: 4326,
},
},
},
orgUrlKey: "dcdev",
layout: {
sections: [{}],
header: {
component: {
settings: {
title: "Title that is already set",
culture: "fr-ca",
map: {
basemaps: {
primary: {
fake: "basemap",
},
},
},
},
};
orgUrlKey: "dcdev",
layout: {
sections: [{}],
header: {
component: {
settings: {
title: "Title that is already set",
},
},
},
},
};

const chk = await commonModule.createSite(site, MOCK_HUB_REQOPTS);
const chk = await commonModule.createSite(site, MOCK_HUB_REQOPTS);

expect(uniqueDomainSpy.calls.count()).toBe(1);
expect(createModelSpy.calls.count()).toBe(1);
expect(updateModelSpy.calls.count()).toBe(1);
expect(uniqueDomainSpy.calls.count()).toBe(1);
expect(createModelSpy.calls.count()).toBe(1);
expect(updateModelSpy.calls.count()).toBe(1);

expect(addDomainsSpy.calls.count()).toBe(1);
expect(addDomainsSpy.calls.count()).toBe(1);

expect(chk.name).toBe("Special Site");
expect(chk.url).toBe("https://site.myorg.com");
expect(chk.culture).toBe("fr-ca");
expect(chk.theme).toEqual({ fake: "theme" });
expect(chk.defaultExtent.xmax).toBe(10);
expect(chk.map.basemaps.primary).toEqual({ fake: "basemap" });
const modelToUpdate = updateModelSpy.calls.argsFor(0)[0];
expect(modelToUpdate.data.values.clientId).toBe("FAKE_CLIENT_KEY");
expect(chk.name).toBe("Special Site");
expect(chk.url).toBe("https://site.myorg.com");
expect(chk.culture).toBe("fr-ca");
expect(chk.theme).toEqual({ fake: "theme" });
expect(chk.defaultExtent.xmax).toBe(10);
expect(chk.map.basemaps.primary).toEqual({ fake: "basemap" });
});
});
it("works in portal", async () => {
const sparseSite: Partial<commonModule.IHubSite> = {
name: "my site",
orgUrlKey: "dcdev",
};

const chk = await commonModule.createSite(
sparseSite,
MOCK_ENTERPRISE_REQOPTS
);

expect(uniqueDomainSpy.calls.count()).toBe(1);
expect(createModelSpy.calls.count()).toBe(1);
expect(updateModelSpy.calls.count()).toBe(1);

expect(addDomainsSpy.calls.count()).toBe(1);

const modelToCreate = createModelSpy.calls.argsFor(0)[0];
expect(modelToCreate.item.title).toBe("my site");
expect(modelToCreate.item.type).toBe("Site Application");
expect(modelToCreate.item.properties.slug).toBe("dcdev|my-site");
expect(modelToCreate.item.properties.orgUrlKey).toBe("org");
expect(chk.url).toBe("https://my-server.com/portal/apps/sites/#/my-site");
expect(chk.typeKeywords).toContain(`hubsubdomain|${chk.subdomain}`);
expect(chk.subdomain).toBe(`my-site`);
describe("portal:", () => {
it("works in portal", async () => {
const sparseSite: Partial<commonModule.IHubSite> = {
name: "my site",
orgUrlKey: "dcdev",
};

const chk = await commonModule.createSite(
sparseSite,
MOCK_ENTERPRISE_REQOPTS
);

expect(uniqueDomainSpy.calls.count()).toBe(1);
expect(createModelSpy.calls.count()).toBe(1);
expect(updateModelSpy.calls.count()).toBe(1);

const modelToCreate = createModelSpy.calls.argsFor(0)[0];
expect(modelToCreate.item.title).toBe("my site");
expect(modelToCreate.item.type).toBe("Site Application");
expect(modelToCreate.item.properties.slug).toBe("dcdev|my-site");
expect(modelToCreate.item.properties.orgUrlKey).toBe("org");
const modelToUpdate = updateModelSpy.calls.argsFor(0)[0];
expect(modelToUpdate.data.values.clientId).toBe("arcgisonline");
expect(chk.url).toBe(
"https://my-server.com/portal/apps/sites/#/my-site"
);
expect(chk.typeKeywords).toContain(`hubsubdomain|${chk.subdomain}`);
expect(chk.subdomain).toBe(`my-site`);
});
});
});
describe("enrichments:", () => {
Expand Down
7 changes: 6 additions & 1 deletion packages/common/test/sites/domains/addSiteDomains.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ describe("addSiteDomains", () => {
},
} as unknown as IHubRequestOptions;

await addSiteDomains(model, ro);
const result = await addSiteDomains(model, ro);

expect(result[0].clientKey).toBe(
"arcgisonline",
"returns portal client key"
);

expect(addSpy.calls.count()).toBe(0);
});
Expand Down
3 changes: 1 addition & 2 deletions packages/sites/src/create-site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@ export function createSite(
})
.then((domainResponses) => {
// client id will be the same for all domain resonses so we can just grab the first one
// no guard clause because the only way we don't get a response is if addSiteDomains throws
// which would not hit this code
model.data.values.clientId = domainResponses[0].clientKey;

// If we have a dcat section, hoist it out as it may contain complex adlib
// templates that are needed at run-time
// If we have data.values.dcatConfig, yank it off b/c that may have adlib template stuff in it
Expand Down

0 comments on commit 7243db8

Please sign in to comment.