Fix passenger startup and clean tracked generated files
This commit is contained in:
@@ -0,0 +1,173 @@
|
||||
const assert = require('node:assert/strict');
|
||||
const http = require('node:http');
|
||||
const test = require('node:test');
|
||||
const jwt = require('jsonwebtoken');
|
||||
|
||||
const app = require('../app');
|
||||
const db = require('../src/config/db');
|
||||
|
||||
const JWT_SECRET = process.env.JWT_SECRET || 'test-jwt-secret';
|
||||
process.env.JWT_SECRET = JWT_SECRET;
|
||||
|
||||
let originalQuery;
|
||||
|
||||
const createToken = (payload = {}) =>
|
||||
jwt.sign(
|
||||
{
|
||||
id: 1,
|
||||
dni: '58045340X',
|
||||
id_proveedor: 675,
|
||||
...payload
|
||||
},
|
||||
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 requestJson = async ({ port, path, authorization }) =>
|
||||
new Promise((resolve, reject) => {
|
||||
const req = http.request(
|
||||
{
|
||||
hostname: '127.0.0.1',
|
||||
port,
|
||||
method: 'GET',
|
||||
path,
|
||||
headers: authorization ? { authorization } : {}
|
||||
},
|
||||
(res) => {
|
||||
let rawBody = '';
|
||||
|
||||
res.on('data', (chunk) => {
|
||||
rawBody += chunk;
|
||||
});
|
||||
|
||||
res.on('end', () => {
|
||||
const body = rawBody ? JSON.parse(rawBody) : null;
|
||||
resolve({ statusCode: res.statusCode, body });
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
req.on('error', reject);
|
||||
req.end();
|
||||
});
|
||||
|
||||
test.before(() => {
|
||||
originalQuery = db.query;
|
||||
});
|
||||
|
||||
test.after(() => {
|
||||
db.query = originalQuery;
|
||||
});
|
||||
|
||||
test('GET /api/trips/states está registrado en /api', () => {
|
||||
const apiRouterLayers = app._router.stack.filter(
|
||||
(layer) =>
|
||||
layer.name === 'router' &&
|
||||
layer.regexp &&
|
||||
layer.regexp.toString().includes('^\\/api\\/?(?=\\/|$)')
|
||||
);
|
||||
|
||||
assert.ok(apiRouterLayers.length > 0, 'Router /api is not mounted');
|
||||
|
||||
const statesRouteLayer = apiRouterLayers
|
||||
.flatMap((routerLayer) => routerLayer.handle.stack)
|
||||
.find(
|
||||
(layer) =>
|
||||
layer.route &&
|
||||
layer.route.path === '/trips/states' &&
|
||||
layer.route.methods.get
|
||||
);
|
||||
|
||||
assert.ok(statesRouteLayer, 'GET /api/trips/states route is not defined');
|
||||
});
|
||||
|
||||
test('GET /api/trips/states devuelve solo estados 2..7 ordenados ASC', async () => {
|
||||
const mockedStates = [
|
||||
{ id_estado: 2, estado: 'EN CURSO', estado_en: 'IN PROGRESS' },
|
||||
{ id_estado: 3, estado: 'POSICIONADO', estado_en: 'POSITIONED' },
|
||||
{ id_estado: 4, estado: 'CARGA DE MERCANCÍA', estado_en: 'CARGO LOADING' },
|
||||
{ id_estado: 5, estado: 'TRÁNSITO', estado_en: 'IN TRANSIT' },
|
||||
{ id_estado: 6, estado: 'LLEGADA AL DESTINO', estado_en: 'ARRIVED AT DESTINATION' },
|
||||
{ id_estado: 7, estado: 'ENTREGADO', estado_en: 'DELIVERED' }
|
||||
];
|
||||
|
||||
db.query = async (sql, params) => {
|
||||
assert.match(sql, /FROM t_viaje_estados/);
|
||||
assert.match(sql, /WHERE id_estado BETWEEN \? AND \?/);
|
||||
assert.match(sql, /ORDER BY id_estado ASC/);
|
||||
assert.deepEqual(params, [2, 7]);
|
||||
return [mockedStates];
|
||||
};
|
||||
|
||||
const response = await withServer(async (server) =>
|
||||
requestJson({
|
||||
port: server.address().port,
|
||||
path: '/api/trips/states',
|
||||
authorization: `Bearer ${createToken()}`
|
||||
})
|
||||
);
|
||||
|
||||
assert.equal(response.statusCode, 200);
|
||||
assert.deepEqual(response.body, {
|
||||
success: true,
|
||||
states: mockedStates
|
||||
});
|
||||
assert.equal(response.body.states.length, 6);
|
||||
});
|
||||
|
||||
test('GET /api/trips/states devuelve 401 sin token', async () => {
|
||||
db.query = async () => {
|
||||
throw new Error('db.query should not be called without token');
|
||||
};
|
||||
|
||||
const response = await withServer(async (server) =>
|
||||
requestJson({
|
||||
port: server.address().port,
|
||||
path: '/api/trips/states'
|
||||
})
|
||||
);
|
||||
|
||||
assert.equal(response.statusCode, 401);
|
||||
assert.deepEqual(response.body, { error: 'Unauthorized' });
|
||||
});
|
||||
|
||||
test('GET /api/trips/states devuelve 500 en error interno', async () => {
|
||||
db.query = async () => {
|
||||
throw new Error('forced db failure');
|
||||
};
|
||||
|
||||
const response = await withServer(async (server) =>
|
||||
requestJson({
|
||||
port: server.address().port,
|
||||
path: '/api/trips/states',
|
||||
authorization: `Bearer ${createToken()}`
|
||||
})
|
||||
);
|
||||
|
||||
assert.equal(response.statusCode, 500);
|
||||
assert.deepEqual(response.body, {
|
||||
success: false,
|
||||
error: 'Internal server error'
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user