Skip to content

Commit

Permalink
Merge pull request #494 from Esri/feat/joinGroup
Browse files Browse the repository at this point in the history
feat: new joinGroup and leaveGroup methods
  • Loading branch information
john gravois authored Mar 27, 2019
2 parents b1a935b + 14bdf88 commit 34e6e4f
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 32 deletions.
4 changes: 2 additions & 2 deletions packages/arcgis-rest-groups/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { IRequestOptions } from "@esri/arcgis-rest-request";
import { IUserRequestOptions } from "@esri/arcgis-rest-auth";

import { IItemUpdate, IGroupAdd, IGroup } from "@esri/arcgis-rest-common-types";

export interface IGroupIdRequestOptions extends IRequestOptions {
export interface IGroupIdRequestOptions extends IUserRequestOptions {
id: string;
}

Expand Down
1 change: 1 addition & 0 deletions packages/arcgis-rest-groups/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from "./protect";
export * from "./remove";
export * from "./search";
export * from "./update";
export * from "./join";
56 changes: 56 additions & 0 deletions packages/arcgis-rest-groups/src/join.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { request, getPortalUrl } from "@esri/arcgis-rest-request";

import { IGroupIdRequestOptions } from "./helpers";

/**
* ```js
* import { joinGroup } from '@esri/arcgis-rest-groups';
* //
* joinGroup({
* id: groupId,
* authentication
* })
* .then(response)
* ```
* Make a request as the authenticated user to join a Group. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/join-group.htm) for more information.
*
* @param requestOptions - Options for the request
* @returns A Promise that will resolve with the success/failure status of the request and the groupId.
*/
export function joinGroup(
requestOptions: IGroupIdRequestOptions
): Promise<{ success: boolean; groupId: string }> {
const url = `${getPortalUrl(requestOptions)}/community/groups/${
requestOptions.id
}/join`;

return request(url, requestOptions);
}

/**
* ```js
* import { leaveGroup } from '@esri/arcgis-rest-groups';
* //
* leaveGroup({
* id: groupId,
* authentication
* })
* .then(response)
* ```
* Make a request as the authenticated user to leave a Group. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/leave-group.htm) for more information.
*
* @param requestOptions - Options for the request
* @returns A Promise that will resolve with the success/failure status of the request and the groupId.
*/
export function leaveGroup(
requestOptions: IGroupIdRequestOptions
): Promise<{ success: boolean; groupId: string }> {
const url = `${getPortalUrl(requestOptions)}/community/groups/${
requestOptions.id
}/leave`;

return request(url, requestOptions);
}
27 changes: 16 additions & 11 deletions packages/arcgis-rest-groups/src/protect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,34 @@ import { IGroupIdRequestOptions } from "./helpers";
*/
export function protectGroup(
requestOptions: IGroupIdRequestOptions
): Promise<any> {
): Promise<{ success: boolean }> {
const url = `${getPortalUrl(requestOptions)}/community/groups/${
requestOptions.id
}/protect`;
const options: IGroupIdRequestOptions = {
...requestOptions
};
return request(url, options);

return request(url, requestOptions);
}

/**
* Unprotect a Group.
* ```js
* import { unprotectGroup } from '@esri/arcgis-rest-groups';
* //
* unprotectGroup({
* id: groupId,
* authentication
* })
* .then(response)
* ```
* Unprotect a Group. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/unprotect-group.htm) for more information.
* @param requestOptions - Options for the request
* @returns A Promise that will resolve with the success/failure status of the request
*/
export function unprotectGroup(
requestOptions: IGroupIdRequestOptions
): Promise<any> {
): Promise<{ success: boolean }> {
const url = `${getPortalUrl(requestOptions)}/community/groups/${
requestOptions.id
}/unprotect`;
const options: IGroupIdRequestOptions = {
...requestOptions
};
return request(url, options);

return request(url, requestOptions);
}
19 changes: 14 additions & 5 deletions packages/arcgis-rest-groups/test/crud.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,28 @@ import { removeGroup } from "../src/remove";
import { GroupEditResponse } from "./mocks/responses";

import { encodeParam } from "@esri/arcgis-rest-request";
import { UserSession } from "@esri/arcgis-rest-auth";
import { TOMORROW } from "@esri/arcgis-rest-auth/test/utils";
import { IGroupAdd } from "@esri/arcgis-rest-common-types";
import * as fetchMock from "fetch-mock";

describe("groups", () => {
afterEach(fetchMock.restore);

describe("authenticted methods", () => {
const MOCK_AUTH = {
getToken() {
return Promise.resolve("fake-token");
},
const MOCK_AUTH = new UserSession({
clientId: "clientId",
redirectUri: "https://example-app.com/redirect-uri",
token: "fake-token",
tokenExpires: TOMORROW,
refreshToken: "refreshToken",
refreshTokenExpires: TOMORROW,
refreshTokenTTL: 1440,
username: "casey",
password: "123456",
portal: "https://myorg.maps.arcgis.com/sharing/rest"
};
});

const MOCK_REQOPTS = {
authentication: MOCK_AUTH
};
Expand Down
72 changes: 72 additions & 0 deletions packages/arcgis-rest-groups/test/join.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { joinGroup, leaveGroup } from "../src/join";

import { GroupEditResponse } from "./mocks/responses";

import { encodeParam } from "@esri/arcgis-rest-request";
import { UserSession } from "@esri/arcgis-rest-auth";
import { TOMORROW } from "@esri/arcgis-rest-auth/test/utils";

import * as fetchMock from "fetch-mock";

describe("groups", () => {
afterEach(fetchMock.restore);

describe("authenticted methods", () => {
const MOCK_REQOPTS = {
authentication: new UserSession({
clientId: "clientId",
redirectUri: "https://example-app.com/redirect-uri",
token: "fake-token",
tokenExpires: TOMORROW,
refreshToken: "refreshToken",
refreshTokenExpires: TOMORROW,
refreshTokenTTL: 1440,
username: "casey",
password: "123456",
portal: "https://myorg.maps.arcgis.com/sharing/rest"
})
};

it("should help a user join a group", done => {
fetchMock.once("*", GroupEditResponse);

joinGroup({ id: "5bc", ...MOCK_REQOPTS })
.then(() => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://myorg.maps.arcgis.com/sharing/rest/community/groups/5bc/join"
);
expect(options.method).toBe("POST");
expect(options.body).toContain(encodeParam("f", "json"));
expect(options.body).toContain(encodeParam("token", "fake-token"));
done();
})
.catch(e => {
fail(e);
});
});
it("should help a user leave a group", done => {
fetchMock.once("*", GroupEditResponse);

leaveGroup({ id: "5bc", ...MOCK_REQOPTS })
.then(() => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://myorg.maps.arcgis.com/sharing/rest/community/groups/5bc/leave"
);
expect(options.method).toBe("POST");
expect(options.body).toContain(encodeParam("f", "json"));
expect(options.body).toContain(encodeParam("token", "fake-token"));
done();
})
.catch(e => {
fail(e);
});
});
});
});
20 changes: 15 additions & 5 deletions packages/arcgis-rest-groups/test/notification.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { encodeParam } from "@esri/arcgis-rest-request";
import { UserSession } from "@esri/arcgis-rest-auth";
import { TOMORROW } from "@esri/arcgis-rest-auth/test/utils";

import * as fetchMock from "fetch-mock";

import { createGroupNotification } from "../src/notification";
Expand All @@ -9,12 +12,19 @@ describe("groups", () => {
afterEach(fetchMock.restore);

describe("createGroupNotification", () => {
const MOCK_AUTH = {
getToken() {
return Promise.resolve("fake-token");
},
const MOCK_AUTH = new UserSession({
clientId: "clientId",
redirectUri: "https://example-app.com/redirect-uri",
token: "fake-token",
tokenExpires: TOMORROW,
refreshToken: "refreshToken",
refreshTokenExpires: TOMORROW,
refreshTokenTTL: 1440,
username: "casey",
password: "123456",
portal: "https://myorg.maps.arcgis.com/sharing/rest"
};
});

const MOCK_REQOPTS = {
authentication: MOCK_AUTH
};
Expand Down
26 changes: 17 additions & 9 deletions packages/arcgis-rest-groups/test/protect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,35 @@ import { protectGroup, unprotectGroup } from "../src/protect";
import { GroupEditResponse } from "./mocks/responses";

import { encodeParam } from "@esri/arcgis-rest-request";
import { UserSession } from "@esri/arcgis-rest-auth";
import { TOMORROW } from "@esri/arcgis-rest-auth/test/utils";

import * as fetchMock from "fetch-mock";

describe("groups", () => {
afterEach(fetchMock.restore);

describe("authenticted methods", () => {
const MOCK_AUTH = {
getToken() {
return Promise.resolve("fake-token");
},
portal: "https://myorg.maps.arcgis.com/sharing/rest"
};
const MOCK_REQOPTS = {
authentication: MOCK_AUTH
authentication: new UserSession({
clientId: "clientId",
redirectUri: "https://example-app.com/redirect-uri",
token: "fake-token",
tokenExpires: TOMORROW,
refreshToken: "refreshToken",
refreshTokenExpires: TOMORROW,
refreshTokenTTL: 1440,
username: "casey",
password: "123456",
portal: "https://myorg.maps.arcgis.com/sharing/rest"
})
};

it("should protect a group", done => {
fetchMock.once("*", GroupEditResponse);

protectGroup({ id: "5bc", ...MOCK_REQOPTS })
.then(response => {
.then(() => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
Expand All @@ -45,7 +53,7 @@ describe("groups", () => {
fetchMock.once("*", GroupEditResponse);

unprotectGroup({ id: "5bc", ...MOCK_REQOPTS })
.then(response => {
.then(() => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
Expand Down

0 comments on commit 34e6e4f

Please sign in to comment.