141 lines
6.3 KiB
JavaScript
141 lines
6.3 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import pngjs from 'pngjs';
|
|
import admin from '../config/admin.js';
|
|
import { models, sequelize } from '../models/index.js';
|
|
import setupAssociations from '../models/setupAssociations.js';
|
|
|
|
async function main() {
|
|
for (const model of Object.values(models)) model.initialize(sequelize);
|
|
setupAssociations();
|
|
const base = process.env.GRADE_API_URL || 'http://127.0.0.1:3080';
|
|
const created = { cases: [], teams: [], users: [] };
|
|
let adminToken;
|
|
async function request(path, { method = 'GET', body, status = 200, token = adminToken } = {}) {
|
|
const res = await globalThis.fetch(base + path, {
|
|
method,
|
|
headers: {
|
|
...(token ? { Authorization: 'Bearer ' + token } : {}),
|
|
...(body ? { 'Content-Type': 'application/json' } : {}),
|
|
},
|
|
body: body ? JSON.stringify(body) : undefined,
|
|
});
|
|
assert.equal(res.status, status, `${method} ${path}: ${await res.clone().text()}`);
|
|
const text = await res.text();
|
|
return text ? JSON.parse(text) : null;
|
|
}
|
|
try {
|
|
adminToken = (
|
|
await request('/auth/login', { method: 'POST', body: { login: admin.login, password: admin.password } })
|
|
).token;
|
|
const png = new pngjs.PNG({ width: 2, height: 2 });
|
|
png.data.fill(120);
|
|
const icon = 'data:image/png;base64,' + pngjs.PNG.sync.write(png).toString('base64');
|
|
const { case: task } = await request('/admin/cases', {
|
|
method: 'POST',
|
|
body: { name: 'Icons test ' + Date.now(), icon },
|
|
status: 201,
|
|
});
|
|
created.cases.push(task.id);
|
|
assert.equal(task.icon, icon);
|
|
await request('/admin/cases/' + task.id, { method: 'PUT', body: { name: task.name } });
|
|
assert.equal((await models.Case.findByPk(task.id)).icon, icon);
|
|
for (const invalid of [
|
|
'data:image/jpeg;base64,AAAA',
|
|
'data:image/png;base64,AAAA',
|
|
'data:image/png;base64,' + Buffer.alloc(270000).toString('base64'),
|
|
])
|
|
await request('/admin/cases/' + task.id, {
|
|
method: 'PUT',
|
|
body: { name: task.name, icon: invalid },
|
|
status: 400,
|
|
});
|
|
for (const name of ['A', 'B']) {
|
|
const { team } = await request(`/admin/cases/${task.id}/teams`, { method: 'POST', body: { name }, status: 201 });
|
|
created.teams.push(team.id);
|
|
}
|
|
for (const name of ['A', 'B']) {
|
|
const { user } = await request('/admin/users', {
|
|
method: 'POST',
|
|
body: { firstName: name, lastName: 'Details' + Date.now() + name, middleName: 'Иванович' },
|
|
status: 201,
|
|
});
|
|
created.users.push(user.id);
|
|
await request(`/admin/users/${user.id}/cases`, { method: 'PUT', body: { caseIds: [task.id] } });
|
|
}
|
|
const user = await models.User.findByPk(created.users[0]);
|
|
const judge = (
|
|
await request('/auth/login', { method: 'POST', body: { login: user.login, password: user.passwordForAdmin } })
|
|
).token;
|
|
assert.equal((await request('/cases', { token: judge })).cases[0].icon, icon);
|
|
await request('/admin/marks/details', { token: judge, status: 403 });
|
|
await request('/admin', {
|
|
method: 'POST',
|
|
body: { marks: [{ id: created.teams[0], implementation: 3, work: 4 }] },
|
|
});
|
|
await request(`/cases/${task.id}/teams/${created.teams[0]}/marks`, {
|
|
token: judge,
|
|
method: 'PUT',
|
|
body: {
|
|
marks: [
|
|
{ markType: 10, mark: 0 },
|
|
{ markType: 40, mark: 2 },
|
|
{ markType: 50, mark: 5 },
|
|
],
|
|
},
|
|
});
|
|
let report = (await request('/admin/marks/details')).details.find(c => c.id === task.id);
|
|
assert.equal(report.rows.length, 4);
|
|
const row = report.rows.find(r => r.teamId === created.teams[0] && r.userId === user.id);
|
|
assert.equal(row.marks[10], 0);
|
|
assert.equal(row.total, 14);
|
|
assert.ok(row.expert.includes('Иванович'));
|
|
const incomplete = report.rows.find(r => r.teamId === created.teams[0] && r.userId === created.users[1]);
|
|
assert.equal(incomplete.marks[10], null);
|
|
assert.equal(incomplete.marks[20], 3);
|
|
assert.equal(incomplete.total, null);
|
|
const { zeroMarks } = await request('/admin/marks');
|
|
assert.ok(zeroMarks[user.firstName + ' ' + user.lastName][task.name].includes('A'));
|
|
assert.ok(!zeroMarks[user.firstName + ' ' + user.lastName][task.name].includes('B'));
|
|
const otherUser = await models.User.findByPk(created.users[1]);
|
|
assert.equal(zeroMarks[otherUser.firstName + ' ' + otherUser.lastName]?.[task.name], undefined);
|
|
await models.Mark.destroy({ where: { userId: user.id, caseId: task.id, markType: [20, 30] }, force: true });
|
|
let missing = (await request('/admin/marks')).noMarks;
|
|
assert.ok(!missing[user.firstName + ' ' + user.lastName][task.name].includes('A'));
|
|
assert.ok(missing[user.firstName + ' ' + user.lastName][task.name].includes('B'));
|
|
await request(`/cases/${task.id}/teams/${created.teams[0]}/marks`, {
|
|
token: judge,
|
|
method: 'PUT',
|
|
body: { marks: [10, 40, 50].map(markType => ({ markType, mark: 0 })) },
|
|
});
|
|
missing = (await request('/admin/marks')).noMarks;
|
|
assert.ok(!missing[user.firstName + ' ' + user.lastName][task.name].includes('A'));
|
|
await models.Mark.destroy({
|
|
where: { userId: user.id, teamId: created.teams[0], caseId: task.id, markType: 40 },
|
|
force: true,
|
|
});
|
|
missing = (await request('/admin/marks')).noMarks;
|
|
assert.ok(missing[user.firstName + ' ' + user.lastName][task.name].includes('A'));
|
|
assert.ok(!JSON.stringify(report).includes(user.passwordForAdmin));
|
|
await request('/admin/users/' + created.users[1], { method: 'DELETE', status: 204 });
|
|
report = (await request('/admin/marks/details')).details.find(c => c.id === task.id);
|
|
assert.equal(report.rows.length, 2);
|
|
await request('/admin/cases/' + task.id, { method: 'PUT', body: { name: task.name, icon: null } });
|
|
assert.equal((await models.Case.findByPk(task.id)).icon, null);
|
|
console.log(
|
|
'PASS PNG create/preserve/remove/validation; assigned expert icon; all-mark matrix; zero vs missing; totals; technical marks; deleted experts; permissions and no credential exposure.'
|
|
);
|
|
} finally {
|
|
await models.Mark.destroy({ where: { caseId: created.cases }, force: true });
|
|
await models.UserInCase.destroy({ where: { caseId: created.cases }, force: true });
|
|
await models.TeamInCase.destroy({ where: { caseId: created.cases }, force: true });
|
|
await models.Team.destroy({ where: { id: created.teams }, force: true });
|
|
await models.User.destroy({ where: { id: created.users }, force: true });
|
|
await models.Case.destroy({ where: { id: created.cases }, force: true });
|
|
await sequelize.close();
|
|
}
|
|
}
|
|
main().catch(error => {
|
|
console.error(error);
|
|
process.exitCode = 1;
|
|
});
|