test: long comment, limit=0, invalid limit (59/59 pass)

This commit is contained in:
2026-05-30 17:22:18 +03:00
parent 7d2b6a309f
commit 09bb56c6f4
+94
View File
@@ -746,6 +746,100 @@ async function cleanup() {
'user: удалённый CIDR не виден в /export');
}
// ─────────────────────────────────────────────────────────────────────────────
// 29. Очень длинный comment — не должен падать с 500
// ─────────────────────────────────────────────────────────────────────────────
console.log('\n── 29. Длинный comment ──');
{
const longComment = 'A'.repeat(10000);
const csrf = await getCsrfFromIndex(userAgent);
const res = await userAgent.post('/add').type('form')
.send({ value: '3.3.3.3', comment: longComment, _csrf: csrf });
log(res.status === 302, 'user: очень длинный comment → redirect (не 500)', `status=${res.status}`);
const err = getRedirectParam(res, 'error');
// Либо добавилось, либо ошибка валидации — но не 500
log(res.status !== 500, 'user: длинный comment не вызывает 500', `err=${err}`);
await pool.query(
`UPDATE whitelist_entries SET deleted_at = NOW(), deleted_by = 'test-cleanup'
WHERE value_cidr = '3.3.3.3/32' AND deleted_at IS NULL`
);
}
// ─────────────────────────────────────────────────────────────────────────────
// 30. Admin limit = 0 — компания сразу не может добавить ничего
// ─────────────────────────────────────────────────────────────────────────────
console.log('\n── 30. Лимит = 0 ──');
{
const { MOCK_USERS } = require('../src/config');
const testUser = MOCK_USERS.find(u => u.id === 'test');
const uRes = await pool.query(
`SELECT id FROM companies WHERE client_id = $1 LIMIT 1`, [testUser?.clientId]
);
const userCompId = uRes.rows[0]?.id;
if (userCompId) {
const csrf = await getCsrfFromIndex(adminAgent);
const limitRes = await adminAgent.post(`/admin/limit/${userCompId}`).type('form')
.send({ limit: '0', _csrf: csrf });
log(limitRes.status === 302, 'admin: выставил лимит=0', `status=${limitRes.status}`);
const csrf2 = await getCsrfFromIndex(userAgent);
const addRes = await userAgent.post('/add').type('form')
.send({ value: '2.2.2.2', comment: 'лимит 0', _csrf: csrf2 });
const err = getRedirectParam(addRes, 'error');
log(err?.includes('Лимит'), 'user: лимит=0 → любое добавление = ошибка', `err=${err}`);
// Сброс
const csrf3 = await getCsrfFromIndex(adminAgent);
await adminAgent.post(`/admin/limit/${userCompId}`).type('form')
.send({ limit: '', _csrf: csrf3 });
} else {
log(false, 'admin: нет компании для теста лимита=0');
log(false, 'user: лимит=0 test пропущен');
}
}
// ─────────────────────────────────────────────────────────────────────────────
// 31. Admin limit с невалидным значением — не должен сломать лимит
// ─────────────────────────────────────────────────────────────────────────────
console.log('\n── 31. Невалидный лимит ──');
{
const { MOCK_USERS } = require('../src/config');
const testUser = MOCK_USERS.find(u => u.id === 'test');
const uRes = await pool.query(
`SELECT id FROM companies WHERE client_id = $1 LIMIT 1`, [testUser?.clientId]
);
const userCompId = uRes.rows[0]?.id;
if (userCompId) {
const csrf = await getCsrfFromIndex(adminAgent);
const r = await adminAgent.post(`/admin/limit/${userCompId}`).type('form')
.send({ limit: '-5', _csrf: csrf });
// Должен либо отклонить (ошибка), либо принять как 0
log(r.status === 302 || r.status === 400,
'admin: лимит=-5 → redirect или 400', `status=${r.status}`);
// Проверяем что limit в БД не стал отрицательным
const limRes = await pool.query(
`SELECT custom_limit FROM companies WHERE id = $1`, [userCompId]
);
const savedLimit = limRes.rows[0]?.custom_limit;
log(savedLimit === null || savedLimit >= 0,
'admin: custom_limit в БД не отрицательный', `saved=${savedLimit}`);
// Чистим если записался -5
if (savedLimit !== null && savedLimit < 0) {
await pool.query(`UPDATE companies SET custom_limit = NULL WHERE id = $1`, [userCompId]);
}
} else {
log(false, 'admin: нет компании для теста невалидного лимита');
log(false, 'admin: пропущено');
}
}
// ─────────────────────────────────────────────────────────────────────────────
// 24. Race condition — одновременные запросы на /add при лимите
// (используем свежий агент, не отравленный rate-limit)