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(hub-common): fix undefined token when getting schedule #1506

Merged
merged 4 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/common/src/content/manageSchedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export const isDownloadSchedulingAvailable = (
requestOptions: IHubRequestOptions,
access: AccessLevel
): boolean => {
const token = requestOptions.authentication.token;
const token = requestOptions.authentication?.token;
return (
requestOptions.portal?.includes("arcgis.com") &&
access === "public" &&
Expand Down
97 changes: 96 additions & 1 deletion packages/common/test/content/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from "../../src";
import * as _enrichmentsModule from "../../src/items/_enrichments";
import * as _fetchModule from "../../src/content/_fetch";
import * as scheduleModule from "../../src/content/manageSchedule";
import * as documentItem from "../mocks/items/document.json";
import * as multiLayerFeatureServiceItem from "../mocks/items/multi-layer-feature-service.json";
import {
Expand All @@ -23,7 +24,7 @@ import {
PDF_GUID,
PDF_ITEM,
} from "./fixtures";
import { MOCK_AUTH } from "../mocks/mock-auth";
import { MOCK_AUTH, MOCK_NOAUTH_HUB_REQOPTS } from "../mocks/mock-auth";

// mock the item enrichments that would be returned for a multi-layer service
const getMultiLayerItemEnrichments = () => {
Expand Down Expand Up @@ -784,4 +785,98 @@ describe("fetchHubContent", () => {

expect(chk.type).toBe("Hub Site Application");
});

it("should get schedule for request with a token", async () => {
const getItemSpy = spyOn(portalModule, "getItem").and.returnValue(
Promise.resolve(HOSTED_FEATURE_SERVICE_ITEM)
);
const getServiceSpy = spyOn(
featureLayerModule,
"getService"
).and.returnValue(HOSTED_FEATURE_SERVICE_DEFINITION);
const fetchItemEnrichmentsSpy = spyOn(
_enrichmentsModule,
"fetchItemEnrichments"
).and.returnValue({ metadata: null });

const getScheduleSpy = spyOn(scheduleModule, "getSchedule").and.returnValue(
Promise.resolve({
mode: "manual",
})
);

const chk = await fetchHubContent(HOSTED_FEATURE_SERVICE_GUID, {
portal: MOCK_AUTH.portal,
authentication: MOCK_AUTH,
});

// test for schedule
expect(chk.schedule).toBeDefined();
expect(getScheduleSpy.calls.count()).toBe(1);

expect(chk.id).toBe(HOSTED_FEATURE_SERVICE_GUID);
expect(chk.owner).toBe(HOSTED_FEATURE_SERVICE_ITEM.owner);
expect(chk.serverExtractCapability).toBeTruthy();

expect(getItemSpy.calls.count()).toBe(1);
expect(getItemSpy.calls.argsFor(0)[0]).toBe(HOSTED_FEATURE_SERVICE_GUID);
expect(getServiceSpy.calls.count()).toBe(1);
expect(getServiceSpy.calls.argsFor(0)[0].url).toBe(
HOSTED_FEATURE_SERVICE_URL
);
// NOTE: the first call to fetchItemEnrichments is done by fetchContent under the hood,
// while the second call is done by fetchHubContent. We only care about the second call here
expect(fetchItemEnrichmentsSpy.calls.count()).toBe(2);
expect(fetchItemEnrichmentsSpy.calls.argsFor(1)[0]).toBe(
HOSTED_FEATURE_SERVICE_ITEM
);
expect(fetchItemEnrichmentsSpy.calls.argsFor(1)[1]).toEqual(["metadata"]);
});

it("should not get schedule for request without a token", async () => {
const getItemSpy = spyOn(portalModule, "getItem").and.returnValue(
Promise.resolve(HOSTED_FEATURE_SERVICE_ITEM)
);
const getServiceSpy = spyOn(
featureLayerModule,
"getService"
).and.returnValue(HOSTED_FEATURE_SERVICE_DEFINITION);
const fetchItemEnrichmentsSpy = spyOn(
_enrichmentsModule,
"fetchItemEnrichments"
).and.returnValue({ metadata: null });

const getScheduleSpy = spyOn(scheduleModule, "getSchedule").and.returnValue(
Promise.resolve({
mode: "manual",
})
);

const chk = await fetchHubContent(
HOSTED_FEATURE_SERVICE_GUID,
MOCK_NOAUTH_HUB_REQOPTS
);

// test for schedule
expect(chk.schedule).not.toBeDefined();
expect(getScheduleSpy.calls.count()).toBe(0);

expect(chk.id).toBe(HOSTED_FEATURE_SERVICE_GUID);
expect(chk.owner).toBe(HOSTED_FEATURE_SERVICE_ITEM.owner);
expect(chk.serverExtractCapability).toBeTruthy();

expect(getItemSpy.calls.count()).toBe(1);
expect(getItemSpy.calls.argsFor(0)[0]).toBe(HOSTED_FEATURE_SERVICE_GUID);
expect(getServiceSpy.calls.count()).toBe(1);
expect(getServiceSpy.calls.argsFor(0)[0].url).toBe(
HOSTED_FEATURE_SERVICE_URL
);
// NOTE: the first call to fetchItemEnrichments is done by fetchContent under the hood,
// while the second call is done by fetchHubContent. We only care about the second call here
expect(fetchItemEnrichmentsSpy.calls.count()).toBe(2);
expect(fetchItemEnrichmentsSpy.calls.argsFor(1)[0]).toBe(
HOSTED_FEATURE_SERVICE_ITEM
);
expect(fetchItemEnrichmentsSpy.calls.argsFor(1)[1]).toEqual(["metadata"]);
});
});
Loading