-
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.
Merge pull request #662 from Esri/f/shared-edit-group-api-update
feat(portal): add reassignItem
- Loading branch information
Showing
5 changed files
with
192 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
import { request, IRequestOptions } from "@esri/arcgis-rest-request"; | ||
import { IItem } from "@esri/arcgis-rest-types"; | ||
import { getPortalUrl } from "../util/get-portal-url"; | ||
import { isOrgAdmin } from "../sharing/helpers"; | ||
import { IUserRequestOptions } from "@esri/arcgis-rest-auth"; | ||
|
||
interface IReassignItemOptions extends IUserRequestOptions { | ||
id: string; | ||
currentOwner: string; | ||
targetUsername: string; | ||
targetFolderName?: string; | ||
} | ||
|
||
interface IReassignItemResponse { | ||
success: boolean; | ||
itemId: string; | ||
} | ||
|
||
/** | ||
* ```js | ||
* import { reassignItem } from '@esri/arcgis-rest-portal'; | ||
* // | ||
* reassignItem({ | ||
* id: "abc123", | ||
* currentOwner: "charles", | ||
* targetUsername: "leslie", | ||
* authentication | ||
* }) | ||
* ``` | ||
* Reassign an item from one user to another. | ||
* Caller must be an org admin or the request will fail. | ||
* `currentOwner` and `targetUsername` must be in the same | ||
* organization or the request will fail | ||
* @param reassignOptions - Options for the request | ||
*/ | ||
export function reassignItem( | ||
reassignOptions: IReassignItemOptions | ||
): Promise<IReassignItemResponse> { | ||
return isOrgAdmin(reassignOptions).then(isAdmin => { | ||
if (!isAdmin) { | ||
throw Error( | ||
`Item ${reassignOptions.id} can not be reassigned because current user is not an organization administrator.` | ||
); | ||
} | ||
// we're operating as an org-admin | ||
const url = `${getPortalUrl(reassignOptions)}/content/users/${ | ||
reassignOptions.currentOwner | ||
}/items/${reassignOptions.id}/reassign`; | ||
|
||
const opts = { | ||
params: { | ||
targetUsername: reassignOptions.targetUsername, | ||
targetFolderName: reassignOptions.targetFolderName | ||
}, | ||
authentication: reassignOptions.authentication | ||
}; | ||
return request(url, opts); | ||
}); | ||
} |
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
123 changes: 123 additions & 0 deletions
123
packages/arcgis-rest-portal/test/items/reassign.test.ts
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,123 @@ | ||
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
|
||
import * as fetchMock from "fetch-mock"; | ||
|
||
import { reassignItem } from "../../src/items/reassign"; | ||
|
||
import { UserSession } from "@esri/arcgis-rest-auth"; | ||
import { TOMORROW } from "@esri/arcgis-rest-auth/test/utils"; | ||
import { | ||
GroupMemberUserResponse, | ||
OrgAdminUserResponse | ||
} from "../mocks/users/user"; | ||
|
||
describe("reassignItem", () => { | ||
afterEach(fetchMock.restore); | ||
|
||
const MOCK_USER_SESSION = 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("shoulds throw if not authd as org_admin", done => { | ||
fetchMock.once( | ||
"https://myorg.maps.arcgis.com/sharing/rest/community/users/casey?f=json&token=fake-token", | ||
GroupMemberUserResponse | ||
); | ||
reassignItem({ | ||
id: "3ef", | ||
currentOwner: "alex", | ||
targetUsername: "blake", | ||
authentication: MOCK_USER_SESSION | ||
}).catch(e => { | ||
expect(e.message).toBe( | ||
"Item 3ef can not be reassigned because current user is not an organization administrator." | ||
); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("should send the folder if passed", done => { | ||
fetchMock | ||
.once( | ||
"https://myorg.maps.arcgis.com/sharing/rest/community/users/casey?f=json&token=fake-token", | ||
OrgAdminUserResponse | ||
) | ||
.once( | ||
"https://myorg.maps.arcgis.com/sharing/rest/content/users/alex/items/3ef/reassign", | ||
{ success: true, itemId: "3ef" } | ||
); | ||
reassignItem({ | ||
id: "3ef", | ||
currentOwner: "alex", | ||
targetUsername: "blake", | ||
targetFolderName: "folder1", | ||
authentication: MOCK_USER_SESSION | ||
}) | ||
.then(resp => { | ||
expect(fetchMock.done()).toBeTruthy( | ||
"All fetchMocks should have been called" | ||
); | ||
expect(resp.success).toBe(true); | ||
const [url, options]: [string, RequestInit] = fetchMock.lastCall( | ||
"https://myorg.maps.arcgis.com/sharing/rest/content/users/alex/items/3ef/reassign" | ||
); | ||
expect(url).toBe( | ||
"https://myorg.maps.arcgis.com/sharing/rest/content/users/alex/items/3ef/reassign" | ||
); | ||
expect(options.method).toBe("POST"); | ||
expect(options.body).toContain("targetUsername=blake"); | ||
expect(options.body).toContain("targetFolderName=folder1"); | ||
done(); | ||
}) | ||
.catch(e => { | ||
fail(e); | ||
}); | ||
}); | ||
|
||
it("should not send the folder if not passed", done => { | ||
fetchMock | ||
.once( | ||
"https://myorg.maps.arcgis.com/sharing/rest/community/users/casey?f=json&token=fake-token", | ||
OrgAdminUserResponse | ||
) | ||
.once( | ||
"https://myorg.maps.arcgis.com/sharing/rest/content/users/alex/items/3ef/reassign", | ||
{ success: true, itemId: "3ef" } | ||
); | ||
reassignItem({ | ||
id: "3ef", | ||
currentOwner: "alex", | ||
targetUsername: "blake", | ||
authentication: MOCK_USER_SESSION | ||
}) | ||
.then(resp => { | ||
expect(fetchMock.done()).toBeTruthy( | ||
"All fetchMocks should have been called" | ||
); | ||
expect(resp.success).toBe(true); | ||
const [url, options]: [string, RequestInit] = fetchMock.lastCall( | ||
"https://myorg.maps.arcgis.com/sharing/rest/content/users/alex/items/3ef/reassign" | ||
); | ||
expect(url).toBe( | ||
"https://myorg.maps.arcgis.com/sharing/rest/content/users/alex/items/3ef/reassign" | ||
); | ||
expect(options.method).toBe("POST"); | ||
expect(options.body).toContain("targetUsername=blake"); | ||
expect(options.body).not.toContain("targetFolderName"); | ||
done(); | ||
}) | ||
.catch(e => { | ||
fail(e); | ||
}); | ||
}); | ||
}); |