Skip to content

Commit

Permalink
feat(arcgis-rest-portal): add searchCommunityUsers method to allow fo…
Browse files Browse the repository at this point in the history
…r searching outside user's org
  • Loading branch information
rweber-esri committed Sep 1, 2023
1 parent 37e6e8e commit 2695a75
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions packages/arcgis-rest-portal/src/users/search-users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,21 @@ export function searchUsers(
): Promise<ISearchResult<IUser>> {
return genericSearch<IUser>(search, "user");
}

/**
* ```js
* import { searchCommunityUsers } from "@esri/arcgis-rest-portal";
* //
* searchCommunityUsers({ q: 'tommy', authentication })
* .then(response) // response.total => 355
* ```
* Search all portals for users.
*
* @param search - A RequestOptions object to pass through to the endpoint.
* @returns A Promise that will resolve with the data from the response.
*/
export function searchCommunityUsers(
search: IUserSearchOptions | SearchQueryBuilder
): Promise<ISearchResult<IUser>> {
return genericSearch<IUser>(search, "communityUser");
}
5 changes: 4 additions & 1 deletion packages/arcgis-rest-portal/src/util/generic-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function genericSearch<T extends IItem | IGroup | IUser>(
| ISearchOptions
| ISearchGroupContentOptions
| SearchQueryBuilder,
searchType: "item" | "group" | "groupContent" | "user"
searchType: "item" | "group" | "groupContent" | "user" | "communityUser"
): Promise<ISearchResult<T>> {
let options: IRequestOptions;
if (typeof search === "string" || search instanceof SearchQueryBuilder) {
Expand Down Expand Up @@ -81,6 +81,9 @@ export function genericSearch<T extends IItem | IGroup | IUser>(
);
}
break;
case "communityUser":
path = "/community/users";
break;
default:
// "users"
path = "/portals/self/users/search";
Expand Down
45 changes: 37 additions & 8 deletions packages/arcgis-rest-portal/test/users/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@
* Apache-2.0 */

import fetchMock from "fetch-mock";
import { searchUsers } from "../../src/users/search-users.js";
import {
searchUsers,
searchCommunityUsers
} from "../../src/users/search-users.js";
import { UserSearchResponse } from "../mocks/users/user-search.js";

describe("users", () => {
const MOCK_AUTH = {
getToken() {
return Promise.resolve("fake-token");
},
portal: "https://myorg.maps.arcgis.com/sharing/rest"
};

afterEach(() => {
fetchMock.restore();
});
describe("searchUsers", () => {
const MOCK_AUTH = {
getToken() {
return Promise.resolve("fake-token");
},
portal: "https://myorg.maps.arcgis.com/sharing/rest"
};

it("should make a simple, authenticated user search request", (done) => {
fetchMock.once("*", UserSearchResponse);

Expand All @@ -32,6 +35,32 @@ describe("users", () => {
"https://myorg.maps.arcgis.com/sharing/rest/portals/self/users/search?f=json&q=role%3Aorg_user%20OR%20role%3Aorg_publisher&num=100&token=fake-token"
);
expect(options.method).toBe("GET");
expect(response).toEqual(UserSearchResponse);
done();
})
.catch((e) => {
fail(e);
});
});
});

describe("searchCommunityUsers", () => {
it("should make a simple, authenticated user search request", (done) => {
fetchMock.once("*", UserSearchResponse);

searchCommunityUsers({
q: "role:org_user OR role:org_publisher",
num: 100,
authentication: MOCK_AUTH
})
.then((response) => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://myorg.maps.arcgis.com/sharing/rest/community/users?f=json&q=role%3Aorg_user%20OR%20role%3Aorg_publisher&num=100&token=fake-token"
);
expect(options.method).toBe("GET");
expect(response).toEqual(UserSearchResponse);
done();
})
.catch((e) => {
Expand Down

0 comments on commit 2695a75

Please sign in to comment.