-
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(arcgis-rest-portal): split set user properties
- Loading branch information
1 parent
826e7fd
commit 46e15fb
Showing
4 changed files
with
131 additions
and
108 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
38 changes: 38 additions & 0 deletions
38
packages/arcgis-rest-portal/src/users/set-user-properties.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,38 @@ | ||
/* Copyright (c) 2023 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
|
||
import { IRequestOptions, request } from "@esri/arcgis-rest-request"; | ||
import { getPortalUrl } from "../util/get-portal-url.js"; | ||
import { IUserProperties } from "./get-user-properties.js"; | ||
|
||
/** | ||
* Updates the properties for a user | ||
* @param username The user whose properties to update | ||
* @param properties IUserProperties object with properties to update | ||
* @param requestOptions An IRequestOptions object | ||
* @returns a promise that resolves to { success: boolean } | ||
*/ | ||
export async function setUserProperties( | ||
username: string, | ||
properties: IUserProperties, | ||
requestOptions: IRequestOptions | ||
): Promise<{ success: boolean }> { | ||
const url = `${getPortalUrl( | ||
requestOptions | ||
)}/community/users/${encodeURIComponent(username)}/setProperties`; | ||
const options: IRequestOptions = { | ||
httpMethod: "POST", | ||
params: { properties }, | ||
...requestOptions | ||
}; | ||
try { | ||
const response = await request(url, options); | ||
if (!response.success) { | ||
throw new Error("Success was false"); | ||
} | ||
return response; | ||
} catch (e) { | ||
const error = e as Error; | ||
throw new Error(`Failed to set user properties: ${error.message}`); | ||
} | ||
} |
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
90 changes: 90 additions & 0 deletions
90
packages/arcgis-rest-portal/test/users/set-user-properties.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,90 @@ | ||
/* Copyright (c) 2023 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
|
||
import { ArcGISIdentityManager } from "@esri/arcgis-rest-request"; | ||
import { IUserProperties } from "../../src/users/get-user-properties.js"; | ||
import { setUserProperties } from "../../src/users/set-user-properties.js"; | ||
import { | ||
userSetPropertiesResponseFailure, | ||
userSetPropertiesResponseSuccess | ||
} from "../mocks/users/user-properties.js"; | ||
import fetchMock from "fetch-mock"; | ||
|
||
const TOMORROW = (function () { | ||
const now = new Date(); | ||
now.setDate(now.getDate() + 1); | ||
return now; | ||
})(); | ||
|
||
describe("users", () => { | ||
afterEach(fetchMock.restore); | ||
|
||
const session = new ArcGISIdentityManager({ | ||
username: "c@sey", | ||
password: "123456", | ||
token: "fake-token", | ||
tokenExpires: TOMORROW, | ||
portal: "https://myorg.maps.arcgis.com/sharing/rest" | ||
}); | ||
|
||
describe("setUserProperties", () => { | ||
it("should make a request to set user properties", (done) => { | ||
fetchMock.postOnce( | ||
"https://myorg.maps.arcgis.com/sharing/rest/community/users/c%40sey/setProperties", | ||
userSetPropertiesResponseSuccess | ||
); | ||
const properties: IUserProperties = { | ||
landingPage: { | ||
url: "index.html" | ||
}, | ||
mapViewer: "modern" | ||
}; | ||
|
||
setUserProperties(session.username, properties, { | ||
authentication: session | ||
}) | ||
.then(() => { | ||
expect(fetchMock.called()).toEqual(true); | ||
const [url, options]: [string, RequestInit] = fetchMock.lastCall(); | ||
console.log("options:", options); | ||
expect(url).toEqual( | ||
"https://myorg.maps.arcgis.com/sharing/rest/community/users/c%40sey/setProperties" | ||
); | ||
expect(options.method).toBe("POST"); | ||
done(); | ||
}) | ||
.catch((e) => { | ||
fail(e); | ||
}); | ||
}); | ||
|
||
it("should handle set user property errors", (done) => { | ||
fetchMock.postOnce( | ||
"https://myorg.maps.arcgis.com/sharing/rest/community/users/c%40sey/setProperties", | ||
userSetPropertiesResponseFailure | ||
); | ||
const properties: IUserProperties = { | ||
landingPage: { | ||
url: "index.html" | ||
}, | ||
mapViewer: "modern" | ||
}; | ||
|
||
setUserProperties(session.username, properties, { | ||
authentication: session | ||
}) | ||
.then(() => { | ||
fail(new Error("API did not serve error response")); | ||
}) | ||
.catch(() => { | ||
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/setProperties" | ||
); | ||
expect(options.method).toBe("POST"); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |