Skip to content

Commit

Permalink
Merge 6555683 into b675a61
Browse files Browse the repository at this point in the history
  • Loading branch information
ssylvia committed Oct 5, 2018
2 parents b675a61 + 6555683 commit d006006
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/arcgis-rest-items/src/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export function addItemResource(
file: requestOptions.resource,
fileName: requestOptions.name,
text: requestOptions.content,
access: requestOptions.private ? "private" : "inherit",
...requestOptions.params
};

Expand Down
4 changes: 4 additions & 0 deletions packages/arcgis-rest-items/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export interface IItemResourceRequestOptions extends IItemIdRequestOptions {
* Text input to be added as a file resource.
*/
content?: string;
/**
* Controls whether access to the file resource is restricted to the owner or inherited from the sharing permissions set for the associated item.
*/
private?: boolean;
resource?: any;
}

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

// mix in user supplied params
requestOptions.params = {
...requestOptions.params,
file: requestOptions.resource,
fileName: requestOptions.name,
text: requestOptions.content
text: requestOptions.content,
...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: 40 additions & 0 deletions packages/arcgis-rest-items/test/add.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,46 @@ describe("search", () => {
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);
});
});

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("private");
expect(params.get("fileName")).toEqual("thebigkahuna");
}

Expand Down
101 changes: 100 additions & 1 deletion packages/arcgis-rest-items/test/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import * as fetchMock from "fetch-mock";

import { attachmentFile } from "../../arcgis-rest-feature-service/test/attachments.test";

import { updateItem, updateItemResource, moveItem } from "../src/update";

import { ItemSuccessResponse } from "./mocks/item";
Expand Down Expand Up @@ -168,6 +170,7 @@ describe("search", () => {
encodeParam("fileName", "image/banner.png")
);
expect(options.body).toContain(encodeParam("text", "jumbotron"));
expect(options.body).not.toContain(encodeParam("access", "inherit"));
expect(options.body).toContain(encodeParam("token", "fake-token"));
done();
})
Expand All @@ -176,6 +179,43 @@ describe("search", () => {
});
});

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

const file = attachmentFile();

updateItemResource({
id: "3ef",
// File() is only available in the browser
resource: file,
name: "thebigkahuna",
...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/updateResources"
);
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("fileName")).toEqual("thebigkahuna");
}

done();
})
.catch(e => {
fail(e);
});
});

it("update an item resource, no owner passed", done => {
fetchMock.once("*", UpdateItemResourceResponse);
updateItemResource({
Expand Down Expand Up @@ -214,7 +254,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 @@ -227,6 +267,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 d006006

Please sign in to comment.