Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(:bowtie:): add new method to update a user profile
AFFECTS PACKAGES: @esri/arcgis-rest-users
- Loading branch information
Showing
5 changed files
with
231 additions
and
6 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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
|
||
import { | ||
request, | ||
IRequestOptions, | ||
getPortalUrl | ||
} from "@esri/arcgis-rest-request"; | ||
|
||
import { IUser } from "@esri/arcgis-rest-common-types"; | ||
import { UserSession } from "@esri/arcgis-rest-auth"; | ||
|
||
export interface IUpdateUserRequestOptions extends IRequestOptions { | ||
/** | ||
* A session representing a logged in user. | ||
*/ | ||
authentication: UserSession; | ||
/** | ||
* The user properties to be updated. | ||
*/ | ||
user: IUser; | ||
} | ||
|
||
export interface IUpdateUserResponse { | ||
success: boolean; | ||
username: string; | ||
} | ||
|
||
/** | ||
* ```js | ||
* import { updateUser } from '@esri/arcgis-rest-users'; | ||
* // any user can update their own profile | ||
* updateUser({ | ||
* authentication, | ||
* user: { description: "better than the last one" } | ||
* }) | ||
* .then(response) | ||
* // org administrators must declare the username that will be updated explicitly | ||
* updateUser({ | ||
* authentication, | ||
* user: { username: "c@sey", description: "" } | ||
* }) | ||
* .then(response) | ||
* // => { "success": true, "username": "c@sey" } | ||
* ``` | ||
* Update a user profile. The username will be extracted from the authentication session unless it is provided explicitly. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/update-user.htm) for more information. | ||
* | ||
* @param requestOptions - options to pass through in the request | ||
* @returns A Promise that will resolve with metadata about the user | ||
*/ | ||
export function updateUser( | ||
requestOptions?: IUpdateUserRequestOptions | ||
): Promise<IUpdateUserResponse> { | ||
// default to the authenticated username unless another username is provided manually | ||
const username = | ||
requestOptions.user.username || requestOptions.authentication.username; | ||
|
||
const updateUrl = `${getPortalUrl( | ||
requestOptions | ||
)}/community/users/${encodeURIComponent(username)}/update`; | ||
|
||
// mixin custom params and the user information, then drop the user info | ||
requestOptions.params = { | ||
...requestOptions.user, | ||
...requestOptions.params | ||
}; | ||
|
||
delete requestOptions.user; | ||
|
||
// send the request | ||
return request(updateUrl, 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,151 @@ | ||
/* Copyright (c) 2018-2019 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
|
||
import { updateUser, IUpdateUserResponse } from "../src/index"; | ||
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("updateUser", () => { | ||
afterEach(fetchMock.restore); | ||
|
||
const session = new UserSession({ | ||
username: "c@sey", | ||
password: "123456", | ||
token: "fake-token", | ||
tokenExpires: TOMORROW, | ||
portal: "https://myorg.maps.arcgis.com/sharing/rest" | ||
}); | ||
|
||
it("should make an authenticated request to update the same user profile.", done => { | ||
fetchMock.once("*", { | ||
success: true, | ||
username: "c@sey" | ||
} as IUpdateUserResponse); | ||
|
||
updateUser({ | ||
authentication: session, | ||
user: { description: "destroyer of worlds" } | ||
}) | ||
.then(() => { | ||
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/update" | ||
); | ||
expect(options.method).toBe("POST"); | ||
expect(options.body).toContain("f=json"); | ||
expect(options.body).toContain("token=fake-token"); | ||
expect(options.body).toContain( | ||
encodeParam("description", "destroyer of worlds") | ||
); | ||
done(); | ||
}) | ||
.catch(e => { | ||
fail(e); | ||
}); | ||
}); | ||
|
||
it("should make an authenticated request to update the same user profile and mixin custom params.", done => { | ||
fetchMock.once("*", { | ||
success: true, | ||
username: "c@sey" | ||
} as IUpdateUserResponse); | ||
|
||
updateUser({ | ||
authentication: session, | ||
user: { description: "destroyer of worlds" }, | ||
params: { foo: "bar" } | ||
}) | ||
.then(() => { | ||
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/update" | ||
); | ||
expect(options.method).toBe("POST"); | ||
expect(options.body).toContain("f=json"); | ||
expect(options.body).toContain("token=fake-token"); | ||
expect(options.body).toContain("foo=bar"); | ||
expect(options.body).toContain( | ||
encodeParam("description", "destroyer of worlds") | ||
); | ||
done(); | ||
}) | ||
.catch(e => { | ||
fail(e); | ||
}); | ||
}); | ||
|
||
it("should make an org administrator authenticated request to update a different user.", done => { | ||
fetchMock.once("*", { | ||
success: true, | ||
username: "jsmith" | ||
} as IUpdateUserResponse); | ||
|
||
updateUser({ | ||
authentication: session, | ||
user: { username: "jsmith", description: "something different" } | ||
}) | ||
.then(() => { | ||
expect(fetchMock.called()).toEqual(true); | ||
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*"); | ||
expect(url).toEqual( | ||
"https://myorg.maps.arcgis.com/sharing/rest/community/users/jsmith/update" | ||
); | ||
expect(options.method).toBe("POST"); | ||
expect(options.body).toContain("f=json"); | ||
expect(options.body).toContain("token=fake-token"); | ||
expect(options.body).toContain( | ||
encodeParam("description", "something different") | ||
); | ||
done(); | ||
}) | ||
.catch(e => { | ||
fail(e); | ||
}); | ||
}); | ||
|
||
it("should throw an error when the authenticated user doesnt have permission to update the user profile in question.", done => { | ||
fetchMock.once("*", { | ||
error: { | ||
code: 403, | ||
messageCode: "GWM_0003", | ||
message: | ||
"You do not have permissions to access this resource or perform this operation.", | ||
details: [] | ||
} | ||
}); | ||
|
||
updateUser({ | ||
authentication: session, | ||
user: { username: "fake", description: "real" } | ||
}) | ||
.then(() => { | ||
fail(); | ||
}) | ||
.catch(e => { | ||
expect(fetchMock.called()).toEqual(true); | ||
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*"); | ||
expect(url).toEqual( | ||
"https://myorg.maps.arcgis.com/sharing/rest/community/users/fake/update" | ||
); | ||
expect(options.method).toBe("POST"); | ||
expect(options.body).toContain("f=json"); | ||
expect(options.body).toContain("token=fake-token"); | ||
expect(options.body).toContain(encodeParam("description", "real")); | ||
expect(e.name).toBe("ArcGISAuthError"); | ||
expect(e.code).toBe("GWM_0003"); | ||
expect(e.originalMessage).toBe( | ||
"You do not have permissions to access this resource or perform this operation." | ||
); | ||
done(); | ||
}); | ||
}); | ||
}); |