Files
wehub-resource-sync d25d482dc2
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
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

92 lines
2.6 KiB
TypeScript

/**
* Tests for the Teams subscription renewal cron route.
*
* @vitest-environment node
*/
import {
authOAuthUtilsMock,
createMockRequest,
dbChainMock,
dbChainMockFns,
redisConfigMock,
redisConfigMockFns,
resetDbChainMock,
} from '@sim/testing'
import { sleep } from '@sim/utils/helpers'
import { beforeEach, describe, expect, it, vi } from 'vitest'
const { mockVerifyCronAuth } = vi.hoisted(() => ({
mockVerifyCronAuth: vi.fn().mockReturnValue(null),
}))
vi.mock('@/lib/auth/internal', () => ({
verifyCronAuth: mockVerifyCronAuth,
}))
vi.mock('@/lib/core/config/redis', () => redisConfigMock)
vi.mock('@sim/db', () => dbChainMock)
vi.mock('@/app/api/auth/oauth/utils', () => authOAuthUtilsMock)
import { GET } from './route'
function createRequest() {
return createMockRequest(
'GET',
undefined,
{},
'http://localhost:3000/api/cron/renew-subscriptions'
)
}
const flushMicrotasks = () => sleep(0)
describe('Teams subscription renewal route (fire-and-forget)', () => {
beforeEach(() => {
vi.clearAllMocks()
resetDbChainMock()
redisConfigMockFns.mockAcquireLock.mockResolvedValue(true)
redisConfigMockFns.mockReleaseLock.mockResolvedValue(true)
mockVerifyCronAuth.mockReturnValue(null)
})
it('returns the auth error when cron auth fails', async () => {
mockVerifyCronAuth.mockReturnValueOnce(new Response(null, { status: 401 }) as never)
const response = await GET(createRequest())
expect(response.status).toBe(401)
expect(redisConfigMockFns.mockAcquireLock).not.toHaveBeenCalled()
})
it('acknowledges with 202 and renews in the background after acquiring the lock', async () => {
const response = await GET(createRequest())
expect(response.status).toBe(202)
const data = await response.json()
expect(data).toMatchObject({ status: 'started' })
expect(redisConfigMockFns.mockAcquireLock).toHaveBeenCalledWith(
'teams-subscription-renewal-lock',
expect.any(String),
expect.any(Number)
)
await flushMicrotasks()
expect(dbChainMockFns.select).toHaveBeenCalled()
expect(redisConfigMockFns.mockReleaseLock).toHaveBeenCalledWith(
'teams-subscription-renewal-lock',
expect.any(String)
)
})
it('skips with 202 when the lock is already held', async () => {
redisConfigMockFns.mockAcquireLock.mockResolvedValueOnce(false)
const response = await GET(createRequest())
expect(response.status).toBe(202)
const data = await response.json()
expect(data).toMatchObject({ status: 'skip' })
expect(dbChainMockFns.select).not.toHaveBeenCalled()
})
})