Files
gongxue-base/scripts/disable-legacy-sms-providers-test.js
2026-07-12 19:26:57 +08:00

70 lines
2.4 KiB
JavaScript

import assert from 'node:assert/strict';
import {
buildConfig,
disableLegacySmsProviders,
findLegacySmsProviderRows,
} from './disable-legacy-sms-providers.js';
const tenantId = '00000000-0000-0000-0000-000000000001';
const localDatabaseUrl = 'postgresql://postgres:postgres@127.0.0.1:54322/postgres';
const rows = [
{ id: 'legacy-aliyun', tenantId, provider: 'aliyun', status: 'active', displayName: '旧阿里云短信' },
{ id: 'legacy-tencent', tenantId, provider: 'tencent-sms', status: 'testing', displayName: '旧腾讯云短信' },
{ id: 'disabled-aliyun', tenantId, provider: 'aliyun-sms', status: 'disabled', displayName: '已停用阿里云短信' },
{ id: 'pnvs', tenantId, provider: 'aliyun-pnvs', status: 'active', displayName: '阿里云短信认证' },
];
function queryFixture() {
return async (sql, params) => {
assert.equal(params[0], tenantId);
if (/select[\s\S]*from public\.tenant_auth_providers/i.test(sql)) {
return {
rows: rows.filter(row =>
params[1].includes(String(row.provider).toLowerCase()) &&
['active', 'testing'].includes(row.status),
),
};
}
throw new Error(`Unexpected SQL: ${sql}`);
};
}
const cfg = buildConfig(
{ DATABASE_URL: localDatabaseUrl, PNVS_TENANT_ID: tenantId },
[],
);
assert.equal(cfg.apply, false);
assert.equal(cfg.tenantId, tenantId);
const applyCfg = buildConfig(
{ DATABASE_URL: localDatabaseUrl, PNVS_TENANT_ID: tenantId },
['--apply'],
);
assert.equal(applyCfg.apply, true);
const found = await findLegacySmsProviderRows(queryFixture(), tenantId);
assert.deepEqual(found.map(row => row.id), ['legacy-aliyun', 'legacy-tencent']);
const dryRun = await disableLegacySmsProviders(
{ databaseUrl: localDatabaseUrl, tenantId, apply: false },
{ query: queryFixture() },
);
assert.equal(dryRun.dryRun, true);
assert.equal(dryRun.changed, 0);
assert.deepEqual(dryRun.rows.map(row => row.id), ['legacy-aliyun', 'legacy-tencent']);
const applyRun = await disableLegacySmsProviders(
{ databaseUrl: localDatabaseUrl, tenantId, apply: true },
{ query: queryFixture() },
);
assert.equal(applyRun.dryRun, false);
assert.equal(applyRun.changed, 2);
assert.deepEqual(applyRun.rows.map(row => row.id), ['legacy-aliyun', 'legacy-tencent']);
assert.throws(
() => buildConfig({ PNVS_TENANT_ID: tenantId }, []),
/Missing required env: DATABASE_URL/,
);
console.log('[PASS] disable legacy SMS providers script');