Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/storage/events/objects/object-admin-delete-all-before.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Job, SendOptions, WorkOptions } from 'pg-boss'
import { getConfig } from '../../../config'
import { Storage } from '../../index'
import { BaseEvent } from '../base-event'
import { ObjectRemoved } from '../lifecycle/object-removed'

const DELETE_JOB_TIME_LIMIT_MS = 10_000

Expand Down Expand Up @@ -87,6 +88,19 @@ export class ObjectAdminDeleteAllBefore extends BaseEvent<ObjectDeleteAllBeforeE
}

await backend.deleteObjects(storageS3Bucket, prefixes)

await Promise.allSettled(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a test to ensure event is triggerred to prevent regressions?

deleted.map((object) =>
ObjectRemoved.sendWebhook({
tenant: job.data.tenant,
name: object.name,
bucketId,
reqId: job.data.reqId,
version: object.version,
metadata: object.metadata,
})
)
)
}
})
}
Expand Down
88 changes: 87 additions & 1 deletion src/test/webhooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { FastifyInstance } from 'fastify'
import FormData from 'form-data'
import fs from 'fs'
import app from '../app'
import { ObjectAdminDeleteAllBefore } from '../storage/events/objects/object-admin-delete-all-before'
import { mockQueue, useMockObject } from './common'

describe('Webhooks', () => {
Expand Down Expand Up @@ -385,10 +386,95 @@ describe('Webhooks', () => {
})
)
})

it('will emit webhooks for each deleted object during empty bucket operation', async () => {
const emptyTestBucketName = 'bucket-empty-webhook-test'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be better to make it unique as well

const authorization = `Bearer ${await serviceKeyAsync}`

// Create a dedicated bucket for this test
await appInstance.inject({
method: 'POST',
url: `/bucket`,
headers: {
authorization,
},
payload: {
name: emptyTestBucketName,
},
})

const objects = await Promise.all([
createObject(pg, emptyTestBucketName),
createObject(pg, emptyTestBucketName),
createObject(pg, emptyTestBucketName),
])

const response = await appInstance.inject({
method: 'POST',
url: `/bucket/${emptyTestBucketName}/empty`,
headers: {
authorization,
},
})

expect(response.statusCode).toBe(200)

// Pass call invoked by empty on to the job handler to trigger the webhooks
expect(sendSpy).toHaveBeenCalledTimes(1)
const deleteJobCall = sendSpy.mock.calls[0][0]
expect(deleteJobCall.name).toBe(ObjectAdminDeleteAllBefore.queueName)
await ObjectAdminDeleteAllBefore.handle(deleteJobCall)

// Check ObjectRemoved:Delete webhooks were sent as expected
expect(sendSpy).toHaveBeenCalledTimes(1 + objects.length) // 1 for the delete job + 3 for webhooks
objects.forEach((obj) => {
expect(sendSpy).toHaveBeenCalledWith(
expect.objectContaining({
name: 'webhooks',
options: expect.objectContaining({
deadLetter: 'webhooks-dead-letter',
expireInSeconds: expect.any(Number),
}),
data: expect.objectContaining({
$version: 'v1',
event: expect.objectContaining({
$version: 'v1',
type: 'ObjectRemoved:Delete',
applyTime: expect.any(Number),
payload: expect.objectContaining({
bucketId: emptyTestBucketName,
name: obj.name,
version: obj.version,
metadata: obj.metadata,
tenant: {
host: undefined,
ref: 'bjhaohmqunupljrqypxz',
},
reqId: expect.any(String),
}),
}),
tenant: {
host: undefined,
ref: 'bjhaohmqunupljrqypxz',
},
}),
})
)
})

// Clean up: delete the bucket
await appInstance.inject({
method: 'DELETE',
url: `/bucket/${emptyTestBucketName}`,
headers: {
authorization,
},
})
})
})

async function createObject(pg: TenantConnection, bucketId: string) {
const objectName = Date.now()
const objectName = randomUUID()
const tnx = await pg.transaction()

const [data] = await tnx
Expand Down