Skip to content

Commit

Permalink
Merge 2a051a7 into dd7a5ac
Browse files Browse the repository at this point in the history
  • Loading branch information
mjuniper committed Oct 8, 2018
2 parents dd7a5ac + 2a051a7 commit 63f7bf7
Show file tree
Hide file tree
Showing 12 changed files with 455 additions and 39 deletions.
2 changes: 1 addition & 1 deletion packages/arcgis-rest-common-types/src/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export interface IGroup extends IGroupAdd {
protected: boolean;
isInvitationOnly: boolean;
isViewOnly: boolean;
isOpenData: boolean;
isOpenData?: boolean;
isFav: boolean;
autoJoin: boolean;
userMembership?: {
Expand Down
2 changes: 1 addition & 1 deletion packages/arcgis-rest-sharing/test/access.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { MOCK_USER_SESSION } from "./mocks/sharing";
import {
AnonUserResponse,
OrgAdminUserResponse
} from "../../arcgis-rest-users/test/mocks/responses";
} from "../../arcgis-rest-users/test/mocks/user";

const SharingResponse = {
notSharedWith: [] as any,
Expand Down
2 changes: 1 addition & 1 deletion packages/arcgis-rest-sharing/test/group-sharing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
GroupMemberUserResponse,
GroupAdminUserResponse,
OrgAdminUserResponse
} from "../../arcgis-rest-users/test/mocks/responses";
} from "../../arcgis-rest-users/test/mocks/user";

import { SearchResponse } from "../../arcgis-rest-items/test/mocks/search";

Expand Down
1 change: 1 addition & 0 deletions packages/arcgis-rest-users/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

export * from "./get";
export * from "./notification";
export * from "./invitation";
166 changes: 166 additions & 0 deletions packages/arcgis-rest-users/src/invitation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

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

import { IUserRequestOptions } from "@esri/arcgis-rest-auth";
import { IGroup } from "@esri/arcgis-rest-common-types";

export interface IInvitation {
id: string;
targetType: string;
targetId: string;
received: number;
accepted: boolean;
mustApprove: boolean;
email: string;
role: string;
type: string;
dateAccepted: number;
expiration: number;
created: number;
username: string;
fromUsername: {
username: string;
fullname?: string;
};
group?: IGroup;
groupId?: string;
}

export interface IInvitationResult {
userInvitations: IInvitation[];
}

/**
* Get all invitations for a user. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/user-invitations.htm) for more information.
*
* ```js
* import { getUserInvitations } from '@esri/arcgis-rest-users';
*
* // username is inferred from UserSession
* getUserInvitations({ authentication })
* .then((results) => {
* console.log(results.userInvitations.length); // 3
* })
* ```
*
* @param requestOptions - options to pass through in the request
* @returns A Promise that will resolve with the user's invitations
*/
export function getUserInvitations(
requestOptions: IUserRequestOptions
): Promise<IInvitationResult> {
let options = { httpMethod: "GET" } as IUserRequestOptions;

const username = encodeURIComponent(requestOptions.authentication.username);
const portalUrl = getPortalUrl(requestOptions);
const url = `${portalUrl}/community/users/${username}/invitations`;
options = { ...requestOptions, ...options };

// send the request
return request(url, options);
}

export interface IInvitationRequestOptions extends IUserRequestOptions {
invitationId: string;
}

/**
* Get an invitation for a user by id. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/user-invitation.htm) for more information.
*
* ```js
* import { getUserInvitation } from '@esri/arcgis-rest-users';
*
* // username is inferred from UserSession
* getUserInvitation({
* invitationId: "3ef",
* authentication
* })
* .then((results) => {
* console.log(results.accepted); // true
* })
* ```
*
* @param requestOptions - options to pass through in the request
* @returns A Promise that will resolve with the invitation
*/
export function getUserInvitation(
requestOptions: IInvitationRequestOptions
): Promise<IInvitation> {
const username = encodeURIComponent(requestOptions.authentication.username);
const portalUrl = getPortalUrl(requestOptions);
const url = `${portalUrl}/community/users/${username}/invitations/${
requestOptions.invitationId
}`;

let options = { httpMethod: "GET" } as IInvitationRequestOptions;
options = { ...requestOptions, ...options };

// send the request
return request(url, options);
}

/**
* Accept an invitation. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/accept-invitation.htm) for more information.
*
* ```js
* import { acceptInvitation } from '@esri/arcgis-rest-users';
*
* // username is inferred from UserSession
* acceptInvitation({
* invitationId: "3ef",
* authentication
* })
* .then((response) => {
* console.log(response.success); // true
* })
* ```
*
* @param requestOptions - Options for the request
* @returns A Promise that will resolve with the success/failure status of the request
*/
export function acceptInvitation(
requestOptions: IInvitationRequestOptions
): Promise<any> {
const username = encodeURIComponent(requestOptions.authentication.username);
const portalUrl = getPortalUrl(requestOptions);
const url = `${portalUrl}/community/users/${username}/invitations/${
requestOptions.invitationId
}/accept`;

const options: IInvitationRequestOptions = { ...requestOptions };
return request(url, options);
}

