Skip to content

Commit

Permalink
feat(:clipboard:): add support for private item resources
Browse files Browse the repository at this point in the history
AFFECTS PACKAGES:
@esri/arcgis-rest-items
  • Loading branch information
jgravois committed Oct 2, 2018
1 parent ca1930b commit b120e9f
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/arcgis-rest-items/src/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export function addItemResource(
file: requestOptions.resource,
fileName: requestOptions.name,
text: requestOptions.content,
access: requestOptions.access,
access: requestOptions.private ? "private" : "inherit",
...requestOptions.params
};

Expand Down
5 changes: 2 additions & 3 deletions packages/arcgis-rest-items/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ export interface IItemResourceRequestOptions extends IItemIdRequestOptions {
*/
content?: string;
/**
* Set file resource to be private regardless of the item access level, or revert it by setting
* it to inherit which makes the item resource have the same access as the item.
* Controls whether access to the file resource is restricted to the owner or inherited from the sharing permissions set for the associated item.
*/
access?: "private" | "inherit";
private?: boolean;
resource?: any;
}

Expand Down
10 changes: 8 additions & 2 deletions packages/arcgis-rest-items/src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,18 @@ export function updateItemResource(

// mix in user supplied params
requestOptions.params = {
...requestOptions.params,
fileName: requestOptions.name,
text: requestOptions.content,
access: requestOptions.access
...requestOptions.params
};

// only override whatever access was specified previously if 'private' was passed explicitly
if (typeof requestOptions.private !== "undefined") {
requestOptions.params.access = requestOptions.private
? "private"
: "inherit";
}

return request(url, requestOptions);
}

Expand Down
40 changes: 39 additions & 1 deletion packages/arcgis-rest-items/test/add.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ describe("search", () => {
id: "3ef",
// File() is only available in the browser
resource: file,
access: "inherit",
name: "thebigkahuna",
...MOCK_USER_REQOPTS
})
Expand All @@ -214,5 +213,44 @@ describe("search", () => {
fail(e);
});
});

it("should add a binary resource to a secret item", done => {
fetchMock.once("*", {
success: true
});

const file = attachmentFile();

addItemResource({
id: "3ef",
// File() is only available in the browser
resource: file,
name: "thebigkahuna",
private: true,
...MOCK_USER_REQOPTS
})
.then(() => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://myorg.maps.arcgis.com/sharing/rest/content/users/casey/items/3ef/addResources"
);
expect(options.method).toBe("POST");
expect(options.body instanceof FormData).toBeTruthy();
const params = options.body as FormData;
if (params.get) {
expect(params.get("token")).toEqual("fake-token");
expect(params.get("f")).toEqual("json");
expect(params.get("file")).toEqual(file);
expect(params.get("access")).toEqual("inherit");
expect(params.get("fileName")).toEqual("thebigkahuna");
}

done();
})
.catch(e => {
fail(e);
});
});
}); // auth requests
});
64 changes: 61 additions & 3 deletions packages/arcgis-rest-items/test/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ describe("search", () => {
owner: "dbouwman",
name: "image/banner.png",
content: "jumbotron",
access: "inherit",
...MOCK_USER_REQOPTS
})
.then(response => {
Expand All @@ -169,7 +168,7 @@ describe("search", () => {
encodeParam("fileName", "image/banner.png")
);
expect(options.body).toContain(encodeParam("text", "jumbotron"));
expect(options.body).toContain(encodeParam("access", "inherit"));
expect(options.body).not.toContain(encodeParam("access", "inherit"));
expect(options.body).toContain(encodeParam("token", "fake-token"));
done();
})
Expand Down Expand Up @@ -216,7 +215,7 @@ describe("search", () => {
resourcesPrefix: "foolder"
}
})
.then(response => {
.then(() => {
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://myorg.maps.arcgis.com/sharing/rest/content/users/casey/items/3ef/updateResources"
Expand All @@ -229,6 +228,65 @@ describe("search", () => {
expect(options.body).toContain("resourcesPrefix=foolder");
expect(options.body).toContain(encodeParam("text", "jumbotron"));
expect(options.body).toContain(encodeParam("token", "fake-token"));
expect(options.body).not.toContain(encodeParam("access", "inherit"));
done();
})
.catch(e => {
fail(e);
});
});

it("update an item resource to make it secret", done => {
fetchMock.once("*", UpdateItemResourceResponse);
updateItemResource({
id: "3ef",
name: "image/banner.png",
content: "jumbotron",
private: true,
...MOCK_USER_REQOPTS
})
.then(() => {
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://myorg.maps.arcgis.com/sharing/rest/content/users/casey/items/3ef/updateResources"
);
expect(options.method).toBe("POST");
expect(options.body).toContain("f=json");
expect(options.body).toContain(
encodeParam("fileName", "image/banner.png")
);
expect(options.body).toContain(encodeParam("text", "jumbotron"));
expect(options.body).toContain(encodeParam("token", "fake-token"));
expect(options.body).toContain(encodeParam("access", "private"));
done();
})
.catch(e => {
fail(e);
});
});

it("update an item resource to spill the beans", done => {
fetchMock.once("*", UpdateItemResourceResponse);
updateItemResource({
id: "3ef",
name: "image/banner.png",
content: "jumbotron",
private: false,
...MOCK_USER_REQOPTS
})
.then(() => {
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://myorg.maps.arcgis.com/sharing/rest/content/users/casey/items/3ef/updateResources"
);
expect(options.method).toBe("POST");
expect(options.body).toContain("f=json");
expect(options.body).toContain(
encodeParam("fileName", "image/banner.png")
);
expect(options.body).toContain(encodeParam("text", "jumbotron"));
expect(options.body).toContain(encodeParam("token", "fake-token"));
expect(options.body).toContain(encodeParam("access", "inherit"));
done();
})
.catch(e => {
Expand Down

0 comments on commit b120e9f

Please sign in to comment.