Skip to content

Commit

Permalink
feat(arcgis-rest-portal): add user properties functions
Browse files Browse the repository at this point in the history
  • Loading branch information
juliannaeapicella committed Jan 11, 2023
1 parent ff90826 commit a9e7352
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
59 changes: 59 additions & 0 deletions packages/arcgis-rest-portal/src/users/get-user-properties.ts
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);
}
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 packages/arcgis-rest-portal/test/users/get-user-properties.test.ts
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);
});
});
});
});

0 comments on commit a9e7352

Please sign in to comment.