-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(users): add user invitation functions
AFFECTS PACKAGES: @esri/arcgis-rest-common-types @esri/arcgis-rest-users
- Loading branch information
Showing
5 changed files
with
343 additions
and
1 deletion.
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
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,102 @@ | ||
/* 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 invitations for a user. | ||
* | ||
* @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); | ||
} | ||
|
||
/** | ||
* Get an invitation for a user by id. | ||
* | ||
* @param requestOptions - options to pass through in the request | ||
* @returns A Promise that will resolve with the invitation | ||
*/ | ||
export function getUserInvitation( | ||
id: string, | ||
requestOptions: IUserRequestOptions | ||
): Promise<IInvitation> { | ||
let options = { httpMethod: "GET" } as IUserRequestOptions; | ||
|
||
const username = encodeURIComponent(requestOptions.authentication.username); | ||
const portalUrl = getPortalUrl(requestOptions); | ||
const url = `${portalUrl}/community/users/${username}/invitations/${id}`; | ||
options = { ...requestOptions, ...options }; | ||
|
||
// send the request | ||
return request(url, options); | ||
} | ||
|
||
/** | ||
* Accept an invitation. | ||
* | ||
* @param requestOptions - Options for the request | ||
* @returns A Promise that will resolve with the success/failure status of the request | ||
*/ | ||
export function acceptInvitation( | ||
id: string, | ||
requestOptions: IUserRequestOptions | ||
): Promise<any> { | ||
const username = encodeURIComponent(requestOptions.authentication.username); | ||
const portalUrl = getPortalUrl(requestOptions); | ||
const url = `${portalUrl}/community/users/${username}/invitations/${id}/accept`; | ||
|
||
return request(url, requestOptions); | ||
} | ||
|
||
export function declineInvitation( | ||
id: string, | ||
requestOptions: IUserRequestOptions | ||
): Promise<any> { | ||
const username = encodeURIComponent(requestOptions.authentication.username); | ||
const portalUrl = getPortalUrl(requestOptions); | ||
const url = `${portalUrl}/community/users/${username}/invitations/${id}/decline`; | ||
|
||
return request(url, requestOptions); | ||
} |
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/responses"; | ||
|
||
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("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("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("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); | ||
}); | ||
}); | ||
}); | ||
}); |
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