/**
* Decline an invitation. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/decline-invitation.htm) for more information.
*
* ```js
* import { declineInvitation } from '@esri/arcgis-rest-users';
*
* // username is inferred from UserSession
* declineInvitation({
* invitationId: "3ef",
* authentication
* })
* .then((response) => {
* console.log(response.success); // true
* })
* ```
*
* @param requestOptions - Options for the request
* @returns A Promise that will resolve with the success/failure status of the request
*/
export function declineInvitation(
requestOptions: IInvitationRequestOptions
): Promise<any> {
const username = encodeURIComponent(requestOptions.authentication.username);
const portalUrl = getPortalUrl(requestOptions);
const url = `${portalUrl}/community/users/${username}/invitations/${
requestOptions.invitationId
}/decline`;

const options: IInvitationRequestOptions = { ...requestOptions };
return request(url, options);
}
10 changes: 10 additions & 0 deletions packages/arcgis-rest-users/src/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ export interface INotificationResult {
/**
* Get notifications for a user.
*
* ```js
* import { getUserNotifications } from '@esri/arcgis-rest-users';
*
* // username is inferred from UserSession
* getUserNotifications({ authentication })
* .then((results) => {
* console.log(results.notifications.length); // 3
* })
* ```
*
* @param requestOptions - options to pass through in the request
* @returns A Promise that will resolve with the user's notifications
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/arcgis-rest-users/test/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
AnonUserResponse,
GroupMemberUserResponse,
GroupAdminUserResponse
} from "./mocks/responses";
} from "./mocks/user";

import { UserSession } from "@esri/arcgis-rest-auth";
import * as fetchMock from "fetch-mock";
Expand Down
169 changes: 169 additions & 0 deletions packages/arcgis-rest-users/test/invitation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import {
getUserInvitations,
getUserInvitation,
acceptInvitation,
declineInvitation
} from "../src/index";

import {
UserInvitationsResponse,
UserInvitationResponse
} from "./mocks/invitation";

import { encodeParam } from "@esri/arcgis-rest-request";
import { UserSession } from "@esri/arcgis-rest-auth";
import * as fetchMock from "fetch-mock";

const TOMORROW = (function() {
const now = new Date();
now.setDate(now.getDate() + 1);
return now;
})();

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

const session = new UserSession({
username: "c@sey",
password: "123456",
portal: "https://myorg.maps.arcgis.com/sharing/rest"
});

fetchMock.postOnce(
"https://myorg.maps.arcgis.com/sharing/rest/generateToken",
{
token: "fake-token",
expires: TOMORROW.getTime(),
username: "c@sey"
}
);

describe("getUserInvitations", () => {
session.refreshSession();

it("should make an authenticated request for user invitations", done => {
fetchMock.once("*", UserInvitationsResponse);

getUserInvitations({ authentication: session })
.then(response => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://myorg.maps.arcgis.com/sharing/rest/community/users/c%40sey/invitations?f=json&token=fake-token"
);
expect(options.method).toBe("GET");
expect(response.userInvitations.length).toEqual(1);
done();
})
.catch(e => {
fail(e);
});
});
});

describe("getUserInvitation", () => {
fetchMock.postOnce(
"https://myorg.maps.arcgis.com/sharing/rest/generateToken",
{
token: "fake-token",
expires: TOMORROW.getTime(),
username: "c@sey"
}
);

session.refreshSession();

it("should make an authenticated request for a user invitation", done => {
fetchMock.once("*", UserInvitationResponse);

getUserInvitation({ invitationId: "3ef", authentication: session })
.then(response => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://myorg.maps.arcgis.com/sharing/rest/community/users/c%40sey/invitations/3ef?f=json&token=fake-token"
);
expect(options.method).toBe("GET");
expect(response.id).toEqual("G45ad52e7560e470598815499003c13f6");
expect(response.id).toEqual("G45ad52e7560e470598815499003c13f6");
done();
})
.catch(e => {
fail(e);
});
});
});

describe("acceptInvitation", () => {
fetchMock.postOnce(
"https://myorg.maps.arcgis.com/sharing/rest/generateToken",
{
token: "fake-token",
expires: TOMORROW.getTime(),
username: "c@sey"
}
);

session.refreshSession();

it("should accept an invitation", done => {
fetchMock.once("*", { success: true });

acceptInvitation({ invitationId: "3ef", authentication: session })
.then(response => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://myorg.maps.arcgis.com/sharing/rest/community/users/c%40sey/invitations/3ef/accept"
);
expect(options.method).toBe("POST");
expect(options.body).toContain(encodeParam("f", "json"));
expect(options.body).toContain(encodeParam("token", "fake-token"));
expect(response.success).toEqual(true);
// expect(response.notificationId).toBe("3ef");
done();
})
.catch(e => {
fail(e);
});
});
});

describe("declineInvitation", () => {
fetchMock.postOnce(
"https://myorg.maps.arcgis.com/sharing/rest/generateToken",
{
token: "fake-token",
expires: TOMORROW.getTime(),
username: "c@sey"
}
);

session.refreshSession();

it("should decline an invitation", done => {
fetchMock.once("*", { success: true });

declineInvitation({ invitationId: "3ef", authentication: session })
.then(response => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://myorg.maps.arcgis.com/sharing/rest/community/users/c%40sey/invitations/3ef/decline"
);
expect(options.method).toBe("POST");
expect(options.body).toContain(encodeParam("f", "json"));
expect(options.body).toContain(encodeParam("token", "fake-token"));
expect(response.success).toEqual(true);
// expect(response.notificationId).toBe("3ef");
done();
})
.catch(e => {
fail(e);
});
});
});
});
Loading

0 comments on commit 63f7bf7

Please sign in to comment.