-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
455 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
|
||
export * from "./get"; | ||
export * from "./notification"; | ||
export * from "./invitation"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.