테스트용 사용자와 그룹
TSv1 테스트는 @foundry/functions-testing-lib의 createUser와 createGroup으로 조회할 보안 주체를 준비할 수 있습니다. 실제 사용자나 그룹을 생성하거나 권한을 부여하는 작업은 아닙니다.
createUser에는 id와 username이 필수이며 나머지 필드는 선택입니다. createGroup에는 id가 필수이며 다른 필드는 선택입니다.
사용자와 그룹 조회 함수
src/principalFunctions.ts에 작성하고 index.ts에서 클래스를 내보냅니다.
import { Function, Principal, Users } from "@foundry/functions-api";
export class PrincipalFunctions {
@Function()
public async searchUsers(userId: string, groupId: string): Promise<Principal[]> {
const results = await Promise.all([
Users.getUserByIdAsync(userId),
Users.getGroupByIdAsync(groupId),
]);
return results.filter((principal): principal is Principal => principal != null);
}
@Function()
public async searchGroups(groupId: string): Promise<Principal[]> {
const group = await Users.getGroupByIdAsync(groupId);
return group == null ? [] : [group];
}
}
searchUsers는 두 조회를 병렬로 수행하여 존재하는 주체만 반환합니다. searchGroups는 그룹 하나를 조회하고 없으면 빈 목록을 반환합니다.
테스트 데이터 준비
src/tests/principalFunctions.ts에서 가상의 ID를 등록한 뒤 실제 함수의 결과와 비교합니다.
import { createGroup, createUser } from "@foundry/functions-testing-lib";
import { PrincipalFunctions } from "../principalFunctions";
const functions = new PrincipalFunctions();
test("returns the prepared user and group", async () => {
const group = createGroup({ id: "groupId" });
const user = createUser({ id: "userId", username: "username" });
await expect(functions.searchUsers("userId", "groupId")).resolves.toEqual([user, group]);
});
test("returns the prepared group", async () => {
const group = createGroup({ id: "group-only-id" });
await expect(functions.searchGroups("group-only-id")).resolves.toEqual([group]);
});
이 테스트는 함수의 조회와 결과 구성을 확인합니다. 사용자에게 실제 객체 또는 액션의 권한이 있는지 판정하는 통합 권한 검사는 아닙니다.