|
| 1 | +jest.mock('@internal/database/migrations', () => { |
| 2 | + const actual = jest.requireActual('@internal/database/migrations') |
| 3 | + return { |
| 4 | + ...actual, |
| 5 | + resetMigrationsOnTenants: jest.fn(), |
| 6 | + resetMigration: jest.fn(), |
| 7 | + runMigrationsOnAllTenants: jest.fn(), |
| 8 | + runMigrationsOnTenant: jest.fn(), |
| 9 | + } |
| 10 | +}) |
| 11 | + |
| 12 | +import * as migrations from '@internal/database/migrations' |
| 13 | +import { DBMigration } from '@internal/database/migrations' |
| 14 | +import { mergeConfig } from '../config' |
| 15 | +import { multitenantKnex } from '../internal/database/multitenant-db' |
| 16 | + |
| 17 | +mergeConfig({ |
| 18 | + pgQueueEnable: true, |
| 19 | +}) |
| 20 | + |
| 21 | +import { adminApp } from './common' |
| 22 | + |
| 23 | +const tenantId = 'admin-migrations-test-tenant' |
| 24 | + |
| 25 | +const tenantPayload = { |
| 26 | + anonKey: 'anon-key', |
| 27 | + databaseUrl: 'postgres://tenant-db', |
| 28 | + jwtSecret: 'jwt-secret', |
| 29 | + serviceKey: 'service-key', |
| 30 | +} |
| 31 | + |
| 32 | +describe('Admin migrations routes', () => { |
| 33 | + beforeAll(async () => { |
| 34 | + await migrations.runMultitenantMigrations() |
| 35 | + }) |
| 36 | + |
| 37 | + afterEach(async () => { |
| 38 | + jest.clearAllMocks() |
| 39 | + |
| 40 | + await adminApp.inject({ |
| 41 | + method: 'DELETE', |
| 42 | + url: `/tenants/${tenantId}`, |
| 43 | + headers: { |
| 44 | + apikey: process.env.ADMIN_API_KEYS, |
| 45 | + }, |
| 46 | + }) |
| 47 | + }) |
| 48 | + |
| 49 | + afterAll(async () => { |
| 50 | + await multitenantKnex.destroy() |
| 51 | + }) |
| 52 | + |
| 53 | + test('rejects invalid markCompletedTillMigration for fleet reset', async () => { |
| 54 | + const resetSpy = jest.mocked(migrations.resetMigrationsOnTenants).mockResolvedValue(undefined) |
| 55 | + |
| 56 | + const response = await adminApp.inject({ |
| 57 | + method: 'POST', |
| 58 | + url: '/migrations/reset/fleet', |
| 59 | + payload: { |
| 60 | + untilMigration: 'storage-schema' satisfies keyof typeof DBMigration, |
| 61 | + markCompletedTillMigration: 'not-a-real-migration', |
| 62 | + }, |
| 63 | + headers: { |
| 64 | + apikey: process.env.ADMIN_API_KEYS, |
| 65 | + }, |
| 66 | + }) |
| 67 | + |
| 68 | + expect(response.statusCode).toBe(400) |
| 69 | + expect(JSON.parse(response.body)).toEqual({ message: 'Invalid migration' }) |
| 70 | + expect(resetSpy).not.toHaveBeenCalled() |
| 71 | + }) |
| 72 | + |
| 73 | + test('rejects invalid markCompletedTillMigration for tenant reset', async () => { |
| 74 | + await adminApp.inject({ |
| 75 | + method: 'POST', |
| 76 | + url: `/tenants/${tenantId}`, |
| 77 | + payload: tenantPayload, |
| 78 | + headers: { |
| 79 | + apikey: process.env.ADMIN_API_KEYS, |
| 80 | + }, |
| 81 | + }) |
| 82 | + |
| 83 | + const resetSpy = jest.mocked(migrations.resetMigration).mockResolvedValue(false) |
| 84 | + |
| 85 | + const response = await adminApp.inject({ |
| 86 | + method: 'POST', |
| 87 | + url: `/tenants/${tenantId}/migrations/reset`, |
| 88 | + payload: { |
| 89 | + untilMigration: 'storage-schema' satisfies keyof typeof DBMigration, |
| 90 | + markCompletedTillMigration: 'not-a-real-migration', |
| 91 | + }, |
| 92 | + headers: { |
| 93 | + apikey: process.env.ADMIN_API_KEYS, |
| 94 | + }, |
| 95 | + }) |
| 96 | + |
| 97 | + expect(response.statusCode).toBe(400) |
| 98 | + expect(JSON.parse(response.body)).toEqual({ message: 'Invalid migration' }) |
| 99 | + expect(resetSpy).not.toHaveBeenCalled() |
| 100 | + }) |
| 101 | + |
| 102 | + test('accepts untilMigration that maps to numeric id 0 for fleet reset', async () => { |
| 103 | + const resetSpy = jest.mocked(migrations.resetMigrationsOnTenants).mockResolvedValue(undefined) |
| 104 | + |
| 105 | + const response = await adminApp.inject({ |
| 106 | + method: 'POST', |
| 107 | + url: '/migrations/reset/fleet', |
| 108 | + payload: { |
| 109 | + untilMigration: 'create-migrations-table' satisfies keyof typeof DBMigration, |
| 110 | + }, |
| 111 | + headers: { |
| 112 | + apikey: process.env.ADMIN_API_KEYS, |
| 113 | + }, |
| 114 | + }) |
| 115 | + |
| 116 | + expect(response.statusCode).toBe(200) |
| 117 | + expect(JSON.parse(response.body)).toEqual({ message: 'Migrations scheduled' }) |
| 118 | + expect(resetSpy).toHaveBeenCalledWith({ |
| 119 | + till: 'create-migrations-table', |
| 120 | + markCompletedTillMigration: undefined, |
| 121 | + signal: expect.any(AbortSignal), |
| 122 | + }) |
| 123 | + }) |
| 124 | +}) |
0 commit comments