- Added TelegramStopAliasService for handling stop aliases in the database. - Created API endpoints for managing stop aliases (list, create, update, delete). - Introduced TelegramStopAliases component in the frontend for user interaction with stop aliases. - Updated TelegramImportService to utilize stop aliases for message processing. - Enhanced telegramCancellationParser to resolve stop names using aliases. - Added migration script to create telegram_stop_aliases table and seed initial data. - Updated documentation to include details about stop alias functionality.
58 lines
3.7 KiB
JavaScript
58 lines
3.7 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
process.env.JWT_SECRET = 'telegram-api-tests-only-secret';
|
|
process.env.NODE_ENV = 'production';
|
|
// This router test never connects to a real database.
|
|
require.cache[require.resolve('../src/config/database')] = { exports: { query: async () => ({ rows: [] }) } };
|
|
const express = require('express');
|
|
const jwt = require('jsonwebtoken');
|
|
const { createTelegramImportRouter } = require('../src/routes/telegramImport');
|
|
|
|
test('Telegram admin API rejects anonymous/non-admin access and preserves selected-city scope', async () => {
|
|
const calls = [];
|
|
const service = {
|
|
config: { citySlug: 'polotsk' },
|
|
getStatus: () => ({ enabled: false }),
|
|
list: async cityId => { calls.push(['list', cityId]); return []; },
|
|
sync: async () => { calls.push(['sync']); return {}; },
|
|
review: async (...args) => { calls.push(['review', ...args]); return { status: 'dismissed' }; },
|
|
stopAliases: {
|
|
list: async (...args) => { calls.push(['aliases', ...args]); return []; },
|
|
save: async (...args) => { calls.push(['saveAlias', ...args]); return { id: 9 }; },
|
|
remove: async (...args) => { calls.push(['removeAlias', ...args]); return { id: 9 }; }
|
|
}
|
|
};
|
|
const app = express();
|
|
app.use(express.json());
|
|
app.use((req, res, next) => { req.city = { id: Number(req.get('X-City-Id') || 1), slug: req.get('X-City-Id') === '2' ? 'novopolotsk' : 'polotsk' }; next(); });
|
|
app.use('/telegram-import', createTelegramImportRouter(service));
|
|
const server = await new Promise(resolve => { const s = app.listen(0, '127.0.0.1', () => resolve(s)); });
|
|
const url = `http://127.0.0.1:${server.address().port}/telegram-import`;
|
|
const token = role => jwt.sign({ id: 7, role }, process.env.JWT_SECRET);
|
|
try {
|
|
assert.equal((await fetch(url)).status, 401);
|
|
assert.equal((await fetch(url, { headers: { Authorization: `Bearer ${token('user')}` } })).status, 403);
|
|
assert.equal(calls.length, 0);
|
|
const headers = { Authorization: `Bearer ${token('admin')}`, 'X-City-Id': '2', 'Content-Type': 'application/json' };
|
|
assert.equal((await fetch(url, { headers })).status, 200);
|
|
assert.deepEqual(calls[0], ['list', 2]);
|
|
assert.equal((await fetch(`${url}/sync`, { method: 'POST', headers })).status, 409);
|
|
assert.equal(calls.length, 1);
|
|
assert.equal((await fetch(`${url}/4/review`, { method: 'POST', headers, body: JSON.stringify({ action: 'dismiss', content_hash: 'hash' }) })).status, 200);
|
|
assert.deepEqual(calls[1], ['review', 4, 2, { action: 'dismiss', content_hash: 'hash' }, 7]);
|
|
assert.equal((await fetch(`${url}/not-an-id/review`, { method: 'POST', headers, body: '{}' })).status, 400);
|
|
for (const method of ['GET', 'POST', 'PUT', 'DELETE']) {
|
|
const endpoint = `${url}/stop-aliases${['PUT', 'DELETE'].includes(method) ? '/9' : ''}`;
|
|
assert.equal((await fetch(endpoint, { method, headers: { Authorization: `Bearer ${token('user')}` } })).status, 403);
|
|
const result = await fetch(endpoint, { method, headers, ...(method === 'POST' || method === 'PUT' ? { body: JSON.stringify({ alias: 'ЗОДЧЕГО', stop_id: 22 }) } : {}) });
|
|
assert.equal(result.status, 200);
|
|
}
|
|
assert.deepEqual(calls.slice(-4), [
|
|
['aliases', 2], ['saveAlias', 2, { alias: 'ЗОДЧЕГО', stop_id: 22 }],
|
|
['saveAlias', 2, { alias: 'ЗОДЧЕГО', stop_id: 22 }, 9], ['removeAlias', 9, 2]
|
|
]);
|
|
assert.equal((await fetch(`${url}/stop-aliases/0`, { method: 'DELETE', headers })).status, 400);
|
|
assert.equal((await fetch(`${url}/stop-aliases/999999999999999999`, { method: 'PUT', headers, body: '{}' })).status, 400);
|
|
} finally { await new Promise(resolve => server.close(resolve)); }
|
|
});
|