diff --git a/packages/server/src/api/controllers/oauth2.ts b/packages/server/src/api/controllers/oauth2.ts index 37b22853de..798a9151c1 100644 --- a/packages/server/src/api/controllers/oauth2.ts +++ b/packages/server/src/api/controllers/oauth2.ts @@ -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) { 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> = { 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( diff --git a/packages/server/src/api/routes/tests/oauth2.spec.ts b/packages/server/src/api/routes/tests/oauth2.spec.ts index 9ea3c3d348..218d1d407d 100644 --- a/packages/server/src/api/routes/tests/oauth2.spec.ts +++ b/packages/server/src/api/routes/tests/oauth2.spec.ts @@ -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, }, ]) }) diff --git a/packages/types/src/api/web/app/oauth2.ts b/packages/types/src/api/web/app/oauth2.ts index 050cdf67fd..1790676c0d 100644 --- a/packages/types/src/api/web/app/oauth2.ts +++ b/packages/types/src/api/web/app/oauth2.ts @@ -1,6 +1,9 @@ export interface OAuth2ConfigResponse { id: string name: string + url: string + clientId: string + clientSecret: string } export interface FetchOAuth2ConfigsResponse {