53 lines
2.1 KiB
JavaScript
53 lines
2.1 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import pngjs from 'pngjs';
|
|
import { validateCaseIcon } from '../utils/case-icon.js';
|
|
import admin from '../config/admin.js';
|
|
import { models, sequelize } from '../models/index.js';
|
|
|
|
function image(size) {
|
|
const png = new pngjs.PNG({ width: size, height: size });
|
|
png.data.fill(120);
|
|
return pngjs.PNG.sync.write(png, { deflateLevel: 0 });
|
|
}
|
|
|
|
const allowed = image(450);
|
|
const oversized = image(600);
|
|
assert.ok(allowed.length > 256 * 1024 && allowed.length <= 1024 * 1024);
|
|
assert.ok(oversized.length > 1024 * 1024);
|
|
const icon = 'data:image/png;base64,' + allowed.toString('base64');
|
|
assert.equal(validateCaseIcon(icon), icon);
|
|
assert.throws(() => validateCaseIcon('data:image/png;base64,' + oversized.toString('base64')));
|
|
|
|
// Use the public proxy too: a 1 MB image exceeds 1 MB after base64 encoding.
|
|
const base = process.env.GRADE_API_URL || 'http://web/api';
|
|
let caseId;
|
|
try {
|
|
models.Case.initialize(sequelize);
|
|
const login = await fetch(base + '/auth/login', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ login: admin.login, password: admin.password }),
|
|
});
|
|
assert.equal(login.status, 200);
|
|
const { token } = await login.json();
|
|
const response = await fetch(base + '/admin/cases', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json', Authorization: 'Bearer ' + token },
|
|
body: JSON.stringify({ name: 'Icon size regression ' + Date.now(), icon }),
|
|
});
|
|
assert.equal(response.status, 201);
|
|
const { case: task } = await response.json();
|
|
caseId = task.id;
|
|
assert.equal(task.icon, icon);
|
|
const rejected = await fetch(base + '/admin/cases/' + caseId, {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json', Authorization: 'Bearer ' + token },
|
|
body: JSON.stringify({ name: task.name, icon: 'data:image/png;base64,' + oversized.toString('base64') }),
|
|
});
|
|
assert.equal(rejected.status, 400);
|
|
console.log('PASS: PNG above 256 KB accepted through proxy and stored; PNG above 1 MB rejected.');
|
|
} finally {
|
|
if (caseId) await models.Case.destroy({ where: { id: caseId }, force: true });
|
|
await sequelize.close();
|
|
}
|