Return client id and secret from api
This commit is contained in:
parent
b43bcb00b3
commit
bdacb51811
|
@ -5,18 +5,27 @@ import {
|
|||
FetchOAuth2ConfigsResponse,
|
||||
OAuth2Config,
|
||||
RequiredKeys,
|
||||
OAuth2ConfigResponse,
|
||||
} from "@budibase/types"
|
||||
import sdk from "../../sdk"
|
||||
|
||||
function toFetchOAuth2ConfigsResponse(
|
||||
config: OAuth2Config
|
||||
): OAuth2ConfigResponse {
|
||||
return {
|
||||
id: config.id,
|
||||
name: config.name,
|
||||
url: config.url,
|
||||
clientId: config.clientId,
|
||||
clientSecret: config.clientSecret,
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetch(ctx: Ctx<void, FetchOAuth2ConfigsResponse>) {
|
||||
const configs = await sdk.oauth2.fetch()
|
||||
|
||||
const response: FetchOAuth2ConfigsResponse = {
|
||||
configs: (configs || []).map(c => ({
|
||||
id: c.id,
|
||||
name: c.name,
|
||||
url: c.url,
|
||||
})),
|
||||
configs: (configs || []).map(toFetchOAuth2ConfigsResponse),
|
||||
}
|
||||
ctx.body = response
|
||||
}
|
||||
|
@ -28,13 +37,15 @@ export async function create(
|
|||
const newConfig: RequiredKeys<Omit<OAuth2Config, "id">> = {
|
||||
name: body.name,
|
||||
url: body.url,
|
||||
clientId: ctx.clientId,
|
||||
clientSecret: ctx.clientSecret,
|
||||
clientId: body.clientId,
|
||||
clientSecret: body.clientSecret,
|
||||
}
|
||||
|
||||
const config = await sdk.oauth2.create(newConfig)
|
||||
ctx.status = 201
|
||||
ctx.body = { config }
|
||||
ctx.body = {
|
||||
config: toFetchOAuth2ConfigsResponse(config),
|
||||
}
|
||||
}
|
||||
|
||||
export async function edit(
|
||||
|
@ -50,7 +61,9 @@ export async function edit(
|
|||
}
|
||||
|
||||
const config = await sdk.oauth2.update(toUpdate)
|
||||
ctx.body = { config }
|
||||
ctx.body = {
|
||||
config: toFetchOAuth2ConfigsResponse(config),
|
||||
}
|
||||
}
|
||||
|
||||
export async function remove(
|
||||
|
|
|
@ -34,6 +34,29 @@ describe("/oauth2", () => {
|
|||
configs: [],
|
||||
})
|
||||
})
|
||||
|
||||
it("returns all created configs", async () => {
|
||||
const existingConfigs = []
|
||||
for (let i = 0; i < 10; i++) {
|
||||
const oauth2Config = makeOAuth2Config()
|
||||
const result = await config.api.oauth2.create(oauth2Config)
|
||||
existingConfigs.push({ ...oauth2Config, id: result.config.id })
|
||||
}
|
||||
|
||||
const response = await config.api.oauth2.fetch()
|
||||
expect(response.configs).toHaveLength(existingConfigs.length)
|
||||
expect(response).toEqual({
|
||||
configs: expect.arrayContaining(
|
||||
existingConfigs.map(c => ({
|
||||
id: c.id,
|
||||
name: c.name,
|
||||
url: c.url,
|
||||
clientId: c.clientId,
|
||||
clientSecret: c.clientSecret,
|
||||
}))
|
||||
),
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("create", () => {
|
||||
|
@ -48,6 +71,8 @@ describe("/oauth2", () => {
|
|||
id: expectOAuth2ConfigId,
|
||||
name: oauth2Config.name,
|
||||
url: oauth2Config.url,
|
||||
clientId: oauth2Config.clientId,
|
||||
clientSecret: oauth2Config.clientSecret,
|
||||
},
|
||||
],
|
||||
})
|
||||
|
@ -65,11 +90,15 @@ describe("/oauth2", () => {
|
|||
id: expectOAuth2ConfigId,
|
||||
name: oauth2Config.name,
|
||||
url: oauth2Config.url,
|
||||
clientId: oauth2Config.clientId,
|
||||
clientSecret: oauth2Config.clientSecret,
|
||||
},
|
||||
{
|
||||
id: expectOAuth2ConfigId,
|
||||
name: oauth2Config2.name,
|
||||
url: oauth2Config2.url,
|
||||
clientId: oauth2Config2.clientId,
|
||||
clientSecret: oauth2Config2.clientSecret,
|
||||
},
|
||||
])
|
||||
expect(response.configs[0].id).not.toEqual(response.configs[1].id)
|
||||
|
@ -93,6 +122,8 @@ describe("/oauth2", () => {
|
|||
id: expectOAuth2ConfigId,
|
||||
name: oauth2Config.name,
|
||||
url: oauth2Config.url,
|
||||
clientId: oauth2Config.clientId,
|
||||
clientSecret: oauth2Config.clientSecret,
|
||||
},
|
||||
])
|
||||
})
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
export interface OAuth2ConfigResponse {
|
||||
id: string
|
||||
name: string
|
||||
url: string
|
||||
clientId: string
|
||||
clientSecret: string
|
||||
}
|
||||
|
||||
export interface FetchOAuth2ConfigsResponse {
|
||||
|
|
Loading…
Reference in New Issue