chore: sincronizar cambios pendientes de desarrollo a produccion
This commit is contained in:
@@ -108,6 +108,7 @@ test('GET /api/trips/active devuelve viaje activo', async () => {
|
||||
cod_viaje: 'AB-2024-001',
|
||||
n_proveedor: 1,
|
||||
id_estado: 4,
|
||||
statusId: 4,
|
||||
estado: 'CARGA DE MERCANCÍA',
|
||||
estado_en: 'CARGO LOADING',
|
||||
nombrea: 'ORIGEN',
|
||||
@@ -122,13 +123,19 @@ test('GET /api/trips/active devuelve viaje activo', async () => {
|
||||
};
|
||||
|
||||
db.query = async (sql, params) => {
|
||||
assert.match(sql, /FROM c_viajes_proveedor p/);
|
||||
assert.match(sql, /INNER JOIN t_viaje_estados e/);
|
||||
assert.match(sql, /fecha_salida/);
|
||||
assert.match(sql, /fecha_llegada/);
|
||||
assert.match(sql, /INNER JOIN m_proveedores_trasportistas t/);
|
||||
assert.deepEqual(params, ['58045340X', 2, 6]);
|
||||
return [[mockedTrip]];
|
||||
if (/FROM c_viajes_proveedor p/.test(sql)) {
|
||||
assert.match(sql, /INNER JOIN t_viaje_estados e/);
|
||||
assert.match(sql, /fecha_salida/);
|
||||
assert.match(sql, /fecha_llegada/);
|
||||
assert.match(sql, /INNER JOIN m_proveedores_trasportistas t/);
|
||||
assert.deepEqual(params, ['58045340X', 2, 6, '58045340X', 1]);
|
||||
return [[mockedTrip]];
|
||||
}
|
||||
|
||||
assert.match(sql, /FROM c_cambios_estado c/);
|
||||
assert.match(sql, /FROM c_viajes_puntos vp/);
|
||||
assert.deepEqual(params, [123, 123, 3, 4, 5]);
|
||||
return [[]];
|
||||
};
|
||||
|
||||
const response = await withServer(async (server) =>
|
||||
|
||||
@@ -540,9 +540,17 @@ test('POST /api/trips/:id/status multipart registra body parseado y ficheros en
|
||||
);
|
||||
|
||||
assert.equal(response.statusCode, 200);
|
||||
await waitFor(() => fs.existsSync(TEST_POSTS_LOG_PATH));
|
||||
await waitFor(() =>
|
||||
fs.existsSync(TEST_POSTS_LOG_PATH) &&
|
||||
readJsonLines(TEST_POSTS_LOG_PATH).some(
|
||||
(logEntry) => logEntry.body?.id_estado === '5'
|
||||
)
|
||||
);
|
||||
|
||||
const [entry] = readJsonLines(TEST_POSTS_LOG_PATH);
|
||||
const entry = readJsonLines(TEST_POSTS_LOG_PATH).find(
|
||||
(logEntry) => logEntry.body?.id_estado === '5'
|
||||
);
|
||||
assert.ok(entry, 'Expected parsed multipart payload log entry');
|
||||
assert.equal(entry.method, 'POST');
|
||||
assert.equal(entry.path, '/api/trips/248230/status');
|
||||
assert.equal(entry.body.id_estado, '5');
|
||||
|
||||
@@ -0,0 +1,221 @@
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const http = require('node:http');
|
||||
const path = require('node:path');
|
||||
const test = require('node:test');
|
||||
const jwt = require('jsonwebtoken');
|
||||
|
||||
const TEST_LOG_PATH = path.resolve(__dirname, '..', 'tmp', 'test-upload-diagnostics.log');
|
||||
process.env.POSTS_LOG_PATH = TEST_LOG_PATH;
|
||||
|
||||
const app = require('../app');
|
||||
|
||||
const JWT_SECRET = process.env.JWT_SECRET || 'test-jwt-secret';
|
||||
process.env.JWT_SECRET = JWT_SECRET;
|
||||
|
||||
const createToken = () =>
|
||||
jwt.sign(
|
||||
{
|
||||
id: 1,
|
||||
dni: '58045340X',
|
||||
id_proveedor: 675
|
||||
},
|
||||
JWT_SECRET,
|
||||
{ expiresIn: '1h' }
|
||||
);
|
||||
|
||||
const withServer = async (callback) =>
|
||||
new Promise((resolve, reject) => {
|
||||
const server = app.listen(0, '127.0.0.1');
|
||||
|
||||
server.on('error', reject);
|
||||
server.on('listening', async () => {
|
||||
try {
|
||||
const result = await callback(server);
|
||||
server.close((closeError) => {
|
||||
if (closeError) {
|
||||
reject(closeError);
|
||||
return;
|
||||
}
|
||||
resolve(result);
|
||||
});
|
||||
} catch (error) {
|
||||
server.close(() => reject(error));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
const requestMultipart = async ({ port, authorization, fields = {}, file }) =>
|
||||
new Promise((resolve, reject) => {
|
||||
const boundary = `----UploadDiagnostics${Date.now().toString(16)}`;
|
||||
const chunks = [];
|
||||
|
||||
for (const [key, value] of Object.entries(fields)) {
|
||||
chunks.push(Buffer.from(`--${boundary}\r\n`));
|
||||
chunks.push(
|
||||
Buffer.from(`Content-Disposition: form-data; name="${key}"\r\n\r\n${String(value)}\r\n`)
|
||||
);
|
||||
}
|
||||
|
||||
if (file) {
|
||||
chunks.push(Buffer.from(`--${boundary}\r\n`));
|
||||
chunks.push(
|
||||
Buffer.from(
|
||||
`Content-Disposition: form-data; name="${file.fieldName}"; filename="${file.filename}"\r\n` +
|
||||
`Content-Type: ${file.contentType}\r\n\r\n`
|
||||
)
|
||||
);
|
||||
chunks.push(file.content);
|
||||
chunks.push(Buffer.from('\r\n'));
|
||||
}
|
||||
|
||||
chunks.push(Buffer.from(`--${boundary}--\r\n`));
|
||||
const bodyBuffer = Buffer.concat(chunks);
|
||||
const request = http.request(
|
||||
{
|
||||
hostname: '127.0.0.1',
|
||||
port,
|
||||
method: 'POST',
|
||||
path: '/update_profile_photo',
|
||||
headers: {
|
||||
...(authorization ? { authorization } : {}),
|
||||
'Content-Type': `multipart/form-data; boundary=${boundary}`,
|
||||
'Content-Length': bodyBuffer.length
|
||||
}
|
||||
},
|
||||
(response) => {
|
||||
let responseBody = '';
|
||||
|
||||
response.on('data', (chunk) => {
|
||||
responseBody += chunk;
|
||||
});
|
||||
response.on('end', () => {
|
||||
resolve({
|
||||
statusCode: response.statusCode,
|
||||
headers: response.headers,
|
||||
body: responseBody ? JSON.parse(responseBody) : null
|
||||
});
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
request.on('error', reject);
|
||||
request.end(bodyBuffer);
|
||||
});
|
||||
|
||||
const readLogEntries = () => {
|
||||
if (!fs.existsSync(TEST_LOG_PATH)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return fs
|
||||
.readFileSync(TEST_LOG_PATH, 'utf8')
|
||||
.split('\n')
|
||||
.map((line) => line.trim())
|
||||
.filter(Boolean)
|
||||
.map((line) => JSON.parse(line));
|
||||
};
|
||||
|
||||
const waitForFinishedLog = async () => {
|
||||
const startedAt = Date.now();
|
||||
|
||||
while (Date.now() - startedAt < 1500) {
|
||||
const finished = readLogEntries().find(
|
||||
(entry) => entry.event === 'upload_request_finished'
|
||||
);
|
||||
if (finished) {
|
||||
return finished;
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, 25));
|
||||
}
|
||||
|
||||
throw new Error('Timed out waiting for upload diagnostics log');
|
||||
};
|
||||
|
||||
test.beforeEach(() => {
|
||||
fs.rmSync(TEST_LOG_PATH, { force: true });
|
||||
});
|
||||
|
||||
test.after(() => {
|
||||
delete process.env.POSTS_LOG_PATH;
|
||||
fs.rmSync(TEST_LOG_PATH, { force: true });
|
||||
});
|
||||
|
||||
test('multipart sin autenticacion identifica fallo antes del parser', async () => {
|
||||
const response = await withServer((server) =>
|
||||
requestMultipart({
|
||||
port: server.address().port,
|
||||
file: {
|
||||
fieldName: 'foto_perfil',
|
||||
filename: 'profile.jpg',
|
||||
contentType: 'image/jpeg',
|
||||
content: Buffer.from('jpeg')
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
assert.equal(response.statusCode, 403);
|
||||
const finished = await waitForFinishedLog();
|
||||
const started = readLogEntries().find(
|
||||
(entry) => entry.event === 'upload_request_started'
|
||||
);
|
||||
|
||||
assert.equal(finished.request_id, response.headers['x-request-id']);
|
||||
assert.equal(started.request_id, finished.request_id);
|
||||
assert.equal(finished.failure_stage, 'authentication_rate_limit_or_route');
|
||||
assert.equal(finished.upload.parser_status, 'not_reached');
|
||||
});
|
||||
|
||||
test('tipo de archivo invalido identifica rechazo del parser multipart', async () => {
|
||||
const token = createToken();
|
||||
const response = await withServer((server) =>
|
||||
requestMultipart({
|
||||
port: server.address().port,
|
||||
authorization: `Bearer ${token}`,
|
||||
file: {
|
||||
fieldName: 'foto_perfil',
|
||||
filename: 'profile.txt',
|
||||
contentType: 'text/plain',
|
||||
content: Buffer.from('not-an-image')
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
assert.equal(response.statusCode, 400);
|
||||
const finished = await waitForFinishedLog();
|
||||
|
||||
assert.equal(finished.failure_stage, 'multipart_parser');
|
||||
assert.equal(finished.upload.flow, 'profile_photo');
|
||||
assert.equal(finished.upload.parser_status, 'rejected');
|
||||
assert.equal(finished.upload.error.code, 'INVALID_FILE_TYPE');
|
||||
assert.equal(JSON.stringify(readLogEntries()).includes(token), false);
|
||||
assert.equal(JSON.stringify(readLogEntries()).includes('profile.txt'), false);
|
||||
});
|
||||
|
||||
test('payload invalido tras guardar el archivo identifica fallo posterior al parser', async () => {
|
||||
const response = await withServer((server) =>
|
||||
requestMultipart({
|
||||
port: server.address().port,
|
||||
authorization: `Bearer ${createToken()}`,
|
||||
fields: {
|
||||
id_proveedor: 675
|
||||
},
|
||||
file: {
|
||||
fieldName: 'foto_perfil',
|
||||
filename: 'profile.jpg',
|
||||
contentType: 'image/jpeg',
|
||||
content: Buffer.from('jpeg')
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
assert.equal(response.statusCode, 400);
|
||||
const finished = await waitForFinishedLog();
|
||||
|
||||
assert.equal(finished.failure_stage, 'controller_or_persistence');
|
||||
assert.equal(finished.upload.parser_status, 'parsed');
|
||||
assert.deepEqual(finished.upload.body_fields, ['id_proveedor']);
|
||||
assert.equal(finished.upload.file_count, 1);
|
||||
assert.equal(finished.upload.files[0].original_extension, '.jpg');
|
||||
assert.equal(finished.upload.files[0].local_file_created, true);
|
||||
});
|
||||
Reference in New Issue
Block a user