Skip to content

Commit

Permalink
feat(arcgis-rest-portal): split set user properties
Browse files Browse the repository at this point in the history
  • Loading branch information
juliannaeapicella committed Jan 11, 2023
1 parent 826e7fd commit 46e15fb
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 108 deletions.
38 changes: 1 addition & 37 deletions packages/arcgis-rest-portal/src/users/get-user-properties.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
/* Copyright (c) 2023 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import {
IRequestOptions,
IUserRequestOptions,
request
} from "@esri/arcgis-rest-request";
import { IRequestOptions, request } from "@esri/arcgis-rest-request";
import { getPortalUrl } from "../util/get-portal-url.js";

export interface IUserProperties {
Expand Down Expand Up @@ -41,35 +37,3 @@ export async function getUserProperties(
}
return response.properties;
}

/**
* 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}`);
}
}
38 changes: 38 additions & 0 deletions packages/arcgis-rest-portal/src/users/set-user-properties.ts
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}`);
}
}
73 changes: 2 additions & 71 deletions packages/arcgis-rest-portal/test/users/get-user-properties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,8 @@
* Apache-2.0 */

import { ArcGISIdentityManager } from "@esri/arcgis-rest-request";
import {
IUserProperties,
getUserProperties,
setUserProperties
} from "../../src/users/get-user-properties.js";
import {
userPropertiesResponse,
userSetPropertiesResponseFailure,
userSetPropertiesResponseSuccess
} from "../mocks/users/user-properties.js";
import { getUserProperties } from "../../src/users/get-user-properties.js";
import { userPropertiesResponse } from "../mocks/users/user-properties.js";
import fetchMock from "fetch-mock";

const TOMORROW = (function () {
Expand Down Expand Up @@ -75,65 +67,4 @@ describe("users", () => {
});
});
});

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();
});
});
});
});
90 changes: 90 additions & 0 deletions packages/arcgis-rest-portal/test/users/set-user-properties.test.ts
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();
});
});
});
});

0 comments on commit 46e15fb

Please sign in to comment.