From 8196277a01d61e61d784314b693e95d41d65b19e Mon Sep 17 00:00:00 2001 From: adrinr Date: Fri, 17 Mar 2023 10:17:12 +0100 Subject: [PATCH] Add scim group api test tools --- packages/worker/src/tests/api/index.ts | 3 ++ packages/worker/src/tests/api/scim/groups.ts | 55 ++++++++++++++++++++ packages/worker/src/tests/api/scim/shared.ts | 43 +++++++++++++++ packages/worker/src/tests/api/scim/users.ts | 49 +++-------------- 4 files changed, 108 insertions(+), 42 deletions(-) create mode 100644 packages/worker/src/tests/api/scim/groups.ts create mode 100644 packages/worker/src/tests/api/scim/shared.ts diff --git a/packages/worker/src/tests/api/index.ts b/packages/worker/src/tests/api/index.ts index 89f38d63fb..fa4e54184c 100644 --- a/packages/worker/src/tests/api/index.ts +++ b/packages/worker/src/tests/api/index.ts @@ -16,6 +16,7 @@ import { TemplatesAPI } from "./templates" import { LicenseAPI } from "./license" import { AuditLogAPI } from "./auditLogs" import { ScimUsersAPI } from "./scim/users" +import { ScimGroupsAPI } from "./scim/groups" export default class API { accounts: AccountAPI @@ -35,6 +36,7 @@ export default class API { license: LicenseAPI auditLogs: AuditLogAPI scimUsersAPI: ScimUsersAPI + scimGroupsAPI: ScimGroupsAPI constructor(config: TestConfiguration) { this.accounts = new AccountAPI(config) @@ -54,5 +56,6 @@ export default class API { this.license = new LicenseAPI(config) this.auditLogs = new AuditLogAPI(config) this.scimUsersAPI = new ScimUsersAPI(config) + this.scimGroupsAPI = new ScimGroupsAPI(config) } } diff --git a/packages/worker/src/tests/api/scim/groups.ts b/packages/worker/src/tests/api/scim/groups.ts new file mode 100644 index 0000000000..2f0da03fe7 --- /dev/null +++ b/packages/worker/src/tests/api/scim/groups.ts @@ -0,0 +1,55 @@ +import { + ScimCreateGroupRequest, + ScimGroupListResponse, + ScimGroupResponse, +} from "@budibase/types" +import TestConfiguration from "../../TestConfiguration" +import { RequestSettings, ScimTestAPI } from "./shared" + +export class ScimGroupsAPI extends ScimTestAPI { + constructor(config: TestConfiguration) { + super(config) + } + + get = async ( + requestSettings?: Partial & { + params?: { + startIndex?: number + pageSize?: number + filter?: string + } + } + ) => { + let url = `/api/global/scim/v2/groups?` + const params = requestSettings?.params + if (params?.pageSize) { + url += `count=${params.pageSize}&` + } + if (params?.startIndex) { + url += `startIndex=${params.startIndex}&` + } + if (params?.filter) { + url += `filter=${params.filter}&` + } + const res = await this.call(url, "get", requestSettings) + return res.body as ScimGroupListResponse + } + + post = async ( + { + body, + }: { + body: ScimCreateGroupRequest + }, + requestSettings?: Partial + ) => { + const res = await this.call( + `/api/global/scim/v2/groups`, + "post", + requestSettings, + body + ) + + return res.body as ScimGroupResponse + } +} diff --git a/packages/worker/src/tests/api/scim/shared.ts b/packages/worker/src/tests/api/scim/shared.ts new file mode 100644 index 0000000000..d99f4c20dc --- /dev/null +++ b/packages/worker/src/tests/api/scim/shared.ts @@ -0,0 +1,43 @@ +import TestConfiguration from "../../TestConfiguration" +import { TestAPI } from "../base" + +const defaultConfig = { + expect: 200, + setHeaders: true, +} + +export type RequestSettings = typeof defaultConfig + +export abstract class ScimTestAPI extends TestAPI { + constructor(config: TestConfiguration) { + super(config) + } + + call = ( + url: string, + method: "get" | "post" | "patch" | "delete", + requestSettings?: Partial, + body?: object + ) => { + const { expect, setHeaders } = { ...defaultConfig, ...requestSettings } + let request = this.request[method](url).expect(expect) + + request = request.set( + "content-type", + "application/scim+json; charset=utf-8" + ) + + if (method !== "delete") { + request = request.expect("Content-Type", /json/) + } + + if (body) { + request = request.send(body) + } + + if (setHeaders) { + request = request.set(this.config.bearerAPIHeaders()) + } + return request + } +} diff --git a/packages/worker/src/tests/api/scim/users.ts b/packages/worker/src/tests/api/scim/users.ts index 85e38e9978..318492d378 100644 --- a/packages/worker/src/tests/api/scim/users.ts +++ b/packages/worker/src/tests/api/scim/users.ts @@ -5,48 +5,13 @@ import { ScimUpdateRequest, } from "@budibase/types" import TestConfiguration from "../../TestConfiguration" -import { TestAPI } from "../base" +import { RequestSettings, ScimTestAPI } from "./shared" -const defaultConfig = { - expect: 200, - setHeaders: true, -} - -type RequestSettings = typeof defaultConfig - -export class ScimUsersAPI extends TestAPI { +export class ScimUsersAPI extends ScimTestAPI { constructor(config: TestConfiguration) { super(config) } - #createRequest = ( - url: string, - method: "get" | "post" | "patch" | "delete", - requestSettings?: Partial, - body?: object - ) => { - const { expect, setHeaders } = { ...defaultConfig, ...requestSettings } - let request = this.request[method](url).expect(expect) - - request = request.set( - "content-type", - "application/scim+json; charset=utf-8" - ) - - if (method !== "delete") { - request = request.expect("Content-Type", /json/) - } - - if (body) { - request = request.send(body) - } - - if (setHeaders) { - request = request.set(this.config.bearerAPIHeaders()) - } - return request - } - get = async ( requestSettings?: Partial & { params?: { @@ -67,12 +32,12 @@ export class ScimUsersAPI extends TestAPI { if (params?.filter) { url += `filter=${params.filter}&` } - const res = await this.#createRequest(url, "get", requestSettings) + const res = await this.call(url, "get", requestSettings) return res.body as ScimUserListResponse } find = async (id: string, requestSettings?: Partial) => { - const res = await this.#createRequest( + const res = await this.call( `/api/global/scim/v2/users/${id}`, "get", requestSettings @@ -88,7 +53,7 @@ export class ScimUsersAPI extends TestAPI { }, requestSettings?: Partial ) => { - const res = await this.#createRequest( + const res = await this.call( `/api/global/scim/v2/users`, "post", requestSettings, @@ -108,7 +73,7 @@ export class ScimUsersAPI extends TestAPI { }, requestSettings?: Partial ) => { - const res = await this.#createRequest( + const res = await this.call( `/api/global/scim/v2/users/${id}`, "patch", requestSettings, @@ -119,7 +84,7 @@ export class ScimUsersAPI extends TestAPI { } delete = async (id: string, requestSettings?: Partial) => { - const res = await this.#createRequest( + const res = await this.call( `/api/global/scim/v2/users/${id}`, "delete", requestSettings