xuất excel
This commit is contained in:
@@ -3,6 +3,7 @@ const assert = require('node:assert/strict');
|
||||
const { randomBytes } = require('node:crypto');
|
||||
const sql = require('mssql');
|
||||
const { ensureAssetArchiveSchema } = require('../backend/asset-archive-schema');
|
||||
const { ensureAssetExportDocumentSchema } = require('../backend/asset-export-documents');
|
||||
|
||||
// Explicit opt-in to a disposable, local SQL Server. Never read .env or reuse the
|
||||
// application's database configuration. The runner creates its own database.
|
||||
@@ -99,6 +100,37 @@ async function historySnapshot(pool) {
|
||||
`)).recordsets;
|
||||
}
|
||||
|
||||
test('SQL Server document migration preserves legacy rows and saved files across repeated startup and archiving', integrationOptions, async () => {
|
||||
await withTestDatabase(async pool => {
|
||||
await pool.request().batch(legacySchema);
|
||||
await pool.request().query(`
|
||||
INSERT dbo.AssetInventory (AssetCode, AssetName) VALUES ('TEST', N'Test asset');
|
||||
INSERT dbo.AssetExportHistory (AssetId, ExportQuantity) VALUES (1, 1);
|
||||
`);
|
||||
await ensureAssetArchiveSchema(pool);
|
||||
await ensureAssetExportDocumentSchema(pool);
|
||||
const oldRow = (await pool.request().query('SELECT * FROM dbo.AssetExportHistory')).recordset[0];
|
||||
assert.equal(oldRow.ExportDocumentId, null);
|
||||
const bytes = Buffer.from('original TEST workbook bytes');
|
||||
const documentId = 'f13b142b-913b-44b8-b15c-d719bd45d197';
|
||||
await pool.request().input('id', sql.UniqueIdentifier, documentId).input('bytes', sql.VarBinary(sql.MAX), bytes).query(`
|
||||
INSERT dbo.AssetExportDocuments (ExportDocumentId, FileName, MimeType, FileContent, SnapshotJson, ExportedDate)
|
||||
VALUES (@id, N'TEST.xlsx', N'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
@bytes, N'{"items":[]}', DATEADD(HOUR, 7, SYSUTCDATETIME()));
|
||||
INSERT dbo.AssetExportHistory (AssetId, ExportQuantity, ExportDocumentId) VALUES (1, 2, @id);
|
||||
UPDATE dbo.AssetInventory SET IsArchived = 1, AssetName = N'Changed asset' WHERE AssetId = 1;
|
||||
`);
|
||||
await ensureAssetArchiveSchema(pool);
|
||||
await ensureAssetExportDocumentSchema(pool);
|
||||
const saved = (await pool.request().query('SELECT * FROM dbo.AssetExportDocuments')).recordset;
|
||||
assert.equal(saved.length, 1);
|
||||
assert.deepEqual(saved[0].FileContent, bytes);
|
||||
assert.equal(saved[0].SnapshotJson, '{"items":[]}');
|
||||
assert.equal((await pool.request().query('SELECT COUNT(*) AS total FROM dbo.AssetExportHistory')).recordset[0].total, 2);
|
||||
await assert.rejects(pool.request().query('DELETE FROM dbo.AssetExportDocuments'), error => error.number === 547);
|
||||
});
|
||||
});
|
||||
|
||||
test('SQL Server migration preserves asset history across inventory replacement', integrationOptions, async t => {
|
||||
await withTestDatabase(async pool => {
|
||||
await pool.request().batch(legacySchema);
|
||||
|
||||
@@ -9,7 +9,7 @@ const serverRequire = createRequire(serverPath);
|
||||
|
||||
// Run the real route handlers with an isolated pool so these regressions do not
|
||||
// need credentials or alter a developer's inventory.
|
||||
function createFixture(answer = () => ({ recordset: [] })) {
|
||||
function createFixture(answer = () => ({ recordset: [] }), { workbookBuilder, userDisplayName = 'Borrower A' } = {}) {
|
||||
const queries = [];
|
||||
const transactions = [];
|
||||
class Request {
|
||||
@@ -36,7 +36,14 @@ function createFixture(answer = () => ({ recordset: [] })) {
|
||||
const sql = { ...serverRequire('mssql'), Request, Transaction };
|
||||
const module = { exports: {} };
|
||||
const context = {
|
||||
require(name) { return name === 'mssql' ? sql : serverRequire(name); },
|
||||
require(name) {
|
||||
if (name === 'mssql') return sql;
|
||||
if (name === 'dotenv') return { config() {} };
|
||||
if (name === './export-request-workbook' && workbookBuilder) {
|
||||
return { buildExportRequestWorkbook: workbookBuilder };
|
||||
}
|
||||
return serverRequire(name);
|
||||
},
|
||||
module,
|
||||
exports: module.exports,
|
||||
__dirname: path.dirname(serverPath),
|
||||
@@ -52,11 +59,12 @@ function createFixture(answer = () => ({ recordset: [] })) {
|
||||
},
|
||||
on() {}
|
||||
},
|
||||
testPool: { request: () => new Request() }
|
||||
testPool: { request: () => new Request() },
|
||||
testDisplayName: userDisplayName
|
||||
};
|
||||
vm.runInNewContext(`${serverSource}
|
||||
pool = testPool;
|
||||
getUserDisplayNameById = async () => 'Borrower A';
|
||||
getUserDisplayNameById = async () => testDisplayName;
|
||||
safeCreateSystemNotification = async () => {};
|
||||
ensureDepartmentExists = async () => {};
|
||||
syncAssetDepartmentsFromInventory = async () => {};
|
||||
@@ -75,6 +83,11 @@ function createFixture(answer = () => ({ recordset: [] })) {
|
||||
};
|
||||
const response = {
|
||||
statusCode: 200,
|
||||
headers: {},
|
||||
setHeader(name, value) { this.headers[name] = value; return this; },
|
||||
type(value) { this.headers['Content-Type'] = value; return this; },
|
||||
attachment(value) { this.fileName = value; return this; },
|
||||
send(value) { this.body = value; return this; },
|
||||
status(value) { this.statusCode = value; return this; },
|
||||
json(value) { this.body = value; return this; }
|
||||
};
|
||||
|
||||
@@ -121,6 +121,19 @@ test('forged legacy identity headers cannot bypass protected APIs', async () =>
|
||||
});
|
||||
});
|
||||
|
||||
test('saved export files, preview and batch exports require an authenticated session', async () => {
|
||||
await withTestServer(async baseUrl => {
|
||||
for (const [route, method] of [
|
||||
['/api/asset-export-documents/f13b142b-913b-44b8-b15c-d719bd45d197', 'GET'],
|
||||
['/api/asset-export-documents/f13b142b-913b-44b8-b15c-d719bd45d197/download', 'GET'],
|
||||
['/api/assets/export', 'POST']
|
||||
]) {
|
||||
const response = await fetch(baseUrl + route, { method, headers: { 'x-user-role': 'admin' } });
|
||||
assert.equal(response.status, 401);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test('security headers and no-store API caching are enabled', async () => {
|
||||
await withTestServer(async baseUrl => {
|
||||
const response = await fetch(`${baseUrl}/api/health`);
|
||||
|
||||
Reference in New Issue
Block a user