diff --git a/packages/server/src/api/routes/tests/role.spec.ts b/packages/server/src/api/routes/tests/role.spec.ts index 134078e6bf..a79f219311 100644 --- a/packages/server/src/api/routes/tests/role.spec.ts +++ b/packages/server/src/api/routes/tests/role.spec.ts @@ -228,29 +228,35 @@ describe("/roles", () => { }) it("should be able to fetch accessible roles (with builder)", async () => { - const res = await config.api.roles.accessible(config.defaultHeaders(), { - status: 200, + await config.withHeaders(config.defaultHeaders(), async () => { + const res = await config.api.roles.accessible({ + status: 200, + }) + expect(res.length).toBe(5) + expect(typeof res[0]).toBe("string") }) - expect(res.length).toBe(5) - expect(typeof res[0]).toBe("string") }) it("should be able to fetch accessible roles (basic user)", async () => { const headers = await config.basicRoleHeaders() - const res = await config.api.roles.accessible(headers, { - status: 200, + await config.withHeaders(headers, async () => { + const res = await config.api.roles.accessible({ + status: 200, + }) + expect(res.length).toBe(2) + expect(res[0]).toBe("BASIC") + expect(res[1]).toBe("PUBLIC") }) - expect(res.length).toBe(2) - expect(res[0]).toBe("BASIC") - expect(res[1]).toBe("PUBLIC") }) it("should be able to fetch accessible roles (no user)", async () => { - const res = await config.api.roles.accessible(config.publicHeaders(), { - status: 200, + await config.withHeaders(config.publicHeaders(), async () => { + const res = await config.api.roles.accessible({ + status: 200, + }) + expect(res.length).toBe(1) + expect(res[0]).toBe("PUBLIC") }) - expect(res.length).toBe(1) - expect(res[0]).toBe("PUBLIC") }) it("should not fetch higher level accessible roles when a custom role header is provided", async () => { @@ -261,13 +267,15 @@ describe("/roles", () => { permissionId: permissions.BuiltinPermissionID.READ_ONLY, version: "name", }) - const res = await config.api.roles.accessible( + await config.withHeaders( { "x-budibase-role": customRoleName }, - { - status: 200, + async () => { + const res = await config.api.roles.accessible({ + status: 200, + }) + expect(res).toEqual([customRoleName, "BASIC", "PUBLIC"]) } ) - expect(res).toEqual([customRoleName, "BASIC", "PUBLIC"]) }) }) @@ -297,10 +305,12 @@ describe("/roles", () => { const headers = await config.roleHeaders({ roleId: role3, }) - const res = await config.api.roles.accessible(headers, { - status: 200, + await config.withHeaders(headers, async () => { + const res = await config.api.roles.accessible({ + status: 200, + }) + expect(res).toEqual([role3, role1, "BASIC", "PUBLIC", role2, "POWER"]) }) - expect(res).toEqual([role3, role1, "BASIC", "PUBLIC", role2, "POWER"]) }) }) }) diff --git a/packages/server/src/tests/utilities/TestConfiguration.ts b/packages/server/src/tests/utilities/TestConfiguration.ts index ca0a6e420a..713f8b31de 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.ts +++ b/packages/server/src/tests/utilities/TestConfiguration.ts @@ -110,6 +110,7 @@ export default class TestConfiguration { tenantId?: string api: API csrfToken?: string + temporaryHeaders?: Record constructor(openServer = true) { if (openServer) { @@ -448,6 +449,18 @@ export default class TestConfiguration { }) } + async withHeaders( + headers: Record, + cb: () => Promise + ) { + this.temporaryHeaders = headers + try { + await cb() + } finally { + this.temporaryHeaders = undefined + } + } + defaultHeaders(extras = {}, prodApp = false) { const tenantId = this.getTenantId() const user = this.getUser() @@ -471,7 +484,10 @@ export default class TestConfiguration { } else if (this.appId) { headers[constants.Header.APP_ID] = this.appId } - return headers + return { + ...headers, + ...this.temporaryHeaders, + } } publicHeaders({ prodApp = true } = {}) { @@ -487,7 +503,10 @@ export default class TestConfiguration { headers[constants.Header.TENANT_ID] = this.getTenantId() - return headers + return { + ...headers, + ...this.temporaryHeaders, + } } async basicRoleHeaders() { diff --git a/packages/server/src/tests/utilities/api/role.ts b/packages/server/src/tests/utilities/api/role.ts index 31bffc6f85..5bd0647384 100644 --- a/packages/server/src/tests/utilities/api/role.ts +++ b/packages/server/src/tests/utilities/api/role.ts @@ -34,12 +34,8 @@ export class RoleAPI extends TestAPI { }) } - accessible = async ( - headers: Record, - expectations?: Expectations - ) => { + accessible = async (expectations?: Expectations) => { return await this._get(`/api/roles/accessible`, { - headers, expectations, }) }