chore: import upstream snapshot with attribution
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
/**
|
||||
* @vitest-environment node
|
||||
*/
|
||||
|
||||
import crypto from 'node:crypto'
|
||||
import { NextRequest } from 'next/server'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
const { mockEnqueue, mockExecuteTikTokWebhookIngress, mockRelease } = vi.hoisted(() => ({
|
||||
mockEnqueue: vi.fn(),
|
||||
mockExecuteTikTokWebhookIngress: vi.fn(),
|
||||
mockRelease: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/background/tiktok-webhook-ingress', () => ({
|
||||
executeTikTokWebhookIngress: mockExecuteTikTokWebhookIngress,
|
||||
TIKTOK_WEBHOOK_INGRESS_CONCURRENCY_LIMIT: 50,
|
||||
TIKTOK_WEBHOOK_INGRESS_MAX_ATTEMPTS: 3,
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/core/admission/gate', () => ({
|
||||
admissionRejectedResponse: vi.fn(() => new Response(null, { status: 503 })),
|
||||
tryAdmit: vi.fn(() => ({ release: mockRelease })),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/core/async-jobs', () => ({
|
||||
getJobQueue: vi.fn(async () => ({ enqueue: mockEnqueue })),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/core/config/env', () => ({
|
||||
env: {
|
||||
TIKTOK_CLIENT_ID: 'client-key',
|
||||
TIKTOK_CLIENT_SECRET: 'client-secret',
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/core/utils/request', () => ({
|
||||
generateRequestId: vi.fn(() => 'request-1'),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/core/utils/with-route-handler', () => ({
|
||||
withRouteHandler:
|
||||
(handler: (request: NextRequest) => Promise<Response>) => (request: NextRequest) =>
|
||||
handler(request),
|
||||
}))
|
||||
|
||||
import { POST } from '@/app/api/webhooks/tiktok/route'
|
||||
|
||||
function signedRequest(overrides?: { clientKey?: string }): NextRequest {
|
||||
const body = JSON.stringify({
|
||||
client_key: overrides?.clientKey ?? 'client-key',
|
||||
event: 'post.publish.complete',
|
||||
create_time: 1_725_000_000,
|
||||
user_openid: 'act.user',
|
||||
content: '{"publish_id":"publish-1"}',
|
||||
})
|
||||
const timestamp = String(Math.floor(Date.now() / 1000))
|
||||
const signature = crypto
|
||||
.createHmac('sha256', 'client-secret')
|
||||
.update(`${timestamp}.${body}`, 'utf8')
|
||||
.digest('hex')
|
||||
|
||||
return new NextRequest('http://localhost/api/webhooks/tiktok', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
'TikTok-Signature': `t=${timestamp},s=${signature}`,
|
||||
},
|
||||
body,
|
||||
})
|
||||
}
|
||||
|
||||
describe('TikTok webhook ingress route', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
mockEnqueue.mockResolvedValue('ingress-job-1')
|
||||
})
|
||||
|
||||
it('returns 200 only after the verified delivery is accepted by the job queue', async () => {
|
||||
const response = await POST(signedRequest())
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
await expect(response.json()).resolves.toEqual({ ok: true })
|
||||
expect(mockEnqueue).toHaveBeenCalledWith(
|
||||
'tiktok-webhook-ingress',
|
||||
expect.objectContaining({
|
||||
envelope: expect.objectContaining({
|
||||
client_key: 'client-key',
|
||||
user_openid: 'act.user',
|
||||
}),
|
||||
requestId: 'request-1',
|
||||
}),
|
||||
expect.objectContaining({
|
||||
maxAttempts: 3,
|
||||
runner: expect.any(Function),
|
||||
})
|
||||
)
|
||||
expect(mockRelease).toHaveBeenCalledOnce()
|
||||
})
|
||||
|
||||
it('returns 503 when durable acceptance fails so TikTok retries', async () => {
|
||||
mockEnqueue.mockRejectedValue(new Error('queue unavailable'))
|
||||
|
||||
const response = await POST(signedRequest())
|
||||
|
||||
expect(response.status).toBe(503)
|
||||
expect(mockRelease).toHaveBeenCalledOnce()
|
||||
})
|
||||
|
||||
it('rejects a signed delivery for a different TikTok app', async () => {
|
||||
const response = await POST(signedRequest({ clientKey: 'other-client-key' }))
|
||||
|
||||
expect(response.status).toBe(401)
|
||||
expect(mockEnqueue).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user