-
Notifications
You must be signed in to change notification settings - Fork 121
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): add user properties functions
- Loading branch information
1 parent
ff90826
commit a9e7352
Showing
3 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
packages/arcgis-rest-portal/src/users/get-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,59 @@ | ||
/* Copyright (c) 2023 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
|
||
import { | ||
IRequestOptions, | ||
IUserRequestOptions, | ||
request | ||
} from "@esri/arcgis-rest-request"; | ||
import { getPortalUrl } from "../util/get-portal-url.js"; | ||
|
||
export interface IUserProperties { | ||
/** | ||
* user landing page configuration | ||
*/ | ||
landingPage: { | ||
url: string; | ||
}; | ||
/** | ||
* user MapViewer configuration | ||
*/ | ||
mapViewer: "classic" | "modern"; | ||
[key: string]: unknown; | ||
} | ||
|
||
/** | ||
* Helper that returns the properties attribute for a user. | ||
* | ||
* @param IGetUserPropertiesOptions - options to pass through in the request | ||
* @returns User properties object | ||
*/ | ||
export async function getUserProperties( | ||
username: string, | ||
requestOptions: IRequestOptions | ||
): Promise<IUserProperties> { | ||
const url = `${getPortalUrl( | ||
requestOptions | ||
)}/community/users/${encodeURIComponent(username)}/properties`; | ||
const response = await request(url, { httpMethod: "GET", ...requestOptions }); | ||
if (!response.mapViewer) { | ||
response.mapViewer = "modern"; | ||
} | ||
return response.properties; | ||
} | ||
|
||
export function setUserProperties( | ||
properties: IUserProperties, | ||
requestOptions: IUserRequestOptions | ||
): Promise<void> { | ||
const username = requestOptions.authentication.username; | ||
const url = `${getPortalUrl( | ||
requestOptions | ||
)}/community/users/${encodeURIComponent(username)}/setProperties`; | ||
const options: IRequestOptions = { | ||
httpMethod: "POST", | ||
params: { properties }, | ||
...requestOptions | ||
}; | ||
return request(url, requestOptions); | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/arcgis-rest-portal/test/mocks/users/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,8 @@ | ||
export const UserPropertiesResponse = { | ||
properties: { | ||
landingPage: { | ||
url: "http://www.esri.com" | ||
}, | ||
mapViewer: "classic" | ||
} | ||
}; |
73 changes: 73 additions & 0 deletions
73
packages/arcgis-rest-portal/test/users/get-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,73 @@ | ||
/* Copyright (c) 2023 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
|
||
import { ArcGISIdentityManager } from "@esri/arcgis-rest-request"; | ||
import { | ||
IUserProperties, | ||
getUserProperties, | ||
setUserProperties | ||
} from "../../src/users/get-user-properties"; | ||
import { UserPropertiesResponse } from "../mocks/users/user-properties"; | ||
import * as fetchMock from "fetch-mock"; | ||
|
||
const TOMORROW = (function () { | ||
const now = new Date(); | ||
now.setDate(now.getDate() + 1); | ||
return now; | ||
})(); | ||
|
||
describe("users", () => { | ||
afterEach(fetchMock.restore); | ||
|
||
describe("getUserProperties", () => { | ||
const session = new ArcGISIdentityManager({ | ||
username: "c@sey", | ||
password: "123456", | ||
token: "fake-token", | ||
tokenExpires: TOMORROW, | ||
portal: "https://myorg.maps.arcgis.com/sharing/rest" | ||
}); | ||
|
||
it("should make a request for user properties", (done) => { | ||
fetchMock.once("*", UserPropertiesResponse); | ||
|
||
getUserProperties("c@sey", { authentication: session }) | ||
.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/properties?f=json&token=fake-token" | ||
); | ||
expect(options.method).toBe("GET"); | ||
done(); | ||
}) | ||
.catch((e) => { | ||
fail(e); | ||
}); | ||
}); | ||
|
||
it("should make a request to set user properties", (done) => { | ||
fetchMock.once("*", UserPropertiesResponse); | ||
const properties: IUserProperties = { | ||
landingPage: { | ||
url: "index.html" | ||
}, | ||
mapViewer: "modern" | ||
}; | ||
|
||
setUserProperties(properties, { authentication: session }) | ||
.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/properties?f=json&token=fake-token" | ||
); | ||
expect(options.method).toBe("POST"); | ||
done(); | ||
}) | ||
.catch((e) => { | ||
fail(e); | ||
}); | ||
}); | ||
}); | ||
}); |