Updating accessible utility API.

This commit is contained in:
mike12345567 2024-10-17 17:15:41 +01:00
parent bd10a3d831
commit c74577c512
3 changed files with 52 additions and 27 deletions

View File

@ -228,29 +228,35 @@ describe("/roles", () => {
}) })
it("should be able to fetch accessible roles (with builder)", async () => { it("should be able to fetch accessible roles (with builder)", async () => {
const res = await config.api.roles.accessible(config.defaultHeaders(), { await config.withHeaders(config.defaultHeaders(), async () => {
status: 200, 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 () => { it("should be able to fetch accessible roles (basic user)", async () => {
const headers = await config.basicRoleHeaders() const headers = await config.basicRoleHeaders()
const res = await config.api.roles.accessible(headers, { await config.withHeaders(headers, async () => {
status: 200, 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 () => { it("should be able to fetch accessible roles (no user)", async () => {
const res = await config.api.roles.accessible(config.publicHeaders(), { await config.withHeaders(config.publicHeaders(), async () => {
status: 200, 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 () => { 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, permissionId: permissions.BuiltinPermissionID.READ_ONLY,
version: "name", version: "name",
}) })
const res = await config.api.roles.accessible( await config.withHeaders(
{ "x-budibase-role": customRoleName }, { "x-budibase-role": customRoleName },
{ async () => {
status: 200, 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({ const headers = await config.roleHeaders({
roleId: role3, roleId: role3,
}) })
const res = await config.api.roles.accessible(headers, { await config.withHeaders(headers, async () => {
status: 200, 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"])
}) })
}) })
}) })

View File

@ -110,6 +110,7 @@ export default class TestConfiguration {
tenantId?: string tenantId?: string
api: API api: API
csrfToken?: string csrfToken?: string
temporaryHeaders?: Record<string, string | string[]>
constructor(openServer = true) { constructor(openServer = true) {
if (openServer) { if (openServer) {
@ -448,6 +449,18 @@ export default class TestConfiguration {
}) })
} }
async withHeaders(
headers: Record<string, string | string[]>,
cb: () => Promise<unknown>
) {
this.temporaryHeaders = headers
try {
await cb()
} finally {
this.temporaryHeaders = undefined
}
}
defaultHeaders(extras = {}, prodApp = false) { defaultHeaders(extras = {}, prodApp = false) {
const tenantId = this.getTenantId() const tenantId = this.getTenantId()
const user = this.getUser() const user = this.getUser()
@ -471,7 +484,10 @@ export default class TestConfiguration {
} else if (this.appId) { } else if (this.appId) {
headers[constants.Header.APP_ID] = this.appId headers[constants.Header.APP_ID] = this.appId
} }
return headers return {
...headers,
...this.temporaryHeaders,
}
} }
publicHeaders({ prodApp = true } = {}) { publicHeaders({ prodApp = true } = {}) {
@ -487,7 +503,10 @@ export default class TestConfiguration {
headers[constants.Header.TENANT_ID] = this.getTenantId() headers[constants.Header.TENANT_ID] = this.getTenantId()
return headers return {
...headers,
...this.temporaryHeaders,
}
} }
async basicRoleHeaders() { async basicRoleHeaders() {

View File

@ -34,12 +34,8 @@ export class RoleAPI extends TestAPI {
}) })
} }
accessible = async ( accessible = async (expectations?: Expectations) => {
headers: Record<string, string | string[]>,
expectations?: Expectations
) => {
return await this._get<AccessibleRolesResponse>(`/api/roles/accessible`, { return await this._get<AccessibleRolesResponse>(`/api/roles/accessible`, {
headers,
expectations, expectations,
}) })
} }