feat: comprehensive API tests (104 pass) + fix: validation errors → 400, comment length limit

This commit is contained in:
2026-05-30 19:44:37 +03:00
parent 7779bf416a
commit ed5b3c8367
2 changed files with 917 additions and 0 deletions
+14
View File
@@ -10,6 +10,7 @@ function createEntriesRouter({ q }) {
// Маппинг сообщений ошибок на HTTP-коды
function handleQueryError(e, res) {
const msg = e.message || '';
// 409: бизнес-конфликты (дубликат, пересечение, лимит)
if (
msg.includes('Лимит исчерпан') ||
msg.includes('Такой адрес уже существует') ||
@@ -17,9 +18,20 @@ function createEntriesRouter({ q }) {
) {
return res.status(409).json({ error: msg });
}
// 404: запись не найдена
if (msg.includes('Запись не найдена')) {
return res.status(404).json({ error: msg });
}
// 400: ошибки валидации от validate() и blocked-ranges
if (
msg.includes('Некорректн') ||
msg.includes('Маска должна быть') ||
msg.includes('Пустое значение') ||
msg.includes('IPv6 не поддерживается') ||
msg.includes('пересекается с запрещённым')
) {
return res.status(400).json({ error: msg });
}
return res.status(500).json({ error: msg });
}
@@ -80,6 +92,7 @@ function createEntriesRouter({ q }) {
router.post('/', json, async (req, res) => {
const { value, comment } = req.body || {};
if (!value) return res.status(400).json({ error: 'value required' });
if (comment && comment.length > 255) return res.status(400).json({ error: 'Комментарий слишком длинный (максимум 255 символов)' });
try {
const company = await resolveCompany(req);
const { entry, wasNormalized } = await q.createEntry(company.id, value, comment || '', req.user.email);
@@ -94,6 +107,7 @@ function createEntriesRouter({ q }) {
router.patch('/:id', json, async (req, res) => {
const { value, comment } = req.body || {};
if (!value) return res.status(400).json({ error: 'value required' });
if (comment && comment.length > 255) return res.status(400).json({ error: 'Комментарий слишком длинный (максимум 255 символов)' });
try {
const company = await resolveCompany(req);
const { entry, wasNormalized } = await q.updateEntry(req.params.id, company.id, value, comment ?? '', req.user.email);