Delete endpoint

This commit is contained in:
Adria Navarro 2025-03-17 14:00:12 +01:00
parent 2b6b91fe18
commit d3f082976c
5 changed files with 69 additions and 2 deletions

View File

@ -52,3 +52,12 @@ export async function edit(
const config = await sdk.oauth2.update(toUpdate)
ctx.body = { config }
}
export async function remove(
ctx: Ctx<UpsertOAuth2ConfigRequest, UpsertOAuth2ConfigResponse>
) {
const configToRemove = ctx.params.id
await sdk.oauth2.remove(configToRemove)
ctx.status = 204
}

View File

@ -18,5 +18,10 @@ router.put(
oAuth2ConfigValidator(),
controller.edit
)
router.delete(
"/api/oauth2/:id",
authorized(PermissionType.BUILDER),
controller.remove
)
export default router

View File

@ -134,7 +134,7 @@ describe("/oauth2", () => {
it("throw if config not found", async () => {
await config.api.oauth2.update("unexisting", makeOAuth2Config(), {
status: 400,
status: 404,
body: { message: "OAuth2 config with id 'unexisting' not found." },
})
})
@ -156,4 +156,35 @@ describe("/oauth2", () => {
)
})
})
describe("delete", () => {
let existingConfigs: OAuth2Config[] = []
beforeEach(async () => {
existingConfigs = []
for (let i = 0; i < 5; i++) {
const oauth2Config = makeOAuth2Config()
const result = await config.api.oauth2.create(oauth2Config)
existingConfigs.push({ ...oauth2Config, id: result.config.id })
}
})
it("can delete an existing configuration", async () => {
const { id: configId } = _.sample(existingConfigs)!
await config.api.oauth2.delete(configId, { status: 204 })
const response = await config.api.oauth2.fetch()
expect(response.configs).toHaveLength(existingConfigs.length - 1)
expect(response.configs.find(c => c.id === configId)).toBeUndefined()
})
it("throw if config not found", async () => {
await config.api.oauth2.delete("unexisting", {
status: 404,
body: { message: "OAuth2 config with id 'unexisting' not found." },
})
})
})
})

View File

@ -57,7 +57,7 @@ export async function update(config: OAuth2Config): Promise<OAuth2Config> {
}
if (!doc.configs[config.id]) {
throw new HTTPError(`OAuth2 config with id '${config.id}' not found.`, 400)
throw new HTTPError(`OAuth2 config with id '${config.id}' not found.`, 404)
}
if (
@ -75,3 +75,19 @@ export async function update(config: OAuth2Config): Promise<OAuth2Config> {
await db.put(doc)
return doc.configs[config.id]
}
export async function remove(configId: string): Promise<void> {
const db = context.getAppDB()
const doc: OAuth2Configs = (await getDocument(db)) ?? {
_id: DocumentType.OAUTH2_CONFIG,
configs: {},
}
if (!doc.configs[configId]) {
throw new HTTPError(`OAuth2 config with id '${configId}' not found.`, 404)
}
delete doc.configs[configId]
await db.put(doc)
}

View File

@ -35,4 +35,10 @@ export class OAuth2API extends TestAPI {
expectations,
})
}
delete = async (id: string, expectations?: Expectations) => {
return await this._delete<void>(`/api/oauth2/${id}`, {
expectations,
})
}
}